Skip to content

Commit 1d986f1

Browse files
committed
refactor(gateway): move request client ip resolution to net
1 parent 904db27 commit 1d986f1

File tree

3 files changed

+24
-18
lines changed

3 files changed

+24
-18
lines changed

src/gateway/auth.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { resolveGatewayCredentialsFromValues } from "./credentials.js";
1616
import {
1717
isLocalishHost,
1818
isLoopbackAddress,
19+
resolveRequestClientIp,
1920
isTrustedProxyAddress,
2021
resolveClientIp,
2122
} from "./net.js";
@@ -105,23 +106,6 @@ function resolveTailscaleClientIp(req?: IncomingMessage): string | undefined {
105106
});
106107
}
107108

108-
export function resolveRequestClientIp(
109-
req?: IncomingMessage,
110-
trustedProxies?: string[],
111-
allowRealIpFallback = false,
112-
): string | undefined {
113-
if (!req) {
114-
return undefined;
115-
}
116-
return resolveClientIp({
117-
remoteAddr: req.socket?.remoteAddress ?? "",
118-
forwardedFor: headerValue(req.headers?.["x-forwarded-for"]),
119-
realIp: headerValue(req.headers?.["x-real-ip"]),
120-
trustedProxies,
121-
allowRealIpFallback,
122-
});
123-
}
124-
125109
export function isLocalDirectRequest(
126110
req?: IncomingMessage,
127111
trustedProxies?: string[],

src/gateway/net.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { IncomingMessage } from "node:http";
12
import net from "node:net";
23
import os from "node:os";
34
import { pickPrimaryTailnetIPv4, pickPrimaryTailnetIPv6 } from "../infra/tailnet.js";
@@ -184,6 +185,27 @@ export function resolveClientIp(params: {
184185
return undefined;
185186
}
186187

188+
function headerValue(value: string | string[] | undefined): string | undefined {
189+
return Array.isArray(value) ? value[0] : value;
190+
}
191+
192+
export function resolveRequestClientIp(
193+
req?: IncomingMessage,
194+
trustedProxies?: string[],
195+
allowRealIpFallback = false,
196+
): string | undefined {
197+
if (!req) {
198+
return undefined;
199+
}
200+
return resolveClientIp({
201+
remoteAddr: req.socket?.remoteAddress ?? "",
202+
forwardedFor: headerValue(req.headers?.["x-forwarded-for"]),
203+
realIp: headerValue(req.headers?.["x-real-ip"]),
204+
trustedProxies,
205+
allowRealIpFallback,
206+
});
207+
}
208+
187209
export function isLocalGatewayAddress(ip: string | undefined): boolean {
188210
if (isLoopbackAddress(ip)) {
189211
return true;

src/gateway/server-http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
import {
2424
authorizeHttpGatewayConnect,
2525
isLocalDirectRequest,
26-
resolveRequestClientIp,
2726
type GatewayAuthResult,
2827
type ResolvedGatewayAuth,
2928
} from "./auth.js";
@@ -53,6 +52,7 @@ import {
5352
} from "./hooks.js";
5453
import { sendGatewayAuthFailure, setDefaultSecurityHeaders } from "./http-common.js";
5554
import { getBearerToken } from "./http-utils.js";
55+
import { resolveRequestClientIp } from "./net.js";
5656
import { handleOpenAiHttpRequest } from "./openai-http.js";
5757
import { handleOpenResponsesHttpRequest } from "./openresponses-http.js";
5858
import {

0 commit comments

Comments
 (0)