400-mesh-vpn-fastd 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/lua
  2. local site = require 'gluon.site'
  3. local util = require 'gluon.util'
  4. local uci = require('simple-uci').cursor()
  5. local syslog_level = uci:get('fastd', 'mesh_vpn', 'syslog_level') or 'verbose'
  6. local methods
  7. if site.mesh_vpn.fastd.configurable(false) then
  8. local has_null = util.contains(site.mesh_vpn.fastd.methods(), 'null')
  9. local old_methods = uci:get('fastd', 'mesh_vpn', 'method')
  10. if old_methods then
  11. has_null = util.contains(old_methods, 'null')
  12. end
  13. methods = {}
  14. if has_null then
  15. table.insert(methods, 'null')
  16. end
  17. for _, method in ipairs(site.mesh_vpn.fastd.methods()) do
  18. if method ~= 'null' then
  19. table.insert(methods, method)
  20. end
  21. end
  22. else
  23. methods = site.mesh_vpn.fastd.methods()
  24. end
  25. uci:section('fastd', 'fastd', 'mesh_vpn', {
  26. group = 'gluon-mesh-vpn',
  27. syslog_level = syslog_level,
  28. interface = 'mesh-vpn',
  29. mode = 'tap',
  30. mtu = site.mesh_vpn.mtu(),
  31. secure_handshakes = true,
  32. method = methods,
  33. packet_mark = 1,
  34. status_socket = '/var/run/fastd.mesh_vpn.socket',
  35. })
  36. uci:delete('fastd', 'mesh_vpn', 'user')
  37. local add_groups
  38. local function add_peer(group, name, config)
  39. uci:section('fastd', 'peer', group .. '_peer_' .. name, {
  40. enabled = true,
  41. net = 'mesh_vpn',
  42. group = group,
  43. key = config.key,
  44. remote = config.remotes,
  45. })
  46. end
  47. local function add_group(name, config, parent)
  48. uci:delete('fastd', name)
  49. uci:delete_all('fastd', 'peer', function(peer)
  50. return (peer.net == 'mesh_vpn' and peer.group == name)
  51. end)
  52. uci:section('fastd', 'peer_group', name, {
  53. enabled = true,
  54. net = 'mesh_vpn',
  55. parent = parent,
  56. peer_limit = config.limit,
  57. })
  58. if config.peers then
  59. for peername, peerconfig in pairs(config.peers) do
  60. add_peer(name, peername, peerconfig)
  61. end
  62. end
  63. add_groups(name, config.groups, name)
  64. end
  65. -- declared local above
  66. function add_groups(prefix, groups, parent)
  67. if groups then
  68. for name, group in pairs(groups) do
  69. add_group(prefix .. '_' .. name, group, parent)
  70. end
  71. end
  72. end
  73. add_groups('mesh_vpn', site.mesh_vpn.fastd.groups())
  74. uci:save('fastd')