luci-app-https-dns-proxy: bugfix: remove eDNS support
[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 providers_dir = "/usr/lib/lua/luci/" .. packageName .. "/providers/"
10 local helperText = ""
11
12 function getPackageVersion()
13 local opkgFile = "/usr/lib/opkg/status"
14 local line
15 local flag = false
16 for line in io.lines(opkgFile) do
17 if flag then
18 return line:match('[%d%.$-]+') or ""
19 elseif line:find("Package: " .. packageName:gsub("%-", "%%%-")) then
20 flag = true
21 end
22 end
23 return ""
24 end
25
26 function createHelperText()
27 local initText = "<br />" .. translate("For more information on different options check") .. " "
28 for filename in fs.dir(providers_dir) do
29 local p_func = loadfile(providers_dir .. filename)
30 setfenv(p_func, { _ = i18n.translate })
31 local p = p_func()
32 if p.help_link then
33 local url, domain
34 url = p.help_link
35 domain = p.help_link_text or url:match('^%w+://([^/]+)')
36 if not helperText:find(domain) then
37 if helperText == "" then
38 helperText = initText
39 else
40 helperText = helperText .. ", "
41 end
42 helperText = helperText .. [[<a href="]] .. url .. [[" target="_blank">]] .. domain .. [[</a>]]
43 end
44 end
45 end
46 if helperText ~= "" then
47 local a = helperText:gsub('(.*),%s.*$', '%1')
48 helperText = a .. " " .. translate("and") .. helperText:sub(#a + 2) .. "."
49 end
50 end
51
52 function getProviderName(value)
53 for filename in fs.dir(providers_dir) do
54 local p_func = loadfile(providers_dir .. filename)
55 setfenv(p_func, { _ = i18n.translate })
56 local p = p_func()
57 value = value:gsub('[%p%c%s]', '')
58 p.url_match = p.resolver_url:gsub('[%p%c%s]', '')
59 if value:match(p.url_match) then
60 return p.label
61 end
62 end
63 return translate("Unknown Provider")
64 end
65
66 local packageStatus, packageStatusCode
67 local ubusStatus = util.ubus("service", "list", { name = packageName })
68 local packageVersion = getPackageVersion()
69
70 if packageVersion == "" then
71 packageStatusCode = -1
72 packageStatus = translatef("%s is not installed or not found", packageName)
73 else
74 if not ubusStatus or not ubusStatus[packageName] then
75 packageStatusCode = 0
76 packageStatus = translate("Stopped")
77 if not sys.init.enabled(packageName) then
78 packageStatus = packageStatus .. " (" .. translate("disabled") .. ")"
79 end
80 else
81 packageStatusCode, packageStatus = 1, ""
82 for n = 1,1000 do
83 if ubusStatus and ubusStatus[packageName] and
84 ubusStatus[packageName]["instances"] and
85 ubusStatus[packageName]["instances"]["instance" .. n] and
86 ubusStatus[packageName]["instances"]["instance" .. n]["running"] then
87 local value, k, v, url, url_flag, la, la_flag, lp, lp_flag
88 for k, v in pairs(ubusStatus[packageName]["instances"]["instance" .. n]["command"]) do
89 if la_flag then la, la_flag = v, false end
90 if lp_flag then lp, lp_flag = v, false end
91 if url_flag then url, url_flag = v, false end
92 if v == "-a" then la_flag = true end
93 if v == "-p" then lp_flag = true end
94 if v == "-r" then url_flag = true end
95 end
96 la = la or "127.0.0.1"
97 lp = lp or n + 5053
98 packageStatus = packageStatus .. translatef("Running: %s DoH at %s:%s", getProviderName(url), la, lp) .. "\n"
99 else
100 break
101 end
102 end
103 end
104 end
105
106 m = Map("https-dns-proxy", translate("DNS HTTPS Proxy Settings"))
107
108 h = m:section(TypedSection, "_dummy", translatef("Service Status [%s %s]", packageName, packageVersion))
109 h.template = "cbi/nullsection"
110 ss = h:option(DummyValue, "_dummy", translate("Service Status"))
111 if packageStatusCode == -1 then
112 ss.template = packageName .. "/status"
113 ss.value = packageStatus
114 else
115 if packageStatusCode == 0 then
116 ss.template = packageName .. "/status"
117 else
118 ss.template = packageName .. "/status-textarea"
119 end
120 ss.value = packageStatus
121 buttons = h:option(DummyValue, "_dummy")
122 buttons.template = packageName .. "/buttons"
123 end
124
125 createHelperText()
126 s3 = m:section(TypedSection, "https-dns-proxy", translate("Instances"),
127 translatef("When you add/remove any instances below, they will be used to override the 'DNS forwardings' section of %sDHCP and DNS%s.", "<a href=\"" .. dispatcher.build_url("admin/network/dhcp") .. "\">", "</a>") .. helperText)
128 s3.template = "cbi/tblsection"
129 s3.sortable = false
130 s3.anonymous = true
131 s3.addremove = true
132
133 prov = s3:option(ListValue, "resolver_url", translate("Resolver"))
134 for filename in fs.dir(providers_dir) do
135 local p_func = loadfile(providers_dir .. filename)
136 setfenv(p_func, { _ = i18n.translate })
137 local p = p_func()
138 prov:value(p.resolver_url, p.label)
139 if p.default then
140 prov.default = p.resolver_url
141 end
142 end
143 prov.forcewrite = true
144 prov.write = function(self, section, value)
145 if not value then return end
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 value = value:gsub('[%p%c%s]', '')
151 p.url_match = p.resolver_url:gsub('[%p%c%s]', '')
152 if value:match(p.url_match) then
153 uci:set(packageName, section, "bootstrap_dns", p.bootstrap_dns)
154 uci:set(packageName, section, "resolver_url", p.resolver_url)
155 end
156 end
157 uci:save(packageName)
158 end
159
160 la = s3:option(Value, "listen_addr", translate("Listen address"))
161 la.datatype = "host"
162 la.placeholder = "127.0.0.1"
163 la.rmempty = true
164
165 local n = 0
166 uci:foreach(packageName, packageName, function(s)
167 if s[".name"] == section then
168 return false
169 end
170 n = n + 1
171 end)
172
173 lp = s3:option(Value, "listen_port", translate("Listen port"))
174 lp.datatype = "port"
175 lp.value = n + 5053
176
177 ps = s3:option(Value, "proxy_server", translate("Proxy server"))
178 ps.rmempty = true
179
180 return m