// Fake api.slinky.gg for MITM experiments. // Logs every request (headers + hex body) to server.log. // Serves status/body from response.json (re-read per request). 'use strict'; const tls = require('tls'); const fs = require('fs'); const path = require('path'); const DIR = 'C:/Users/nicot/AppData/Local/Temp/opencode/mitm'; const PFX = path.join(DIR, 'slinky.pfx'); const LOG = path.join(DIR, 'server.log'); const CFG = path.join(DIR, 'response.json'); function log(s) { try { fs.appendFileSync(LOG, s + '\n'); } catch (e) {} } const server = tls.createServer({ pfx: fs.readFileSync(PFX), passphrase: 'slinkymitm', requestCert: true, rejectUnauthorized: false, ALPNProtocols: ['http/1.1'], }, (socket) => { log('=== TLS handshake from ' + socket.remoteAddress + ':' + socket.remotePort + ' alpn=' + socket.alpnProtocol + ' cipher=' + socket.getCipher().name + ' ==='); try { const pc = socket.getPeerCertificate(); if (pc && Object.keys(pc).length > 0) { log('>>> CLIENT CERT PRESENTED: subject=' + JSON.stringify(pc.subject) + ' issuer=' + JSON.stringify(pc.issuer) + ' fpr=' + pc.fingerprint + ' valid=' + pc.valid_from + ' -> ' + pc.valid_to); } else { log('>>> no client certificate presented'); } } catch (e) { log('cert read err: ' + e.message); } let buf = Buffer.alloc(0); socket.on('data', (d) => { buf = Buffer.concat([buf, d]); // process as many pipelined requests as are complete for (;;) { const idx = buf.indexOf('\r\n\r\n'); if (idx === -1) break; const head = buf.slice(0, idx).toString('latin1'); let cl = 0; const m = head.match(/Content-Length:\s*(\d+)/i); if (m) cl = parseInt(m[1], 10); if (buf.length < idx + 4 + cl) break; const body = buf.slice(idx + 4, idx + 4 + cl); log('--- REQUEST ---\n' + head + '\nBODY(' + body.length + 'B hex): ' + body.toString('hex')); let status = 200, payload = Buffer.from(''); let ct = 'application/json'; try { const cfg = JSON.parse(fs.readFileSync(CFG, 'utf8')); status = cfg.status || 200; if (cfg.echo) { payload = body; // challenge-response test: echo client's own body } else if (cfg.bodyB64) { payload = Buffer.from(cfg.bodyB64, 'base64'); } else { payload = Buffer.from(cfg.body || '', 'utf8'); } ct = cfg.contentType || 'application/json'; } catch (e) { status = 500; payload = Buffer.from('{"error":"mitm cfg error"}'); } const reason = status === 200 ? 'OK' : (status === 403 ? 'Forbidden' : (status === 404 ? 'Not Found' : 'Status')); const resp = 'HTTP/1.1 ' + status + ' ' + reason + '\r\n' + 'Content-Type: ' + ct + '\r\n' + 'Content-Length: ' + payload.length + '\r\n' + 'Connection: keep-alive\r\n\r\n'; log('--- RESPONSE ' + status + ' body: ' + payload.toString('latin1').slice(0, 200)); socket.write(Buffer.concat([Buffer.from(resp), payload])); buf = buf.slice(idx + 4 + cl); } }); socket.on('error', (e) => log('sock err: ' + e.message)); }); server.listen(443, '127.0.0.1', () => { console.log('mitm listening 127.0.0.1:443'); }); server.on('error', (e) => { console.log('server err: ' + e.message); process.exit(1); });