diff --git a/backend/tracker.mjs b/backend/tracker.mjs index 49b6336..baa2144 100644 --- a/backend/tracker.mjs +++ b/backend/tracker.mjs @@ -57,10 +57,14 @@ function refuse(socket, status) { // MAX_FORWARDED limits what each sender sends, this what a peer gets from all of them, e.g. from // receivers that reconnect over and over. A peer that stopped reading is dropped at once, close would // wait for its close frame. +function fits(peer, bytes) { + if (peer.ws.bufferedAmount + bytes <= MAX_QUEUED) return true; + peer.ws.terminate(); +} + function send(peer, message) { const data = JSON.stringify(message); - if (peer.ws.bufferedAmount + Buffer.byteLength(data) > MAX_QUEUED) return peer.ws.terminate(); - peer.ws.send(data); + if (fits(peer, Buffer.byteLength(data))) peer.ws.send(data); } // HellCon shows the reason, the connection closes. @@ -133,8 +137,12 @@ function open(ws) { const age = setTimeout(() => ws.close(), MAX_AGE); // E.g. a message beyond MAX_MESSAGE, the connection closes then. Unhandled, it would end the process. ws.on('error', () => {}); + // Pings and pongs count as messages. ws would answer every ping on its own, past the limits. + const counted = () => ++peer.messages <= MAX_MESSAGES || reject(peer, 'too many messages'); + ws.on('ping', (data) => counted() && fits(peer, data.length) && ws.pong(data)); + ws.on('pong', counted); ws.on('message', (data, binary) => { - if (++peer.messages > MAX_MESSAGES) return reject(peer, 'too many messages'); + if (!counted()) return; let msg; try { msg = binary ? undefined : JSON.parse(data); @@ -158,7 +166,7 @@ function open(ws) { }); } -const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_MESSAGE, clientTracking: false }); +const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_MESSAGE, clientTracking: false, autoPong: false }); export const server = createServer((_, res) => res.writeHead(404).end()); server.on('upgrade', (req, socket, head) => { // Node leaves an upgraded socket without one, ws adds its own only once it takes over. Unhandled, a diff --git a/backend/tracker.spec.mjs b/backend/tracker.spec.mjs index ed1edd0..5c9d3a0 100644 --- a/backend/tracker.spec.mjs +++ b/backend/tracker.spec.mjs @@ -234,6 +234,22 @@ test('limits the message rate', async () => { await rejected(client, fields, 'too many messages'); }); +test('limits pings like messages, also from a peer that stops reading', async () => { + // ws's client, browsers send no pings. ws used to answer every one, its pongs piled up in the tracker. + const client = new ServerSocket(url, { headers: { origin: ORIGIN, 'x-forwarded-for': ip } }); + const closed = once(client, 'close'); + const reasons = []; + client.on('message', (data) => reasons.push(JSON.parse(data)['failure reason'])); + await once(client, 'open'); + client.ping('x'); + expect(String((await once(client, 'pong'))[0])).toBe('x'); + client._socket.pause(); + for (let i = 0; i < 1000; i++) client.ping('x'.repeat(125)); + client._socket.resume(); + await closed; + expect(reasons).toEqual(['too many messages']); +}); + test('limits the signaling forwarded per connection', async () => { const { hash, receiver } = await swarm(); const big = () => ({ offer_id: id(), offer: sdp('offer', 'x'.repeat(15_000)) });