Spaces:
Sleeping
Sleeping
File size: 12,041 Bytes
3c56493 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 | import { env } from '$env/dynamic/private';
import { Buffer } from 'node:buffer';
import { createHash } from 'node:crypto';
import type Redis from 'ioredis';
import type { RequestHandler } from './$types';
import { isProxyTarget } from '$lib/config';
import { getRedisClient } from '$lib/server/redis';
const allowOrigin = (origin?: string | null): boolean => {
void origin;
return true;
};
const hopByHopHeaders = new Set([
'connection',
'keep-alive',
'proxy-authenticate',
'proxy-authorization',
'te',
'trailer',
'transfer-encoding',
'upgrade'
]);
const CACHE_NAMESPACE = 'tidal:proxy:v2:';
const DEFAULT_CACHE_TTL_SECONDS = getEnvNumber('REDIS_CACHE_TTL_SECONDS', 300);
const SEARCH_CACHE_TTL_SECONDS = getEnvNumber('REDIS_CACHE_TTL_SEARCH_SECONDS', 300);
const TRACK_CACHE_TTL_SECONDS = getEnvNumber('REDIS_CACHE_TTL_TRACK_SECONDS', 120);
const MAX_CACHE_BODY_BYTES = getEnvNumber('REDIS_CACHE_MAX_BODY_BYTES', 200_000);
interface CachedProxyEntry {
status: number;
statusText: string;
headers: [string, string][];
bodyBase64: string;
}
function getEnvNumber(name: string, fallback: number): number {
const raw = env[name];
if (!raw) return fallback;
const parsed = Number.parseInt(raw, 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}
function sanitizeHeaderEntries(entries: Array<[string, string]>): Array<[string, string]> {
const blocklist = new Set(['content-encoding', 'content-length', 'transfer-encoding']);
return entries.filter(([key]) => !blocklist.has(key.toLowerCase()));
}
function isCacheableContentType(contentType: string | null): boolean {
if (!contentType) return false;
const normalized = contentType.split(';')[0]?.trim().toLowerCase() ?? '';
return normalized.includes('json') || normalized.startsWith('text/');
}
function hasDisqualifyingCacheControl(cacheControl: string | null): boolean {
if (!cacheControl) return false;
const normalized = cacheControl.toLowerCase();
return normalized.includes('no-store') || normalized.includes('private');
}
function getCacheTtlSeconds(url: URL): number {
const path = url.pathname.toLowerCase();
if (path.includes('/track/') || path.includes('/song/')) {
return TRACK_CACHE_TTL_SECONDS;
}
if (path.includes('/search/')) {
return SEARCH_CACHE_TTL_SECONDS;
}
if (path.includes('/album/') || path.includes('/artist/') || path.includes('/playlist/')) {
return DEFAULT_CACHE_TTL_SECONDS;
}
return DEFAULT_CACHE_TTL_SECONDS;
}
function createCacheKey(url: URL, headers: Headers): string {
const accept = headers.get('accept') ?? '';
const range = headers.get('range') ?? '';
const keyMaterial = `${url.toString()}|accept=${accept}|range=${range}`;
const hash = createHash('sha256').update(keyMaterial).digest('hex');
return `${CACHE_NAMESPACE}${hash}`;
}
function applyProxyHeaders(sourceHeaders: Array<[string, string]>, origin: string | null): Headers {
const headers = new Headers();
const sanitized = sanitizeHeaderEntries(sourceHeaders);
for (const [key, value] of sanitized) {
headers.append(key, value);
}
headers.set('Access-Control-Allow-Origin', origin ?? '*');
headers.set('Access-Control-Allow-Methods', 'GET, OPTIONS');
headers.set('Access-Control-Allow-Headers', 'Content-Type, Range');
headers.set('Vary', ensureVaryIncludesOrigin(headers.get('vary')));
if (!headers.has('Cache-Control')) {
headers.set('Cache-Control', 'public, max-age=300');
}
return headers;
}
function isCachedProxyEntry(value: unknown): value is CachedProxyEntry {
if (!value || typeof value !== 'object') return false;
const candidate = value as Partial<CachedProxyEntry>;
return (
typeof candidate.status === 'number' &&
typeof candidate.statusText === 'string' &&
Array.isArray(candidate.headers) &&
typeof candidate.bodyBase64 === 'string'
);
}
function base64ToUint8Array(base64: string): Uint8Array {
return Uint8Array.from(Buffer.from(base64, 'base64'));
}
function uint8ArrayToBase64(bytes: Uint8Array): string {
return Buffer.from(bytes).toString('base64');
}
function toArrayBuffer(bytes: Uint8Array): ArrayBuffer {
const copy = new Uint8Array(bytes.byteLength);
copy.set(bytes);
return copy.buffer;
}
async function readCachedResponse(redis: Redis, key: string): Promise<CachedProxyEntry | null> {
try {
const raw = await redis.get(key);
if (!raw) {
return null;
}
const parsed = JSON.parse(raw) as unknown;
return isCachedProxyEntry(parsed) ? (parsed as CachedProxyEntry) : null;
} catch (error) {
console.error('Failed to read proxy cache entry:', error);
return null;
}
}
async function writeCachedResponse(
redis: Redis,
key: string,
entry: CachedProxyEntry,
ttlSeconds: number
): Promise<void> {
if (ttlSeconds <= 0) return;
try {
await redis.set(key, JSON.stringify(entry), 'EX', ttlSeconds);
} catch (error) {
console.error('Failed to store proxy cache entry:', error);
}
}
const ensureVaryIncludesOrigin = (value: string | null): string => {
const entries = value
? value
.split(',')
.map((v) => v.trim())
.filter(Boolean)
: [];
if (!entries.includes('Origin')) {
entries.push('Origin');
}
return entries.join(', ');
};
const RETRYABLE_PATH_SEGMENTS = ['/track/', '/search/'];
const TOKEN_INVALID_MESSAGE = 'token has invalid payload';
const MAX_RETRY_ATTEMPTS = 2;
function isRetryEligibleTarget(url: URL): boolean {
const path = url.pathname.toLowerCase();
return RETRYABLE_PATH_SEGMENTS.some((segment) => path.includes(segment));
}
async function shouldRetryInvalidToken(response: Response, targetUrl: URL): Promise<boolean> {
if (response.status !== 401) {
return false;
}
if (!isRetryEligibleTarget(targetUrl)) {
return false;
}
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.toLowerCase().includes('application/json')) {
return false;
}
try {
const payload = (await response.clone().json()) as {
subStatus?: unknown;
userMessage?: unknown;
detail?: unknown;
};
const rawSubStatus =
typeof payload?.subStatus === 'number'
? payload.subStatus
: typeof payload?.subStatus === 'string'
? Number.parseInt(payload.subStatus, 10)
: undefined;
const subStatus =
typeof rawSubStatus === 'number' && Number.isFinite(rawSubStatus) ? rawSubStatus : undefined;
const combinedMessage = [payload?.userMessage, payload?.detail]
.filter((value): value is string => typeof value === 'string')
.join(' ')
.toLowerCase();
if (subStatus === 11002) {
return true;
}
if (combinedMessage.includes(TOKEN_INVALID_MESSAGE)) {
return true;
}
} catch (error) {
console.debug('Proxy retry check failed to parse response', error);
}
return false;
}
async function fetchWithRetry(
url: URL,
options: RequestInit,
fetchFn: typeof fetch
): Promise<Response> {
let response: Response | null = null;
for (let attempt = 1; attempt <= MAX_RETRY_ATTEMPTS; attempt += 1) {
response = await fetchFn(url.toString(), options);
if (attempt < MAX_RETRY_ATTEMPTS && (await shouldRetryInvalidToken(response, url))) {
// Cancel the body if it exists to free resources before retrying
try {
if (response.body && typeof response.body.cancel === 'function') {
await response.body.cancel();
}
} catch (error) {
console.debug('Failed to cancel upstream response body before retry', error);
}
continue;
}
return response;
}
// Fallback: return the last response if all attempts exhausted without early return
if (response) {
return response;
}
return fetchFn(url.toString(), options);
}
export const GET: RequestHandler = async ({ url, request, fetch }) => {
const target = url.searchParams.get('url');
const origin = request.headers.get('origin');
if (!allowOrigin(origin)) {
return new Response('Forbidden', { status: 403 });
}
if (!target) {
return new Response(JSON.stringify({ error: 'Missing url parameter' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
let parsedTarget: URL;
try {
parsedTarget = new URL(target);
} catch {
return new Response(JSON.stringify({ error: 'Invalid target URL' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
if (!isProxyTarget(parsedTarget)) {
return new Response(JSON.stringify({ error: 'Invalid target host' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}
const upstreamHeaders = new Headers();
let hasRangeRequest = false;
let hasAuthorizationHeader = false;
let hasCookieHeader = false;
request.headers.forEach((value, key) => {
const lowerKey = key.toLowerCase();
if (hopByHopHeaders.has(lowerKey) || lowerKey === 'host') {
return;
}
if (lowerKey === 'range') {
hasRangeRequest = true;
}
if (lowerKey === 'authorization') {
hasAuthorizationHeader = true;
}
if (lowerKey === 'cookie') {
hasCookieHeader = true;
}
upstreamHeaders.set(key, value);
});
if (!upstreamHeaders.has('User-Agent')) {
upstreamHeaders.set('User-Agent', 'Mozilla/5.0 (compatible; TIDAL-UI/1.0)');
}
// Force identity encoding so the upstream sends plain data that Node can forward without zstd artifacts.
upstreamHeaders.set('Accept-Encoding', 'identity');
const shouldUseCache = !hasRangeRequest && !hasAuthorizationHeader && !hasCookieHeader;
const redis = shouldUseCache ? getRedisClient() : null;
const cacheKey = redis ? createCacheKey(parsedTarget, upstreamHeaders) : null;
if (redis && cacheKey) {
const cached = await readCachedResponse(redis, cacheKey);
if (cached) {
const headers = applyProxyHeaders(cached.headers, origin);
const bodyBytes = base64ToUint8Array(cached.bodyBase64);
return new Response(toArrayBuffer(bodyBytes), {
status: cached.status,
statusText: cached.statusText,
headers
});
}
}
try {
const upstream = await fetchWithRetry(
parsedTarget,
{
headers: upstreamHeaders,
redirect: 'follow'
},
fetch
);
const upstreamHeaderEntries = Array.from(upstream.headers.entries());
const sanitizedHeaderEntries = sanitizeHeaderEntries(upstreamHeaderEntries);
const headers = applyProxyHeaders(sanitizedHeaderEntries, origin);
const bodyArrayBuffer = await upstream.arrayBuffer();
const bodyBytes = new Uint8Array(bodyArrayBuffer);
if (redis && cacheKey) {
const ttlSeconds = getCacheTtlSeconds(parsedTarget);
const contentType = upstream.headers.get('content-type');
const cacheControl = upstream.headers.get('cache-control');
const byteLength = bodyBytes.byteLength;
const cacheable =
upstream.status === 200 &&
ttlSeconds > 0 &&
!hasDisqualifyingCacheControl(cacheControl) &&
isCacheableContentType(contentType) &&
byteLength <= MAX_CACHE_BODY_BYTES;
if (cacheable) {
const entry: CachedProxyEntry = {
status: upstream.status,
statusText: upstream.statusText,
headers: sanitizedHeaderEntries,
bodyBase64: uint8ArrayToBase64(bodyBytes)
};
await writeCachedResponse(redis, cacheKey, entry, ttlSeconds);
}
}
return new Response(bodyArrayBuffer, {
status: upstream.status,
statusText: upstream.statusText,
headers
});
} catch (error) {
console.error('Proxy error:', error);
return new Response(
JSON.stringify({
error: 'Proxy request failed',
message: error instanceof Error ? error.message : 'Unknown error'
}),
{
status: 500,
headers: { 'Content-Type': 'application/json' }
}
);
}
};
export const OPTIONS: RequestHandler = async ({ request }) => {
const origin = request.headers.get('origin');
if (!allowOrigin(origin)) {
return new Response(null, { status: 403 });
}
const headers = new Headers();
headers.set('Access-Control-Allow-Origin', origin ?? '*');
headers.set('Vary', 'Origin');
headers.set('Access-Control-Allow-Methods', 'GET, OPTIONS');
headers.set('Access-Control-Allow-Headers', 'Content-Type, Range');
headers.set('Access-Control-Max-Age', '86400');
return new Response(null, {
status: 204,
headers
});
};
|