mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-18 12:11:36 +00:00
adds handling for x-forwarded-for comma-separated syntax (#452)
This commit is contained in:
parent
bf4481c09a
commit
bf8831c64a
@ -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
|
||||||
|
@ -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}`
|
||||||
|
Loading…
Reference in New Issue
Block a user