File tree Expand file tree Collapse file tree 3 files changed +24
-18
lines changed
Expand file tree Collapse file tree 3 files changed +24
-18
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ import { resolveGatewayCredentialsFromValues } from "./credentials.js";
1616import {
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-
125109export function isLocalDirectRequest (
126110 req ?: IncomingMessage ,
127111 trustedProxies ?: string [ ] ,
Original file line number Diff line number Diff line change 1+ import type { IncomingMessage } from "node:http" ;
12import net from "node:net" ;
23import os from "node:os" ;
34import { 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+
187209export function isLocalGatewayAddress ( ip : string | undefined ) : boolean {
188210 if ( isLoopbackAddress ( ip ) ) {
189211 return true ;
Original file line number Diff line number Diff line change @@ -23,7 +23,6 @@ import {
2323import {
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" ;
5453import { sendGatewayAuthFailure , setDefaultSecurityHeaders } from "./http-common.js" ;
5554import { getBearerToken } from "./http-utils.js" ;
55+ import { resolveRequestClientIp } from "./net.js" ;
5656import { handleOpenAiHttpRequest } from "./openai-http.js" ;
5757import { handleOpenResponsesHttpRequest } from "./openresponses-http.js" ;
5858import {
You can’t perform that action at this time.
0 commit comments