|
| 1 | +// decryptor.ts |
| 2 | +// calls enc-dec.app to decrypt videasy's encrypted blob. |
| 3 | +// the blob is plain text hex returned directly from api.videasy.net. |
| 4 | +// enc-dec.app handles the wasm/cryptojs decryption server-side. |
| 5 | + |
| 6 | +const DEC_API = 'https://enc-dec.app/api/dec-videasy'; |
| 7 | + |
| 8 | +// response shape from enc-dec.app |
| 9 | +interface DecApiResponse { |
| 10 | + status: number; |
| 11 | + result: { |
| 12 | + sources: Array<{ quality?: string; url: string; type?: string }>; |
| 13 | + subtitles: Array<{ url: string; lang?: string; language?: string }>; |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +export interface DecryptedPayload { |
| 18 | + sources: Array<{ quality?: string; url: string; type?: string }>; |
| 19 | + subtitles: Array<{ url: string; lang?: string; language?: string }>; |
| 20 | +} |
| 21 | + |
| 22 | +// simple in-memory cache: key = `${tmdbId}:${blobHash}`, value = decrypted payload |
| 23 | +// avoids re-calling the api for the same blob within a server session |
| 24 | +const cache = new Map<string, DecryptedPayload>(); |
| 25 | + |
| 26 | +function blobKey(tmdbId: string, blob: string): string { |
| 27 | + // soo i think it's better to use first 32 chars of blob as a cheap fingerprint as blobs are unique per request |
| 28 | + return `${tmdbId}:${blob.slice(0, 32)}`; |
| 29 | +} |
| 30 | + |
| 31 | +export async function decryptResponse( |
| 32 | + blob: string, |
| 33 | + tmdbId: string |
| 34 | +): Promise<DecryptedPayload | null> { |
| 35 | + if (!blob || blob.length < 10) return null; |
| 36 | + |
| 37 | + const key = blobKey(tmdbId, blob); |
| 38 | + if (cache.has(key)) return cache.get(key)!; |
| 39 | + |
| 40 | + try { |
| 41 | + const res = await fetch(DEC_API, { |
| 42 | + method: 'POST', |
| 43 | + headers: { 'Content-Type': 'application/json' }, |
| 44 | + body: JSON.stringify({ text: blob, id: tmdbId }) |
| 45 | + }); |
| 46 | + |
| 47 | + if (!res.ok) return null; |
| 48 | + |
| 49 | + const json = (await res.json()) as DecApiResponse; |
| 50 | + |
| 51 | + if (json.status !== 200 || !json.result?.sources) return null; |
| 52 | + |
| 53 | + const payload: DecryptedPayload = { |
| 54 | + sources: json.result.sources ?? [], |
| 55 | + subtitles: json.result.subtitles ?? [] |
| 56 | + }; |
| 57 | + |
| 58 | + cache.set(key, payload); |
| 59 | + return payload; |
| 60 | + } catch { |
| 61 | + return null; |
| 62 | + } |
| 63 | +} |
0 commit comments