1f22f6d6d7826f98ad8b028e125c17d7752c73df
[project/luci.git] /
1 "require ui";
2 "require rpc";
3 "require uci";
4 "require form";
5 "require baseclass";
6
7 var pkg = {
8 get Name() {
9 return "https-dns-proxy";
10 },
11 get URL() {
12 return "https://docs.openwrt.melmac.net/" + pkg.Name + "/";
13 },
14 templateToRegexp: function (template) {
15 return RegExp(
16 "^" +
17 template
18 .split(/(\{\w+\})/g)
19 .map((part) => {
20 let placeholder = part.match(/^\{(\w+)\}$/);
21 if (placeholder) return `(?<${placeholder[1]}>.*?)`;
22 else return part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
23 })
24 .join("") +
25 "$"
26 );
27 },
28 };
29
30 var getInitStatus = rpc.declare({
31 object: "luci." + pkg.Name,
32 method: "getInitStatus",
33 params: ["name"],
34 });
35
36 var getPlatformSupport = rpc.declare({
37 object: "luci." + pkg.Name,
38 method: "getPlatformSupport",
39 params: ["name"],
40 });
41
42 var getProviders = rpc.declare({
43 object: "luci." + pkg.Name,
44 method: "getProviders",
45 params: ["name"],
46 });
47
48 var getRuntime = rpc.declare({
49 object: "luci." + pkg.Name,
50 method: "getRuntime",
51 params: ["name"],
52 });
53
54 return baseclass.extend({
55 title: _("HTTPS DNS Proxy Instances"),
56
57 load: function () {
58 return Promise.all([
59 getInitStatus(pkg.Name),
60 getProviders(pkg.Name),
61 getRuntime(pkg.Name),
62 ]);
63 },
64
65 render: function (data) {
66 var reply = {
67 status: (data[0] && data[0][pkg.Name]) || {
68 enabled: null,
69 running: null,
70 force_dns_active: null,
71 version: null,
72 },
73 providers: (data[1] && data[1][pkg.Name]) || { providers: [] },
74 runtime: (data[2] && data[2][pkg.Name]) || { instances: [] },
75 };
76 reply.providers.sort(function (a, b) {
77 return _(a.title).localeCompare(_(b.title));
78 });
79 reply.providers.push({
80 title: "Custom",
81 template: "{option}",
82 params: { option: { type: "text" } },
83 });
84
85 var forceDnsText = "";
86 if (reply.status.force_dns_active) {
87 reply.status.force_dns_ports.forEach((element) => {
88 forceDnsText += element + " ";
89 });
90 } else {
91 forceDnsText = "-";
92 }
93
94 var table = E(
95 "table",
96 { class: "table", id: "https-dns-proxy_status_table" },
97 [
98 E("tr", { class: "tr table-titles" }, [
99 E("th", { class: "th" }, _("Name / Type")),
100 E("th", { class: "th" }, _("Listen Address")),
101 E("th", { class: "th" }, _("Listen Port")),
102 E("th", { class: "th" }, _("Force DNS Ports")),
103 ]),
104 ]
105 );
106
107 var rows = [];
108 Object.values(reply.runtime.instances).forEach((element) => {
109 var resolver;
110 var address;
111 var port;
112 var name;
113 var option;
114 var found;
115 element.command.forEach((param, index, arr) => {
116 if (param === "-r") resolver = arr[index + 1];
117 if (param === "-a") address = arr[index + 1];
118 if (param === "-p") port = arr[index + 1];
119 });
120 resolver = resolver || "Unknown";
121 address = address || "127.0.0.1";
122 port = port || "Unknown";
123 reply.providers.forEach((prov) => {
124 let regexp = pkg.templateToRegexp(prov.template);
125 if (!found && regexp.test(resolver)) {
126 found = true;
127 name = _(prov.title);
128 let match = resolver.match(regexp);
129 if (match[1] != null) {
130 if (
131 prov.params &&
132 prov.params.option &&
133 prov.params.option.options
134 ) {
135 prov.params.option.options.forEach((opt) => {
136 if (opt.value === match[1]) option = _(opt.description);
137 });
138 name += " (" + option + ")";
139 } else {
140 if (match[1] !== "") name += " (" + match[1] + ")";
141 }
142 }
143 }
144 });
145 rows.push([name, address, port, forceDnsText]);
146 });
147
148 cbi_update_table(table, rows, E("em", _("There are no active instances.")));
149
150 return table;
151 },
152 });