2014-07-20 01:25:22 +00:00
|
|
|
#!/usr/bin/lua
|
|
|
|
|
|
|
|
local RESOLV_CONF_DIR = '/var/gluon/wan-dnsmasq'
|
|
|
|
local RESOLV_CONF = RESOLV_CONF_DIR .. '/resolv.conf'
|
|
|
|
|
|
|
|
|
|
|
|
local ubus = require('ubus').connect()
|
2017-01-19 11:51:04 +00:00
|
|
|
local uci = require('simple-uci').cursor()
|
2014-07-20 01:25:22 +00:00
|
|
|
local fs = require 'nixio.fs'
|
|
|
|
|
|
|
|
|
2015-02-18 00:23:11 +00:00
|
|
|
local new_servers = ''
|
|
|
|
|
2016-09-18 20:37:05 +00:00
|
|
|
local function append_server(server)
|
|
|
|
new_servers = new_servers .. 'nameserver ' .. server .. '\n'
|
|
|
|
end
|
|
|
|
|
2015-02-18 00:23:11 +00:00
|
|
|
|
2016-08-28 20:04:47 +00:00
|
|
|
local function handle_interface(status)
|
|
|
|
local ifname = status.device
|
|
|
|
local servers = status.inactive['dns-server']
|
|
|
|
|
2014-07-20 01:25:22 +00:00
|
|
|
for _, server in ipairs(servers) do
|
2016-08-28 20:04:47 +00:00
|
|
|
if server:match('^fe80:') then
|
2016-09-18 20:37:05 +00:00
|
|
|
append_server(server .. '%' .. ifname)
|
|
|
|
else
|
|
|
|
append_server(server)
|
2016-08-28 20:04:47 +00:00
|
|
|
end
|
2014-07-20 01:25:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-18 00:23:11 +00:00
|
|
|
local function append_interface_servers(iface)
|
2016-08-28 20:04:47 +00:00
|
|
|
handle_interface(ubus:call('network.interface.' .. iface, 'status', {}))
|
2014-08-09 12:29:24 +00:00
|
|
|
end
|
|
|
|
|
2014-07-20 01:25:22 +00:00
|
|
|
|
2014-08-09 12:29:24 +00:00
|
|
|
local static = uci:get_first('gluon-wan-dnsmasq', 'static', 'server')
|
|
|
|
|
|
|
|
if type(static) == 'table' and #static > 0 then
|
2016-09-18 20:37:05 +00:00
|
|
|
for _, server in ipairs(static) do
|
|
|
|
append_server(server)
|
|
|
|
end
|
2014-08-09 12:29:24 +00:00
|
|
|
else
|
2015-02-18 00:23:11 +00:00
|
|
|
pcall(append_interface_servers, 'wan6')
|
|
|
|
pcall(append_interface_servers, 'wan')
|
2014-08-09 12:29:24 +00:00
|
|
|
end
|
2014-07-20 01:25:22 +00:00
|
|
|
|
2015-02-18 00:23:11 +00:00
|
|
|
|
|
|
|
fs.mkdirr(RESOLV_CONF_DIR)
|
|
|
|
|
|
|
|
local old_servers = fs.readfile(RESOLV_CONF)
|
|
|
|
|
|
|
|
if new_servers ~= old_servers then
|
|
|
|
local f = io.open(RESOLV_CONF .. '.tmp', 'w')
|
|
|
|
f:write(new_servers)
|
|
|
|
f:close()
|
|
|
|
|
|
|
|
fs.rename(RESOLV_CONF .. '.tmp', RESOLV_CONF)
|
|
|
|
end
|