luci-app-https-dns-proxy: package name change, support for RFC8484 binary
[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
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 })
15 local p = p_func()
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
19 return p.label
20 end
21 end
22 return translate("Unknown Provider")
23 end
24
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}'")))
28
29 if not tmpfsVersion or tmpfsVersion == "" then
30 tmpfsStatusCode = -1
31 tmpfsVersion = ""
32 tmpfsStatus = packageName .. " " .. translate("is not installed or not found")
33 else
34 tmpfsVersion = " [" .. packageName .. " " .. tmpfsVersion .. "]"
35 if not ubusStatus or not ubusStatus[packageName] then
36 tmpfsStatusCode = 0
37 tmpfsStatus = translate("Stopped")
38 else
39 tmpfsStatusCode, tmpfsStatus = 1, ""
40 for n = 1,1000 do
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
53 end
54 la = la or "127.0.0.1"
55 lp = lp or n + 5053
56 tmpfsStatus = tmpfsStatus .. translate("Running") .. ": " .. get_provider_name(url) .. " " .. translate("DoH") .. " " .. translate("at") .. " " .. la .. ":" .. lp .. "\n"
57 else
58 break
59 end
60 end
61 end
62 end
63
64 m = Map("https-dns-proxy", translate("DNS Over HTTPS Proxy Settings"))
65
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
72 else
73 if tmpfsStatusCode == 0 then
74 ss.template = packageName .. "/status"
75 else
76 ss.template = packageName .. "/status-textarea"
77 end
78 ss.value = tmpfsStatus
79 buttons = h:option(DummyValue, "_dummy")
80 buttons.template = packageName .. "/buttons"
81 end
82
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>]] .. "."
86 .. "<br />"
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"
95 s3.sortable = false
96 s3.anonymous = true
97 s3.addremove = true
98
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 })
103 local p = p_func()
104 prov:value(p.resolver_url, p.label)
105 if p.default then
106 prov.default = p.resolver_url
107 end
108 end
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 })
115 local p = p_func()
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)
121 end
122 end
123 uci:save(packageName)
124 end
125
126 la = s3:option(Value, "listen_addr", translate("Listen address"))
127 la.datatype = "host"
128 la.placeholder = "127.0.0.1"
129 la.rmempty = true
130
131 local n = 0
132 uci:foreach(packageName, packageName, function(s)
133 if s[".name"] == section then
134 return false
135 end
136 n = n + 1
137 end)
138
139 lp = s3:option(Value, "listen_port", translate("Listen port"))
140 lp.datatype = "port"
141 lp.value = n + 5053
142
143 sa = s3:option(Value, "edns_subnet", translate("EDNS client subnet"))
144 sa.rmempty = true
145
146 ps = s3:option(Value, "proxy_server", translate("Proxy server"))
147 ps.rmempty = true
148
149 return m