| 1 | // Canary server for the attack suite. Every attack vector points at |
| 2 | // 127.0.0.1:9999; anything that arrives here — an HTTP request, a WebSocket |
| 3 | // upgrade, a bare TCP connection (preconnect, TURN/TCP) or a UDP datagram |
| 4 | // (STUN) — is a leak. `GET /__hits` returns the log, `GET /__reset` clears it. |
| 5 |
| 6 | import dgram from 'node:dgram'; |
| 7 | import http from 'node:http'; |
| 8 | import type { Socket } from 'node:net'; |
| 9 | import { WebSocketServer } from 'ws'; |
| 10 |
| 11 | export const CANARY_PORT = 9999; |
| 12 | // CANARY_HOST=0.0.0.0 exposes it on the LAN for the native demos on real devices. |
| 13 | export const CANARY_HOST = process.env.CANARY_HOST ?? '127.0.0.1'; |
| 14 |
| 15 | interface Hit { kind: 'http' | 'ws' | 'tcp' | 'udp'; time: number; url?: string; method?: string; detail?: string } |
| 16 |
| 17 | const hits: Hit[] = []; |
| 18 | const socketRecord = new WeakMap<Socket, Hit>(); |
| 19 |
| 20 | const server = http.createServer((req, res) => { |
| 21 | const url = req.url ?? '/'; |
| 22 | res.setHeader('Access-Control-Allow-Origin', '*'); |
| 23 | res.setHeader('Access-Control-Allow-Private-Network', 'true'); |
| 24 | if (url === '/__hits' || url === '/__reset') { |
| 25 | // Control traffic from the test runner is not a leak: drop the TCP record of this connection. |
| 26 | const rec = socketRecord.get(req.socket); |
| 27 | if (rec) { |
| 28 | const i = hits.indexOf(rec); |
| 29 | if (i >= 0) hits.splice(i, 1); |
| 30 | } |
| 31 | if (url === '/__reset') hits.length = 0; |
| 32 | res.setHeader('Content-Type', 'application/json'); |
| 33 | res.end(JSON.stringify(hits)); |
| 34 | return; |
| 35 | } |
| 36 | const hit: Hit = { kind: 'http', time: Date.now(), method: req.method, url, detail: String(req.headers['sec-fetch-dest'] ?? '') }; |
| 37 | hits.push(hit); |
| 38 | console.log('HIT http', req.method, url); |
| 39 | res.setHeader('Content-Type', 'text/plain'); |
| 40 | res.end('canary'); |
| 41 | }); |
| 42 |
| 43 | server.on('connection', (socket) => { |
| 44 | const hit: Hit = { kind: 'tcp', time: Date.now(), detail: `${socket.remoteAddress}:${socket.remotePort}` }; |
| 45 | hits.push(hit); |
| 46 | socketRecord.set(socket, hit); |
| 47 | }); |
| 48 |
| 49 | const wss = new WebSocketServer({ server }); |
| 50 | wss.on('connection', (_ws, req) => { |
| 51 | hits.push({ kind: 'ws', time: Date.now(), url: req.url }); |
| 52 | console.log('HIT ws', req.url); |
| 53 | }); |
| 54 |
| 55 | const udp = dgram.createSocket('udp4'); |
| 56 | udp.on('message', (msg, rinfo) => { |
| 57 | hits.push({ kind: 'udp', time: Date.now(), detail: `${rinfo.address}:${rinfo.port} ${msg.length} bytes` }); |
| 58 | console.log('HIT udp', rinfo.address, rinfo.port, msg.length, 'bytes'); |
| 59 | }); |
| 60 | udp.bind(CANARY_PORT, CANARY_HOST); |
| 61 |
| 62 | server.listen(CANARY_PORT, CANARY_HOST, () => { |
| 63 | console.log(`canary listening on http://${CANARY_HOST}:${CANARY_PORT} (tcp+ws+udp)`); |
| 64 | }); |
| 65 |