fix(api): 🐛 Fix incorrect proxy behaviour

This commit is contained in:
Jesse Wierzbinski 2024-05-06 17:31:12 +00:00
parent afda1f1211
commit 8557867ad8
No known key found for this signature in database
5 changed files with 52 additions and 4 deletions

View file

@ -105,7 +105,9 @@ if (isEntry) {
} }
} }
const app = new Hono(); const app = new Hono({
strict: false,
});
app.use(ipBans); app.use(ipBans);
app.use(agentBans); app.use(agentBans);

View file

@ -6,7 +6,7 @@ import { z } from "zod";
export const meta = applyConfig({ export const meta = applyConfig({
allowedMethods: ["GET"], allowedMethods: ["GET"],
route: "/api/media/:id", route: "/media/:id",
ratelimits: { ratelimits: {
max: 100, max: 100,
duration: 60, duration: 60,

View file

@ -0,0 +1,46 @@
import { applyConfig, handleZodError } from "@api";
import { zValidator } from "@hono/zod-validator";
import { errorResponse, response } from "@response";
import type { Hono } from "hono";
import { z } from "zod";
export const meta = applyConfig({
allowedMethods: ["GET"],
route: "/media/proxy/:id",
ratelimits: {
max: 100,
duration: 60,
},
auth: {
required: false,
},
});
export const schemas = {
param: z.object({
id: z
.string()
.transform((val) => Buffer.from(val, "base64url").toString()),
}),
};
export default (app: Hono) =>
app.on(
meta.allowedMethods,
meta.route,
zValidator("param", schemas.param, handleZodError),
async (context) => {
const { id } = context.req.valid("param");
// Check if URL is valid
if (!URL.canParse(id))
return errorResponse(
"Invalid URL (it should be encoded as base64url",
400,
);
return fetch(id).then((res) => {
return response(res.body, res.status, res.headers.toJSON());
});
},
);

View file

@ -6,7 +6,7 @@ import { z } from "zod";
export const meta = applyConfig({ export const meta = applyConfig({
allowedMethods: ["GET"], allowedMethods: ["GET"],
route: "/api/media/proxy", route: "/media/proxy",
ratelimits: { ratelimits: {
max: 100, max: 100,
duration: 60, duration: 60,

View file

@ -76,7 +76,7 @@ export const proxyUrl = (url: string | null) => {
const urlAsBase64Url = Buffer.from(url || "").toString("base64url"); const urlAsBase64Url = Buffer.from(url || "").toString("base64url");
return url return url
? new URL( ? new URL(
`/media/proxy?url=${urlAsBase64Url}`, `/media/proxy/${urlAsBase64Url}`,
config.http.base_url, config.http.base_url,
).toString() ).toString()
: url; : url;