// A fake media CDN on loopback (release 32 R7): what Odysee's // `player.odycdn.com` answered on 2026-10-16, for the stored-URL ranged // download. No test reaches the real CDN. // // const cdn = await startFakeRangedCdn({ files: { "/v6/streams//.mp4": buf } }); // cdn.fetch a fetch that sends https://player.odycdn.com/… here // cdn.url(p) the real-shaped URL of a file (https://player.odycdn.com

) // cdn.requests every request, in order: { path, range, referer, origin, ua, status, bytes, at } // cdn.refuseAfterBytes(n) from now on, once n more bytes are served, answer 429 // await cdn.close() // // Behaviour, as measured: // - a RANGED GET with the player's Referer and Origin answers 206 with // `Content-Range: bytes a-b/total`; // - an UNRANGED GET answers 429 (the CDN treats it as a bulk download — what // yt-dlp's generic extractor got), unless opts.allowUnranged (then 200); // - no Referer → 403; // - a range past the end → 416 `Content-Range: bytes */total`; // - opts.refuseAfterBytes: once that many bytes have been served in all, every // request answers opts.refuseStatus (default 429) — "one file per rest"; // - opts.corruptOnce: { path, buf, from, to } — the first time a read overlaps // bytes [from, to) of that file it is served from `buf` (a damaged copy of // the same length) instead: a source that serves malformed audio // intermittently. Every later read of those bytes is clean. // - opts.ignoreRange: answer every GET 200 with the whole file. // - opts.requireUnlock (2026-10-17's finding): every read answers 429 until // `cdn.unlock()` — what a player opening the video's page does — and, with // opts.unlockBytes, the unlock lapses after that many bytes are served; // `cdn.lock()` takes it back at once. // - opts.dropAfterBytes / opts.drops / opts.dropGapBytes (release 32 R10, // 2026-10-17's `fetch failed` mid-file): once that many bytes have been // served in all, the connection is CUT — mid-body (the 206 headers and part // of the body sent, then the socket destroyed: undici's body read throws // "terminated"), or before any response when the cut falls on a request's // first byte (the fetch throws "fetch failed"). `drops` cuts in all // (default 1, Infinity allowed), `dropGapBytes` bytes served between two // cuts (default dropAfterBytes; 0 = every later request is cut before its // first byte). A cut request is recorded `dropped: true` with the bytes it sent. import http from "node:http"; export const CDN_ORIGIN = "https://player.odycdn.com"; export async function startFakeRangedCdn(opts = {}) { const files = opts.files ?? {}; const requests = []; let refuseBudget = typeof opts.refuseAfterBytes === "number" ? opts.refuseAfterBytes : null; const refuseStatus = opts.refuseStatus ?? 429; const corrupt = opts.corruptOnce ? { ...opts.corruptOnce, used: false } : null; let unlockedBudget = opts.requireUnlock ? 0 : null; let dropsLeft = typeof opts.dropAfterBytes === "number" ? (opts.drops ?? 1) : 0; let dropBudget = typeof opts.dropAfterBytes === "number" ? opts.dropAfterBytes : 0; const dropGap = typeof opts.dropGapBytes === "number" ? opts.dropGapBytes : (opts.dropAfterBytes ?? 0); const server = http.createServer((req, res) => { const u = new URL(req.url ?? "/", "http://x"); const range = req.headers.range; const rec = { path: u.pathname, range: range ?? null, referer: req.headers.referer ?? null, origin: req.headers.origin ?? null, ua: req.headers["user-agent"] ?? null, status: 0, bytes: 0, dropped: false, at: Date.now(), }; requests.push(rec); const end = (status, headers = {}, body) => { rec.status = status; res.writeHead(status, headers); res.end(body); }; const file = files[u.pathname]; if (!file) return end(404, { "content-type": "text/plain" }, "not found"); if (!req.headers.referer) return end(403, { "content-type": "text/plain" }, "forbidden"); if (unlockedBudget !== null && unlockedBudget <= 0) { return end(429, { "content-type": "text/plain" }, "Too Many Requests"); } if (refuseBudget !== null && refuseBudget <= 0) { return end(refuseStatus, { "content-type": "text/plain" }, refuseStatus === 429 ? "Too Many Requests" : "Forbidden"); } const total = file.length; const serve = (status, from, to, headers) => { // [from, to] inclusive, capped by the remaining budget. let last = to; if (refuseBudget !== null) last = Math.min(last, from + refuseBudget - 1); if (unlockedBudget !== null && unlockedBudget !== Infinity) last = Math.min(last, from + unlockedBudget - 1); let body = file.subarray(from, last + 1); if (corrupt && !corrupt.used && corrupt.path === u.pathname && from < corrupt.to && last >= corrupt.from) { corrupt.used = true; body = Buffer.from(corrupt.buf.subarray(from, last + 1)); } // The cut, if one falls inside this body. let cutAt = -1; if (dropsLeft > 0 && body.length > dropBudget) { cutAt = dropBudget; dropsLeft--; dropBudget = dropGap; } else if (dropsLeft > 0) { dropBudget -= body.length; } const sent = cutAt >= 0 ? cutAt : body.length; if (refuseBudget !== null) refuseBudget -= sent; if (unlockedBudget !== null && unlockedBudget !== Infinity) unlockedBudget -= sent; rec.bytes = sent; const h = { "content-type": "video/mp4", "accept-ranges": "bytes", "content-length": String(body.length), ...headers }; if (status === 206) h["content-range"] = `bytes ${from}-${from + body.length - 1}/${total}`; if (cutAt === 0) { rec.dropped = true; req.socket.destroy(); return; } if (cutAt > 0) { rec.dropped = true; rec.status = status; res.writeHead(status, h); res.write(body.subarray(0, cutAt), () => { setTimeout(() => req.socket.destroy(), 20); }); return; } end(status, h, body); }; if (opts.ignoreRange) return serve(200, 0, total - 1, {}); if (!range) { if (opts.allowUnranged) return serve(200, 0, total - 1, {}); return end(429, { "content-type": "text/plain" }, "Too Many Requests"); } const m = /^bytes=(\d+)-(\d*)$/.exec(range); if (!m) return end(416, { "content-range": `bytes */${total}` }, ""); const from = Number(m[1]); const to = m[2] === "" ? total - 1 : Math.min(Number(m[2]), total - 1); if (from >= total) return end(416, { "content-range": `bytes */${total}` }, ""); return serve(206, from, to, {}); }); await new Promise((r) => server.listen(0, "127.0.0.1", r)); const base = `http://127.0.0.1:${server.address().port}`; return { base, requests, url: (p) => `${CDN_ORIGIN}${p}`, fetch: (url, init) => fetch(String(url).replace(/^https?:\/\/player\.odycdn\.com/, base), init), refuseAfterBytes: (n) => { refuseBudget = n; }, unlocks: 0, unlock() { this.unlocks++; unlockedBudget = typeof opts.unlockBytes === "number" ? opts.unlockBytes : Infinity; }, lock: () => { unlockedBudget = 0; }, allowAgain: () => { refuseBudget = null; }, close: () => new Promise((r) => server.close(() => r())), }; }