update.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/lua
  2. local RESOLV_CONF_DIR = '/var/gluon/wan-dnsmasq'
  3. local RESOLV_CONF = RESOLV_CONF_DIR .. '/resolv.conf'
  4. local ubus = require('ubus').connect()
  5. local uci = require('simple-uci').cursor()
  6. local fs = require 'nixio.fs'
  7. local new_servers = ''
  8. local function append_server(server)
  9. new_servers = new_servers .. 'nameserver ' .. server .. '\n'
  10. end
  11. local function handle_interface(status)
  12. local ifname = status.device
  13. local servers = status.inactive['dns-server']
  14. for _, server in ipairs(servers) do
  15. if server:match('^fe80:') then
  16. append_server(server .. '%' .. ifname)
  17. else
  18. append_server(server)
  19. end
  20. end
  21. end
  22. local function append_interface_servers(iface)
  23. handle_interface(ubus:call('network.interface.' .. iface, 'status', {}))
  24. end
  25. local static = uci:get_first('gluon-wan-dnsmasq', 'static', 'server')
  26. if type(static) == 'table' and #static > 0 then
  27. for _, server in ipairs(static) do
  28. append_server(server)
  29. end
  30. else
  31. pcall(append_interface_servers, 'wan6')
  32. pcall(append_interface_servers, 'wan')
  33. end
  34. fs.mkdirr(RESOLV_CONF_DIR)
  35. local old_servers = fs.readfile(RESOLV_CONF)
  36. if new_servers ~= old_servers then
  37. local f = io.open(RESOLV_CONF .. '.tmp', 'w')
  38. f:write(new_servers)
  39. f:close()
  40. fs.rename(RESOLV_CONF .. '.tmp', RESOLV_CONF)
  41. end