fixed vidvault

This commit is contained in:
2026-11-06 20:02:20 -08:00
parent 88385e2475
commit 509e006fdd

View File

@@ -13,10 +13,42 @@ const HEADERS = {
'Accept-Language': 'en-US,en;q=0.9',
};
let cookieJar = null;
let cookiePromise = null;
function ensureCookies() {
if (cookieJar !== null) return Promise.resolve();
if (cookiePromise) return cookiePromise;
cookiePromise = (async () => {
try {
const res = await fetch(`${BASE_URL}/`, {
headers: {
'User-Agent': USER_AGENT,
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.9',
},
signal: AbortSignal.timeout(10000),
});
const setCookie = typeof res.headers.getSetCookie === 'function'
? res.headers.getSetCookie()
: (res.headers.get('set-cookie') ? [res.headers.get('set-cookie')] : []);
cookieJar = setCookie.map(c => c.split(';')[0].trim()).filter(Boolean).join('; ');
} catch {
cookieJar = '';
}
})();
return cookiePromise;
}
function withCookies(headers = {}) {
return cookieJar ? { ...headers, Cookie: cookieJar } : headers;
}
async function getToken() {
await ensureCookies();
const res = await fetch(`${API_BASE}/get-token`, {
method: 'GET',
headers: HEADERS,
headers: withCookies(HEADERS),
signal: AbortSignal.timeout(10000),
});
if (!res.ok) throw new Error(`Token fetch failed: ${res.status}`);
@@ -44,7 +76,7 @@ async function resolveDirect(directPath, headers) {
const res = await fetch(fullUrl, {
method: 'HEAD',
redirect: 'manual',
headers,
headers: withCookies(headers),
});
const loc = res.headers.get('location');
if (loc) return loc;
@@ -60,9 +92,11 @@ export async function getStream(args) {
? `${BASE_URL}/media/tv/${id}/${s}/${e}`
: `${BASE_URL}/media/movie/${id}`;
await ensureCookies();
const [mediaData, tokenData] = await Promise.all([
fetchJson(mediaPath, { headers: HEADERS }).catch(() => null),
fetchJson(`${BASE_URL}/api/get-token`, { headers: HEADERS }).catch(() => null),
fetchJson(mediaPath, { headers: withCookies(HEADERS) }).catch(() => null),
fetchJson(`${BASE_URL}/api/get-token`, { headers: withCookies(HEADERS) }).catch(() => null),
]);
const allUrls = [];
@@ -81,7 +115,7 @@ export async function getStream(args) {
server: `VidVault - Direct ${format.toUpperCase()} (${item.quality || item.size || 'Auto'})`,
quality: item.quality || 'Auto',
type: format,
headers: HEADERS,
headers: withCookies(HEADERS),
});
}
})
@@ -96,11 +130,11 @@ export async function getStream(args) {
const proxyData = await fetchJson(`${BASE_URL}/api/download-proxy`, {
method: 'POST',
headers: {
headers: withCookies({
...HEADERS,
'Content-Type': 'application/json',
'x-request-token': token,
},
}),
body: JSON.stringify(payload),
}).catch(() => null);
@@ -118,7 +152,7 @@ export async function getStream(args) {
server: `VidVault - ${label} (${data.quality || data.size || 'Auto'})`,
quality: data.quality || 'Auto',
type,
headers: HEADERS,
headers: withCookies(HEADERS),
});
}
}