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