From c544741ef0e572289935edd2480f89b5f7b1fd1b Mon Sep 17 00:00:00 2001 From: MathMan05 Date: Thu, 26 Dec 2024 12:37:46 -0600 Subject: [PATCH] try to fix oembed --- src/index.ts | 6 +++--- src/utils.ts | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 59aee5b..bf64caf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -70,7 +70,7 @@ interface Instance { const app = express(); -type instace = { +export type instace = { name: string; description?: string; descriptionLong?: string; @@ -146,7 +146,7 @@ app.use("/getupdates", async (_req: Request, res: Response) => { }); app.use("/services/oembed", (req: Request, res: Response) => { - inviteResponse(req, res); + inviteResponse(req, res, instances); }); app.use("/uptime", (req: Request, res: Response) => { @@ -159,7 +159,7 @@ app.use("/", async (req: Request, res: Response) => { const host = `${scheme}://${req.get("Host")}`; const ref = host + req.originalUrl; - if (host && ref && false) { + if (host && ref) { const link = `${host}/services/oembed?url=${encodeURIComponent(ref)}`; res.set( "Link", diff --git a/src/utils.ts b/src/utils.ts index b553fa1..f9b7b53 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ import {Request, Response} from "express"; - +import {instace} from "./index.js"; interface ApiUrls { api: string; gateway: string; @@ -19,10 +19,32 @@ interface Invite { }; } -export async function getApiUrls(url: string): Promise { +export async function getApiUrls( + url: string, + instances: instace[], + check = true, +): Promise { if (!url.endsWith("/")) { url += "/"; } + if (check) { + let valid = false; + for (const instace of instances) { + const urlstr = instace.url || instace.urls?.api; + if (!urlstr) { + continue; + } + try { + if (new URL(urlstr).host === new URL(url).host) { + valid = true; + break; + } + } catch (e) {} + } + if (!valid) { + throw new Error("Invalid instance"); + } + } try { const info: ApiUrls = await fetch(`${url}.well-known/spacebar`).then((res) => res.json()); const api = info.api; @@ -42,7 +64,11 @@ export async function getApiUrls(url: string): Promise { } } -export async function inviteResponse(req: Request, res: Response): Promise { +export async function inviteResponse( + req: Request, + res: Response, + instances: instace[], +): Promise { let url: URL; try { url = new URL(req.query.url as string); @@ -53,7 +79,7 @@ export async function inviteResponse(req: Request, res: Response): Promise } try { - if (url.pathname.startsWith("invite")) { + if (!url.pathname.startsWith("invite") && !url.pathname.startsWith("/invite")) { throw new Error("Invalid invite URL"); } @@ -63,7 +89,7 @@ export async function inviteResponse(req: Request, res: Response): Promise throw new Error("Instance not specified"); } - const urls = await getApiUrls(instance); + const urls = await getApiUrls(instance, instances); if (!urls) { throw new Error("Failed to get API URLs"); }