From 03f230dbca06a25bea24a82c42296901b9d4cd64 Mon Sep 17 00:00:00 2001 From: Abdullah Atta Date: Thu, 15 Jan 2026 15:07:34 +0500 Subject: [PATCH] cors: add special handling for youtube embeds to bypass referer policy restrictions --- cors-proxy/src/index.ts | 91 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/cors-proxy/src/index.ts b/cors-proxy/src/index.ts index 641bbbf..1dc837c 100644 --- a/cors-proxy/src/index.ts +++ b/cors-proxy/src/index.ts @@ -210,7 +210,19 @@ const server = Bun.serve({ }); } - // Proxy the request + // Check if it's a YouTube URL and redirect instead of proxying + if (isYouTubeEmbed(targetUrl)) { + // YouTube URL detected, redirect to youtube-nocookie.com + logRequest(req.method, targetUrl, 200); + return new Response(serveYouTubeEmbed(targetUrl), { + status: 200, + headers: { + "Content-Type": "text/html; charset=utf-8", + }, + }); + } + + // Proxy the request for non-YouTube URLs const response = await proxyRequest(targetUrl); logRequest(req.method, targetUrl, response.status); return response; @@ -229,3 +241,80 @@ console.log( ); console.log(`📋 Health check: http://${server.hostname}:${server.port}/health`); console.log(`🌍 Environment: ${Bun.env.NODE_ENV || "development"}`); + +/** + * This is required to bypass YouTube's Referrer Policy restrictions when + * embedding videos on the mobile app. It basically "proxies" the Referrer and + * allows any YouTube video to be embedded anywhere without restrictions. + */ +function serveYouTubeEmbed(url: string) { + return ` + + + + + + + YouTube Video Embed + + + + + +`; +} + +// Check if URL is a YouTube embed (including youtube-nocookie.com) +function isYouTubeEmbed(urlString: string) { + const url = new URL(urlString); + return ( + (url.hostname === "www.youtube.com" || + url.hostname === "youtube.com" || + url.hostname === "m.youtube.com" || + url.hostname === "www.youtube-nocookie.com" || + url.hostname === "youtube-nocookie.com") && + url.pathname.startsWith("/embed/") + ); +} + +// Transform YouTube URLs to use youtube-nocookie.com for enhanced privacy +function transformYouTubeUrl(urlString: string): string { + try { + const url = new URL(urlString); + + // Check if it's a YouTube domain + if ( + url.hostname === "www.youtube.com" || + url.hostname === "youtube.com" || + url.hostname === "m.youtube.com" + ) { + // Replace with youtube-nocookie.com + url.hostname = "www.youtube-nocookie.com"; + return url.toString(); + } + + return urlString; + } catch { + return urlString; + } +}