ssh-client-wrapper 3.4.4 → 3.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/fork.js +14 -1
- package/lib/sshExec.js +28 -22
- package/package.json +1 -1
package/lib/fork.js
CHANGED
|
@@ -68,6 +68,19 @@ export function sshLoginCallback(output, ptyProcess, pw, ph, logger = debugSendP
|
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/**
|
|
72
|
+
* end the shell of a master pty. The shell has no other purpose than to start the master ssh.
|
|
73
|
+
* Typing "exit" is not enough, ssh which is still running in the foreground would read it.
|
|
74
|
+
* kill() without a signal sends SIGHUP, which the shell sometimes survives while ssh is ending.
|
|
75
|
+
* A left shell keeps the Node.js process alive.
|
|
76
|
+
* Signals are not supported on win32, where kill() closes the pseudo console.
|
|
77
|
+
* @param {object} pty - pty object returned by node-pty spawn()
|
|
78
|
+
* @param {string} [platform] - platform name
|
|
79
|
+
*/
|
|
80
|
+
export function endMasterShell(pty, platform = process.platform) {
|
|
81
|
+
pty.kill(platform === "win32" ? undefined : "SIGKILL");
|
|
82
|
+
}
|
|
83
|
+
|
|
71
84
|
/**
|
|
72
85
|
* create pty for master ssh session and store to hostInfo.masterPty
|
|
73
86
|
*/
|
|
@@ -76,7 +89,7 @@ export function createMasterPty(hostInfo) {
|
|
|
76
89
|
|
|
77
90
|
if (hostInfo.masterPty) {
|
|
78
91
|
debug(`kill existing master pty for ${hostInfo.host}`);
|
|
79
|
-
hostInfo.masterPty
|
|
92
|
+
endMasterShell(hostInfo.masterPty);
|
|
80
93
|
}
|
|
81
94
|
const ptyProcess = spawn(nodePtyOpt.shell, [], nodePtyOpt);
|
|
82
95
|
ptyProcess.onExit(()=>{
|
package/lib/sshExec.js
CHANGED
|
@@ -2,7 +2,7 @@ import crypto from "crypto";
|
|
|
2
2
|
import { setTimeout as setTimeoutPromise } from "timers/promises";
|
|
3
3
|
import Debug from "debug";
|
|
4
4
|
import { sendPty, sshCmd, getSshOption, getNoStdinOption, isControlMasterEnabled, sanityCheck, watchDogTimer, rePwPrompt, rePhPrompt } from "./util.js";
|
|
5
|
-
import { fork, createMasterPty, sshLoginCallback } from "./fork.js";
|
|
5
|
+
import { fork, createMasterPty, endMasterShell, sshLoginCallback } from "./fork.js";
|
|
6
6
|
import { shouldUseAgent, ensureAgentForHost } from "./agent.js";
|
|
7
7
|
import { makeSshError, makeMasterCommand, extractSshOutput, isFatalOutput } from "./sshError.js";
|
|
8
8
|
|
|
@@ -119,9 +119,9 @@ export async function canConnect(hostInfo, timeout) {
|
|
|
119
119
|
*/
|
|
120
120
|
async function existsMaster(hostInfo, argTimeout) {
|
|
121
121
|
debugVerbose("existsMaster called");
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
args
|
|
122
|
+
//-O must be in front of the host name: after a word which is not an option (e.g. in sshOpt),
|
|
123
|
+
//ssh would take it as a part of the remote command
|
|
124
|
+
const args = ["-Ocheck", ...getSshOption(hostInfo), ...getNoStdinOption()];
|
|
125
125
|
|
|
126
126
|
let output = "";
|
|
127
127
|
const cb = (e)=>{
|
|
@@ -141,10 +141,12 @@ async function existsMaster(hostInfo, argTimeout) {
|
|
|
141
141
|
debug("check master session failed", err);
|
|
142
142
|
throw err;
|
|
143
143
|
}
|
|
144
|
+
//only the exit code 0 tells that the master answered. The reason of 255 does not matter here:
|
|
145
|
+
//if ssh can not work at all, the following connection tells why
|
|
146
|
+
debug("no master session:", output);
|
|
147
|
+
return false;
|
|
144
148
|
}
|
|
145
|
-
|
|
146
|
-
//"Connection refused" means that the socket is left but nobody listens (the master was killed, or it is exiting)
|
|
147
|
-
return !/Control socket connect.*: (No such file or directory|Connection refused)/.test(output);
|
|
149
|
+
return true;
|
|
148
150
|
}
|
|
149
151
|
|
|
150
152
|
/**
|
|
@@ -229,6 +231,10 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
229
231
|
|
|
230
232
|
debug(`create master pty for ${hostInfo.host}`);
|
|
231
233
|
createMasterPty(hostInfo, debugSendMasterPty);
|
|
234
|
+
//this connection deals only with its own pty. hostInfo.masterPty can be released or replaced at any time
|
|
235
|
+
const pty = hostInfo.masterPty;
|
|
236
|
+
//the output which arrives after the result of the connection is known must be ignored
|
|
237
|
+
let over = false;
|
|
232
238
|
|
|
233
239
|
debug(`create master ssh session to ${hostInfo.host}`);
|
|
234
240
|
const words = crypto.randomUUID();
|
|
@@ -242,9 +248,6 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
242
248
|
return makeSshError(extractSshOutput(output, failMark), hostInfo);
|
|
243
249
|
};
|
|
244
250
|
ac = watchDogTimer(timeout, "make connection", ()=>{
|
|
245
|
-
sendPty(hostInfo.masterPty, "\n", debugSendMasterPty, 137);
|
|
246
|
-
sendPty(hostInfo.masterPty, "exit\n", debugSendMasterPty, 138);
|
|
247
|
-
|
|
248
251
|
const err = failure();
|
|
249
252
|
err.message = `can not connect within ${timeout} sec`;
|
|
250
253
|
err.code = "TIMEEXPIRE";
|
|
@@ -252,8 +255,8 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
252
255
|
});
|
|
253
256
|
|
|
254
257
|
let done = false;
|
|
255
|
-
|
|
256
|
-
if (done) {
|
|
258
|
+
pty.onData((data)=>{
|
|
259
|
+
if (done || over) {
|
|
257
260
|
//safeguard
|
|
258
261
|
return;
|
|
259
262
|
}
|
|
@@ -275,7 +278,10 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
275
278
|
});
|
|
276
279
|
}
|
|
277
280
|
});
|
|
278
|
-
|
|
281
|
+
pty.onData((data)=>{
|
|
282
|
+
if (over) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
279
285
|
const output = data.toString();
|
|
280
286
|
|
|
281
287
|
if (shouldUseAgent(hostInfo) && (rePwPrompt.test(output) || rePhPrompt.test(output))) {
|
|
@@ -293,28 +299,29 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
293
299
|
return;
|
|
294
300
|
}
|
|
295
301
|
}
|
|
296
|
-
sshLoginCallback(output,
|
|
302
|
+
sshLoginCallback(output, pty, hostInfo.password, hostInfo.passphrase, debugSendMasterPty)
|
|
297
303
|
.catch(reject);
|
|
298
304
|
});
|
|
299
|
-
|
|
305
|
+
pty.onExit((e)=>{
|
|
300
306
|
debug("master pty exit", e);
|
|
301
307
|
resolve(e);
|
|
302
308
|
});
|
|
303
309
|
});
|
|
304
310
|
|
|
305
311
|
try {
|
|
306
|
-
await sendPty(
|
|
312
|
+
await sendPty(pty, line, debugSendMasterPty, 190);
|
|
307
313
|
await p;
|
|
308
314
|
debug(`connected to ${hostInfo.host}`);
|
|
309
315
|
} catch (e) {
|
|
310
316
|
//the pty of a failed connection must not be left behind. It keeps the process alive.
|
|
311
317
|
debug(`failed to connect to ${hostInfo.host}, kill master pty`);
|
|
312
|
-
|
|
313
|
-
|
|
318
|
+
endMasterShell(pty);
|
|
319
|
+
if (hostInfo.masterPty === pty) {
|
|
314
320
|
hostInfo.masterPty = null;
|
|
315
321
|
}
|
|
316
322
|
throw e;
|
|
317
323
|
} finally {
|
|
324
|
+
over = true;
|
|
318
325
|
if (ac !== null) {
|
|
319
326
|
ac.abort();
|
|
320
327
|
}
|
|
@@ -336,9 +343,8 @@ export async function disconnect(hostInfo) {
|
|
|
336
343
|
|
|
337
344
|
try {
|
|
338
345
|
if (await existsMaster(hostInfo)) {
|
|
339
|
-
|
|
340
|
-
args
|
|
341
|
-
args.push("-Oexit");
|
|
346
|
+
//-O in front of the host name, for the same reason as in existsMaster()
|
|
347
|
+
const args = ["-Oexit", ...getSshOption(hostInfo), ...getNoStdinOption()];
|
|
342
348
|
|
|
343
349
|
let output = "";
|
|
344
350
|
const cb = (e)=>{
|
|
@@ -362,7 +368,7 @@ export async function disconnect(hostInfo) {
|
|
|
362
368
|
//the pty must be released even if the master is already gone. It keeps the process alive.
|
|
363
369
|
if (hostInfo.masterPty) {
|
|
364
370
|
debug("exit masterPty");
|
|
365
|
-
|
|
371
|
+
endMasterShell(hostInfo.masterPty);
|
|
366
372
|
hostInfo.masterPty = null;
|
|
367
373
|
}
|
|
368
374
|
}
|