mirror of
https://github.com/imputnet/cobalt.git
synced 2025-06-28 09:28:29 +00:00
web/api: re-request session if server claims it's invalid
This commit is contained in:
parent
1f768df4ec
commit
59665af44a
@ -3,7 +3,7 @@ import { get } from "svelte/store";
|
|||||||
import settings from "$lib/state/settings";
|
import settings from "$lib/state/settings";
|
||||||
import lazySettingGetter from "$lib/settings/lazy-get";
|
import lazySettingGetter from "$lib/settings/lazy-get";
|
||||||
|
|
||||||
import { getSession } from "$lib/api/session";
|
import { getSession, resetSession } from "$lib/api/session";
|
||||||
import { currentApiURL } from "$lib/api/api-url";
|
import { currentApiURL } from "$lib/api/api-url";
|
||||||
import { turnstileEnabled, turnstileSolved } from "$lib/state/turnstile";
|
import { turnstileEnabled, turnstileSolved } from "$lib/state/turnstile";
|
||||||
import cachedInfo from "$lib/state/server-info";
|
import cachedInfo from "$lib/state/server-info";
|
||||||
@ -43,10 +43,10 @@ const getAuthorization = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = async (url: string) => {
|
const request = async (url: string, justRetried = false) => {
|
||||||
const getSetting = lazySettingGetter(get(settings));
|
const getSetting = lazySettingGetter(get(settings));
|
||||||
|
|
||||||
const request = {
|
const requestBody = {
|
||||||
url,
|
url,
|
||||||
|
|
||||||
downloadMode: getSetting("save", "downloadMode"),
|
downloadMode: getSetting("save", "downloadMode"),
|
||||||
@ -100,7 +100,7 @@ const request = async (url: string) => {
|
|||||||
method: "POST",
|
method: "POST",
|
||||||
redirect: "manual",
|
redirect: "manual",
|
||||||
signal: AbortSignal.timeout(20000),
|
signal: AbortSignal.timeout(20000),
|
||||||
body: JSON.stringify(request),
|
body: JSON.stringify(requestBody),
|
||||||
headers: {
|
headers: {
|
||||||
"Accept": "application/json",
|
"Accept": "application/json",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@ -119,9 +119,31 @@ const request = async (url: string) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
response?.status === 'error'
|
||||||
|
&& response?.error.code === 'error.api.auth.jwt.invalid'
|
||||||
|
&& !justRetried
|
||||||
|
) {
|
||||||
|
resetSession();
|
||||||
|
await waitForTurnstile().catch(() => {});
|
||||||
|
return request(url, true);
|
||||||
|
}
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const waitForTurnstile = async () => {
|
||||||
|
await getAuthorization();
|
||||||
|
return new Promise<void>(resolve => {
|
||||||
|
const unsub = turnstileSolved.subscribe(solved => {
|
||||||
|
if (solved) {
|
||||||
|
unsub();
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
const probeCobaltTunnel = async (url: string) => {
|
const probeCobaltTunnel = async (url: string) => {
|
||||||
const request = await fetch(`${url}&p=1`).catch(() => {});
|
const request = await fetch(`${url}&p=1`).catch(() => {});
|
||||||
if (request?.status === 200) {
|
if (request?.status === 200) {
|
||||||
|
@ -62,3 +62,5 @@ export const getSession = async () => {
|
|||||||
}
|
}
|
||||||
return newSession;
|
return newSession;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const resetSession = () => cache = undefined;
|
||||||
|
Loading…
Reference in New Issue
Block a user