Commits
Alan Orth authored and GitHub committed a705067aed5 Merge
11 11 | constructor(@Inject(REQUEST) protected req: any) { |
12 12 | } |
13 13 | |
14 14 | /** |
15 15 | * Intercept http requests and add the client's IP to the X-Forwarded-For header |
16 16 | * @param httpRequest |
17 17 | * @param next |
18 18 | */ |
19 19 | intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { |
20 20 | const clientIp = this.req.get('x-forwarded-for') || this.req.connection.remoteAddress; |
21 - | return next.handle(httpRequest.clone({ setHeaders: { 'X-Forwarded-For': clientIp } })); |
21 + | const headers = { 'X-Forwarded-For': clientIp }; |
22 + | |
23 + | // if the request has a user-agent retain it |
24 + | const userAgent = this.req.get('user-agent'); |
25 + | if (userAgent) { |
26 + | headers['User-Agent'] = userAgent; |
27 + | } |
28 + | |
29 + | return next.handle(httpRequest.clone({ setHeaders: headers })); |
22 30 | } |
23 31 | } |