mirror of
https://github.com/overleaf/overleaf.git
synced 2024-09-16 02:52:31 -04:00
618cf99548
realtime log re-levelling GitOrigin-RevId: 81d86ba648e7fc1436b638b67371b35fd831a62f
30 lines
1,013 B
JavaScript
30 lines
1,013 B
JavaScript
const proxyaddr = require('proxy-addr')
|
|
|
|
module.exports = class WebsocketAddressManager {
|
|
constructor(behindProxy, trustedProxyIps) {
|
|
if (behindProxy) {
|
|
// parse trustedProxyIps comma-separated list the same way as express
|
|
this.trust = proxyaddr.compile(
|
|
trustedProxyIps ? trustedProxyIps.split(/ *, */) : []
|
|
)
|
|
}
|
|
}
|
|
|
|
getRemoteIp(clientHandshake) {
|
|
if (this.trust) {
|
|
// create a dummy req object using the client handshake and
|
|
// connection.remoteAddress for the proxy-addr module to parse
|
|
const addressPort = clientHandshake.address
|
|
const req = Object.create(clientHandshake, {
|
|
connection: {
|
|
value: { remoteAddress: addressPort && addressPort.address },
|
|
},
|
|
})
|
|
// return the address parsed from x-forwarded-for
|
|
return proxyaddr(req, this.trust)
|
|
} else {
|
|
// return the address from the client handshake itself
|
|
return clientHandshake.address && clientHandshake.address.address
|
|
}
|
|
}
|
|
}
|