getClientIP respect trustProxy setting

This commit is contained in:
Louis Lam
2022-07-31 23:36:33 +08:00
parent a07f54f35b
commit a3b1612938
2 changed files with 36 additions and 21 deletions

View File

@@ -8,6 +8,7 @@ const { log } = require("../src/util");
const Database = require("./database");
const util = require("util");
const { CacheableDnsHttpAgent } = require("./cacheable-dns-http-agent");
const { setting } = require("./util-server");
/**
* `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue.
@@ -128,6 +129,18 @@ class UptimeKumaServer {
errorLogStream.end();
}
async getClientIP(socket) {
const clientIP = socket.client.conn.remoteAddress.replace(/^.*:/, "");
if (await setting("trustProxy")) {
return socket.client.conn.request.headers["x-forwarded-for"]
|| socket.client.conn.request.headers["x-real-ip"]
|| clientIP;
} else {
return clientIP;
}
}
}
module.exports = {