overleaf/services/real-time/app/js/WebsocketAddressManager.js
Brian Gough 6cece12369 Merge pull request #5109 from overleaf/bg-fix-handshake-exception
fix handshake exception

GitOrigin-RevId: 917518ccac3f5fddf4029f1b1ec3e3806dde9a29
2021-09-16 08:03:08 +00:00

39 lines
1.3 KiB
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 (!clientHandshake) {
return 'client-handshake-missing'
} else if (this.trust) {
// create a dummy req object using the client handshake and
// connection.remoteAddress for the proxy-addr module to parse
try {
const addressPort = clientHandshake.address
const req = {
headers: {
'x-forwarded-for':
clientHandshake.headers &&
clientHandshake.headers['x-forwarded-for'],
},
connection: { remoteAddress: addressPort && addressPort.address },
}
// return the address parsed from x-forwarded-for
return proxyaddr(req, this.trust)
} catch (err) {
return 'client-handshake-invalid'
}
} else {
// return the address from the client handshake itself
return clientHandshake.address && clientHandshake.address.address
}
}
}