summaryrefslogtreecommitdiffstats
path: root/net/travelmate/src/common.uc
blob: 736e1d8268c39f8dfeceb249feda13bea734cb18 (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
// travelmate - shared settings: typed 'global' options, curl arguments and
// the command runner for the daemon and the login scripts, no shell involved

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

import { popen, access } from 'fs';

export const RUNDIR = '/var/run/travelmate';

// seconds per curl attempt
export const FETCH_TIMEOUT = 5;

// option: [ type, default, min ]; bool: '1' is on, int: default if not a
// number or below min, list: a uci list or the words of an option
export const OPTIONS = {
	trm_enabled: [ 'bool', false ],
	trm_debug: [ 'bool', false ],
	trm_iface: [ 'string', '' ],
	trm_laniface: [ 'string', '' ],
	trm_captive: [ 'bool', true ],
	trm_proactive: [ 'bool', false ],
	trm_vpn: [ 'bool', false ],
	trm_netcheck: [ 'bool', false ],
	trm_autoadd: [ 'bool', false ],
	trm_randomize: [ 'bool', false ],
	trm_eviltwin: [ 'bool', false ],
	trm_mail: [ 'bool', false ],
	trm_mailreceiver: [ 'string', '' ],
	trm_mailsender: [ 'string', 'no-reply@travelmate' ],
	trm_mailtopic: [ 'string', '' ],
	trm_mailprofile: [ 'string', 'trm_notify' ],
	trm_mailtemplate: [ 'string', '/etc/travelmate/mail.template' ],
	trm_minquality: [ 'int', 35 ],
	trm_maxretry: [ 'int', 3 ],
	trm_maxwait: [ 'int', 30, 1 ],
	trm_maxautoadd: [ 'int', 5 ],
	trm_autoaddcnt: [ 'int', 0 ],
	trm_timeout: [ 'int', 30, 1 ],
	trm_radio: [ 'list', [] ],
	trm_revradio: [ 'bool', false ],
	trm_ssidfilter: [ 'list', [] ],
	trm_vpnifacelist: [ 'list', [] ],
	trm_stdvpniface: [ 'string', '' ],
	trm_captiveurl: [ 'string', 'http://detectportal.firefox.com' ],
	trm_useragent: [ 'string', 'Mozilla/5.0 (X11; Linux x86_64; rv:144.0) Gecko/20100101 Firefox/144.0' ]
};

export function words(s) {
	return filter(split(trim('' + (s ?? '')), /[ \t\n]+/), length);
};

// one option value as its type
export function typed(kind, v, dflt, min) {
	if (v == null)
		return dflt;

	switch (kind) {
	case 'bool':
		return v == '1';

	case 'int':
		let n = int(v);

		return (n == n && (min == null || n >= min)) ? n : dflt;

	case 'list':
		return (type(v) == 'array') ? v : words(v);

	default:
		return (type(v) == 'array') ? join(' ', v) : '' + v;
	}
};

// the 'global' section, typed, unknown options are left out
export function global(c) {
	let g = c.get_all('travelmate', 'global') ?? {};
	let cfg = {};

	for (let k, o in OPTIONS)
		cfg[k] = typed(o[0], g[k], o[1], o[2]);

	// the probe url ends up on the curl command line, http(s) only
	if (!match(cfg.trm_captiveurl, /^https?:\/\/[^\/ \t]/i))
		cfg.trm_captiveurl = OPTIONS.trm_captiveurl[1];

	return cfg;
};

// curl arguments of every fetch, http(s) only (also after a redirect) and
// bound to the uplink device if known; one
// retry after a second, a fetch without reply ends after 2 * timeout + 1
// seconds - the daemon is blocked that long and probes again anyway
export function fetch_args(cfg, device) {
	let args = [ '--silent', '--location', '--fail', '--referer', 'http://www.example.com',
		'--retry', '1', '--retry-delay', '1', '--max-time', '' + FETCH_TIMEOUT,
		'--proto', '=http,https', '--proto-redir', '=http,https' ];

	if (length(device ?? ''))
		push(args, '--interface', device);

	return args;
};

// run a command directly (no shell), return { out: stdout, rc: exit code }
export function capture(argv) {
	let p = popen(argv, 'r');

	if (!p)
		return { out: '', rc: -1 };

	let out = p.read('all') ?? '';
	let rc = p.close();

	return { out, rc: rc ?? -1 };
};

// command lookup in PATH, like 'command -v'
export function have(cmd) {
	for (let dir in split(getenv('PATH') ?? '/usr/sbin:/usr/bin:/sbin:/bin', ':'))
		if (length(dir) && access(dir + '/' + cmd, 'x') == true)
			return true;

	return false;
};