summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-https-dns-proxy/htdocs/luci-static/resources/view/status/include/71_https-dns-proxy.js
blob: 0ebc03c2178c7e15297c28208ae7fa6ecc661921 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"require ui";
"require rpc";
"require uci";
"require form";
"require baseclass";
"require https-dns-proxy.status as hdp";
/* globals hdp */

var pkg = hdp.pkg;

return baseclass.extend({
	title: _("HTTPS DNS Proxy Instances"),

	load: function () {
		return Promise.all([
			hdp.getInitStatus(pkg.Name),
			hdp.getProviders(pkg.Name),
			hdp.getServiceInfo(pkg.Name),
		]);
	},

	render: function (data) {
		try {
			var reply = {
				status: (data[0] && data[0][pkg.Name]) || {},
				providers: (data[1] && data[1][pkg.Name]) || [],
				runtime: (data[2] && data[2][pkg.Name]) || {},
			};
			if (Array.isArray(reply.providers)) {
				reply.providers.sort(function (a, b) {
					return _(a.title).localeCompare(_(b.title));
				});
			} else {
				reply.providers = [];
			}
			reply.providers.push({
				title: "Custom",
				template: "{option}",
				params: { option: { type: "text" } },
			});

			var forceDnsText = "-";
			if (reply.status.force_dns_active && Array.isArray(reply.status.force_dns_ports)) {
				var ports = reply.status.force_dns_ports.join(" ");
				if (ports) forceDnsText = ports;
			}

			var table = E(
				"table",
				{ class: "table", id: "https-dns-proxy_status_table" },
				[
					E("tr", { class: "tr table-titles" }, [
						E("th", { class: "th" }, _("Name / Type")),
						E("th", { class: "th" }, _("Listen Address")),
						E("th", { class: "th" }, _("Listen Port")),
						E("th", { class: "th" }, _("Force DNS Ports")),
					]),
				]
			);

			var rows = [];
			var instances = (reply.runtime && reply.runtime.instances) || {};
			Object.values(instances).forEach((element) => {
				var resolver;
				var address;
				var port;
				var name;
				var option;
				var found;
				if (Array.isArray(element.command)) {
					element.command.forEach((param, index, arr) => {
						if (param === "-r") resolver = arr[index + 1];
						if (param === "-a") address = arr[index + 1];
						if (param === "-p") port = arr[index + 1];
					});
				}
				resolver = resolver || "Unknown";
				address = address || "127.0.0.1";
				port = port || "Unknown";
				reply.providers.forEach((prov) => {
					let regexp = pkg.templateToRegexp(prov.template);
					if (!found && regexp.test(resolver)) {
						found = true;
						name = _(prov.title);
						let match = resolver.match(regexp);
						if (match && match[1] != null) {
							if (
								prov.params &&
								prov.params.option &&
								Array.isArray(prov.params.option.options)
							) {
								prov.params.option.options.forEach((opt) => {
									if (opt.value === match[1]) option = _(opt.description);
								});
								if (option) name += " (" + option + ")";
							} else {
								if (match[1] !== "") name += " (" + match[1] + ")";
							}
						}
					}
				});
				rows.push([name || _("Unknown"), address, port, forceDnsText]);
			});
			cbi_update_table(table, rows, E("em", _("There are no active instances.")));

			return table;
		} catch (e) {
			return E("div", { class: "alert-message warning" },
				_("Unable to retrieve %s status").format("HTTPS DNS Proxy"));
		}
	},
});