// The link check every planted program goes through (the 11-07 rule from the decision pack: green shows, yellow goes to Marty's // queue, red is blocked; rechecked daily; members can report a packet). // verdict(flags, probe) pure: the colour for a packet from the editor's flags (myheads.validate) and the live probe answer // probe(url) the referral template with a sample ID, fetched from a public address only (safety.get: private // addresses refused, 6 s timeout, no body read), up to 3 redirects followed by hand // GREEN the page answers 2xx/3xx on https, stays on the same site (or a www/sub of it), and the text has no flags // YELLOW it works but something needs a person: spam-like words, a link in the description, all caps, plain http, a redirect to // another site, a 4xx that may be a bot wall (403/429), or a failed recheck of a packet that was green before (one miss) // RED refused or taken down: the probe cannot reach it, a 404/410/5xx, a private address, a shortener (myheads refuses those // before the probe), or two failed rechecks in a row 'use strict'; const safety = require('./safety'); const hostOf = u => { try { return new URL(u).hostname.toLowerCase().replace(/^www\./, '').replace(/\.$/, ''); } catch (e) { return ''; } }; const sameSite = (a, b) => a === b || a.endsWith('.' + b) || b.endsWith('.' + a); const sample = url => String(url).replace(/\{id\}/g, 'ypbcheck'); async function probe(url) { let cur = sample(url); const start = hostOf(cur); if (!/^https:\/\//i.test(cur)) return { ok: true, http: true, status: null, host: start, note: 'plain http (not probed)' }; for (let hop = 0; hop < 4; hop++) { const r = await safety.get(cur, 0, 6000); if (r.error === 'private') return { ok: false, hard: true, why: 'points to a private network address' }; if (r.error) return { ok: false, why: 'could not be reached (' + r.error + ')' }; if (r.status >= 300 && r.status < 400 && r.location) { let next; try { next = new URL(r.location, cur).toString(); } catch (e) { return { ok: false, hard: true, why: 'redirects somewhere invalid' }; } if (!/^https:\/\//i.test(next)) return { ok: true, status: r.status, host: hostOf(next), moved: !sameSite(hostOf(next), start), note: 'redirects to plain http' }; cur = next; continue; } const fin = hostOf(cur); if (r.status === 403 || r.status === 429) return { ok: true, status: r.status, host: fin, moved: !sameSite(fin, start), wall: true }; if (r.status >= 400) return { ok: false, hard: r.status === 404 || r.status === 410, status: r.status, why: 'answered ' + r.status }; return { ok: true, status: r.status, host: fin, moved: !sameSite(fin, start) }; } return { ok: false, why: 'redirects too many times' }; } // flags: [] from myheads.validate; pr: probe() answer (or { skipped: true } when probing is off); prev: the packet's last check function verdict(flags, pr, prev) { const why = (flags || []).slice(); if (pr && pr.skipped) return { state: why.length ? 'yellow' : 'green', why, probed: false }; if (!pr || !pr.ok) { const miss = ((prev && prev.misses) || 0) + 1; const r = (pr && pr.why) || 'could not be checked'; // a packet that was green yesterday gets one miss as yellow (sites go down for a night); planting always needs a working link if (prev && prev.state === 'green' && !(pr && pr.hard) && miss < 2) return { state: 'yellow', why: why.concat(['recheck: ' + r]), misses: miss, probed: true }; return { state: 'red', why: why.concat(['link ' + r]), misses: miss, probed: true }; } if (pr.http) why.push('not https'); if (pr.moved) why.push('redirects to another site (' + pr.host + ')'); if (pr.wall) why.push('the site answered ' + pr.status + ' to the checker (may be a bot wall)'); return { state: why.length ? 'yellow' : 'green', why, misses: 0, probed: true, status: pr.status || null }; } module.exports = { probe, verdict, hostOf, sameSite, sample };