mirror of
https://github.com/versia-pub/server.git
synced 2026-01-26 12:16:01 +01:00
fix(api): 🐛 Fix incorrect proxy behaviour
This commit is contained in:
parent
afda1f1211
commit
8557867ad8
4
index.ts
4
index.ts
|
|
@ -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);
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
46
server/api/media/proxy/:id.ts
Normal file
46
server/api/media/proxy/:id.ts
Normal 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());
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue