summaryrefslogtreecommitdiffstats
path: root/net/travelmate/src/vpn.uc
blob: 6dd5da51907d6703100a43a7120e2ae99df8d0db (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
// travelmate - vpn handling decisions: plan() returns the steps for an action
// (op: down, clear, rc, pgm, check), infolist() the usable vpn interfaces

// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (c) 2016-2026 Dirk Brenken <dev@brenken.org>

// netifd protocols that are vpn interfaces without further notice
const VPN_PROTOS = [ 'wireguard', 'openvpn' ];

// the vpn service of a network interface section, its netifd protocol
export function service(s) {
	return '' + (s?.proto ?? '');
};

// st: { vpn, list, action, connection, connected, ntp }, up: { vpn, iface,
// service } of the current uplink; returns { steps, pgm }
export function plan(st, up, is_up) {
	let out = [], connected = st.connected, action = st.action, pgm = false;

	if (!st.vpn || !length(st.list))
		return { steps: out, pgm };

	// initial cleanup: tear down all known vpn interfaces
	if (!connected || action == 'enable') {
		for (let iface in st.list)
			if (is_up(iface))
				push(out, { op: 'down', iface, why: 'initial' });

		push(out, { op: 'clear' });
		connected = false;
	}

	// switch: tear down a foreign vpn interface, keep the configured one
	else if (up.vpn && length(up.iface) && action == 'enable_keep') {
		for (let iface in st.list) {
			if (is_up(iface) && iface != up.iface) {
				push(out, { op: 'down', iface, why: 'switch' }, { op: 'rc' }, { op: 'clear' });
				connected = false;
				break;
			}
		}
	}

	// start or stop the vpn connection on valid enable/disable transitions
	if (length(up.service) && length(up.iface)) {
		let enable = (action == 'enable' || action == 'enable_keep');

		pgm = true;

		if ((action == 'disable' && connected) ||
		    (!up.vpn && enable && connected) ||
		    (st.ntp && up.vpn && enable && !connected)) {
			if (st.connection || action == 'disable')
				push(out, { op: 'pgm', action: up.vpn ? action : 'disable', iface: up.iface, service: up.service });
		}

		// an established vpn connection of a working uplink is verified
		// in every run cycle
		else if (up.vpn && action == 'enable_keep' && connected && st.connection) {
			push(out, { op: 'check', iface: up.iface, service: up.service });
		}
	}

	return { steps: out, pgm };
};

// usable: all wireguard/openvpn interfaces and any other netifd protocol
// that is referenced (refs) or listed in trm_vpnifacelist
export function infolist(ifaces, filter_list, refs) {
	let list = [], logs = [];
	let dash = (v) => length(v ?? '') ? v : '-';

	for (let s in ifaces) {
		let iface = s['.name'], proto = service(s);

		if ((!length(filter_list) || iface in filter_list) && !(iface in list) &&
		    (proto in VPN_PROTOS || (length(proto) && proto != 'none' && (length(filter_list) || iface in (refs ?? [])))))
			push(list, iface);

		push(logs, sprintf('f_getvpn    ::: iface: %s, proto: %s, vpn_ifacelist: %s, vpn_infolist: %s',
			dash(iface), dash(proto), dash(join(' ', filter_list)), dash(join(' ', list))));
	}

	return { list, logs };
};