2019-11-13 11:25:53 +00:00
local sys = require " luci.sys "
local util = require " luci.util "
local fs = require " nixio.fs "
local dispatcher = require " luci.dispatcher "
local i18n = require " luci.i18n "
2018-08-23 22:11:49 +00:00
local uci = require ( " luci.model.uci " ) . cursor ( )
2019-10-08 16:36:25 +00:00
2019-12-19 18:19:59 +00:00
local packageName = " https-dns-proxy "
2019-11-13 11:25:53 +00:00
local providers_dir = " /usr/lib/lua/luci/ " .. packageName .. " /providers/ "
2019-07-20 11:24:33 +00:00
2019-11-13 11:25:53 +00:00
function get_provider_name ( value )
for filename in fs.dir ( providers_dir ) do
local p_func = loadfile ( providers_dir .. filename )
setfenv ( p_func , { _ = i18n.translate } )
local p = p_func ( )
value = value : gsub ( ' [%p%c%s] ' , ' ' )
2019-12-19 18:19:59 +00:00
p.url_match = p.resolver_url : gsub ( ' [%p%c%s] ' , ' ' )
2019-11-13 11:25:53 +00:00
if value : match ( p.url_match ) then
return p.label
end
end
2019-12-19 18:19:59 +00:00
return translate ( " Unknown Provider " )
2019-07-20 11:24:33 +00:00
end
2019-11-13 11:25:53 +00:00
local tmpfsStatus , tmpfsStatusCode
local ubusStatus = util.ubus ( " service " , " list " , { name = packageName } )
local tmpfsVersion = tostring ( util.trim ( sys.exec ( " opkg list-installed " .. packageName .. " | awk '{print $3}' " ) ) )
2019-07-20 11:24:33 +00:00
2019-11-13 11:25:53 +00:00
if not tmpfsVersion or tmpfsVersion == " " then
tmpfsStatusCode = - 1
tmpfsVersion = " "
tmpfsStatus = packageName .. " " .. translate ( " is not installed or not found " )
else
tmpfsVersion = " [ " .. packageName .. " " .. tmpfsVersion .. " ] "
if not ubusStatus or not ubusStatus [ packageName ] then
tmpfsStatusCode = 0
tmpfsStatus = translate ( " Stopped " )
else
tmpfsStatusCode , tmpfsStatus = 1 , " "
for n = 1 , 1000 do
if ubusStatus and ubusStatus [ packageName ] and
ubusStatus [ packageName ] [ " instances " ] and
ubusStatus [ packageName ] [ " instances " ] [ " instance " .. n ] and
ubusStatus [ packageName ] [ " instances " ] [ " instance " .. n ] [ " running " ] then
local value , k , v , url , url_flag , la , la_flag , lp , lp_flag
for k , v in pairs ( ubusStatus [ packageName ] [ " instances " ] [ " instance " .. n ] [ " command " ] ) do
if la_flag then la , la_flag = v , false end
if lp_flag then lp , lp_flag = v , false end
if url_flag then url , url_flag = v , false end
if v == " -a " then la_flag = true end
if v == " -p " then lp_flag = true end
if v == " -r " then url_flag = true end
end
la = la or " 127.0.0.1 "
lp = lp or n + 5053
tmpfsStatus = tmpfsStatus .. translate ( " Running " ) .. " : " .. get_provider_name ( url ) .. " " .. translate ( " DoH " ) .. " " .. translate ( " at " ) .. " " .. la .. " : " .. lp .. " \n "
else
break
end
end
end
2019-07-20 11:24:33 +00:00
end
2018-08-23 22:11:49 +00:00
2019-12-19 18:19:59 +00:00
m = Map ( " https-dns-proxy " , translate ( " DNS Over HTTPS Proxy Settings " ) )
2019-11-13 11:25:53 +00:00
h = m : section ( TypedSection , " _dummy " , translate ( " Service Status " ) .. tmpfsVersion )
h.template = " cbi/nullsection "
ss = h : option ( DummyValue , " _dummy " , translate ( " Service Status " ) )
if tmpfsStatusCode == - 1 then
ss.template = packageName .. " /status "
ss.value = tmpfsStatus
else
if tmpfsStatusCode == 0 then
ss.template = packageName .. " /status "
else
ss.template = packageName .. " /status-textarea "
end
ss.value = tmpfsStatus
buttons = h : option ( DummyValue , " _dummy " )
buttons.template = packageName .. " /buttons "
end
2018-08-23 22:11:49 +00:00
2019-12-19 18:19:59 +00:00
s3 = m : section ( TypedSection , " https-dns-proxy " , translate ( " Instances " ) , translate ( " When you add/remove any instances below, they will be used to override the 'DNS forwardings' section of " )
2019-10-08 16:36:25 +00:00
.. [[ <a href="]] .. dispatcher.build_url ( " admin/network/dhcp " ) .. [[">]]
2019-12-19 18:19:59 +00:00
.. translate ( " DHCP and DNS " ) .. [[</a>]] .. " . "
.. " <br /> "
.. translate ( " For more information on different options check " )
.. [[ <a href="https://adguard.com/en/adguard-dns/overview.html">]]
.. " AdGuard.com " .. [[</a>]] .. " , "
.. [[ <a href="https://cleanbrowsing.org/guides/dnsoverhttps">]]
.. " CleanBrowsing.org " .. [[</a>]] .. " " .. translate ( " and " ) .. " "
.. [[ <a href="https://www.quad9.net/doh-quad9-dns-servers/">]]
.. " Quad9.net " .. [[</a>]] .. " . " )
2018-08-23 22:11:49 +00:00
s3.template = " cbi/tblsection "
s3.sortable = false
s3.anonymous = true
s3.addremove = true
2019-12-19 18:19:59 +00:00
prov = s3 : option ( ListValue , " resolver_url " , translate ( " Resolver " ) )
2019-11-13 11:25:53 +00:00
for filename in fs.dir ( providers_dir ) do
local p_func = loadfile ( providers_dir .. filename )
setfenv ( p_func , { _ = i18n.translate } )
local p = p_func ( )
2019-12-19 18:19:59 +00:00
prov : value ( p.resolver_url , p.label )
2019-11-13 11:25:53 +00:00
if p.default then
2019-12-19 18:19:59 +00:00
prov.default = p.resolver_url
2019-11-13 11:25:53 +00:00
end
end
2019-10-08 16:36:25 +00:00
prov.forcewrite = true
2018-08-23 22:11:49 +00:00
prov.write = function ( self , section , value )
2019-11-13 11:25:53 +00:00
if not value then return end
for filename in fs.dir ( providers_dir ) do
local p_func = loadfile ( providers_dir .. filename )
setfenv ( p_func , { _ = i18n.translate } )
local p = p_func ( )
value = value : gsub ( ' [%p%c%s] ' , ' ' )
2019-12-19 18:19:59 +00:00
p.url_match = p.resolver_url : gsub ( ' [%p%c%s] ' , ' ' )
2019-11-13 11:25:53 +00:00
if value : match ( p.url_match ) then
2019-12-19 18:19:59 +00:00
uci : set ( packageName , section , " bootstrap_dns " , p.bootstrap_dns )
uci : set ( packageName , section , " resolver_url " , p.resolver_url )
2019-11-13 11:25:53 +00:00
end
end
2019-12-19 18:19:59 +00:00
uci : save ( packageName )
2018-08-23 22:11:49 +00:00
end
la = s3 : option ( Value , " listen_addr " , translate ( " Listen address " ) )
2019-10-08 16:36:25 +00:00
la.datatype = " host "
la.placeholder = " 127.0.0.1 "
la.rmempty = true
local n = 0
2019-12-19 18:19:59 +00:00
uci : foreach ( packageName , packageName , function ( s )
2019-11-13 11:25:53 +00:00
if s [ " .name " ] == section then
return false
end
n = n + 1
2019-10-08 16:36:25 +00:00
end )
2018-08-23 22:11:49 +00:00
lp = s3 : option ( Value , " listen_port " , translate ( " Listen port " ) )
2019-10-08 16:36:25 +00:00
lp.datatype = " port "
lp.value = n + 5053
2018-08-23 22:11:49 +00:00
2019-12-19 18:19:59 +00:00
sa = s3 : option ( Value , " edns_subnet " , translate ( " EDNS client subnet " ) )
2018-08-23 22:11:49 +00:00
sa.rmempty = true
ps = s3 : option ( Value , " proxy_server " , translate ( " Proxy server " ) )
2019-10-08 16:36:25 +00:00
ps.rmempty = true
2018-08-23 22:11:49 +00:00
return m