summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-https-dns-proxy/htdocs/luci-static/resources/view/https-dns-proxy/overview.js
blob: 08fa355b5be4ac1cb48a620f364c30e9b43f7bc2 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
// Copyright 2023 MOSSDeF, Stan Grishin <stangri@melmac.ca>
// This code wouldn't have been possible without help from:
// - [@jow-](https://github.com/jow-)
// - [@stokito](https://github.com/stokito)
// - [@vsviridov](https://github.com/vsviridov)
// noinspection JSAnnotator

"use strict";
"require form";
"require rpc";
"require view";
"require https-dns-proxy.status as hdp";

var pkg = hdp.pkg;

return view.extend({
	load: function () {
		return Promise.all([
			L.resolveDefault(hdp.getPlatformSupport(pkg.Name), {}),
			L.resolveDefault(hdp.getProviders(pkg.Name), {}),
			L.resolveDefault(L.uci.load(pkg.Name), {}),
			L.resolveDefault(L.uci.load("dhcp"), {}),
		]);
	},

	render: function (data) {
		var reply = {
			platform: (data[0] && data[0][pkg.Name]) || {
				http2_support: null,
				http3_support: null,
			},
			providers: (data[1] && data[1][pkg.Name]) || [{ title: "empty" }],
		};
		reply.providers.sort(function (a, b) {
			return _(a.title).localeCompare(_(b.title));
		});
		reply.providers.push({
			title: "Custom",
			template: "{option}",
			params: { option: { type: "text" } },
		});

		var status, m, s, o, p;
		var text;

		status = new hdp.status();

		m = new form.Map(pkg.Name, _("HTTPS DNS Proxy - Configuration"));
		s = m.section(form.NamedSection, "config", pkg.Name);

		s.tab("service", _("Service Options"));
		s.tab("global", _("Global Instance Options"));

		var dhcp_dnsmasq_values = Object.values(L.uci.sections("dhcp", "dnsmasq"));
		function isEmpty(obj) {
			return Object.keys(obj).length === 0;
		}

		if (!isEmpty(dhcp_dnsmasq_values)) {
			o = s.taboption(
				"service",
				form.ListValue,
				"dnsmasq_config_update_option",
				_("Update DNSMASQ Config on Start/Stop"),
				_(
					"If update option is selected, the %s'DNS Forwards' section of DHCP and DNS%s will be automatically updated to use selected DoH providers (%smore information%s)."
				).format(
					'<a href="' + L.url("admin", "network", "dhcp") + '">',
					"</a>",
					'<a href="' + pkg.URL + "#default-settings" + '" target="_blank">',
					"</a>"
				)
			);
			o.value("*", _("Update all configs"));
			o.value("+", _("Update select configs"));
			o.value("-", _("Do not update configs"));
			o.default = "*";
			o.retain = true;
			o.cfgvalue = function (section_id) {
				let val = this.map.data.get(
					this.map.config,
					section_id,
					"dnsmasq_config_update"
				);
				if (val && val[0]) {
					switch (val[0]) {
						case "*":
						case "-":
							return val[0];
						default:
							return "+";
					}
				} else return "*";
			};
			o.write = function (section_id, formvalue) {
				L.uci.set(pkg.Name, section_id, "dnsmasq_config_update", formvalue);
			};

			o = s.taboption(
				"service",
				form.MultiValue,
				"dnsmasq_config_update",
				_("Select the DNSMASQ Configs to update")
			);

			dhcp_dnsmasq_values.forEach(function (element) {
				var description;
				var key;
				if (element[".name"] === L.uci.resolveSID("dhcp", element[".name"])) {
					key = element[".index"];
					description = "dnsmasq[" + element[".index"] + "]";
				} else {
					key = element[".name"];
					description = element[".name"];
				}
				o.value(key, description);
			});
			o.depends("dnsmasq_config_update_option", "+");
			o.retain = true;
		}

		o = s.taboption(
			"service",
			form.ListValue,
			"force_dns",
			_("Force Router DNS"),
			_(
				"Forces Router DNS use on local devices, also known as DNS Hijacking. Only works on `lan` interface by default (%smore information%s)."
			).format(
				'<a href="' + pkg.URL + "#force_dns" + '" target="_blank">',
				"</a>"
			)
		);
		o.value("0", _("Let local devices use their own DNS servers if set"));
		o.value("1", _("Force Router DNS server to all local devices"));
		o.default = "1";

		o = s.taboption(
			"service",
			form.ListValue,
			"canary_domains_icloud",
			_("Canary Domains iCloud"),
			_(
				"Blocks access to iCloud Private Relay resolvers, forcing local devices to use router for DNS resolution (%smore information%s)."
			).format(
				'<a href="' + pkg.URL + "#canary_domains_icloud" + '" target="_blank">',
				"</a>"
			)
		);
		o.value("0", _("Let local devices use iCloud Private Relay"));
		o.value("1", _("Force Router DNS server to all local devices"));
		o.depends("force_dns", "1");
		o.default = "1";

		o = s.taboption(
			"service",
			form.ListValue,
			"canary_domains_mozilla",
			_("Canary Domains Mozilla"),
			_(
				"Blocks access to Mozilla Encrypted resolvers, forcing local devices to use router for DNS resolution (%smore information%s)."
			).format(
				'<a href="' +
				pkg.URL +
				"#canary_domains_mozilla" +
				'" target="_blank">',
				"</a>"
			)
		);
		o.value("0", _("Let local devices use Mozilla Private Relay"));
		o.value("1", _("Force Router DNS server to all local devices"));
		o.depends("force_dns", "1");
		o.default = "1";

		o = s.taboption(
			"service",
			form.Value,
			"heartbeat_domain",
			_("Heartbeat Domain"),
			_(
				"The domain used for connectivity checks (%smore information%s)."
			).format(
				'<a href="' + pkg.URL + "#heartbeat_domain" + '" target="_blank">',
				"</a>"
			)
		);
		o.optional = true;
		o.placeholder = "heartbeat.melmac.ca";

		o = s.taboption(
			"service",
			form.Value,
			"heartbeat_sleep_timeout",
			_("Heartbeat Sleep Timeout"),
			_("Time to wait before checking connectivity (seconds).")
		);
		o.datatype = "uinteger";
		o.optional = true;
		o.placeholder = "10";

		o = s.taboption(
			"service",
			form.Value,
			"heartbeat_wait_timeout",
			_("Heartbeat Wait Timeout"),
			_("Time to wait for connectivity check response (seconds).")
		);
		o.datatype = "uinteger";
		o.optional = true;
		o.placeholder = "30";

		o = s.taboption(
			"global",
			form.ListValue,
			"force_http1",
			_("Use HTTP/1")
		);
		o.optional = true;
		o.rmempty = true;
		o.value("", _("Use negotiated HTTP version"));
		o.value("1", _("Force use of HTTP/1"));
		o.default = "";

		o = s.taboption(
			"global",
			form.ListValue,
			"force_http3",
			_("Use HTTP/3 (QUIC)")
		);
		o.optional = true;
		o.rmempty = true;
		o.value("", _("Use negotiated HTTP version"));
		o.value("1", _("Force use of HTTP/3 (QUIC)"));
		o.default = "";
		o.depends("force_http1", "");

		o = s.taboption(
			"global",
			form.ListValue,
			"force_ipv6_resolvers",
			_("Use IPv6 resolvers")
		);
		o.optional = true;
		o.rmempty = true;
		o.value("", _("Use any family DNS resolvers"));
		o.value("1", _("Force use of IPv6 DNS resolvers"));
		o.default = "";

		o = s.taboption("global", form.ListValue, "verbosity", _("Logging Verbosity Level"));
		o.optional = true;
		o.value("", _("0: Fatal"));
		o.value("1", _("1: Error"));
		o.value("2", _("2: Warning"));
		o.value("3", _("3: Info"));
		o.value("4", _("4: Debug"));
		o.default = "";

		o = s.taboption("global", form.Value, "listen_addr", _("Listen Address"));
		o.datatype = "ipaddr('nomask')";
		o.optional = true;
		o.placeholder = "127.0.0.1";

		o = s.taboption("global", form.Value, "user", _("Run As User"));
		o.optional = true;
		o.placeholder = "nobody";

		o = s.taboption("global", form.Value, "group", _("Run As Group"));
		o.optional = true;
		o.placeholder = "nogroup";

		o = s.taboption("global", form.Value, "source_addr", _("Source Address"));
		o.datatype = "ipaddr('nomask')";
		o.optional = true;
		o.placeholder = "";

		o = s.taboption("global", form.Value, "logfile", _("Logging File Path"));
		o.datatype = "file";
		o.optional = true;
		o.placeholder = "";

		o = s.taboption("global", form.Value, "polling_interval", _("Polling Interval"));
		o.datatype = "range(5,3600)";
		o.optional = true;
		o.placeholder = "120";

		o = s.taboption("global", form.Value, "proxy_server", _("Proxy Server"));
		o.optional = true;

		o = s.taboption("global", form.Value, "ca_certs_file", _("CA Certs File"));
		o.datatype = "file";
		o.optional = true;

		o = s.taboption("global", form.Value, "conn_loss_time", _("Connection Loss Time"));
		o.datatype = "uinteger";
		o.optional = true;
		o.placeholder = "15";

		o = s.taboption("global", form.Value, "log_limit", _("Log Limit"));
		o.datatype = "uinteger";
		o.optional = true;
		o.placeholder = "0";

		o = s.taboption("global", form.Value, "max_idle_time", _("Max Idle Time"));
		o.datatype = "uinteger";
		o.optional = true;
		o.placeholder = "118";

		o = s.taboption("global", form.Value, "statistic_interval", _("Statistic Interval"));
		o.datatype = "uinteger";
		o.optional = true;
		o.placeholder = "0";

		o = s.taboption("global", form.Value, "tcp_client_limit", _("TCP Client Limit"));
		o.datatype = "uinteger";
		o.optional = true;
		o.placeholder = "20";

		text = "";
		if (!reply.platform.http2_support)
			text +=
				_(
					"Please note that %s is not supported on this system (%smore information%s)."
				).format(
					"<i>HTTP/2</i>",
					'<a href="' + pkg.URL + "#http2-support" + '" target="_blank">',
					"</a>"
				) + "<br />";
		if (!reply.platform.http3_support)
			text +=
				_(
					"Please note that %s is not supported on this system (%smore information%s)."
				).format(
					"<i>HTTP/3 (QUIC)</i>",
					'<a href="' + pkg.URL + "#http3-quic-support" + '" target="_blank">',
					"</a>"
				) + "<br />";

		s = m.section(
			form.GridSection,
			"https-dns-proxy",
			_("HTTPS DNS Proxy - Instances"),
			text
		);
		s.rowcolors = true;
		s.sortable = true;
		s.anonymous = true;
		s.addremove = true;

		s.sectiontitle = (section_id) => {
			var provText;
			var found;
			reply.providers.forEach((prov) => {
				var option;
				let regexp = pkg.templateToRegexp(prov.template);
				let resolver = L.uci.get(pkg.Name, section_id, "resolver_url");
				resolver = resolver === undefined ? null : resolver;
				if (!found && resolver && regexp.test(resolver)) {
					found = true;
					provText = _(prov.title);
					let match = resolver.match(regexp);
					if (match[1] != null) {
						if (
							prov.params &&
							prov.params.option &&
							prov.params.option.options
						) {
							prov.params.option.options.forEach((opt) => {
								if (opt.value === match[1]) {
									option = _(opt.description);
								}
							});
							provText += " (" + option + ")";
						} else {
							if (match[1] !== "") provText += " (" + match[1] + ")";
						}
					}
				}
			});
			return provText || _("Unknown");
		};

		var _provider;
		_provider = s.option(form.ListValue, "_provider", _("Provider"));
		_provider.modalonly = true;
		_provider.cfgvalue = function (section_id) {
			let resolver = this.map.data.get(
				this.map.config,
				section_id,
				"resolver_url"
			);
			if (resolver === undefined || resolver === null) return null;
			let found;
			let ret;
			reply.providers.forEach((prov, i) => {
				let regexp = pkg.templateToRegexp(prov.template);
				if (!found && regexp.test(resolver)) {
					found = true;
					ret = prov.template;
				}
			});
			return ret || "";
		};

		_provider.write = function (section_id, formvalue) {
			let providerTemplate = formvalue;
			let resolverUrl = providerTemplate;
			let bootstrapDns = "";
			let section = this.section;

			reply.providers.forEach((prov, i) => {
				if (prov.template === providerTemplate) {
					let paramValue = "";
					let paramWidgetOption = "";

					if (prov.params && prov.params.option) {
						if (prov.params.option.type === "select") {
							paramWidgetOption = "_paramList_" + i;
						} else if (prov.params.option.type === "text") {
							paramWidgetOption = "_paramText_" + i;
						}
					}

					if (paramWidgetOption) {
						// Find the widget object to get the value safely
						let widget = section.children.find(w => w.option === paramWidgetOption);
						if (widget) {
							paramValue = widget.formvalue(section_id) || "";
						}
					}

					resolverUrl = pkg.templateToResolver(providerTemplate, {
						option: paramValue
					});

					let bootWidget = section.children.find(w => w.option === "_bootstrap_dns_" + i);
					if (bootWidget) {
						bootstrapDns = bootWidget.formvalue(section_id);
					}

					// Fallback to default if empty
					if (!bootstrapDns) {
						bootstrapDns = prov.bootstrap_dns || "";
					}
				}
			});

			if (resolverUrl) {
				L.uci.set(pkg.Name, section_id, "resolver_url", resolverUrl);
			}

			if (bootstrapDns) {
				L.uci.set(pkg.Name, section_id, "bootstrap_dns", bootstrapDns);
			} else {
				L.uci.unset(pkg.Name, section_id, "bootstrap_dns");
			}
		};
		_provider.remove = function (section_id) {
			L.uci.unset(pkg.Name, section_id, "resolver_url");
			L.uci.unset(pkg.Name, section_id, "bootstrap_dns");
		};

		function createProviderWidget(s, i, prov) {
			if (
				prov.params &&
				prov.params.option &&
				prov.params.option.type
			) {
				if (prov.params.option.type === "select") {
					let optName = prov.params.option.description || _("Parameter");
					var _paramList = s.option(form.ListValue, "_paramList_" + i, optName);
					_paramList.template = prov.template;
					_paramList.modalonly = true;
					if (prov.params.option.default) {
						_paramList.default = prov.params.option.default;
					}
					prov.params.option.options.forEach((opt) => {
						let val = opt.value || "";
						let descr = opt.description || "";
						_paramList.value(val, descr);
					});
					_paramList.depends("_provider", prov.template);
					_paramList.write = function (section_id, formvalue) { };
					_paramList.remove = function (section_id, formvalue) { };
				} else if (prov.params.option.type === "text") {
					let optName = prov.params.option.description || _("Parameter");
					var _paramText = s.option(form.Value, "_paramText_" + i, optName);
					_paramText.template = prov.template;
					_paramText.modalonly = true;
					_paramText.depends("_provider", prov.template);
					_paramText.optional = !(
						prov.params.option.default && prov.params.option.default !== ""
					);
					_paramText.cfgvalue = function (section_id) {
						let resolver = this.map.data.get(
							this.map.config,
							section_id,
							"resolver_url"
						);
						if (resolver === undefined || resolver === null) return null;
						let regexp = pkg.templateToRegexp(prov.template);
						let match = resolver.match(regexp);
						return (match && match[1]) || null;
					};
					_paramText.write = function (section_id, formvalue) { };
					_paramText.remove = function (section_id, formvalue) { };
				}
			}
		}

		function createBootstrapWidget(s, i, prov) {
			const _boot_dns = s.option(
				form.Value,
				"_bootstrap_dns_" + i,
				_("Bootstrap DNS")
			);
			_boot_dns.template = prov.template;
			_boot_dns.modalonly = true;
			_boot_dns.depends("_provider", prov.template);
			_boot_dns.cfgvalue = function (section_id) {
				const c_value = this.map.data.get(
					this.map.config,
					section_id,
					"bootstrap_dns"
				);
				return c_value || prov.bootstrap_dns || "";
			};
			_boot_dns.write = function (section_id, formvalue) { };
			_boot_dns.remove = function (section_id, formvalue) { };
		}

		reply.providers.forEach((prov, i) => {
			if (prov.http2_only && !reply.platform.http2_support) return;
			if (prov.http3_only && !reply.platform.http3_support) return;
			_provider.value(prov.template, _(prov.title));
			createProviderWidget(s, i, prov);
			createBootstrapWidget(s, i, prov);
		});

		o = s.option(form.Value, "listen_addr", _("Listen Address"));
		o.datatype = "ipaddr('nomask')";
		o.optional = true;
		o.placeholder = "127.0.0.1";
		o.textvalue = function (section_id) {
			var value = this.cfgvalue(section_id);
			if (value) return value;
			var globalValue = this.map.data.get(this.map.config, "config", "listen_addr");
			return globalValue || this.placeholder || _("auto");
		};

		o = s.option(form.Value, "listen_port", _("Listen Port"));
		o.datatype = "port";
		o.optional = true;
		o.placeholder = "5053";
		o.textvalue = function (section_id) {
			var value = this.cfgvalue(section_id);
			return value || _("auto");
		};

		o = s.option(form.Value, "source_addr", _("Source (Bind To) Address"));
		o.datatype = "ipaddr('nomask')";
		o.optional = true;
		o.textvalue = function (section_id) {
			var value = this.cfgvalue(section_id);
			return value || _("*");
		};

		o = s.option(form.Value, "user", _("Run As User"));
		o.modalonly = true;
		o.optional = true;
		o.placeholder = "nobody";

		o = s.option(form.Value, "group", _("Run As Group"));
		o.modalonly = true;
		o.optional = true;
		o.placeholder = "nogroup";

		o = s.option(form.Value, "dscp_codepoint", _("DSCP Codepoint"));
		o.datatype = "range(0,63)";
		o.modalonly = true;
		o.optional = true;

		o = s.option(form.ListValue, "verbosity", _("Logging Verbosity Level"));
		o.modalonly = true;
		o.optional = true;
		o.value("", _("0: Fatal"));
		o.value("1", _("1: Error"));
		o.value("2", _("2: Warning"));
		o.value("3", _("3: Info"));
		o.value("4", _("4: Debug"));
		o.default = "";

		o = s.option(form.Value, "logfile", _("Logging File Path"));
		o.modalonly = true;
		o.optional = true;

		o = s.option(form.Value, "polling_interval", _("Polling Interval"));
		o.datatype = "range(5,3600)";
		o.modalonly = true;
		o.optional = true;
		o.placeholder = "120";

		o = s.option(form.Value, "proxy_server", _("Proxy Server"));
		o.modalonly = true;
		o.optional = true;

		o = s.option(form.Value, "ca_certs_file", _("CA Certs File"));
		o.datatype = "file";
		o.modalonly = true;
		o.optional = true;

		o = s.option(form.Value, "conn_loss_time", _("Connection Loss Time"));
		o.datatype = "uinteger";
		o.modalonly = true;
		o.optional = true;
		o.placeholder = "15";

		o = s.option(form.Value, "log_limit", _("Log Limit"));
		o.datatype = "uinteger";
		o.modalonly = true;
		o.optional = true;
		o.placeholder = "0";

		o = s.option(form.Value, "max_idle_time", _("Max Idle Time"));
		o.datatype = "uinteger";
		o.modalonly = true;
		o.optional = true;
		o.placeholder = "118";

		o = s.option(form.Value, "statistic_interval", _("Statistic Interval"));
		o.datatype = "uinteger";
		o.modalonly = true;
		o.optional = true;
		o.placeholder = "0";

		o = s.option(form.Value, "tcp_client_limit", _("TCP Client Limit"));
		o.datatype = "uinteger";
		o.modalonly = true;
		o.optional = true;
		o.placeholder = "20";

		o = s.option(form.ListValue, "force_http1", _("Use HTTP/1"));
		o.modalonly = true;
		o.optional = true;
		o.rmempty = true;
		o.value("", _("Use negotiated HTTP version"));
		o.value("1", _("Force use of HTTP/1"));
		o.default = "";

		o = s.option(form.ListValue, "force_http3", _("Use HTTP/3 (QUIC)"));
		o.modalonly = true;
		o.optional = true;
		o.rmempty = true;
		o.value("", _("Use negotiated HTTP version"));
		o.value("1", _("Force use of HTTP/3 (QUIC)"));
		o.default = "";
		o.depends("force_http1", "");

		o = s.option(form.ListValue, "force_ipv6_resolvers", _("Use IPv6 resolvers"));
		o.modalonly = true;
		o.optional = true;
		o.rmempty = true;
		o.value("", _("Use any family DNS resolvers"));
		o.value("1", _("Force use of IPv6 DNS resolvers"));
		o.default = "";

		return Promise.all([status.render(), m.render()]);
	},
});