4e7e02524fd8d551e1e262322a90a347e3fa7d33
[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.url_prefix:gsub('[%p%c%s]', '')
18 if value:match(p.url_match) then
19 return p.label
20 end
21 end
22 return translate("Uknown 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 s3.template = "cbi/tblsection"
87 s3.sortable = false
88 s3.anonymous = true
89 s3.addremove = true
90
91 prov = s3:option(ListValue, "url_prefix", translate("Provider"))
92 for filename in fs.dir(providers_dir) do
93 local p_func = loadfile(providers_dir .. filename)
94 setfenv(p_func, { _ = i18n.translate })
95 local p = p_func()
96 prov:value(p.url_prefix, p.label)
97 if p.default then
98 prov.default = p.url_prefix
99 end
100 end
101 prov.forcewrite = true
102 prov.write = function(self, section, value)
103 if not value then return end
104 for filename in fs.dir(providers_dir) do
105 local p_func = loadfile(providers_dir .. filename)
106 setfenv(p_func, { _ = i18n.translate })
107 local p = p_func()
108 value = value:gsub('[%p%c%s]', '')
109 p.url_match = p.url_prefix:gsub('[%p%c%s]', '')
110 if value:match(p.url_match) then
111 uci:set("https_dns_proxy", section, "bootstrap_dns", p.bootstrap_dns)
112 uci:set("https_dns_proxy", section, "url_prefix", p.url_prefix)
113 end
114 end
115 uci:save("https_dns_proxy")
116 end
117
118 la = s3:option(Value, "listen_addr", translate("Listen address"))
119 la.datatype = "host"
120 la.placeholder = "127.0.0.1"
121 la.rmempty = true
122
123 local n = 0
124 uci:foreach("https_dns_proxy", "https_dns_proxy", function(s)
125 if s[".name"] == section then
126 return false
127 end
128 n = n + 1
129 end)
130
131 lp = s3:option(Value, "listen_port", translate("Listen port"))
132 lp.datatype = "port"
133 lp.value = n + 5053
134
135 sa = s3:option(Value, "subnet_addr", translate("Subnet address"))
136 sa.datatype = "host"
137 sa.rmempty = true
138
139 ps = s3:option(Value, "proxy_server", translate("Proxy server"))
140 ps.datatype = "host"
141 ps.rmempty = true
142
143 return m