@@ -210,7 +210,19 @@ const server = Bun.serve({
210210 } ) ;
211211 }
212212
213- // Proxy the request
213+ // Check if it's a YouTube URL and redirect instead of proxying
214+ if ( isYouTubeEmbed ( targetUrl ) ) {
215+ // YouTube URL detected, redirect to youtube-nocookie.com
216+ logRequest ( req . method , targetUrl , 200 ) ;
217+ return new Response ( serveYouTubeEmbed ( targetUrl ) , {
218+ status : 200 ,
219+ headers : {
220+ "Content-Type" : "text/html; charset=utf-8" ,
221+ } ,
222+ } ) ;
223+ }
224+
225+ // Proxy the request for non-YouTube URLs
214226 const response = await proxyRequest ( targetUrl ) ;
215227 logRequest ( req . method , targetUrl , response . status ) ;
216228 return response ;
@@ -229,3 +241,80 @@ console.log(
229241) ;
230242console . log ( `📋 Health check: http://${ server . hostname } :${ server . port } /health` ) ;
231243console . log ( `🌍 Environment: ${ Bun . env . NODE_ENV || "development" } ` ) ;
244+
245+ /**
246+ * This is required to bypass YouTube's Referrer Policy restrictions when
247+ * embedding videos on the mobile app. It basically "proxies" the Referrer and
248+ * allows any YouTube video to be embedded anywhere without restrictions.
249+ */
250+ function serveYouTubeEmbed ( url : string ) {
251+ return `<!DOCTYPE html>
252+ <html lang="en">
253+ <head>
254+ <meta charset="UTF-8">
255+ <meta name="viewport" content="width=device-width,initial-scale=1">
256+ <meta name="referrer" content="strict-origin-when-cross-origin">
257+ <meta name="robots" content="noindex,nofollow">
258+ <title>YouTube Video Embed</title>
259+ <style>
260+ * {
261+ margin: 0;
262+ padding: 0;
263+ box-sizing:border-box
264+ }
265+
266+ body, html {
267+ overflow: hidden;
268+ background:#000
269+ }
270+
271+ iframe {
272+ border: 0;
273+ width: 100vw;
274+ height: 100vh;
275+ display: block
276+ }
277+ </style>
278+ </head>
279+ <body>
280+ <iframe src="${ transformYouTubeUrl (
281+ url
282+ ) } " allow="accelerometer;autoplay;clipboard-write;encrypted-media;gyroscope;picture-in-picture;web-share" allowfullscreen referrerpolicy="strict-origin-when-cross-origin" title="Video player"></iframe>
283+ </body>
284+ </html>` ;
285+ }
286+
287+ // Check if URL is a YouTube embed (including youtube-nocookie.com)
288+ function isYouTubeEmbed ( urlString : string ) {
289+ const url = new URL ( urlString ) ;
290+ return (
291+ ( url . hostname === "www.youtube.com" ||
292+ url . hostname === "youtube.com" ||
293+ url . hostname === "m.youtube.com" ||
294+ url . hostname === "www.youtube-nocookie.com" ||
295+ url . hostname === "youtube-nocookie.com" ) &&
296+ url . pathname . startsWith ( "/embed/" )
297+ ) ;
298+ }
299+
300+ // Transform YouTube URLs to use youtube-nocookie.com for enhanced privacy
301+ function transformYouTubeUrl ( urlString : string ) : string {
302+ try {
303+ const url = new URL ( urlString ) ;
304+
305+ // Check if it's a YouTube domain
306+ if (
307+ url . hostname === "www.youtube.com" ||
308+ url . hostname === "youtube.com" ||
309+ url . hostname === "m.youtube.com"
310+ ) {
311+ // Replace with youtube-nocookie.com
312+ url . hostname = "www.youtube-nocookie.com" ;
313+ return url . toString ( ) ;
314+ }
315+
316+ return urlString ;
317+ } catch {
318+ return urlString ;
319+ }
320+ }
0 commit comments