import { test, after } from "node:test"; import assert from "node:assert/strict"; import { addressInEcho, getThroughProxy, openTunnel, preflightProxy, } from "./networkProxy-server"; import { closedPort, startCountingServer, startEchoServer, startFakeHttpProxy, startFakeSocks5, } from "../testing/fakeProxy"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/networkProxy-server.test.ts // // Every server is in this process, on 127.0.0.1. The destination names // (`echo.exit.test`) are never resolved here: a socks5h proxy is handed the // name, and the fake maps it to the loopback echo server. const echo = await startEchoServer("203.0.113.7\n"); const direct = await startCountingServer(); // The fakes tunnel to the echo server only; anything else is refused. const routeEcho = (host: string, port: number) => ["echo.exit.test", "127.0.0.1", "::1"].includes(host) && port === echo.port ? { host: "127.0.0.1", port } : null; const socksAuth = await startFakeSocks5({ username: "example-user", password: "fake:value#1", route: routeEcho }); const socksOpen = await startFakeSocks5({ route: routeEcho }); const httpProxy = await startFakeHttpProxy({ auth: "example-user:fake-value", route: routeEcho }); after(async () => { await Promise.all([echo.close(), direct.close(), socksAuth.close(), socksOpen.close(), httpProxy.close()]); }); const authUrl = `socks5h://example-user:${encodeURIComponent("fake:value#1")}@127.0.0.1:${socksAuth.port}`; test("preflight: a SOCKS5 proxy that accepts the exit's login answers", async () => { const r = await preflightProxy(authUrl); assert.equal(r.ok, true); assert.equal(socksAuth.connects.length, 0, "a preflight asks no destination"); }); test("preflight: wrong credentials and a dead proxy fail, never naming the password", async () => { const bad = await preflightProxy(`socks5h://example-user:fake-wrong-value@127.0.0.1:${socksAuth.port}`); assert.equal(bad.ok, false); assert.match((bad as { reason: string }).reason, /refused the exit's username and password/); assert.ok(!(bad as { reason: string }).reason.includes("fake-wrong-value")); const dead = await preflightProxy(`socks5h://u:fake-dead-value@127.0.0.1:${await closedPort()}`); assert.equal(dead.ok, false); assert.equal((dead as { reason: string }).reason, "connection refused"); const noCreds = await preflightProxy(`socks5h://127.0.0.1:${socksAuth.port}`); assert.equal(noCreds.ok, false); }); test("socks5h hands the NAME to the proxy (DNS through the proxy)", async () => { const before = socksAuth.connects.length; const r = await getThroughProxy(authUrl, `http://echo.exit.test:${echo.port}/`); assert.deepEqual(r, { ok: true, status: 200, body: "203.0.113.7\n" }); assert.deepEqual(socksAuth.connects[before], { atyp: "domain", host: "echo.exit.test", port: echo.port }); }); test("socks5 resolves here and sends an address", async () => { const r = await getThroughProxy(`socks5://127.0.0.1:${socksOpen.port}`, `http://localhost:${echo.port}/`); assert.equal(r.ok, true); assert.notEqual(socksOpen.connects.at(-1)!.atyp, "domain"); assert.ok(["127.0.0.1", "::1"].includes(socksOpen.connects.at(-1)!.host)); }); test("an HTTP CONNECT proxy with Basic auth tunnels; a 407 is a refusal", async () => { const ok = await getThroughProxy(`http://example-user:fake-value@127.0.0.1:${httpProxy.port}`, `http://echo.exit.test:${echo.port}/`); assert.equal(ok.ok, true); assert.equal(addressInEcho((ok as { body: string }).body), "203.0.113.7"); const no = await openTunnel(`http://example-user:fake-wrong@127.0.0.1:${httpProxy.port}`, "echo.exit.test", echo.port); assert.equal(no.ok, false); assert.match((no as { reason: string }).reason, /HTTP 407/); }); test("a destination the proxy will not reach fails through the proxy — never direct", async () => { const r = await openTunnel(authUrl, "127.0.0.1", direct.port); assert.equal(r.ok, false); assert.match((r as { reason: string }).reason, /could not reach the destination/); assert.equal(direct.connections(), 0); }); test("addressInEcho reads plain and JSON echo bodies", () => { assert.equal(addressInEcho("198.51.100.4"), "198.51.100.4"); assert.equal(addressInEcho('{"ip":"198.51.100.4"}'), "198.51.100.4"); assert.equal(addressInEcho("2001:db8::1\n"), "2001:db8::1"); assert.equal(addressInEcho("nothing"), null); });