luci-app-https-dns-proxy: WebUI for dnsmasq config update, quad9 bootstrap IPv6
[project/luci.git] / applications / luci-app-https-dns-proxy / luasrc / model / cbi / https-dns-proxy.lua
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()
7
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/"
11 local helperText = ""
12
13 function getPackageVersion()
14 local opkgFile = "/usr/lib/opkg/status"
15 local line
16 local flag = false
17 for line in io.lines(opkgFile) do
18 if flag then
19 return line:match('[%d%.$-]+') or ""
20 elseif line:find("Package: " .. packageName:gsub("%-", "%%%-")) then
21 flag = true
22 end
23 end
24 return ""
25 end
26
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 })
32 local p = p_func()
33 if p.help_link then
34 local url, domain
35 url = p.help_link
36 domain = p.help_link_text or url:match('^%w+://([^/]+)')
37 if not helperText:find(domain) then
38 if helperText == "" then
39 helperText = initText
40 else
41 helperText = helperText .. ", "
42 end
43 helperText = helperText .. [[<a href="]] .. url .. [[" target="_blank">]] .. domain .. [[</a>]]
44 end
45 end
46 end
47 if helperText ~= "" then
48 local a = helperText:gsub('(.*),%s.*$', '%1')
49 helperText = a .. " " .. translate("and") .. helperText:sub(#a + 2) .. "."
50 end
51 end
52
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 })
57 local p = p_func()
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
61 return p.label
62 end
63 end
64 return translate("Unknown Provider")
65 end
66
67 local packageStatus, packageStatusCode
68 local ubusStatus = util.ubus("service", "list", { name = packageName })
69 local packageVersion = getPackageVersion()
70
71 if packageVersion == "" then
72 packageStatusCode = -1
73 packageStatus = translatef("%s is not installed or not found", packageName)
74 else
75 if not ubusStatus or not ubusStatus[packageName] then
76 packageStatusCode = 0
77 packageStatus = translate("Stopped")
78 if not sys.init.enabled(packageName) then
79 packageStatus = packageStatus .. " (" .. translate("disabled") .. ")"
80 end
81 else
82 packageStatusCode, packageStatus = 1, ""
83 for n = 1,1000 do
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
96 end
97 la = la or "127.0.0.1"
98 lp = lp or n + 5053
99 packageStatus = packageStatus .. translatef("Running: %s DoH at %s:%s", getProviderName(url), la, lp) .. "\n"
100 else
101 break
102 end
103 end
104 end
105 end
106
107 m = Map("https-dns-proxy", translate("DNS HTTPS Proxy Settings"))
108
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
115 else
116 if packageStatusCode == 0 then
117 ss.template = packageName .. "/status"
118 else
119 ss.template = packageName .. "/status-textarea"
120 end
121 ss.value = packageStatus
122 buttons = h:option(DummyValue, "_dummy")
123 buttons.template = packageName .. "/buttons"
124 end
125
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
133 end)
134 d1:value('-', translate("Do not update configs"))
135 d1.default = '*'
136
137 createHelperText()
138 s3 = m:section(TypedSection, "https-dns-proxy", translate("Instances"),
139 helperText)
140 s3.template = "cbi/tblsection"
141 s3.sortable = false
142 s3.anonymous = true
143 s3.addremove = true
144
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 })
149 local p = p_func()
150 prov:value(p.resolver_url, p.label)
151 if p.default then
152 prov.default = p.resolver_url
153 end
154 end
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 })
161 local p = p_func()
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)
167 end
168 end
169 uci:save(packageName)
170 end
171
172 la = s3:option(Value, "listen_addr", translate("Listen address"))
173 la.datatype = "host"
174 la.placeholder = "127.0.0.1"
175 la.rmempty = true
176
177 local n = 0
178 uci:foreach(packageName, packageName, function(s)
179 if s[".name"] == section then
180 return false
181 end
182 n = n + 1
183 end)
184
185 lp = s3:option(Value, "listen_port", translate("Listen port"))
186 lp.datatype = "port"
187 lp.value = n + 5053
188
189 ps = s3:option(Value, "proxy_server", translate("Proxy server"))
190 ps.rmempty = true
191
192 return m