Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1176,9 +1176,10 @@ class Client extends EventEmitter {
return this;
}

end() {
end(reason = null, message = '', lang = '') {
reason = reason || DISCONNECT_REASON.BY_APPLICATION;
if (this._sock && isWritable(this._sock)) {
this._protocol.disconnect(DISCONNECT_REASON.BY_APPLICATION);
this._protocol.disconnect(reason, message, lang);
this._sock.end();
}
return this;
Expand Down
23 changes: 18 additions & 5 deletions lib/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,23 +321,36 @@ class Protocol {

// Global
// ------
disconnect(reason) {
const pktLen = 1 + 4 + 4 + 4;
disconnect(reason, message = '', lang = '') {
const msgLen = Buffer.byteLength(message);
const langLen = Buffer.byteLength(lang);
const pktLen = 1 + 4 + 4 + msgLen + 4 + langLen;
// We don't use _packetRW.write.* here because we need to make sure that
// we always get a full packet allocated because this message can be sent
// at any time -- even during a key exchange
let p = this._packetRW.write.allocStartKEX;
const packet = this._packetRW.write.alloc(pktLen, true);
const end = p + pktLen;

if (!VALID_DISCONNECT_REASONS.has(reason))
reason = DISCONNECT_REASON.PROTOCOL_ERROR;

packet[p] = MESSAGE.DISCONNECT;
writeUInt32BE(packet, reason, ++p);
packet.fill(0, p += 4, end);
p += 4;

writeUInt32BE(packet, msgLen, p);
p += 4;
if (msgLen > 0) {
packet.utf8Write(message, p, msgLen);
p += msgLen;
}

writeUInt32BE(packet, langLen, p);
p += 4;
if (langLen > 0)
packet.utf8Write(lang, p, langLen);

this._debug && this._debug(`Outbound: Sending DISCONNECT (${reason})`);
this._debug && this._debug(`Outbound: Sending DISCONNECT (${reason}, message: ${message})`);
sendPacket(this, this._packetRW.write.finalize(packet, true), true);
}
ping() {
Expand Down
5 changes: 3 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1278,9 +1278,10 @@ class Client extends EventEmitter {
}
}

end() {
end(reason = null, message = '', lang = '') {
reason = reason || DISCONNECT_REASON.BY_APPLICATION;
if (this._sock && isWritable(this._sock)) {
this._protocol.disconnect(DISCONNECT_REASON.BY_APPLICATION);
this._protocol.disconnect(reason, message, lang);
this._sock.end();
}
return this;
Expand Down
31 changes: 31 additions & 0 deletions test/test-misc-client-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1458,3 +1458,34 @@ const setup = setupSimple.bind(undefined, debug);
}));
}));
}

{
const { DISCONNECT_REASON } = require('../lib/protocol/constants.js');
const { client, server } = setup_(
'Server disconnect with custom reason and message',
{
client: clientCfg,
server: serverCfg,
noClientError: true,
},
);

server.on('connection', mustCall((conn) => {
conn.on('authentication', mustCall((ctx) => {
ctx.accept();
})).on('ready', mustCall(() => {
conn.end(
DISCONNECT_REASON.PROTOCOL_ERROR,
'Too many authentication failures'
);
}));
}));

client.on('ready', mustCall(() => {}));
client.on('error', mustCall((err) => {
assert(err.code === DISCONNECT_REASON.PROTOCOL_ERROR,
`Expected reason ${DISCONNECT_REASON.PROTOCOL_ERROR}, got: ${err.code}`);
assert(err.message === 'Too many authentication failures',
`Expected custom message, got: ${err.message}`);
}));
}