summaryrefslogtreecommitdiffstats
path: root/net/travelmate/src/net.uc
blob: 5f65b9dd9f56e775e641146f9b5d8a4c43a69121 (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
// travelmate - internet and captive portal check: classifies the curl
// probe reply as net ok, nok or cp (captive portal) with its host

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

export const MARKER = '#trm-meta#';

// the probe reply curl may load at most, in bytes
export const BODY_MAX = 1048576;

// the part of a long body line that is searched for a redirect, in bytes,
// and how much of it lies before the hit
const LINE_MAX = 4096;
const LINE_LEAD = 256;

// lowercased host, port stripped (the host ends up in the dnsmasq rebind
// allowlist), an IPv6 literal keeps its brackets
function host(h) {
	h = lc(h ?? '');

	if (substr(h, 0, 1) == '[') {
		let i = index(h, ']');

		return (i >= 0) ? substr(h, 0, i + 1) : h;
	}

	return replace(h, /:[0-9]*$/, '');
}

export function probe_host(url) {
	let i = index(url, '://');

	if (i >= 0)
		url = substr(url, i + 3);

	return host(split(url, '/')[0]);
};

// bytes above 0x7f as '?', libc regex may refuse invalid UTF-8 from portal
// pages; lengths stay the same, so match offsets still apply
function ascii(s) {
	let out = '';

	for (let i = 0; i < length(s); i++)
		out += (ord(s, i) > 127) ? '?' : substr(s, i, 1);

	return out;
}

// host of an absolute ('scheme://') or scheme-relative ('//') redirect
// target, '' for a relative one; an IPv6 literal keeps its brackets
function target_host(t) {
	t = replace(t, /^[ \t"']+/, '');

	let m = match(t, /^([a-z][a-z0-9+.-]*:)?\/\//);

	if (!m)
		return '';

	t = substr(t, length(m[0]));

	if (substr(t, 0, 1) == '[') {
		let i = index(t, ']');

		return (i >= 0) ? substr(t, 0, i + 1) : t;
	}

	return replace(t, /[:\/?#;>"' \t].*$/, '');
}

// first body line with a redirect, the host of its target; of a longer
// line (minified pages) only the part around the first hit is looked at,
// ascii() and the regex are too expensive for a line of some 100 KB
function redirect(lines, needle, re) {
	for (let l in lines) {
		l = lc(l);

		let i = index(l, needle);

		if (i < 0)
			continue;

		if (length(l) > LINE_MAX)
			l = substr(l, (i > LINE_LEAD) ? i - LINE_LEAD : 0, LINE_MAX);

		l = ascii(l);

		let m = match(l, re);

		if (m)
			return target_host(substr(l, length(m[0])));
	}

	return '';
}

// last word of a curl error message that ends in a domain name
function err_domain(msg) {
	msg = ascii(msg);

	if (!match(msg, /([[:alnum:]_-]{1,63}\.)+[[:alpha:]]+$/))
		return '';

	let w = split(trim(msg), /[ \t]+/);

	return lc(w[length(w) - 1]);
}

// a dns name: letters, digits and hyphens in dot separated labels, at
// least two labels, the last one not numeric; no ip address literal
export function hostname(h) {
	if (type(h) != 'string' || length(h) > 253)
		return false;

	let labels = split(h, '.');

	if (length(labels) < 2 || match(labels[-1], /^[0-9]+$/))
		return false;

	for (let l in labels)
		if (!match(l, /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/i))
			return false;

	return true;
};

export function classify(raw, probe_url, captive) {
	let i = rindex(raw, MARKER);
	let json_raw = (i >= 0) ? substr(raw, i + length(MARKER)) : raw;
	let html_raw = (i >= 0) ? substr(raw, 0, i) : raw;
	let r = {
		result: 'net nok', probe_host: probe_host(probe_url),
		ec: '', rc: '', eff_url: '', cp: '', html: '', js: '', err: ''
	};

	if (length(json_raw)) {
		let meta = null;

		try { meta = json(json_raw); } catch (e) { }

		if (type(meta) == 'object') {
			r.ec = '' + (meta.exitcode ?? '');
			r.rc = '' + (meta.response_code ?? '');
			r.eff_url = '' + (meta.url_effective ?? '');
		}

		r.cp = host(split(r.eff_url, '/')[2]);

		if (r.ec == '0') {
			if (length(r.cp) && r.cp != r.probe_host) {
				r.result = sprintf("net cp '%s'", r.cp);
			}
			else if (r.rc == '200' || r.rc == '204') {
				let lines = split(html_raw, '\n');

				r.html = redirect(lines, 'http-equiv=', /^.*<meta[ \t]+http-equiv=['"]*refresh.*[ \t;]url=/);
				r.js = redirect(lines, 'location.href=', /^.*location\.href=['"]*/);

				if (length(r.html))
					r.result = sprintf("net cp '%s'", r.html);
				else if (length(r.js))
					r.result = sprintf("net cp '%s'", r.js);
				else
					r.result = 'net ok';
			}
		}
		else {
			r.err = (type(meta) == 'object' && type(meta.errormsg) == 'string') ? meta.errormsg : '';

			let ed = err_domain(r.err);

			if (r.ec == '6') {
				if (length(ed) && ed != r.probe_host)
					r.result = sprintf("net cp '%s'", ed);
			}
			else if (length(r.cp) && r.cp != r.probe_host) {
				r.result = sprintf("net cp '%s'", r.cp);
			}
		}
	}

	// without captive portal handling a detected portal is a dead end
	if (substr(r.result, 0, 6) == 'net cp' && !captive)
		r.result = 'net nok';

	let m = match(r.result, /^net cp '(.*)'$/);

	r.net = m ? 'cp' : substr(r.result, 4);
	// the portal host, as far as it consists of host name or address
	// characters; it is logged and, if a dns name, allowlisted
	r.domain = m ? match(m[1], /^[]a-z0-9._:[-]*/)[0] : '';

	return r;
};


// the debug fields of the net check
export function debug(r) {
	let d = (v) => length(v) ? v : '-';

	return sprintf('cp (url/html/js): %s/%s/%s, result: %s, error (rc/msg): %s/%s, probe_host: %s, eff_url: %s',
		d(r.cp), d(r.html), d(r.js), r.result, r.ec, d(r.err), d(r.probe_host), d(r.eff_url));
};