try to fix oembed

This commit is contained in:
MathMan05 2024-12-26 12:37:46 -06:00
parent cb9ed1931f
commit c544741ef0
2 changed files with 34 additions and 8 deletions

View file

@ -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",

View file

@ -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<ApiUrls | null> {
export async function getApiUrls(
url: string,
instances: instace[],
check = true,
): Promise<ApiUrls | null> {
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<ApiUrls | null> {
}
}
export async function inviteResponse(req: Request, res: Response): Promise<void> {
export async function inviteResponse(
req: Request,
res: Response,
instances: instace[],
): Promise<void> {
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<void>
}
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<void>
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");
}