400-mesh-vpn-fastd 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/lua
  2. local site = require 'gluon.site_config'
  3. local users = require 'gluon.users'
  4. local util = require 'gluon.util'
  5. local uci = require('luci.model.uci').cursor()
  6. local lutil = require 'luci.util'
  7. -- The previously used user is removed, we need root privileges to use the packet_mark option
  8. users.remove_user('gluon-fastd')
  9. -- Group for iptables rule
  10. users.add_group('gluon-fastd', 800)
  11. local enabled = uci:get('fastd', 'mesh_vpn', 'enabled')
  12. if not enabled then
  13. enabled = site.fastd_mesh_vpn.enabled and 1 or 0
  14. end
  15. local methods
  16. if site.fastd_mesh_vpn.configurable then
  17. local has_null = lutil.contains(site.fastd_mesh_vpn.methods, 'null')
  18. local old_methods = uci:get('fastd', 'mesh_vpn', 'method')
  19. if old_methods then
  20. has_null = lutil.contains(old_methods, 'null')
  21. end
  22. methods = {}
  23. if has_null then
  24. table.insert(methods, 'null')
  25. end
  26. for _, method in ipairs(site.fastd_mesh_vpn.methods) do
  27. if method ~= 'null' then
  28. table.insert(methods, method)
  29. end
  30. end
  31. else
  32. methods = site.fastd_mesh_vpn.methods
  33. end
  34. uci:section('fastd', 'fastd', 'mesh_vpn',
  35. {
  36. enabled = enabled,
  37. group = 'gluon-fastd',
  38. syslog_level = 'verbose',
  39. interface = 'mesh-vpn',
  40. mode = 'tap',
  41. mtu = site.fastd_mesh_vpn.mtu,
  42. secure_handshakes = 1,
  43. method = methods,
  44. packet_mark = 1,
  45. status_socket = '/var/run/fastd.mesh_vpn.socket',
  46. }
  47. )
  48. uci:delete('fastd', 'mesh_vpn', 'user')
  49. local add_groups
  50. local function add_peer(group, name, config)
  51. uci:section('fastd', 'peer', group .. '_peer_' .. name,
  52. {
  53. enabled = 1,
  54. net = 'mesh_vpn',
  55. group = group,
  56. key = config.key,
  57. remote = config.remotes,
  58. }
  59. )
  60. end
  61. local function add_group(name, config, parent)
  62. uci:delete('fastd', name)
  63. uci:delete_all('fastd', 'peer',
  64. function(peer)
  65. return (peer.net == 'mesh_vpn' and peer.group == name)
  66. end
  67. )
  68. uci:section('fastd', 'peer_group', name,
  69. {
  70. enabled = 1,
  71. net = 'mesh_vpn',
  72. parent = parent,
  73. peer_limit = config.limit,
  74. }
  75. )
  76. if config.peers then
  77. for peername, peerconfig in pairs(config.peers) do
  78. add_peer(name, peername, peerconfig)
  79. end
  80. end
  81. add_groups(name, config.groups, name)
  82. end
  83. -- declared local above
  84. function add_groups(prefix, groups, parent)
  85. if groups then
  86. for name, group in pairs(groups) do
  87. add_group(prefix .. '_' .. name, group, parent)
  88. end
  89. end
  90. end
  91. add_groups('mesh_vpn', site.fastd_mesh_vpn.groups)
  92. uci:save('fastd')
  93. uci:section('network', 'interface', 'mesh_vpn',
  94. {
  95. ifname = 'mesh-vpn',
  96. proto = 'gluon_mesh',
  97. transitive = 1,
  98. fixed_mtu = 1,
  99. macaddr = util.generate_mac(7),
  100. }
  101. )
  102. uci:save('network')
  103. uci:section('firewall', 'include', 'mesh_vpn_dns',
  104. {
  105. type = 'restore',
  106. path = '/lib/gluon/mesh-vpn-fastd/iptables.rules',
  107. family = 'ipv4',
  108. }
  109. )
  110. uci:save('firewall')