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 readmeURL = "https://docs.openwrt.melmac.net/" .. packageName .. "/"
10 local providers_dir = "/usr/lib/lua/luci/" .. packageName .. "/providers/"
13 function getPackageVersion()
14 local opkgFile = "/usr/lib/opkg/status"
17 for line in io.lines(opkgFile) do
19 return line:match('[%d%.$-]+') or ""
20 elseif line:find("Package: " .. packageName:gsub("%-", "%%%-")) then
27 function createHelperText()
28 local initText = translate("For more information on different options check") .. " "
29 for filename in fs.dir(providers_dir) do
30 local p_func = loadfile(providers_dir .. filename)
31 setfenv(p_func, { _ = i18n.translate })
36 domain = p.help_link_text or url:match('^%w+://([^/]+)')
37 if not helperText:find(domain) then
38 if helperText == "" then
41 helperText = helperText .. ", "
43 helperText = helperText .. [[<a href="]] .. url .. [[" target="_blank">]] .. domain .. [[</a>]]
47 if helperText ~= "" then
48 local a = helperText:gsub('(.*),%s.*$', '%1')
49 helperText = a .. " " .. translate("and") .. helperText:sub(#a + 2) .. "."
53 function getProviderName(value)
54 for filename in fs.dir(providers_dir) do
55 local p_func = loadfile(providers_dir .. filename)
56 setfenv(p_func, { _ = i18n.translate })
58 value = value:gsub('[%p%c%s]', '')
59 p.url_match = p.resolver_url:gsub('[%p%c%s]', '')
60 if value:match(p.url_match) then
64 return translate("Unknown Provider")
67 local packageStatus, packageStatusCode
68 local ubusStatus = util.ubus("service", "list", { name = packageName })
69 local packageVersion = getPackageVersion()
71 if packageVersion == "" then
72 packageStatusCode = -1
73 packageStatus = translatef("%s is not installed or not found", packageName)
75 if not ubusStatus or not ubusStatus[packageName] then
77 packageStatus = translate("Stopped")
78 if not sys.init.enabled(packageName) then
79 packageStatus = packageStatus .. " (" .. translate("disabled") .. ")"
82 packageStatusCode, packageStatus = 1, ""
84 if ubusStatus and ubusStatus[packageName] and
85 ubusStatus[packageName]["instances"] and
86 ubusStatus[packageName]["instances"]["instance" .. n] and
87 ubusStatus[packageName]["instances"]["instance" .. n]["running"] then
88 local value, k, v, url, url_flag, la, la_flag, lp, lp_flag
89 for k, v in pairs(ubusStatus[packageName]["instances"]["instance" .. n]["command"]) do
90 if la_flag then la, la_flag = v, false end
91 if lp_flag then lp, lp_flag = v, false end
92 if url_flag then url, url_flag = v, false end
93 if v == "-a" then la_flag = true end
94 if v == "-p" then lp_flag = true end
95 if v == "-r" then url_flag = true end
97 la = la or "127.0.0.1"
99 packageStatus = packageStatus .. translatef("Running: %s DoH at %s:%s", getProviderName(url), la, lp) .. "\n"
107 m = Map("https-dns-proxy", translate("DNS HTTPS Proxy Settings"))
109 h = m:section(TypedSection, "_dummy", translatef("Service Status [%s %s]", packageName, packageVersion))
110 h.template = "cbi/nullsection"
111 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
112 if packageStatusCode == -1 then
113 ss.template = packageName .. "/status"
114 ss.value = packageStatus
116 if packageStatusCode == 0 then
117 ss.template = packageName .. "/status"
119 ss.template = packageName .. "/status-textarea"
121 ss.value = packageStatus
122 buttons = h:option(DummyValue, "_dummy")
123 buttons.template = packageName .. "/buttons"
126 c = m:section(NamedSection, "config", "https-dns-proxy", translate("Configuration"), translatef("If update DNSMASQ config is selected, when you add/remove any instances below, they will be used to override the 'DNS forwardings' section of %sDHCP and DNS%s (%smore information%s).", "<a href=\"" .. dispatcher.build_url("admin/network/dhcp") .. "\">", "</a>", "<a href=\"" .. readmeURL .. "#default-settings" .. "\" target=\"_blank\">", "</a>"))
127 d1 = c:option(ListValue, "update_dnsmasq_config", translate("Update DNSMASQ Config on Start/Stop"))
128 d1:value('*', translate("Update all configs"))
129 local dnsmasq_num = 0
130 uci:foreach("dhcp", "dnsmasq", function(s)
131 d1:value(tostring(dnsmasq_num), translatef("Update %s config", "dhcp.@dnsmasq[" .. tostring(dnsmasq_num) .. "]"))
132 dnsmasq_num = dnsmasq_num + 1
134 d1:value('-', translate("Do not update configs"))
138 s3 = m:section(TypedSection, "https-dns-proxy", translate("Instances"),
140 s3.template = "cbi/tblsection"
145 prov = s3:option(ListValue, "resolver_url", translate("Resolver"))
146 for filename in fs.dir(providers_dir) do
147 local p_func = loadfile(providers_dir .. filename)
148 setfenv(p_func, { _ = i18n.translate })
150 prov:value(p.resolver_url, p.label)
152 prov.default = p.resolver_url
155 prov.forcewrite = true
156 prov.write = function(self, section, value)
157 if not value then return end
158 for filename in fs.dir(providers_dir) do
159 local p_func = loadfile(providers_dir .. filename)
160 setfenv(p_func, { _ = i18n.translate })
162 value = value:gsub('[%p%c%s]', '')
163 p.url_match = p.resolver_url:gsub('[%p%c%s]', '')
164 if value:match(p.url_match) then
165 uci:set(packageName, section, "bootstrap_dns", p.bootstrap_dns)
166 uci:set(packageName, section, "resolver_url", p.resolver_url)
169 uci:save(packageName)
172 la = s3:option(Value, "listen_addr", translate("Listen Address"))
174 la.placeholder = "127.0.0.1"
178 uci:foreach(packageName, packageName, function(s)
179 if s[".name"] == section then
185 lp = s3:option(Value, "listen_port", translate("Listen Port"))
189 dscp = s3:option(Value, "dscp_codepoint", translate("DSCP Codepoint"))
190 dscp.datatype = "range(0,63)"
193 ps = s3:option(Value, "proxy_server", translate("Proxy Server"))