Limit pings like messages in the tracker
ws answered every ping on its own, past the message rate limit and past the bound on bytes queued for a peer: a client pinging without reading made the tracker buffer pongs until it ran out of memory. Pongs are now sent by the tracker, pings and pongs count as messages, and a pong is queued only within the same bound as any message. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
22d794965e
commit
4725b14a40
2 files changed
+28
-4
No files matched your search
+12
-4
@@ -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
|
||||
|
||||
Reference in new issue
Block a user