Skip to content

Commit

Permalink
Client: emit error on pre-handshake connection loss
Browse files Browse the repository at this point in the history
Fixes: mscdex#610
  • Loading branch information
mscdex committed Nov 8, 2020
1 parent 11f7d90 commit 93666dc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
29 changes: 28 additions & 1 deletion lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const {
const Protocol = require('./protocol/Protocol.js');
const { parseKey } = require('./protocol/keyParser.js');
const { SFTP } = require('./protocol/SFTP.js');
const { readUInt32BE } = require('./protocol/utils.js');
const { makeError, readUInt32BE } = require('./protocol/utils.js');

const agentQuery = require('./agent.js');
const {
Expand Down Expand Up @@ -270,6 +270,7 @@ class Client extends EventEmitter {

const sock = this._sock = (cfg.sock || new Socket());
let ready = false;
let sawHeader = false;
if (this._protocol)
this._protocol.cleanup();
const DEBUG_HANDLER = (!debug ? undefined : (p, display, msg) => {
Expand All @@ -292,6 +293,7 @@ class Client extends EventEmitter {
} catch {}
},
onHeader: (header) => {
sawHeader = true;
this._remoteVer = header.versions.software;
if (header.greeting)
this.emit('greeting', header.greeting);
Expand Down Expand Up @@ -680,6 +682,27 @@ class Client extends EventEmitter {
}
this._resetKA = resetKA;

{
let errorEmitted = false;
this.once('error', (err) => {
errorEmitted = true;
if (this.listenerCount('error') === 0)
this.emit('error', err);
});
let called = false;
// Intentional `var`
// eslint-disable-next-line no-var
var onDone = () => {
if (called)
return;
called = true;
if (!errorEmitted && !sawHeader) {
const err =
makeError('Connection lost before handshake', 'protocol', true);
this.emit('error', err);
}
};
}
sock.on('connect', () => {
debug && debug('Socket connected');
this.emit('connect');
Expand All @@ -692,12 +715,16 @@ class Client extends EventEmitter {
this.emit('error', err);
}).on('end', () => {
debug && debug('Socket ended');
// eslint-disable-next-line block-scoped-var
onDone();
proto.cleanup();
clearTimeout(this._readyTimeout);
clearInterval(katimer);
this.emit('end');
}).on('close', () => {
debug && debug('Socket closed');
// eslint-disable-next-line block-scoped-var
onDone();
proto.cleanup();
clearTimeout(this._readyTimeout);
clearInterval(katimer);
Expand Down
29 changes: 27 additions & 2 deletions test/test-misc-client-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,7 @@ const setup = setupSimple.bind(undefined, debug);
let cliError;
client.on('ready', mustNotCall()).on('error', mustCall((err) => {
if (cliError) {
assert(/all configured/i.test(err.message),
'Wrong error message');
assert(/all configured/i.test(err.message), 'Wrong error message');
} else {
cliError = err;
assert(/signing/i.test(err.message), 'Wrong error message');
Expand Down Expand Up @@ -1246,3 +1245,29 @@ const setup = setupSimple.bind(undefined, debug);
});
}));
}

{
const { client } = setup_(
'Client error should be emitted on bad/nonexistent greeting',
{
client: clientCfg,
noClientError: true,
noForceClientReady: true,
},
);

const badServer = net.createServer(mustCall((s) => {
badServer.close();
s.end();
})).listen(0, 'localhost', mustCall(() => {
client.on('error', mustCall((err) => {
client.end();
})).on('ready', mustNotCall()).on('close', mustCall(() => {}));
client.connect({
host: 'localhost',
port: badServer.address().port,
user: 'foo',
password: 'bar',
});
}));
}

0 comments on commit 93666dc

Please sign in to comment.