adds handling for x-forwarded-for comma-separated syntax (#452)

This commit is contained in:
Tom Snelling 2023-03-17 01:07:39 +00:00 committed by GitHub
parent bf4481c09a
commit bf8831c64a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 6 deletions

View File

@ -33,9 +33,17 @@ function parseHttpRequest (req, opts) {
common.MAX_ANNOUNCE_PEERS common.MAX_ANNOUNCE_PEERS
) )
params.ip = opts.trustProxy if (opts.trustProxy) {
? req.headers['x-forwarded-for'] || req.connection.remoteAddress if (req.headers['x-forwarded-for']) {
: req.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 const [realIp] = req.headers['x-forwarded-for'].split(',')
params.ip = realIp.trim()
} else {
params.ip = req.connection.remoteAddress
}
} else {
params.ip = req.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4
}
params.addr = `${common.IPV6_RE.test(params.ip) ? `[${params.ip}]` : params.ip}:${params.port}` params.addr = `${common.IPV6_RE.test(params.ip) ? `[${params.ip}]` : params.ip}:${params.port}`
params.headers = req.headers params.headers = req.headers

View File

@ -55,9 +55,17 @@ function parseWebSocketRequest (socket, opts, params) {
// On first parse, save important data from `socket.upgradeReq` and delete it // On first parse, save important data from `socket.upgradeReq` and delete it
// to reduce memory usage. // to reduce memory usage.
if (socket.upgradeReq) { if (socket.upgradeReq) {
socket.ip = opts.trustProxy if (opts.trustProxy) {
? socket.upgradeReq.headers['x-forwarded-for'] || socket.upgradeReq.connection.remoteAddress if (socket.upgradeReq.headers['x-forwarded-for']) {
: socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 const [realIp] = socket.upgradeReq.headers['x-forwarded-for'].split(',')
socket.ip = realIp.trim()
} else {
socket.ip = socket.upgradeReq.connection.remoteAddress
}
} else {
socket.ip = socket.upgradeReq.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4
}
socket.port = socket.upgradeReq.connection.remotePort socket.port = socket.upgradeReq.connection.remotePort
if (socket.port) { if (socket.port) {
socket.addr = `${common.IPV6_RE.test(socket.ip) ? `[${socket.ip}]` : socket.ip}:${socket.port}` socket.addr = `${common.IPV6_RE.test(socket.ip) ? `[${socket.ip}]` : socket.ip}:${socket.port}`