summaryrefslogtreecommitdiffhomepage
path: root/protocols/luci-proto-yggdrasil/htdocs/luci-static/resources/protocol/yggdrasil.js
blob: d02095beb4c4d8868a3b1b4f9da887c3645367e1 (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
'use strict';
'require form';
'require network';
'require rpc';
'require tools.widgets as widgets';
'require uci';
'require ui';
network.registerPatternVirtual(/^yggdrasil-.+$/);

function validatePrivateKey(section_id,value) {
	if (value.length == 0) {
		return true;
	};
	if (!value.match(/^([0-9a-fA-F]){128}$/)) {
		if (value != "auto") {
			return _('Invalid private key string %s').format(value);
		}
		return true;
	}
	return true;
};

function validatePublicKey(section_id,value) {
	if (value.length == 0) {
		return true;
	};
	if (!value.match(/^([0-9a-fA-F]){64}$/))
		return _('Invalid public key string %s').format(value);
	return true;
};

function validateYggdrasilListenUri(section_id,value) {
	if (value.length == 0) {
		return true;
	};
	if (!value.match(/^(tls|tcp|unix|quic):\/\//))
		return _('Unsupported URI scheme in %s').format(value);
	return true;
};

function validateYggdrasilPeerUri(section_id,value) {
	if (!value.match(/^(tls|tcp|unix|quic|socks|sockstls):\/\//))
		return _('URI scheme %s not supported').format(value);
	return true;
};

var cbiKeyPairGenerate = form.DummyValue.extend({
	cfgvalue: function(section_id, value) {
		return E('button', {
			'class':'btn',
			'click':ui.createHandlerFn(this, function(section_id,ev) {
				var prv = this.section.getUIElement(section_id,'private_key'),
					pub = this.section.getUIElement(section_id,'public_key'),
					map = this.map;

				return generateKey().then(function(keypair){
					prv.setValue(keypair.priv);
					pub.setValue(keypair.pub);
					map.save(null,true);
				});
			},section_id)
		},[_('Generate new key pair')]);
	}
});

function updateActivePeers(ifname) {
	getPeers(ifname).then(function(peers){
		var table = document.querySelector('#yggdrasil-active-peerings-' + ifname);
		if (table) {
			while (table.rows.length > 1) { table.deleteRow(1); }
			peers.forEach(function(peer) {
				var row = table.insertRow(-1);
				row.style.fontSize = "xx-small";
				if (!peer.up) {
					row.style.opacity = "66%";
				}
				var cell = row.insertCell(-1)
				cell.className = "td"
				cell.textContent = peer.remote;

				cell = row.insertCell(-1)
				cell.className = "td"
				cell.textContent = peer.up ? "Up" : "Down";

				cell = row.insertCell(-1)
				cell.className = "td"
				cell.textContent = peer.inbound ? "In" : "Out";

				cell = row.insertCell(-1)
				cell.className = "td"
				cell.innerHTML = "<u style='cursor: default'>" + peer.address + "</u>"
				cell.dataToggle = "tooltip";
				cell.title = "Key: " + peer.key;

				cell = row.insertCell(-1)
				cell.className = "td"
				cell.textContent = '%t'.format(peer.uptime);

				cell = row.insertCell(-1)
				cell.className = "td"
				cell.textContent = '%.2mB'.format(peer.bytes_recvd);

				cell = row.insertCell(-1)
				cell.className = "td"
				cell.textContent = '%.2mB'.format(peer.bytes_sent);

				cell = row.insertCell(-1)
				cell.className = "td"
				cell.textContent = peer.priority;

				cell = row.insertCell(-1)
				cell.className = "td"
				if (!peer.up) {
					cell.innerHTML = "<u style='cursor: default'>%t ago</u>".format(peer.last_error_time)
					cell.dataToggle = "tooltip"
					cell.title = peer.last_error
				} else {
					cell.innerHTML = "-"
				}
			});
			setTimeout(updateActivePeers.bind(this, ifname), 5000);
		}
	});
}

var cbiActivePeers = form.DummyValue.extend({
	cfgvalue: function(section_id, value) {
		updateActivePeers(this.option);
		return E('table', {
			'class': 'table',
			'id': 'yggdrasil-active-peerings-' + this.option,
		},[
			E('tr', {'class': 'tr'}, [
				E('th', {'class': 'th'}, _('URI')),
				E('th', {'class': 'th'}, _('State')),
				E('th', {'class': 'th'}, _('Dir')),
				E('th', {'class': 'th'}, _('IP Address')),
				E('th', {'class': 'th'}, _('Uptime')),
				E('th', {'class': 'th'}, _('RX')),
				E('th', {'class': 'th'}, _('TX')),
				E('th', {'class': 'th'}, _('Priority')),
				E('th', {'class': 'th'}, _('Last Error')),
			])
		]);
	}
});

var generateKey = rpc.declare({
	object:'luci.yggdrasil',
	method:'generateKeyPair',
	expect:{keys:{}}
});

var getPeers = rpc.declare({
	object:'luci.yggdrasil',
	method:'getPeers',
	params:['interface'],
	expect:{peers:[]}
});

var callIsJumperInstalled = rpc.declare({
	object:'luci.yggdrasil-jumper',
	method:'isInstalled',
	expect:{isInstalled: false}
});

var callValidateJumperConfig = rpc.declare({
	object:'luci.yggdrasil-jumper',
	method:'validateConfig',
	params:['config'],
	expect:{output: "Unknown error."}
});

function validateJumperConfig(section) {
	var last_input = "", last_output = "";

	return function(section_id, input) {
		if (last_input != input) {
			last_input = input

			callValidateJumperConfig(input).then(function(output) {
				last_output = output;

				var option = section.getUIElement(section_id).jumper_config;
				option.triggerValidation(section_id);
			});
		}

		if (last_output.length == 0) {
			return true;
		}

		return _(last_output);
	};
};

return network.registerProtocol('yggdrasil',
	{
		getI18n: function() {
			return _('Yggdrasil Network');
		},
		getIfname: function() {
			return this._ubus('l3_device') || this.sid;
		},
		getType: function() {
			return "tunnel";
		},
		getOpkgPackage: function() {
			return 'yggdrasil';
		},
		isFloating: function() {
			return true;
		},
		isVirtual: function() {
			return true;
		},
		getDevices: function() {
			return null;
		},
		containsDevice: function(ifname) {
			return(network.getIfnameOf(ifname)==this.getIfname());
		},
		renderFormOptions: function(s) {
			var o, ss;
			o=s.taboption('general',form.Value,'private_key',_('Private key'),_('The private key for your Yggdrasil node'));
			o.optional=false;
			o.password=true;
			o.validate=validatePrivateKey;

			o=s.taboption('general',form.Value,'public_key',_('Public key'),_('The public key for your Yggdrasil node'));
			o.optional=true;
			o.validate=validatePublicKey;

			s.taboption('general',cbiKeyPairGenerate,'_gen_server_keypair',' ');

			o=s.taboption('advanced',form.Value,'mtu',_('MTU'),_('A default MTU of 65535 is set by Yggdrasil. It is recomended to utilize the default.'));
			o.optional=true;
			o.placeholder=65535;
			o.datatype='range(1280, 65535)';

			o=s.taboption('general',form.TextValue,'node_info',_('Node info'),_('Optional node info. This must be a { "key": "value", ... } map or set as null. This is entirely optional but, if set, is visible to the whole network on request.'));
			o.optional=true;
			o.placeholder="{}";

			o=s.taboption('general',form.Flag,'node_info_privacy',_('Node info privacy'),_('Enable node info privacy so that only items specified in "Node info" are sent back. Otherwise defaults including the platform, architecture and Yggdrasil version are included.'));
			o.default=o.disabled;

			try {
				s.tab('peers',_('Peers'));
			} catch(e) {};
			o=s.taboption('peers', form.SectionValue, '_active', form.NamedSection, this.sid, "interface", _("Active peers"))
			ss=o.subsection;
			ss.option(cbiActivePeers, this.sid);

			o=s.taboption('peers', form.SectionValue, '_listen', form.NamedSection, this.sid, "interface", _("Listen for peers"))
			ss=o.subsection;

			o=ss.option(form.DynamicList,'listen_address',_('Listen addresses'), _('Add listeners in order to accept incoming peerings from non-local nodes. Multicast peer discovery works regardless of listeners set here. URI Format: <code>tls://0.0.0.0:0</code> or <code>tls://[::]:0</code> to listen on all interfaces. Choose an acceptable URI <code>tls://</code>, <code>tcp://</code>, <code>unix://</code> or <code>quic://</code>'));
			o.placeholder="tls://0.0.0.0:0"
			o.validate=validateYggdrasilListenUri;

			o=s.taboption('peers',form.DynamicList,'allowed_public_key',_('Accept from public keys'),_('If empty, all incoming connections will be allowed (default). This does not affect outgoing peerings, nor link-local peers discovered via multicast.'));
			o.validate=validatePublicKey;

			o=s.taboption('peers', form.SectionValue, '_peers', form.TableSection, 'yggdrasil_%s_peer'.format(this.sid), _("Peer addresses"))
			ss=o.subsection;
			ss.addremove=true;
			ss.anonymous=true;
			ss.addbtntitle=_("Add peer address");

			o=ss.option(form.Value,"address",_("Peer URI"));
			o.placeholder="tls://0.0.0.0:0"
			o.validate=validateYggdrasilPeerUri;
			ss.option(widgets.NetworkSelect,"interface",_("Peer interface"));

			o=s.taboption('peers', form.SectionValue, '_interfaces', form.TableSection, 'yggdrasil_%s_interface'.format(this.sid), _("Multicast rules"))
			ss=o.subsection;
			ss.addbtntitle=_("Add multicast rule");
			ss.addremove=true;
			ss.anonymous=true;

			o=ss.option(widgets.DeviceSelect,"interface",_("Devices"));
			o.multiple=true;

			ss.option(form.Flag,"beacon",_("Send multicast beacon"));

			ss.option(form.Flag,"listen",_("Listen to multicast beacons"));

			o=ss.option(form.Value,"port",_("Port"));
			o.optional=true;
			o.datatype='range(1, 65535)';

			o=ss.option(form.Value,"password",_("Password"));
			o.optional=true;

			// Jumper tab
			try {
				s.tab('jumper',_('Jumper'));
			} catch(e) {};

			o=s.taboption(
				'jumper',
				form.HiddenValue,
				'hidden_value',
				' ',
				_('%s is an independent project that aims to transparently reduce latency of a connection over Yggdrasil network, utilizing NAT traversal to bypass intermediary nodes.'.format('<a href="https://github.com/one-d-wide/yggdrasil-jumper">Yggdrasil Jumper</a>'))
					+ ' ' + _('It periodically probes for active sessions and automatically establishes direct peerings over internet with remote nodes running Yggdrasil Jumper without requiring firewall or port configuration.')
			);

			o=s.taboption(
				'jumper',
				form.Flag,
				'jumper_enable',
				_('Enable Yggdrasil Jumper'),
				_('The checkbox cannot be modified unless the <code>yggdrasil-jumper</code> package is installed.')
			);
			o.default=false;
			o.rmempty=false;
			o.readonly=true;

			// Unlock enable option if jumper is installed
			callIsJumperInstalled().then(function(isInstalled) {
				if (isInstalled) {
					var o = s.children.find(function(o) { return o.option == "jumper_enable"; });
					o.readonly = false;
					// Explicit rerendering request isn't needed because the protocol tab
					// is constructed only after all async functions is done
				}
			});

			o=s.taboption(
				'jumper',
				form.ListValue,
				'jumper_loglevel',
				_('Log level')
			);
			o.value('off', _('Off'));
			o.value('error', _('Error'));
			o.value('warn', _('Warn'));
			o.value('info', _('Info'));
			o.value('debug', _('Debug'));
			o.value('trace', _('Trace'));
			o.default='info';
			o.rmempty=false;

			o=s.taboption(
				'jumper',
				form.Flag,
				'allocate_listen_addresses',
				_('Allocate listen addresses'),
				_('Allow Yggdrasil Jumper to automatically configure Yggdrasil with proper listen address and random port.')
			);
			o.default=true;
			o.rmempty=false;

			o=s.taboption(
				'jumper',
				form.Flag,
				'jumper_autofill_listen_addresses',
				_('Autofill listen addresses'),
				_('Retrieve the listener addresses from the Yggdrasil interface configuration.')
			);
			o.default=true;
			o.rmempty=false;

			o=s.taboption(
				'jumper',
				form.TextValue,
				'jumper_config',
				_('Extra config'),
				_('Additional configuration settings (in TOML format).')
			);
			o.optional=true;
			o.validate=validateJumperConfig(s);

			return;
		},
		deleteConfiguration: function() {
			uci.sections('network', 'yggdrasil_%s_interface'.format(this.sid), function(s) {
				uci.remove('network', s['.name']);
			});
			uci.sections('network', 'yggdrasil_%s_peer'.format(this.sid), function(s) {
				uci.remove('network', s['.name']);
			});
		}
	}
);