const test = require("node:test"); const assert = require("node:assert"); const net = require("node:net"); const { SIGNATURE } = require("./proxy-protocol"); const { proxyFront } = require("./proxy-front"); const header = ip => { const address = Buffer.from(ip.split(".").map(Number)); const body = Buffer.concat([address, address, Buffer.alloc(4)]); const buffer = Buffer.alloc(16 + body.length); SIGNATURE.copy(buffer, 0); buffer[12] = 0x21; buffer[13] = 0x11; buffer.writeUInt16BE(body.length, 14); body.copy(buffer, 16); return buffer; }; const withFront = async (payload, assertOnTunnel) => new Promise((resolve, reject) => { const front = proxyFront({}, tunnel => { try { assertOnTunnel(tunnel); tunnel.destroy(); front.close(() => resolve()); } catch (error) { front.close(() => reject(error)); } }); front.listen(0, "127.0.0.1", () => { const socket = net.connect(front.address().port, "127.0.0.1"); socket.on("connect", () => socket.write(payload)); socket.on("error", () => {}); }); setTimeout(() => reject(new Error("the front did not hand over a connection")), 3000).unref(); }); test("hands the source address of a proxied connection to the TLS socket", async () => { await withFront(header("203.0.113.7"), tunnel => { assert.strictEqual(tunnel.frislitIp, "203.0.113.7"); }); }); test("passes a direct connection through untouched", async () => { await withFront(Buffer.from([0x16, 0x03, 0x01, 0x00, 0x2a]), tunnel => { assert.strictEqual(tunnel.frislitIp, undefined); }); });