401e1546d1490c8bc99cdaa9d2f6992ad3e16415
[openwrt-luci.git] /
1 "require ui";
2 "require rpc";
3 "require uci";
4 "require form";
5 "require baseclass";
6 "require https-dns-proxy.status as hdp";
7
8 var pkg = hdp.pkg;
9
10 return baseclass.extend({
11         title: _("HTTPS DNS Proxy Instances"),
12
13         load: function () {
14                 return Promise.all([
15                         hdp.getInitStatus(pkg.Name),
16                         hdp.getProviders(pkg.Name),
17                         hdp.getRuntime(pkg.Name),
18                 ]);
19         },
20
21         render: function (data) {
22                 var reply = {
23                         status: (data[0] && data[0][pkg.Name]) || {
24                                 enabled: null,
25                                 running: null,
26                                 force_dns_active: null,
27                                 version: null,
28                         },
29                         providers: (data[1] && data[1][pkg.Name]) || { providers: [] },
30                         runtime: (data[2] && data[2][pkg.Name]) || { instances: [] },
31                 };
32                 reply.providers.sort(function (a, b) {
33                         return _(a.title).localeCompare(_(b.title));
34                 });
35                 reply.providers.push({
36                         title: "Custom",
37                         template: "{option}",
38                         params: { option: { type: "text" } },
39                 });
40
41                 var forceDnsText = "";
42                 if (reply.status.force_dns_active) {
43                         reply.status.force_dns_ports.forEach((element) => {
44                                 forceDnsText += element + " ";
45                         });
46                 } else {
47                         forceDnsText = "-";
48                 }
49
50                 var table = E(
51                         "table",
52                         { class: "table", id: "https-dns-proxy_status_table" },
53                         [
54                                 E("tr", { class: "tr table-titles" }, [
55                                         E("th", { class: "th" }, _("Name / Type")),
56                                         E("th", { class: "th" }, _("Listen Address")),
57                                         E("th", { class: "th" }, _("Listen Port")),
58                                         E("th", { class: "th" }, _("Force DNS Ports")),
59                                 ]),
60                         ]
61                 );
62
63                 var rows = [];
64                 if (reply.runtime.instances) {
65                         Object.values(reply.runtime.instances).forEach((element) => {
66                                 var resolver;
67                                 var address;
68                                 var port;
69                                 var name;
70                                 var option;
71                                 var found;
72                                 element.command.forEach((param, index, arr) => {
73                                         if (param === "-r") resolver = arr[index + 1];
74                                         if (param === "-a") address = arr[index + 1];
75                                         if (param === "-p") port = arr[index + 1];
76                                 });
77                                 resolver = resolver || "Unknown";
78                                 address = address || "127.0.0.1";
79                                 port = port || "Unknown";
80                                 reply.providers.forEach((prov) => {
81                                         let regexp = pkg.templateToRegexp(prov.template);
82                                         if (!found && regexp.test(resolver)) {
83                                                 found = true;
84                                                 name = _(prov.title);
85                                                 let match = resolver.match(regexp);
86                                                 if (match[1] != null) {
87                                                         if (
88                                                                 prov.params &&
89                                                                 prov.params.option &&
90                                                                 prov.params.option.options
91                                                         ) {
92                                                                 prov.params.option.options.forEach((opt) => {
93                                                                         if (opt.value === match[1]) option = _(opt.description);
94                                                                 });
95                                                                 name += " (" + option + ")";
96                                                         } else {
97                                                                 if (match[1] !== "") name += " (" + match[1] + ")";
98                                                         }
99                                                 }
100                                         }
101                                 });
102                                 rows.push([name, address, port, forceDnsText]);
103                         });
104                 }
105                 cbi_update_table(table, rows, E("em", _("There are no active instances.")));
106
107                 return table;
108         },
109 });
git clone https://git.99rst.org/PROJECT