feat(api): Add x-forwarded-for and x-forwarded-proto support

This commit is contained in:
Jesse Wierzbinski 2025-12-11 01:19:08 +01:00
parent b35e54c9b4
commit 8ec1c6d027
No known key found for this signature in database
4 changed files with 36 additions and 1 deletions

View file

@ -2,6 +2,7 @@ import type { ConfigSchema } from "@versia-server/config";
import { debugResponse } from "@versia-server/kit/api";
import { type Server, serve } from "bun";
import type { Hono } from "hono";
import { matches } from "ip-matching";
import type { z } from "zod";
import type { HonoEnv } from "~/types/api.ts";
@ -22,7 +23,34 @@ export const createServer = (
: undefined,
hostname: config.http.bind,
async fetch(req, server): Promise<Response> {
const output = await app.fetch(req, { ip: server.requestIP(req) });
const ip = server.requestIP(req);
const isTrustedProxy =
config.http.proxy_ips.includes("*") ||
(ip &&
config.http.proxy_ips.some((proxyIp) =>
matches(ip.address, proxyIp),
));
const url = new URL(req.url);
if (ip && isTrustedProxy) {
const xff = req.headers.get("x-forwarded-for");
const xfp = req.headers.get("x-forwarded-proto");
if (xff) {
const forwardedIps = xff.split(",").map((s) => s.trim());
const originalIp = forwardedIps[0];
ip.address = originalIp;
ip.family = originalIp.includes(":") ? "IPv6" : "IPv4";
}
if (xfp) {
url.protocol = xfp;
}
}
const output = await app.fetch(req, { ip });
await debugResponse(output.clone());