mirror of
https://github.com/MathMan05/Fermi.git
synced 2026-05-26 01:04:12 +00:00
make checks universal
This commit is contained in:
+101
-89
@@ -491,13 +491,18 @@ export async function getApiUrlsV2(str: string): Promise<InstanceUrls | null> {
|
||||
}
|
||||
try {
|
||||
const info = await fetch(str + "/.well-known/spacebar/client").then((r) => r.json());
|
||||
return {
|
||||
let urls: InstanceUrls = {
|
||||
admin: info.admin?.baseUrl,
|
||||
api: info.api.baseUrl + "/api/v" + info.api.apiVersions.default,
|
||||
api: info.api.baseUrl + "/api/v9", //We don't support anything other than v9
|
||||
gateway: info.gateway.baseUrl,
|
||||
cdn: info.cdn.baseUrl,
|
||||
wellknown: str,
|
||||
};
|
||||
const check = await checkURLs(str, urls);
|
||||
if (check) {
|
||||
urls = check;
|
||||
}
|
||||
return urls;
|
||||
} catch (e) {
|
||||
console.log("No .well-known v2 for", str, (e as Error).message);
|
||||
return null;
|
||||
@@ -509,6 +514,97 @@ export async function getApiUrlsV2(str: string): Promise<InstanceUrls | null> {
|
||||
/**
|
||||
* this function checks if a string is an instance, it'll either return the API urls or null
|
||||
*/
|
||||
async function checkURLs(wellknown: string, urls: InstanceUrls) {
|
||||
if (isLoopback(urls.api) !== isLoopback(wellknown)) {
|
||||
return new Promise<InstanceUrls | null>((res) => {
|
||||
const menu = new Dialog("");
|
||||
const options = menu.float.options;
|
||||
options.addMDText(new MarkDown(I18n.incorrectURLS(), undefined));
|
||||
const opt = options.addOptions("", {ltr: true});
|
||||
let clicked = false;
|
||||
opt.addButtonInput("", I18n.yes(), async () => {
|
||||
if (clicked) return;
|
||||
clicked = true;
|
||||
if (urls == null) throw new Error("Unexpected undefined, exiting");
|
||||
const temp = new URL(wellknown);
|
||||
temp.port = "";
|
||||
const newOrigin = temp.host;
|
||||
const protocol = temp.protocol;
|
||||
const tempurls = {
|
||||
api: new URL(urls.api),
|
||||
cdn: new URL(urls.cdn),
|
||||
gateway: new URL(urls.gateway),
|
||||
wellknown: new URL(urls.wellknown),
|
||||
};
|
||||
tempurls.api.host = newOrigin;
|
||||
tempurls.api.protocol = protocol;
|
||||
|
||||
tempurls.cdn.host = newOrigin;
|
||||
tempurls.api.protocol = protocol;
|
||||
|
||||
tempurls.gateway.host = newOrigin;
|
||||
tempurls.gateway.protocol = temp.protocol === "http:" ? "ws:" : "wss:";
|
||||
|
||||
tempurls.wellknown.host = newOrigin;
|
||||
tempurls.wellknown.protocol = protocol;
|
||||
|
||||
try {
|
||||
if (
|
||||
!(
|
||||
await fetch(
|
||||
tempurls.api + (tempurls.api.toString().endsWith("/") ? "" : "/") + "ping",
|
||||
)
|
||||
).ok
|
||||
) {
|
||||
res(null);
|
||||
menu.hide();
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
res(null);
|
||||
menu.hide();
|
||||
return;
|
||||
}
|
||||
res({
|
||||
api: tempurls.api.toString(),
|
||||
cdn: tempurls.cdn.toString(),
|
||||
gateway: tempurls.gateway.toString(),
|
||||
wellknown: tempurls.wellknown.toString(),
|
||||
});
|
||||
menu.hide();
|
||||
});
|
||||
const no = opt.addButtonInput("", I18n.no(), async () => {
|
||||
if (clicked) return;
|
||||
clicked = true;
|
||||
if (urls == null) throw new Error("URLs is undefined");
|
||||
try {
|
||||
//TODO make this a promise race for when the server just never responds
|
||||
//TODO maybe try to strip ports as another way to fix it
|
||||
if (!(await fetch(urls.api + "ping")).ok) {
|
||||
res(null);
|
||||
menu.hide();
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
res(null);
|
||||
return;
|
||||
}
|
||||
res(urls);
|
||||
menu.hide();
|
||||
});
|
||||
const span = document.createElement("span");
|
||||
options.addHTMLArea(span);
|
||||
const t = setInterval(() => {
|
||||
if (!document.contains(span)) {
|
||||
clearInterval(t);
|
||||
no.onClick();
|
||||
}
|
||||
}, 100);
|
||||
menu.show();
|
||||
});
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
export async function getApiUrlsV1(str: string): Promise<InstanceUrls | null> {
|
||||
function appendApi(str: string) {
|
||||
return str.includes("api") ? str : str.endsWith("/") ? str + "api" : str + "/api";
|
||||
@@ -596,93 +692,9 @@ export async function getApiUrlsV1(str: string): Promise<InstanceUrls | null> {
|
||||
}
|
||||
}
|
||||
if (urls) {
|
||||
if (isLoopback(urls.api) !== isLoopback(str)) {
|
||||
return new Promise<InstanceUrls | null>((res) => {
|
||||
const menu = new Dialog("");
|
||||
const options = menu.float.options;
|
||||
options.addMDText(new MarkDown(I18n.incorrectURLS(), undefined));
|
||||
const opt = options.addOptions("", {ltr: true});
|
||||
let clicked = false;
|
||||
opt.addButtonInput("", I18n.yes(), async () => {
|
||||
if (clicked) return;
|
||||
clicked = true;
|
||||
if (urls == null) throw new Error("Unexpected undefined, exiting");
|
||||
const temp = new URL(str);
|
||||
temp.port = "";
|
||||
const newOrigin = temp.host;
|
||||
const protocol = temp.protocol;
|
||||
const tempurls = {
|
||||
api: new URL(urls.api),
|
||||
cdn: new URL(urls.cdn),
|
||||
gateway: new URL(urls.gateway),
|
||||
wellknown: new URL(urls.wellknown),
|
||||
};
|
||||
tempurls.api.host = newOrigin;
|
||||
tempurls.api.protocol = protocol;
|
||||
|
||||
tempurls.cdn.host = newOrigin;
|
||||
tempurls.api.protocol = protocol;
|
||||
|
||||
tempurls.gateway.host = newOrigin;
|
||||
tempurls.gateway.protocol = temp.protocol === "http:" ? "ws:" : "wss:";
|
||||
|
||||
tempurls.wellknown.host = newOrigin;
|
||||
tempurls.wellknown.protocol = protocol;
|
||||
|
||||
try {
|
||||
if (
|
||||
!(
|
||||
await fetch(
|
||||
tempurls.api + (tempurls.api.toString().endsWith("/") ? "" : "/") + "ping",
|
||||
)
|
||||
).ok
|
||||
) {
|
||||
res(null);
|
||||
menu.hide();
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
res(null);
|
||||
menu.hide();
|
||||
return;
|
||||
}
|
||||
res({
|
||||
api: tempurls.api.toString(),
|
||||
cdn: tempurls.cdn.toString(),
|
||||
gateway: tempurls.gateway.toString(),
|
||||
wellknown: tempurls.wellknown.toString(),
|
||||
});
|
||||
menu.hide();
|
||||
});
|
||||
const no = opt.addButtonInput("", I18n.no(), async () => {
|
||||
if (clicked) return;
|
||||
clicked = true;
|
||||
if (urls == null) throw new Error("URLs is undefined");
|
||||
try {
|
||||
//TODO make this a promise race for when the server just never responds
|
||||
//TODO maybe try to strip ports as another way to fix it
|
||||
if (!(await fetch(urls.api + "ping")).ok) {
|
||||
res(null);
|
||||
menu.hide();
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
res(null);
|
||||
return;
|
||||
}
|
||||
res(urls);
|
||||
menu.hide();
|
||||
});
|
||||
const span = document.createElement("span");
|
||||
options.addHTMLArea(span);
|
||||
const t = setInterval(() => {
|
||||
if (!document.contains(span)) {
|
||||
clearInterval(t);
|
||||
no.onClick();
|
||||
}
|
||||
}, 100);
|
||||
menu.show();
|
||||
});
|
||||
const check = await checkURLs(str, urls);
|
||||
if (check) {
|
||||
urls = check;
|
||||
}
|
||||
//*/
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user