// 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 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=', /^.* 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)); };