Files
overleaf-cep/services/real-time/app/js/WebsocketAddressManager.js
Andrew Rumble 3073c94522 Merge pull request #30215 from overleaf/ar/convert-real-time-to-esm
[real-time] convert real time to esm

GitOrigin-RevId: 7cc530cc977549d3274be42585735e1fd72cad5f
2026-01-13 09:06:30 +00:00

40 lines
1.2 KiB
JavaScript

import proxyaddr from 'proxy-addr'
export default 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
}
}
}