| 1 | // Baliza de las pruebas de privacidad: cualquier cosa que llegue aquí es una |
| 2 | // fuga. Un solo puerto TCP que mira el primer byte (0x16 = TLS → HTTPS con un |
| 3 | // certificado propio de esta ejecución; si no, HTTP), cuenta cada conexión |
| 4 | // aceptada (aunque no llegue a pedir nada), las mejoras a WebSocket y los |
| 5 | // CONNECT, y un puerto UDP para STUN (WebRTC). |
| 6 | // |
| 7 | // const b = await startBeacon(); b.hits, b.tcp, b.udp, b.resolverRules |
| 8 | // const b = await startBeacon({ tls: true }) (necesita openssl; sin él, sin HTTPS) |
| 9 | // |
| 10 | // resolverRules (las de siempre): *.test, open-vsx.org y openvsx.eclipsecontent.org |
| 11 | // van a la baliza. strictRules además deja sin resolver cualquier otro nombre |
| 12 | // menos el bucle local y cdn.jsdelivr.net (Monaco, el camino de siempre). |
| 13 | import http from 'node:http'; |
| 14 | import https from 'node:https'; |
| 15 | import net from 'node:net'; |
| 16 | import dgram from 'node:dgram'; |
| 17 | import fs from 'node:fs'; |
| 18 | import os from 'node:os'; |
| 19 | import path from 'node:path'; |
| 20 | import { execFileSync } from 'node:child_process'; |
| 21 | |
| 22 | function selfSigned() { |
| 23 | const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'x2-beacon-')); |
| 24 | try { |
| 25 | execFileSync('openssl', ['req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-days', '1', '-subj', '/CN=beacon.test', |
| 26 | '-keyout', path.join(dir, 'k.pem'), '-out', path.join(dir, 'c.pem')], { stdio: 'ignore' }); |
| 27 | return { key: fs.readFileSync(path.join(dir, 'k.pem')), cert: fs.readFileSync(path.join(dir, 'c.pem')) }; |
| 28 | } catch { return null; } |
| 29 | finally { fs.rmSync(dir, { recursive: true, force: true }); } |
| 30 | } |
| 31 | |
| 32 | export async function startBeacon({ tls = false } = {}) { |
| 33 | const hits = []; // peticiones HTTP(S), mejoras y CONNECT |
| 34 | const tcp = []; // conexiones TCP aceptadas |
| 35 | const udp = []; // paquetes UDP (STUN) |
| 36 | const record = kind => (req, res) => { |
| 37 | hits.push({ kind, host: String(req.headers.host || ''), url: req.url, method: req.method }); |
| 38 | res.writeHead(req.url === '/favicon.ico' ? 404 : 200, { 'Content-Type': 'text/plain', 'Access-Control-Allow-Origin': '*' }); |
| 39 | res.end('beacon'); |
| 40 | }; |
| 41 | const plain = http.createServer(record('http')); |
| 42 | const cert = tls ? selfSigned() : null; |
| 43 | const secure = cert ? https.createServer(cert, record('https')) : null; |
| 44 | for (const s of [plain, secure].filter(Boolean)) { |
| 45 | s.on('upgrade', (req, socket) => { hits.push({ kind: 'upgrade', host: String(req.headers.host || ''), url: req.url }); socket.destroy(); }); |
| 46 | s.on('connect', (req, socket) => { hits.push({ kind: 'connect', host: String(req.headers.host || ''), url: req.url }); socket.destroy(); }); |
| 47 | } |
| 48 | const server = net.createServer(socket => { |
| 49 | tcp.push({ at: tcp.length + 1 }); |
| 50 | socket.once('data', buf => { |
| 51 | socket.pause(); |
| 52 | socket.unshift(buf); |
| 53 | (buf[0] === 0x16 && secure ? secure : plain).emit('connection', socket); |
| 54 | process.nextTick(() => socket.resume()); |
| 55 | }); |
| 56 | socket.on('error', () => {}); |
| 57 | }); |
| 58 | await new Promise(r => server.listen(0, '127.0.0.1', r)); |
| 59 | const port = server.address().port; |
| 60 | const stun = dgram.createSocket('udp4'); |
| 61 | stun.on('message', (msg, rinfo) => udp.push({ bytes: msg.length, from: rinfo.port })); |
| 62 | await new Promise(r => stun.bind(0, '127.0.0.1', r)); |
| 63 | const map = `MAP *.test 127.0.0.1:${port}, MAP open-vsx.org 127.0.0.1:${port}, MAP openvsx.eclipsecontent.org 127.0.0.1:${port}`; |
| 64 | return { |
| 65 | port, hits, tcp, udp, udpPort: stun.address().port, https: !!secure, |
| 66 | resolverRules: map, |
| 67 | strictRules: `${map}, EXCLUDE localhost, EXCLUDE 127.0.0.1, EXCLUDE cdn.jsdelivr.net, MAP * ~NOTFOUND`, |
| 68 | // lo que cuenta como fuga (favicon aparte: una pestaña abierta a propósito lo pide sola) |
| 69 | leaks: () => hits.filter(h => h.url !== '/favicon.ico'), |
| 70 | close: () => Promise.all([ |
| 71 | new Promise(r => server.close(r)), new Promise(r => plain.close(r)), secure ? new Promise(r => secure.close(r)) : null, |
| 72 | new Promise(r => stun.close(r)), |
| 73 | ].filter(Boolean)), |
| 74 | }; |
| 75 | } |