Skip to content

Commit

Permalink
Forwarding requests to upstream added "x-forwarded-for". (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanpf authored Oct 9, 2023
1 parent 3806e78 commit a6ec5a0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
11 changes: 9 additions & 2 deletions pjs/http/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
serviceHandlers = new algo.Cache(makeServiceHandler),

) => pipy({
_xff: null,
_serviceName: null,
_unauthorized: undefined,
})
Expand All @@ -27,15 +28,21 @@

.pipeline()
.handleMessageStart(
() => (
msg => (
__route?.config?.EnableHeadersAuthorization && (
(!__consumer || !__consumer?.['Headers-Authorization']) ? (_unauthorized = true) : (_unauthorized = false)
),
__route?.serverRoot ? (
__root = __route.serverRoot
) : (
(_serviceName = __route?.backendServiceBalancer?.borrow?.({})?.id) && (
__service = serviceHandlers.get(_serviceName)
(__service = serviceHandlers.get(_serviceName)) && msg?.head?.headers && (
(_xff = msg.head.headers['x-forwarded-for']) ? (
msg.head.headers['x-forwarded-for'] = _xff + ', ' + __inbound.localAddress
) : (
msg.head.headers['x-forwarded-for'] = __inbound.localAddress
)
)
)
)
)
Expand Down
4 changes: 2 additions & 2 deletions pjs/tcp/forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
(_serviceConfig = serviceConfigs.get(__service)) && (
__metricLabel = __service.name,
_unhealthCache = __healthCheckServices?.[__service.name],
(__target = _serviceConfig.targetBalancer?.borrow?.({}, undefined, _unhealthCache)?.id) && (
(__target = _serviceConfig.targetBalancer?.borrow?.(__inbound, undefined, _unhealthCache)?.id) && (
(
attrs = _serviceConfig?.endpointAttributes?.[__target]
) => (
Expand Down Expand Up @@ -138,4 +138,4 @@
)
)

)()
)()
4 changes: 2 additions & 2 deletions pjs/tls/forward.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
(_serviceConfig = serviceConfigs.get(__service)) && (
__metricLabel = __service.name,
_unhealthCache = __healthCheckServices?.[__service.name],
(__target = _serviceConfig.targetBalancer?.borrow?.({}, undefined, _unhealthCache)?.id) && (
(__target = _serviceConfig.targetBalancer?.borrow?.(__inbound, undefined, _unhealthCache)?.id) && (
(
attrs = _serviceConfig?.endpointAttributes?.[__target]
) => (
Expand Down Expand Up @@ -156,4 +156,4 @@
)
)

)()
)()

0 comments on commit a6ec5a0

Please sign in to comment.