8723b6e653a6fc797b67f8c35602df450733bfcf
[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 if (reply.runtime.instances) {
109 Object.values(reply.runtime.instances).forEach((element) => {
110 var resolver;
111 var address;
112 var port;
113 var name;
114 var option;
115 var found;
116 element.command.forEach((param, index, arr) => {
117 if (param === "-r") resolver = arr[index + 1];
118 if (param === "-a") address = arr[index + 1];
119 if (param === "-p") port = arr[index + 1];
120 });
121 resolver = resolver || "Unknown";
122 address = address || "127.0.0.1";
123 port = port || "Unknown";
124 reply.providers.forEach((prov) => {
125 let regexp = pkg.templateToRegexp(prov.template);
126 if (!found && regexp.test(resolver)) {
127 found = true;
128 name = _(prov.title);
129 let match = resolver.match(regexp);
130 if (match[1] != null) {
131 if (
132 prov.params &&
133 prov.params.option &&
134 prov.params.option.options
135 ) {
136 prov.params.option.options.forEach((opt) => {
137 if (opt.value === match[1]) option = _(opt.description);
138 });
139 name += " (" + option + ")";
140 } else {
141 if (match[1] !== "") name += " (" + match[1] + ")";
142 }
143 }
144 }
145 });
146 rows.push([name, address, port, forceDnsText]);
147 });
148 }
149 cbi_update_table(table, rows, E("em", _("There are no active instances.")));
150
151 return table;
152 },
153 });