'use strict'; /* * fast.benlamb.net — a teaching speed test for ITN 101. * * This is the whole back end. It does five small jobs: * * GET /api/ping answer instantly, so the browser can time a round trip * GET /api/down send N bytes of random data (the download test) * POST /api/up receive bytes and throw them away (the upload test) * GET /api/whoami look up the visitor's IP address in a local GeoIP database * GET /api/trace run a traceroute from this server back to the visitor * * It listens only on 127.0.0.1. nginx sits in front, handles HTTPS on port 443, * and tells us the visitor's real IP address in the X-Real-IP header. */ const http = require('node:http'); const net = require('node:net'); const path = require('node:path'); const crypto = require('node:crypto'); const dns = require('node:dns').promises; const { spawn } = require('node:child_process'); const maxmind = require('maxmind'); // ---------- settings (all overridable with environment variables) ---------- const PORT = Number(process.env.PORT || 8742); const HOST = process.env.HOST || '127.0.0.1'; const GEO_DIR = process.env.GEO_DIR || path.join(__dirname, 'geo'); const SERVER_IP = process.env.SERVER_IP || '148.230.90.150'; const OPERATOR_NOTE = process.env.OPERATOR_NOTE || 'A rented virtual private server (VPS) in a Hostinger data center just outside Boston, Massachusetts.'; const MAX_DOWN_BYTES = Number(process.env.MAX_DOWN_BYTES || 50e6); // per request const MAX_UP_BYTES = Number(process.env.MAX_UP_BYTES || 32 * 2 ** 20); // per request const MAX_STREAMS = Number(process.env.MAX_STREAMS || 300); // transfers at once const DAILY_BUDGET_BYTES = Number(process.env.DAILY_BUDGET_BYTES || 200e9); const MAX_TRACES = Number(process.env.MAX_TRACES || 4); // traceroutes at once const TRACE_CACHE_MS = Number(process.env.TRACE_CACHE_MS || 10 * 60 * 1000); const MTR = process.env.MTR || '/usr/bin/mtr'; // One mebibyte of random bytes, reused for every download. Random data cannot // be compressed, so nothing between us and the browser can "cheat" the test. const CHUNK = crypto.randomBytes(2 ** 20); // ---------- data budget: protects the server's monthly bandwidth ---------- let budgetDay = ''; let bytesToday = 0; let activeStreams = 0; function account(n) { const day = new Date().toISOString().slice(0, 10); if (day !== budgetDay) { if (budgetDay) console.log(`budget: ${budgetDay} used ${(bytesToday / 1e9).toFixed(2)} GB`); budgetDay = day; bytesToday = 0; } bytesToday += n; } function overBudget() { account(0); return bytesToday >= DAILY_BUDGET_BYTES; } // ---------- small helpers ---------- const NO_STORE = { 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' }; function sendJson(res, status, body) { const data = JSON.stringify(body); res.writeHead(status, { ...NO_STORE, 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': Buffer.byteLength(data), }); res.end(data); } function clientIp(req) { let ip = String(req.headers['x-real-ip'] || req.socket.remoteAddress || ''); if (ip.startsWith('::ffff:')) ip = ip.slice(7); return net.isIP(ip) ? ip : ''; } // What kind of address is this? ITN 101 students meet all of these. function ipKind(ip) { if (net.isIPv4(ip)) { const [a, b] = ip.split('.').map(Number); if (a === 10 || (a === 172 && b >= 16 && b <= 31) || (a === 192 && b === 168)) return 'private'; if (a === 100 && b >= 64 && b <= 127) return 'cgnat'; if (a === 169 && b === 254) return 'link-local'; if (a === 127) return 'loopback'; return 'public'; } const low = ip.toLowerCase(); if (low === '::1') return 'loopback'; if (low.startsWith('fe8') || low.startsWith('fe9') || low.startsWith('fea') || low.startsWith('feb')) return 'link-local'; if (low.startsWith('fc') || low.startsWith('fd')) return 'private'; return 'public'; } // ---------- GeoIP: a database file on this server's disk ---------- let cityDb = null; let asnDb = null; let geoBuilt = null; async function loadGeo() { try { cityDb = await maxmind.open(path.join(GEO_DIR, 'city.mmdb')); asnDb = await maxmind.open(path.join(GEO_DIR, 'asn.mmdb')); geoBuilt = cityDb.metadata.buildEpoch.toISOString().slice(0, 10); console.log(`geo: loaded ${cityDb.metadata.databaseType}, built ${geoBuilt}`); } catch (err) { console.error('geo: databases not available —', err.message); } } function lookup(ip) { const kind = ipKind(ip); const out = { ip, version: net.isIPv6(ip) ? 6 : 4, kind }; if (kind !== 'public') return out; try { const c = cityDb && cityDb.get(ip); if (c) { out.city = c.city && c.city.names && c.city.names.en; out.region = c.subdivisions && c.subdivisions[0] && c.subdivisions[0].names && c.subdivisions[0].names.en; out.country = c.country && c.country.names && c.country.names.en; out.countryCode = c.country && c.country.iso_code; if (c.location && typeof c.location.latitude === 'number') { out.lat = c.location.latitude; out.lon = c.location.longitude; } } const a = asnDb && asnDb.get(ip); if (a) { out.asn = a.autonomous_system_number; out.org = a.autonomous_system_organization; } } catch { /* an unknown address is fine */ } return out; } // Great-circle ("as the crow flies") distance between two points on Earth. function distanceKm(a, b) { if (a.lat == null || b.lat == null) return null; const rad = (d) => (d * Math.PI) / 180; const dLat = rad(b.lat - a.lat); const dLon = rad(b.lon - a.lon); const h = Math.sin(dLat / 2) ** 2 + Math.cos(rad(a.lat)) * Math.cos(rad(b.lat)) * Math.sin(dLon / 2) ** 2; return 6371 * 2 * Math.asin(Math.sqrt(h)); } // ---------- /api/down ---------- function handleDown(req, res, url) { let total = Math.floor(Number(url.searchParams.get('bytes')) || 1e6); total = Math.max(1, Math.min(total, MAX_DOWN_BYTES)); if (overBudget()) return sendJson(res, 503, { error: 'budget', message: 'The server has used its data budget for today.' }); if (activeStreams >= MAX_STREAMS) return sendJson(res, 503, { error: 'busy', message: 'The server is busy. Try again in a moment.' }); activeStreams++; let left = total; res.writeHead(200, { ...NO_STORE, 'Content-Type': 'application/octet-stream', 'Content-Encoding': 'identity', 'Content-Length': total, }); res.on('close', () => { left = 0; activeStreams--; }); // Write until the socket's buffer is full, wait for it to drain, repeat. // That waiting is "backpressure": we can only send as fast as the path allows. const pump = () => { while (left > 0) { const n = Math.min(left, CHUNK.length); left -= n; account(n); if (!res.write(n === CHUNK.length ? CHUNK : CHUNK.subarray(0, n))) { res.once('drain', pump); return; } } res.end(); }; pump(); } // ---------- /api/up ---------- function handleUp(req, res) { if (overBudget()) { req.resume(); return sendJson(res, 503, { error: 'budget', message: 'The server has used its data budget for today.' }); } if (activeStreams >= MAX_STREAMS) { req.resume(); return sendJson(res, 503, { error: 'busy', message: 'The server is busy. Try again in a moment.' }); } activeStreams++; const started = process.hrtime.bigint(); let bytes = 0; let finished = false; req.on('data', (buf) => { bytes += buf.length; // count it... account(buf.length); // ...and that is all. The data is never saved. if (bytes > MAX_UP_BYTES && !finished) { finished = true; sendJson(res, 413, { error: 'too-large' }); req.destroy(); } }); req.on('end', () => { if (finished) return; finished = true; const ms = Number(process.hrtime.bigint() - started) / 1e6; sendJson(res, 200, { bytes, ms: Math.round(ms) }); }); req.on('close', () => { activeStreams--; }); } // ---------- /api/whoami ---------- function handleWhoami(req, res) { const client = lookup(clientIp(req)); const server = lookup(SERVER_IP); const km = distanceKm(client, server); sendJson(res, 200, { client, server: { ...server, operatorNote: OPERATOR_NOTE }, distanceKm: km == null ? null : Math.round(km), // Light in glass fiber covers roughly 200 km per millisecond (about 2/3 of // its speed in a vacuum). A round trip is there AND back. minRttMs: km == null ? null : Math.round(((2 * km) / 200) * 10) / 10, geoDatabase: { name: 'DB-IP Lite', built: geoBuilt }, }); } // ---------- /api/trace ---------- // A "job" is one traceroute. Everyone asking from the same public IP address // shares one job (a whole classroom behind one NAT router looks like one IP). const jobs = new Map(); // ip -> job let runningTraces = 0; async function reverseName(ip) { try { const names = await Promise.race([ dns.reverse(ip), new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 2000)), ]); const name = String(names[0] || ''); // PTR records are written by whoever owns the address — never trust them. return /^[A-Za-z0-9._-]{1,253}$/.test(name) ? name : null; } catch { return null; } } function startTrace(ip) { const job = { ip, startedAt: Date.now(), finishedAt: null, events: [], listeners: new Set(), hops: new Map(), targetIdx: Infinity }; jobs.set(ip, job); runningTraces++; const emit = (ev) => { job.events.push(ev); for (const fn of job.listeners) fn(ev); }; const snapshot = (hop) => ({ type: 'hop', n: hop.n, ip: hop.ip, kind: hop.kind, host: hop.host, others: hop.others.length, asn: hop.asn, org: hop.org, city: hop.city, region: hop.region, countryCode: hop.countryCode, sent: hop.sent, rtts: hop.rtts, isTarget: hop.ip === ip, }); const hopAt = (idx) => { if (!job.hops.has(idx)) { job.hops.set(idx, { n: idx + 1, ip: null, kind: null, host: null, others: [], sent: 0, rtts: [] }); } return job.hops.get(idx); }; emit({ type: 'start', target: ip, at: job.startedAt }); // mtr sends packets with TTL 1, 2, 3 ... and listens for the ICMP // "time exceeded" message each router sends back. --raw prints one line per // event; -n means "numbers only" (we do the name lookups ourselves). // The target is always the visitor's own IP address, never anything typed in. const child = spawn(MTR, ['--raw', '-n', '-c', '3', '-i', '1', '-G', '1', '-Z', '1', '-m', '24', ip], { stdio: ['ignore', 'pipe', 'ignore'] }); const killer = setTimeout(() => child.kill('SIGKILL'), 45000); const pending = []; let buffer = ''; child.stdout.on('data', (data) => { buffer += data; let nl; while ((nl = buffer.indexOf('\n')) >= 0) { const [kind, idxText, value] = buffer.slice(0, nl).trim().split(/\s+/); buffer = buffer.slice(nl + 1); const idx = Number(idxText); if (!Number.isInteger(idx) || idx < 0 || idx > 40) continue; // The destination answers every probe whose TTL is big enough, so mtr can // list it at several hop numbers. Only its first appearance is the real one. if (idx > job.targetIdx) continue; const hop = hopAt(idx); if (kind === 'x') { hop.sent++; // a probe was sent } else if (kind === 'h' && net.isIP(value || '')) { if (!hop.ip) { // a router answered Object.assign(hop, lookup(value), { n: idx + 1 }); if (value === ip) job.targetIdx = idx; if (hop.kind === 'public') { pending.push(reverseName(value).then((name) => { if (name) { hop.host = name; emit(snapshot(hop)); } })); } emit(snapshot(hop)); } else if (value !== hop.ip && !hop.others.includes(value)) { hop.others.push(value); // load balancing: a second router at this hop } } else if (kind === 'p' && Number.isFinite(Number(value))) { hop.rtts.push(Math.round(Number(value) / 10) / 100); // microseconds -> ms emit(snapshot(hop)); } } }); const finish = async (failed) => { clearTimeout(killer); await Promise.allSettled(pending); const hops = [...job.hops.values()].sort((a, b) => a.n - b.n); // Drop silent hops after the last one that answered. let last = hops.length - 1; while (last >= 0 && !hops[last].ip) last--; if (Number.isFinite(job.targetIdx)) last = Math.min(last, hops.findIndex((h) => h.n === job.targetIdx + 1)); // If the tail is one router answering over and over, the packets were // going in circles (or nowhere). Keep only its first appearance. if (last >= 0 && hops[last].ip !== ip) { const tailIp = hops[last].ip; let first = last; for (let i = last; i >= 0; i--) { if (hops[i].ip === tailIp) first = i; else if (hops[i].ip) break; } last = first; } for (const hop of hops.slice(0, last + 1)) emit(snapshot(hop)); job.finishedAt = Date.now(); emit({ type: 'done', failed: Boolean(failed), hops: last + 1, reached: hops.some((h) => h.ip === ip), seconds: Math.round((job.finishedAt - job.startedAt) / 100) / 10, }); job.listeners.clear(); runningTraces--; setTimeout(() => { if (jobs.get(ip) === job) jobs.delete(ip); }, TRACE_CACHE_MS).unref(); }; let ended = false; child.on('error', () => { if (!ended) { ended = true; finish(true); } }); child.on('close', (code) => { if (!ended) { ended = true; finish(code !== 0 && job.hops.size === 0); } }); return job; } function handleTrace(req, res) { const ip = clientIp(req); res.writeHead(200, { ...NO_STORE, 'Content-Type': 'application/x-ndjson; charset=utf-8', 'X-Accel-Buffering': 'no' }); const write = (ev) => res.write(JSON.stringify(ev) + '\n'); if (!ip || ipKind(ip) !== 'public') { write({ type: 'unavailable', reason: 'not-public', ip }); return res.end(); } let job = jobs.get(ip); if (!job) { if (runningTraces >= MAX_TRACES) { write({ type: 'unavailable', reason: 'busy' }); return res.end(); } job = startTrace(ip); } else if (job.finishedAt) { write({ type: 'cached', ageSeconds: Math.round((Date.now() - job.finishedAt) / 1000) }); } for (const ev of job.events) write(ev); // replay what already happened if (job.finishedAt) return res.end(); const listener = (ev) => { write(ev); if (ev.type === 'done') res.end(); }; job.listeners.add(listener); // then follow along live res.on('close', () => job.listeners.delete(listener)); } // ---------- the web server ---------- const server = http.createServer((req, res) => { const url = new URL(req.url, 'http://localhost'); const route = `${req.method} ${url.pathname}`; switch (route) { case 'GET /api/ping': return sendJson(res, 200, { pong: true }); case 'GET /api/down': return handleDown(req, res, url); case 'POST /api/up': return handleUp(req, res); case 'GET /api/whoami': return handleWhoami(req, res); case 'GET /api/trace': return handleTrace(req, res); default: req.resume(); return sendJson(res, 404, { error: 'not-found' }); } }); server.keepAliveTimeout = 65000; loadGeo().then(() => { server.listen(PORT, HOST, () => console.log(`fast: listening on http://${HOST}:${PORT}`)); });