diff --git a/bifrost-fastify/index.ts b/bifrost-fastify/index.ts index fef0302..d4d53a6 100644 --- a/bifrost-fastify/index.ts +++ b/bifrost-fastify/index.ts @@ -8,7 +8,6 @@ import { import { FastifyRequest, RequestGenericInterface } from "fastify/types/request"; import proxy, { type FastifyHttpProxyOptions } from "@fastify/http-proxy"; import accepts from "@fastify/accepts"; -import forwarded from "@fastify/forwarded"; import type { GetLayout, WrappedServerOnly } from "@alignable/bifrost/config"; import { renderPage } from "vike/server"; import { PageContextServer } from "vike/types"; @@ -192,12 +191,18 @@ export const viteProxyPlugin: FastifyPluginAsync< }, replyOptions: { rewriteRequestHeaders(request, headers) { - if (!(request.raw instanceof Http2ServerRequest)) { - const fwd = forwarded(request.raw).reverse(); - headers["X-Forwarded-For"] = fwd.join(", "); - headers["X-Forwarded-Host"] = host.host; - headers["X-Forwarded-Proto"] = host.protocol; + // Build X-Forwarded-For from existing header + socket address + const existingFor = request.headers["x-forwarded-for"]; + const clientIp = request.raw.socket?.remoteAddress; + if (existingFor && clientIp) { + headers["X-Forwarded-For"] = `${clientIp}, ${existingFor}`; + } else if (existingFor) { + headers["X-Forwarded-For"] = existingFor; + } else if (clientIp) { + headers["X-Forwarded-For"] = clientIp; } + headers["X-Forwarded-Host"] = host.host; + headers["X-Forwarded-Proto"] = host.protocol.replace(":", ""); if ((request.raw as RawRequestExtendedWithProxy)._bfproxy) { // Proxying and wrapping diff --git a/bifrost-fastify/package.json b/bifrost-fastify/package.json index 2d1f85c..ae6182b 100644 --- a/bifrost-fastify/package.json +++ b/bifrost-fastify/package.json @@ -7,7 +7,6 @@ "types": "./dist/index.d.ts", "dependencies": { "@fastify/accepts": "^5.0.4", - "@fastify/forwarded": "^3.0.1", "@fastify/http-proxy": "^11.4.1", "@fastify/reply-from": "^12.5.0", "fast-content-type-parse": "^3.0.0", diff --git a/package-lock.json b/package-lock.json index 1b9eba0..0e79a97 100644 --- a/package-lock.json +++ b/package-lock.json @@ -53,7 +53,6 @@ "version": "1.0.23", "dependencies": { "@fastify/accepts": "^5.0.4", - "@fastify/forwarded": "^3.0.1", "@fastify/http-proxy": "^11.4.1", "@fastify/reply-from": "^12.5.0", "fast-content-type-parse": "^3.0.0",