import { test, after } from "node:test"; import assert from "node:assert/strict"; import http from "node:http"; import net from "node:net"; import { execFileSync } from "node:child_process"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test ytdlp/networkExitRealYtdlp.test.ts // // THE REAL yt-dlp ON PATH, through the routed spawn (runYtdlp's // listThroughExit), against an in-process fake SOCKS5 proxy and a loopback // fake site (an RSS feed the generic extractor lists). Nothing leaves // 127.0.0.1: the listing URL names a TRAP port that only counts connections; // the proxy maps a CONNECT to that port onto the real site. So a listing that // arrives at the site came through the proxy, and one that went direct hits // the trap. Skipped when no yt-dlp is installed. const ROOT = mkdtempSync(path.join(os.tmpdir(), "net-real-ytdlp-")); process.env.TRANSCRIPTS_DIR = ROOT; process.env.SETTINGS_FILE = path.join(ROOT, "settings.json"); writeFileSync(process.env.SETTINGS_FILE, "{}\n"); let haveYtdlp = true; try { execFileSync("yt-dlp", ["--version"], { stdio: "ignore", timeout: 30_000 }); } catch { haveYtdlp = false; } const { listThroughExit } = await import("./runYtdlp"); const { startCountingServer, startFakeSocks5 } = await import("../testing/fakeProxy"); const RSS = 't' + "ahttp://127.0.0.1:1/v1-a.html" + "bhttp://127.0.0.1:1/v2-b.html"; const seen: Array> = []; const site = http.createServer((req, res) => { seen.push(req.headers); res.writeHead(200, { "content-type": "application/rss+xml" }); res.end(RSS); }); await new Promise((r) => site.listen(0, "127.0.0.1", () => r())); const sitePort = (site.address() as net.AddressInfo).port; const trap = await startCountingServer(); const FAKE_CRED = "fake-not-a-credential"; const proxy = await startFakeSocks5({ username: "example-user-r", password: FAKE_CRED, route: (_h, p) => (p === trap.port ? { host: "127.0.0.1", port: sitePort } : null), }); const refusing = await startFakeSocks5({ route: () => null }); after(async () => { await Promise.all([trap.close(), proxy.close(), refusing.close(), new Promise((r) => site.close(r))]); rmSync(ROOT, { recursive: true, force: true }); }); const FEED = `http://127.0.0.1:${trap.port}/feed.rss`; const PROXY_URL = `socks5h://example-user-r:${FAKE_CRED}@127.0.0.1:${proxy.port}`; async function routed(extra: string[], opts: { proxyUrl?: string; cwd?: string } = {}) { const log: string[] = []; const urls = await listThroughExit({ bin: "yt-dlp", cwd: opts.cwd ?? ROOT, args: ["--flat-playlist", "--skip-download", "--print", "url", ...extra, FEED], exit: "exit-r", platform: "rumble", proxyUrl: opts.proxyUrl ?? PROXY_URL, secrets: [FAKE_CRED, `example-user-r:${FAKE_CRED}`, "example-user-r"], onLog: (s) => log.push(s), signal: new AbortController().signal, }); return { urls, log: log.join("") }; } const skip = haveYtdlp ? false : "no yt-dlp on PATH"; test("the listing arrives through the proxy, not directly — urllib and curl_cffi (impersonate)", { skip }, async () => { for (const extra of [[], ["--impersonate", "chrome"]]) { const before = { site: seen.length, connects: proxy.connects.length }; const r = await routed(extra); assert.deepEqual(r.urls, ["http://127.0.0.1:1/v1-a.html", "http://127.0.0.1:1/v2-b.html"], extra.join(" ")); assert.equal(seen.length, before.site + 1); assert.deepEqual(proxy.connects.slice(before.connects), [{ atyp: "ipv4", host: "127.0.0.1", port: trap.port }]); assert.equal(trap.connections(), 0, "nothing went direct"); assert.ok(!r.log.includes(FAKE_CRED) && !r.log.includes("example-user-r"), r.log); } }); test("an inherited NO_PROXY=* and HTTP(S)_PROXY cannot send it direct (finding 2)", { skip }, async () => { const saved = { ...process.env }; Object.assign(process.env, { NO_PROXY: "*", no_proxy: "*", HTTP_PROXY: `http://127.0.0.1:${trap.port}`, HTTPS_PROXY: `http://127.0.0.1:${trap.port}` }); try { const before = proxy.connects.length; const r = await routed(["--impersonate", "chrome"]); assert.equal(r.urls.length, 2); assert.equal(proxy.connects.length, before + 1); assert.equal(trap.connections(), 0); } finally { for (const k of ["NO_PROXY", "no_proxy", "HTTP_PROXY", "HTTPS_PROXY"]) if (saved[k] === undefined) delete process.env[k]; } }); test("a yt-dlp.conf in the channel directory is ignored (finding 4)", { skip }, async () => { const cwd = mkdtempSync(path.join(ROOT, "chan-")); writeFileSync(path.join(cwd, "yt-dlp.conf"), '--add-headers "X-Leak: from-config"\n'); const before = seen.length; await routed([], { cwd }); assert.equal(seen[before]!["x-leak"], undefined); }); test("a proxy that refuses the destination fails the listing naming the exit; the direct trap stays at 0", { skip }, async () => { await assert.rejects(routed([], { proxyUrl: `socks5h://127.0.0.1:${refusing.port}` }), (err: unknown) => { assert.ok(err instanceof Error); assert.match(err.message, /network exit "exit-r"/); return true; }); assert.equal(trap.connections(), 0); }); test("a dead proxy: yt-dlp fails, the direct trap stays at 0", { skip }, async () => { const dead = net.createServer(); await new Promise((r) => dead.listen(0, "127.0.0.1", () => r())); const port = (dead.address() as net.AddressInfo).port; await new Promise((r) => dead.close(r)); await assert.rejects(routed([], { proxyUrl: `socks5h://127.0.0.1:${port}` }), /network exit "exit-r"/); assert.equal(trap.connections(), 0); });