1 local sys = require "luci.sys"
2 local util = require "luci.util"
3 local fs = require "nixio.fs"
4 local dispatcher = require "luci.dispatcher"
5 local i18n = require "luci.i18n"
6 local uci = require("luci.model.uci").cursor()
8 local packageName = "https-dns-proxy"
9 local providers_dir = "/usr/lib/lua/luci/" .. packageName .. "/providers/"
11 function get_provider_name(value)
12 for filename in fs.dir(providers_dir) do
13 local p_func = loadfile(providers_dir .. filename)
14 setfenv(p_func, { _ = i18n.translate })
16 value = value:gsub('[%p%c%s]', '')
17 p.url_match = p.resolver_url:gsub('[%p%c%s]', '')
18 if value:match(p.url_match) then
22 return translate("Unknown Provider")
25 local tmpfsStatus, tmpfsStatusCode
26 local ubusStatus = util.ubus("service", "list", { name = packageName })
27 local tmpfsVersion = tostring(util.trim(sys.exec("opkg list-installed " .. packageName .. " | awk '{print $3}'")))
29 if not tmpfsVersion or tmpfsVersion == "" then
32 tmpfsStatus = packageName .. " " .. translate("is not installed or not found")
34 tmpfsVersion = " [" .. packageName .. " " .. tmpfsVersion .. "]"
35 if not ubusStatus or not ubusStatus[packageName] then
37 tmpfsStatus = translate("Stopped")
39 tmpfsStatusCode, tmpfsStatus = 1, ""
41 if ubusStatus and ubusStatus[packageName] and
42 ubusStatus[packageName]["instances"] and
43 ubusStatus[packageName]["instances"]["instance" .. n] and
44 ubusStatus[packageName]["instances"]["instance" .. n]["running"] then
45 local value, k, v, url, url_flag, la, la_flag, lp, lp_flag
46 for k, v in pairs(ubusStatus[packageName]["instances"]["instance" .. n]["command"]) do
47 if la_flag then la, la_flag = v, false end
48 if lp_flag then lp, lp_flag = v, false end
49 if url_flag then url, url_flag = v, false end
50 if v == "-a" then la_flag = true end
51 if v == "-p" then lp_flag = true end
52 if v == "-r" then url_flag = true end
54 la = la or "127.0.0.1"
56 tmpfsStatus = tmpfsStatus .. translate("Running") .. ": " .. get_provider_name(url) .. " " .. translate("DoH") .. " " .. translate("at") .. " " .. la .. ":" .. lp .. "\n"
64 m = Map("https-dns-proxy", translate("DNS Over HTTPS Proxy Settings"))
66 h = m:section(TypedSection, "_dummy", translate("Service Status") .. tmpfsVersion)
67 h.template = "cbi/nullsection"
68 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
69 if tmpfsStatusCode == -1 then
70 ss.template = packageName .. "/status"
71 ss.value = tmpfsStatus
73 if tmpfsStatusCode == 0 then
74 ss.template = packageName .. "/status"
76 ss.template = packageName .. "/status-textarea"
78 ss.value = tmpfsStatus
79 buttons = h:option(DummyValue, "_dummy")
80 buttons.template = packageName .. "/buttons"
83 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 ")
84 .. [[ <a href="]] .. dispatcher.build_url("admin/network/dhcp") .. [[">]]
85 .. translate("DHCP and DNS") .. [[</a>]] .. "."
87 .. translate("For more information on different options check ")
88 .. [[ <a href="https://adguard.com/en/adguard-dns/overview.html">]]
89 .. "AdGuard.com" .. [[</a>]] .. ", "
90 .. [[ <a href="https://cleanbrowsing.org/guides/dnsoverhttps">]]
91 .. "CleanBrowsing.org" .. [[</a>]] .. " " .. translate("and") .. " "
92 .. [[ <a href="https://www.quad9.net/doh-quad9-dns-servers/">]]
93 .. "Quad9.net" .. [[</a>]] .. ".")
94 s3.template = "cbi/tblsection"
99 prov = s3:option(ListValue, "resolver_url", translate("Resolver"))
100 for filename in fs.dir(providers_dir) do
101 local p_func = loadfile(providers_dir .. filename)
102 setfenv(p_func, { _ = i18n.translate })
104 prov:value(p.resolver_url, p.label)
106 prov.default = p.resolver_url
109 prov.forcewrite = true
110 prov.write = function(self, section, value)
111 if not value then return end
112 for filename in fs.dir(providers_dir) do
113 local p_func = loadfile(providers_dir .. filename)
114 setfenv(p_func, { _ = i18n.translate })
116 value = value:gsub('[%p%c%s]', '')
117 p.url_match = p.resolver_url:gsub('[%p%c%s]', '')
118 if value:match(p.url_match) then
119 uci:set(packageName, section, "bootstrap_dns", p.bootstrap_dns)
120 uci:set(packageName, section, "resolver_url", p.resolver_url)
123 uci:save(packageName)
126 la = s3:option(Value, "listen_addr", translate("Listen address"))
128 la.placeholder = "127.0.0.1"
132 uci:foreach(packageName, packageName, function(s)
133 if s[".name"] == section then
139 lp = s3:option(Value, "listen_port", translate("Listen port"))
143 sa = s3:option(Value, "edns_subnet", translate("EDNS client subnet"))
146 ps = s3:option(Value, "proxy_server", translate("Proxy server"))