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 "
2020-12-21 17:34:24 +00:00
local readmeURL = " https://docs.openwrt.melmac.net/ " .. packageName .. " / "
2019-11-13 11:25:53 +00:00
local providers_dir = " /usr/lib/lua/luci/ " .. packageName .. " /providers/ "
2020-04-06 14:00:09 +00:00
local helperText = " "
2020-05-30 11:40:54 +00:00
function getPackageVersion ( )
local opkgFile = " /usr/lib/opkg/status "
local line
local flag = false
for line in io.lines ( opkgFile ) do
if flag then
return line : match ( ' [%d%.$-]+ ' ) or " "
elseif line : find ( " Package: " .. packageName : gsub ( " %- " , " %%%- " ) ) then
flag = true
end
end
return " "
end
function createHelperText ( )
2020-12-21 17:34:24 +00:00
local initText = translate ( " For more information on different options check " ) .. " "
2020-04-06 14:00:09 +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 ( )
if p.help_link then
local url , domain
url = p.help_link
domain = p.help_link_text or url : match ( ' ^%w+://([^/]+) ' )
if not helperText : find ( domain ) then
if helperText == " " then
helperText = initText
else
helperText = helperText .. " , "
end
2020-05-30 11:40:54 +00:00
helperText = helperText .. [[<a href="]] .. url .. [[" target="_blank">]] .. domain .. [[</a>]]
2020-04-06 14:00:09 +00:00
end
end
end
if helperText ~= " " then
local a = helperText : gsub ( ' (.*),%s.*$ ' , ' %1 ' )
helperText = a .. " " .. translate ( " and " ) .. helperText : sub ( # a + 2 ) .. " . "
end
end
2019-07-20 11:24:33 +00:00
2020-05-30 11:40:54 +00:00
function getProviderName ( value )
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 ( )
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
2020-04-11 02:30:03 +00:00
local packageStatus , packageStatusCode
2019-11-13 11:25:53 +00:00
local ubusStatus = util.ubus ( " service " , " list " , { name = packageName } )
2020-05-30 11:40:54 +00:00
local packageVersion = getPackageVersion ( )
2019-07-20 11:24:33 +00:00
2020-04-11 02:30:03 +00:00
if packageVersion == " " then
2021-02-10 01:59:59 +00:00
packageStatusCode , packageStatus = - 1 , translatef ( " %s is not installed or not found " , packageName )
2019-11-13 11:25:53 +00:00
else
2021-02-10 01:59:59 +00:00
packageStatusCode , packageStatus = 1 , " "
for n = 1 , 20 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
2021-03-08 10:41:51 +00:00
packageStatus = packageStatus .. translatef ( " %s DoH at %s:%s " , getProviderName ( url ) , la , lp ) .. " \n "
2021-02-10 01:59:59 +00:00
else
break
end
end
if packageStatus == " " then
2020-04-11 02:30:03 +00:00
packageStatusCode = 0
packageStatus = translate ( " Stopped " )
if not sys.init . enabled ( packageName ) then
packageStatus = packageStatus .. " ( " .. translate ( " disabled " ) .. " ) "
2020-04-03 12:50:10 +00:00
end
2019-11-13 11:25:53 +00:00
end
2019-07-20 11:24:33 +00:00
end
2018-08-23 22:11:49 +00:00
2020-04-24 10:05:45 +00:00
m = Map ( " https-dns-proxy " , translate ( " DNS HTTPS Proxy Settings " ) )
2019-11-13 11:25:53 +00:00
2020-04-11 02:30:03 +00:00
h = m : section ( TypedSection , " _dummy " , translatef ( " Service Status [%s %s] " , packageName , packageVersion ) )
2019-11-13 11:25:53 +00:00
h.template = " cbi/nullsection "
ss = h : option ( DummyValue , " _dummy " , translate ( " Service Status " ) )
2021-03-08 10:41:51 +00:00
ss.template = packageName .. " /status "
ss.value = packageStatus
if packageStatusCode ~= - 1 then
buttons = h : option ( DummyValue , " _dummy " , translate ( " Service Control " ) )
2019-11-13 11:25:53 +00:00
buttons.template = packageName .. " /buttons "
end
2018-08-23 22:11:49 +00:00
2021-02-10 01:59:59 +00:00
c = m : section ( NamedSection , " config " , " https-dns-proxy " , translate ( " Configuration " ) )
d1 = c : option ( ListValue , " update_dnsmasq_config " , translate ( " Update DNSMASQ Config on Start/Stop " ) , translatef ( " If update option is selected, the 'DNS forwardings' section of %sDHCP and DNS%s will be automatically updated to use selected DoH providers (%smore information%s). " , " <a href= \" " .. dispatcher.build_url ( " admin/network/dhcp " ) .. " \" > " , " </a> " , " <a href= \" " .. readmeURL .. " #default-settings " .. " \" target= \" _blank \" > " , " </a> " ) )
2020-12-21 17:34:24 +00:00
d1 : value ( ' * ' , translate ( " Update all configs " ) )
local dnsmasq_num = 0
uci : foreach ( " dhcp " , " dnsmasq " , function ( s )
d1 : value ( tostring ( dnsmasq_num ) , translatef ( " Update %s config " , " dhcp.@dnsmasq[ " .. tostring ( dnsmasq_num ) .. " ] " ) )
dnsmasq_num = dnsmasq_num + 1
end )
d1 : value ( ' - ' , translate ( " Do not update configs " ) )
d1.default = ' * '
2021-02-10 01:59:59 +00:00
f1 = c : option ( ListValue , " force_dns " , translate ( " Force Router DNS " ) , translate ( " Forces Router DNS use on local devices, also known as DNS Hijacking. " ) )
f1 : value ( " 0 " , translate ( " Let local devices use their own DNS servers if set " ) )
f1 : value ( " 1 " , translate ( " Force Router DNS server to all local devices " ) )
f1.default = ' 1 '
2020-12-21 17:34:24 +00:00
2020-05-30 11:40:54 +00:00
createHelperText ( )
2020-04-11 02:30:03 +00:00
s3 = m : section ( TypedSection , " https-dns-proxy " , translate ( " Instances " ) ,
2020-12-21 17:34:24 +00:00
helperText )
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
2021-02-05 05:52:22 +00:00
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
2021-02-05 05:52:22 +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
2021-01-13 12:01:33 +00:00
dscp = s3 : option ( Value , " dscp_codepoint " , translate ( " DSCP Codepoint " ) )
dscp.datatype = " range(0,63) "
dscp.rmempty = true
2021-02-05 05:52:22 +00:00
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