update.lua 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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('luci.model.uci').cursor()
  6. local fs = require 'nixio.fs'
  7. local new_servers = ''
  8. local function handle_interface(status)
  9. local ifname = status.device
  10. local servers = status.inactive['dns-server']
  11. for _, server in ipairs(servers) do
  12. if server:match('^fe80:') then
  13. server = server .. '%' .. ifname
  14. end
  15. new_servers = new_servers .. 'nameserver ' .. server .. '\n'
  16. end
  17. end
  18. local function append_interface_servers(iface)
  19. handle_interface(ubus:call('network.interface.' .. iface, 'status', {}))
  20. end
  21. local static = uci:get_first('gluon-wan-dnsmasq', 'static', 'server')
  22. if type(static) == 'table' and #static > 0 then
  23. append_servers(static)
  24. else
  25. pcall(append_interface_servers, 'wan6')
  26. pcall(append_interface_servers, 'wan')
  27. end
  28. fs.mkdirr(RESOLV_CONF_DIR)
  29. local old_servers = fs.readfile(RESOLV_CONF)
  30. if new_servers ~= old_servers then
  31. local f = io.open(RESOLV_CONF .. '.tmp', 'w')
  32. f:write(new_servers)
  33. f:close()
  34. fs.rename(RESOLV_CONF .. '.tmp', RESOLV_CONF)
  35. end