From 30635443862637600201c80cf3626d7cfdfb14ac Mon Sep 17 00:00:00 2001 From: alwaysmesmerizingyou Date: Sun, 15 Nov 2026 20:58:13 -0800 Subject: [PATCH] fixed vidapi --- src/sources/vidapi.js | 255 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 223 insertions(+), 32 deletions(-) diff --git a/src/sources/vidapi.js b/src/sources/vidapi.js index ccabd2e..bd313d5 100644 --- a/src/sources/vidapi.js +++ b/src/sources/vidapi.js @@ -1,6 +1,7 @@ -import { fetchJson, USER_AGENT } from '../utils/helpers.js'; +import { USER_AGENT } from '../utils/helpers.js'; -const API_BASE = 'https://streamdata.vaplayer.ru/api.php'; +const VA_EMBED_BASE = 'https://vaplayer.ru'; +const DEFAULT_API_BASE = 'https://streamdata.vaplayer.ru/api.php'; const EMBED_ORIGIN = 'https://nextgencloudfabric.com'; export const VERIFY_HEADERS = { @@ -9,57 +10,247 @@ export const VERIFY_HEADERS = { Origin: EMBED_ORIGIN, }; -function extractStreamUrls(obj, urls = new Set()) { - if (!obj) return urls; - if (typeof obj === 'string') { - if (obj.startsWith('http') && (obj.includes('.m3u8') || obj.includes('.mp4') || obj.includes('.mpd'))) { - urls.add(obj); - } - } else if (Array.isArray(obj)) { - for (const item of obj) extractStreamUrls(item, urls); - } else if (typeof obj === 'object') { - for (const val of Object.values(obj)) { - extractStreamUrls(val, urls); - } +const wasmModuleCache = new Map(); + +function parseHostToken(text) { + if (text == null) return ''; + const t = String(text).trim(); + if (!t) return ''; + if (t.startsWith('{') || t.startsWith('[')) { + try { + const j = JSON.parse(t); + if (typeof j === 'string') return j; + if (j && typeof j === 'object') return j.token || j.data || j.string || j.result || ''; + } catch { } + } + return t; +} + +function applyHostToken(url, token) { + if (!token) return url; + if (url.includes('__TOKEN__')) return url.split('__TOKEN__').join(token); + return `${url}${url.includes('?') ? '&' : '?'}token=${token}`; +} + +async function getWasmModule(va, headers) { + const cacheKey = va.wasm_url ? `u:${va.w ?? va.wasm_url}` : `b:${va.w ?? 'inline'}`; + if (wasmModuleCache.has(cacheKey)) { + return wasmModuleCache.get(cacheKey); + } + + const promise = (async () => { + let bytes; + if (va.wasm_url) { + const res = await fetch(va.wasm_url, { + headers, + signal: AbortSignal.timeout(8000), + }); + if (!res.ok) throw new Error(`WASM HTTP ${res.status}`); + bytes = await res.arrayBuffer(); + } else if (va.wasm) { + bytes = Buffer.from(va.wasm, 'base64'); + } else { + return null; + } + return await WebAssembly.compile(bytes); + })().catch(err => { + wasmModuleCache.delete(cacheKey); + throw err; + }); + + wasmModuleCache.set(cacheKey, promise); + return promise; +} + +async function decryptStreamUrls(encB64, va, headers) { + const mod = await getWasmModule(va, headers); + if (!mod) return []; + + const inst = await WebAssembly.instantiate(mod, {}); + const ex = inst.exports; + const enc = new Uint8Array(Buffer.from(encB64, 'base64')); + const ptr = ex.alloc(enc.length); + new Uint8Array(ex.memory.buffer, ptr, enc.length).set(enc); + const outLen = ex.decrypt(ptr, enc.length); + + if (!outLen || outLen <= 0) return []; + const txt = new TextDecoder().decode(new Uint8Array(ex.memory.buffer, ptr + 12, outLen)); + return txt.split('\n').map(s => s.trim()).filter(s => /^https?:\/\//i.test(s)); +} + +async function fetchEmbedSrc(embedPath) { + const vaSrcUrl = `${VA_EMBED_BASE}/embed/va_src.php?path=${encodeURIComponent(embedPath)}`; + const baseHeaders = { + 'User-Agent': USER_AGENT, + Accept: '*/*', + Referer: `${VA_EMBED_BASE}/`, + }; + + try { + const res = await fetch(vaSrcUrl, { + headers: baseHeaders, + signal: AbortSignal.timeout(8000), + }); + if (res.ok) { + const data = await res.json().catch(() => null); + if (data?.src) return data.src; + } + } catch { } + + try { + const pageRes = await fetch(`${VA_EMBED_BASE}${embedPath}`, { + headers: { + 'User-Agent': USER_AGENT, + Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + Referer: `${VA_EMBED_BASE}/`, + }, + signal: AbortSignal.timeout(8000), + }); + + const rawCookies = typeof pageRes.headers.getSetCookie === 'function' + ? pageRes.headers.getSetCookie() + : [pageRes.headers.get('set-cookie')].filter(Boolean); + + const cookie = rawCookies + .map(c => c.split(';')[0].trim()) + .filter(Boolean) + .join('; '); + + const res = await fetch(vaSrcUrl, { + headers: { + ...baseHeaders, + ...(cookie ? { Cookie: cookie } : {}), + }, + signal: AbortSignal.timeout(8000), + }); + if (!res.ok) return null; + const data = await res.json().catch(() => null); + return data?.src || null; + } catch { + return null; } - return urls; } export async function getStream(args) { const { id, s, e } = args || {}; + if (!id) return null; + const isTv = s != null && e != null; const type = isTv ? 'tv' : 'movie'; - - const url = `${API_BASE}?tmdb=${id}&type=${type}${isTv ? `&season=${s}&episode=${e}` : ''}`; + const embedPath = isTv ? `/embed/tv/${id}/${s}/${e}` : `/embed/movie/${id}`; try { - const data = await fetchJson(url, { + const rawSrc = await fetchEmbedSrc(embedPath); + if (!rawSrc) return null; + + const embedUrl = rawSrc.startsWith('http') ? rawSrc : `${VA_EMBED_BASE}${rawSrc}`; + const embedOrigin = new URL(embedUrl).origin; + + const embedRes = await fetch(embedUrl, { headers: { - Accept: '*/*', - Origin: EMBED_ORIGIN, - Referer: `${EMBED_ORIGIN}/`, 'User-Agent': USER_AGENT, + Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', + Referer: `${VA_EMBED_BASE}/`, }, + signal: AbortSignal.timeout(8000), + }); + if (!embedRes.ok) return null; + + const html = await embedRes.text(); + const tokenMatch = html.match(/"apiToken"\s*:\s*"([^"]+)"/) || html.match(/'apiToken'\s*:\s*'([^']+)'/); + const apiToken = tokenMatch?.[1]; + if (!apiToken) return null; + + const apiUrlMatch = html.match(/"streamDataApiUrl"\s*:\s*"([^"]+)"/); + const apiBase = apiUrlMatch?.[1] ? apiUrlMatch[1].replace(/\\\//g, '/') : DEFAULT_API_BASE; + + const apiHeaders = { + Accept: 'application/json', + Origin: embedOrigin, + Referer: `${embedOrigin}/`, + 'User-Agent': USER_AGENT, + }; + + const dataUrl = `${apiBase}?tmdb=${encodeURIComponent(id)}&type=${type}${isTv ? `&season=${encodeURIComponent(s)}&episode=${encodeURIComponent(e)}` : ''}&stream_urls&api_token=${encodeURIComponent(apiToken)}`; + + const apiRes = await fetch(dataUrl, { + headers: apiHeaders, signal: AbortSignal.timeout(10000), }); + if (!apiRes.ok) return null; - const streamUrls = Array.from(extractStreamUrls(data)); - if (!streamUrls.length) return null; + const payload = await apiRes.json().catch(() => null); + if (!payload || !payload.data) return null; + + let rawStreams = []; + const va = payload.vp || payload.va; + + if (Array.isArray(payload.data.stream_urls)) { + rawStreams = payload.data.stream_urls; + } else if (typeof payload.data.stream_urls === 'string' && va) { + rawStreams = await decryptStreamUrls(payload.data.stream_urls, va, apiHeaders); + } + + if (!rawStreams.length) return null; + + const origins = [...new Set(rawStreams.map(u => { + try { return new URL(u).origin; } catch { return ''; } + }).filter(Boolean))]; + + const originTokens = new Map(); + await Promise.allSettled( + origins.map(async (origin) => { + try { + const tokRes = await fetch(`${origin}/generate.php?api_token=${encodeURIComponent(apiToken)}`, { + headers: { + Accept: '*/*', + Origin: embedOrigin, + Referer: `${embedOrigin}/`, + 'User-Agent': USER_AGENT, + }, + signal: AbortSignal.timeout(6000), + }); + if (!tokRes.ok) return; + const tokText = await tokRes.text(); + const token = parseHostToken(tokText); + if (token) originTokens.set(origin, token); + } catch { } + }) + ); + + const playbackHeaders = { + 'User-Agent': USER_AGENT, + Referer: `${embedOrigin}/`, + Origin: embedOrigin, + }; + + const seen = new Set(); + const allUrls = []; + + for (let i = 0; i < rawStreams.length; i++) { + const rawUrl = String(rawStreams[i] || '').trim(); + if (!/^https?:\/\//i.test(rawUrl)) continue; + + let origin = ''; + try { origin = new URL(rawUrl).origin; } catch { } + const stampedUrl = applyHostToken(rawUrl, originTokens.get(origin) || ''); + + if (seen.has(stampedUrl)) continue; + seen.add(stampedUrl); - const allUrls = streamUrls.map(streamUrl => { let streamType = 'mp4'; - if (streamUrl.includes('.mpd')) streamType = 'dash'; - else if (streamUrl.includes('.m3u8')) streamType = 'hls'; + if (stampedUrl.includes('.mpd')) streamType = 'dash'; + else if (stampedUrl.includes('.m3u8')) streamType = 'hls'; - return { - url: streamUrl, - server: 'VidAPI', + allUrls.push({ + url: stampedUrl, + server: rawStreams.length > 1 ? `VidAPI - Server ${i + 1}` : 'VidAPI', quality: 'Auto', type: streamType, - headers: VERIFY_HEADERS, + headers: playbackHeaders, skipProxy: false, - }; - }); + }); + } if (!allUrls.length) return null;