400-mesh-vpn-fastd 2.1 KB

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