From 4dbddfab41f2586a26d53fc47d3dd53ce825a7ab Mon Sep 17 00:00:00 2001 From: Samidy Date: Sat, 18 Apr 2026 01:31:50 +0300 Subject: [PATCH] please please please work --- functions/proxy-audio.js | 65 ++++++++++++++++++++++++++++++++++++++++ js/api.js | 10 +++++++ 2 files changed, 75 insertions(+) create mode 100644 functions/proxy-audio.js diff --git a/functions/proxy-audio.js b/functions/proxy-audio.js new file mode 100644 index 0000000..50944cc --- /dev/null +++ b/functions/proxy-audio.js @@ -0,0 +1,65 @@ +export async function onRequest(context) { + const { request } = context; + const url = new URL(request.url); + const targetUrl = url.searchParams.get('url'); + + if (!targetUrl) { + return new Response('Missing url parameter', { status: 400 }); + } + + try { + const cacheUrl = new URL(request.url); + try { + const tidalUrl = new URL(targetUrl); + cacheUrl.searchParams.set('cache_key', tidalUrl.pathname); + } catch (e) {} + + const cacheKey = new Request(cacheUrl.toString(), request); + const cache = caches.default; + let response = await cache.match(cacheKey); + + if (!response) { + console.log('Cache Miss. Fetching from Tidal...'); + + const headers = new Headers(request.headers); + headers.delete('host'); + headers.delete('referer'); + headers.set( + 'User-Agent', + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36' + ); + + response = await fetch(targetUrl, { + method: request.method, + headers: headers, + redirect: 'follow', + cf: { + cacheTtl: 2592000, + cacheEverything: true, + }, + }); + + if (request.method === 'GET' && response.ok) { + const cacheResponse = new Response(response.body, response); + cacheResponse.headers.set('Access-Control-Allow-Origin', '*'); + cacheResponse.headers.set('Cache-Control', 'public, max-age=2592000'); + + context.waitUntil(cache.put(cacheKey, cacheResponse.clone())); + response = cacheResponse; + } + } else { + console.log('Cache Hit! Serving from Edge.'); + } + + const newResponse = new Response(response.body, response); + newResponse.headers.set('Access-Control-Allow-Origin', '*'); + newResponse.headers.set('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS'); + newResponse.headers.set('Access-Control-Expose-Headers', '*'); + newResponse.headers.delete('content-security-policy'); + newResponse.headers.delete('x-frame-options'); + + return newResponse; + } catch (error) { + return new Response('Proxy Error: ' + error.message, { status: 500 }); + } +} diff --git a/js/api.js b/js/api.js index 09390ed..6bfb61c 100644 --- a/js/api.js +++ b/js/api.js @@ -1507,6 +1507,11 @@ export class LosslessAPI { }; } + if (streamUrl && streamUrl.includes('tidal.com')) { + const encodedUrl = encodeURIComponent(streamUrl); + streamUrl = `/proxy-audio?url=${encodedUrl}`; + } + const result = { url: streamUrl, rgInfo: manifestRgInfo }; this.streamCache.set(cacheKey, result); @@ -1554,6 +1559,11 @@ export class LosslessAPI { throw new Error(`Could not resolve video stream URL for ID: ${id}`); } + if (streamUrl && streamUrl.includes('tidal.com')) { + const encodedUrl = encodeURIComponent(streamUrl); + streamUrl = `/proxy-audio?url=${encodedUrl}`; + } + if (!(lookup instanceof TidalResponse)) { this.streamCache.set(cacheKey, streamUrl); }