please please please work
This commit is contained in:
parent
f5c4f61c6f
commit
4dbddfab41
2 changed files with 75 additions and 0 deletions
65
functions/proxy-audio.js
Normal file
65
functions/proxy-audio.js
Normal file
|
|
@ -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 });
|
||||
}
|
||||
}
|
||||
10
js/api.js
10
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue