Skip to content

Commit

Permalink
Server: Overhaul proxy checker code
Browse files Browse the repository at this point in the history
This was a mess. Now it should work properly.
  • Loading branch information
mia-pi-git committed Feb 28, 2024
1 parent 4a72301 commit 9e57116
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,19 @@ export class ActionContext {
_ip = '';
getIp() {
if (this._ip) return this._ip;
const ip = this.request.socket.remoteAddress || "";
let forwarded = this.request.headers['x-forwarded-for'] || '';
if (!Array.isArray(forwarded)) forwarded = forwarded.split(',');
const notProxy = forwarded.filter(f => !this.isTrustedProxy(f));
if (notProxy.length !== forwarded.length) {
this._ip = notProxy.pop() || ip;
return this._ip;
let ip = this.request.socket.remoteAddress || "";
if (this.isTrustedProxy(ip)) {
const ips = ((this.request.headers['x-forwarded-for'] || '') + "").split(',').reverse();
for (let proxy of ips) {
proxy = proxy.trim();
if (!this.isTrustedProxy(proxy)) {
ip = proxy;
break;
}
}
}
this._ip = ip || '';
return this._ip;
this._ip = ip;
return ip;
}
setHeader(name: string, value: string | string[]) {
this.response.setHeader(name, value);
Expand Down

0 comments on commit 9e57116

Please sign in to comment.