Skip to content

Commit

Permalink
Merge pull request #3152 from alanorth/client-user-agent
Browse files Browse the repository at this point in the history
Forward client's user-agent instead of Node's
  • Loading branch information
alanorth authored Jul 3, 2024
2 parents 28bd6a5 + 5b2966c commit a705067
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/app/core/forward-client-ip/forward-client-ip.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export class ForwardClientIpInterceptor implements HttpInterceptor {
*/
intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const clientIp = this.req.get('x-forwarded-for') || this.req.connection.remoteAddress;
return next.handle(httpRequest.clone({ setHeaders: { 'X-Forwarded-For': clientIp } }));
const headers = { 'X-Forwarded-For': clientIp };

// if the request has a user-agent retain it
const userAgent = this.req.get('user-agent');
if (userAgent) {
headers['User-Agent'] = userAgent;
}

return next.handle(httpRequest.clone({ setHeaders: headers }));
}
}

0 comments on commit a705067

Please sign in to comment.