summaryrefslogtreecommitdiffstats
path: root/net/travelmate/src/login.uc
blob: 8d0c23ace07d69cb9b74fcade8fa83db1b03f176 (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
// travelmate - helpers for the login scripts /etc/travelmate/*.uc (args:
// credentials, exit code 0 on success), test: ucode /etc/travelmate/x.uc u p

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

import { cursor } from 'uci';
import { connect } from 'ubus';
import { query } from 'resolv';
import { global as cfg_global, fetch_args, capture, FETCH_TIMEOUT } from 'travelmate.common';
import { hostname } from 'travelmate.net';

const cfg = cfg_global(cursor());

export const captiveurl = cfg.trm_captiveurl;
export const useragent = cfg.trm_useragent;

let base = null;

// curl arguments like the daemon's, bound to the uplink device
function args() {
	if (base == null) {
		let dev = connect()?.call('network.interface.' + cfg.trm_iface, 'status')?.device;

		base = fetch_args(cfg, (dev != null) ? '' + dev : '');
	}

	return base;
}

// fetch an url with the travelmate curl settings, returns { rc, body }; opts:
// args (extra curl arguments), follow (false: no redirects), write_out
export function fetch(url, opts) {
	let a = [ ...args() ];

	if (opts?.follow === false)
		a = filter(a, (x) => x != '--location');

	let argv = [ 'curl', ...a, '--user-agent', useragent ];

	if (opts?.write_out != null)
		push(argv, '--write-out', opts.write_out);

	// '--url': an url from a portal page must never be taken as option
	push(argv, ...(opts?.args ?? []), '--url', '' + url);

	let r = capture(argv);

	return { rc: r.rc, body: r.out };
};

// true if the dns name resolves to an address, via ucode's resolver
export function lookup(domain) {
	if (!hostname(domain))
		return false;

	let r = query([ domain ], { type: [ 'A', 'AAAA' ], timeout: FETCH_TIMEOUT * 1000, retries: 1 });

	for (let name, rr in (r ?? {}))
		if (length(rr?.A) || length(rr?.AAAA))
			return true;

	return false;
};

// true if url is an https url on host (the login post of a script must not
// go to a host taken from a page of a spoofed hotspot)
export function https_on(url, host) {
	return index(lc('' + url), 'https://' + lc(host) + '/') == 0;
};

// percent-encode everything but unreserved characters, a blank as %20
export function urlencode(s) {
	let out = '';

	for (let i = 0; i < length(s); i++) {
		let c = substr(s, i, 1);

		out += match(c, /^[a-zA-Z0-9.~_-]$/) ? c : sprintf('%%%02X', ord(s, i));
	}

	return out;
};

// per line the text between the first opening and the last closing tag,
// tags matched case-insensitively, all hits joined
export function between(text, open, close) {
	let out = '';

	open = lc(open);
	close = lc(close);

	for (let l in split(text ?? '', '\n')) {
		let ll = lc(l), a = index(ll, open), b = rindex(ll, close);

		if (a >= 0 && b >= a + length(open))
			out += substr(l, a + length(open), b - a - length(open));
	}

	return out;
};