ssh-client-wrapper 3.4.4 → 3.4.6
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/index.js +1 -1
- package/lib/sshError.js +9 -7
- package/lib/sshExec.js +30 -26
- 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/index.js
CHANGED
package/lib/sshError.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { stripTerminalSequences } from "./util.js";
|
|
2
2
|
|
|
3
3
|
const MAX_OUTPUT = 4096;
|
|
4
|
+
const OK_PREFIX = "SCWOK";
|
|
4
5
|
const FAIL_PREFIX = "SCWFAIL";
|
|
5
6
|
|
|
6
7
|
//messages of ssh after which there is nothing to wait for. The first match wins.
|
|
@@ -56,18 +57,19 @@ export const makeSshError = (output, hostInfo)=>{
|
|
|
56
57
|
|
|
57
58
|
/**
|
|
58
59
|
* make the command line which is typed into the shell of the master pty
|
|
59
|
-
* the
|
|
60
|
-
* so that the line which the terminal echoes back
|
|
61
|
-
* "||" is understood by sh, bash, zsh, fish and csh.
|
|
60
|
+
* the remote host echoes okMark if the login succeeds, and the shell prints failMark only if ssh fails.
|
|
61
|
+
* Both marks are written with "" in them, so that the line which the terminal echoes back (in one or more pieces)
|
|
62
|
+
* does not contain them. "||" is understood by sh, bash, zsh, fish and csh.
|
|
62
63
|
* @param {string} cmd - ssh command
|
|
63
64
|
* @param {Array} args - arguments of ssh
|
|
64
|
-
* @param {string} words - string which
|
|
65
|
-
* @param {string} failId - string which makes
|
|
66
|
-
* @returns {object} - { line: command line,
|
|
65
|
+
* @param {string} words - string which makes okMark unique
|
|
66
|
+
* @param {string} failId - string which makes failMark unique. It must differ from words
|
|
67
|
+
* @returns {object} - { line: command line, okMark: string which tells success, failMark: string which tells failure }
|
|
67
68
|
*/
|
|
68
69
|
export const makeMasterCommand = (cmd, args, words, failId)=>{
|
|
69
70
|
return {
|
|
70
|
-
line: `${cmd} ${args.join(" ")} echo ${words} || echo ${FAIL_PREFIX}""-${failId}\n`,
|
|
71
|
+
line: `${cmd} ${args.join(" ")} echo ${OK_PREFIX}""-${words} || echo ${FAIL_PREFIX}""-${failId}\n`,
|
|
72
|
+
okMark: `${OK_PREFIX}-${words}`,
|
|
71
73
|
failMark: `${FAIL_PREFIX}-${failId}`
|
|
72
74
|
};
|
|
73
75
|
};
|
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,11 +231,13 @@ 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
|
-
const
|
|
235
|
-
const re = new RegExp(`^${words}`);
|
|
236
|
-
const { line, failMark } = makeMasterCommand(sshCmd, getSshOption(hostInfo), words, crypto.randomUUID());
|
|
240
|
+
const { line, okMark, failMark } = makeMasterCommand(sshCmd, getSshOption(hostInfo), crypto.randomUUID(), crypto.randomUUID());
|
|
237
241
|
|
|
238
242
|
let ac = null;
|
|
239
243
|
let output = "";
|
|
@@ -242,9 +246,6 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
242
246
|
return makeSshError(extractSshOutput(output, failMark), hostInfo);
|
|
243
247
|
};
|
|
244
248
|
ac = watchDogTimer(timeout, "make connection", ()=>{
|
|
245
|
-
sendPty(hostInfo.masterPty, "\n", debugSendMasterPty, 137);
|
|
246
|
-
sendPty(hostInfo.masterPty, "exit\n", debugSendMasterPty, 138);
|
|
247
|
-
|
|
248
249
|
const err = failure();
|
|
249
250
|
err.message = `can not connect within ${timeout} sec`;
|
|
250
251
|
err.code = "TIMEEXPIRE";
|
|
@@ -252,8 +253,8 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
252
253
|
});
|
|
253
254
|
|
|
254
255
|
let done = false;
|
|
255
|
-
|
|
256
|
-
if (done) {
|
|
256
|
+
pty.onData((data)=>{
|
|
257
|
+
if (done || over) {
|
|
257
258
|
//safeguard
|
|
258
259
|
return;
|
|
259
260
|
}
|
|
@@ -261,7 +262,7 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
261
262
|
output = `${output}${chunk}`.slice(-65536);
|
|
262
263
|
debugVerbose(chunk);
|
|
263
264
|
|
|
264
|
-
if (
|
|
265
|
+
if (output.includes(okMark)) {
|
|
265
266
|
debug("test done. call exit on remotehost");
|
|
266
267
|
done = true;
|
|
267
268
|
resolve();
|
|
@@ -275,7 +276,10 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
275
276
|
});
|
|
276
277
|
}
|
|
277
278
|
});
|
|
278
|
-
|
|
279
|
+
pty.onData((data)=>{
|
|
280
|
+
if (over) {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
279
283
|
const output = data.toString();
|
|
280
284
|
|
|
281
285
|
if (shouldUseAgent(hostInfo) && (rePwPrompt.test(output) || rePhPrompt.test(output))) {
|
|
@@ -293,28 +297,29 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
293
297
|
return;
|
|
294
298
|
}
|
|
295
299
|
}
|
|
296
|
-
sshLoginCallback(output,
|
|
300
|
+
sshLoginCallback(output, pty, hostInfo.password, hostInfo.passphrase, debugSendMasterPty)
|
|
297
301
|
.catch(reject);
|
|
298
302
|
});
|
|
299
|
-
|
|
303
|
+
pty.onExit((e)=>{
|
|
300
304
|
debug("master pty exit", e);
|
|
301
305
|
resolve(e);
|
|
302
306
|
});
|
|
303
307
|
});
|
|
304
308
|
|
|
305
309
|
try {
|
|
306
|
-
await sendPty(
|
|
310
|
+
await sendPty(pty, line, debugSendMasterPty, 190);
|
|
307
311
|
await p;
|
|
308
312
|
debug(`connected to ${hostInfo.host}`);
|
|
309
313
|
} catch (e) {
|
|
310
314
|
//the pty of a failed connection must not be left behind. It keeps the process alive.
|
|
311
315
|
debug(`failed to connect to ${hostInfo.host}, kill master pty`);
|
|
312
|
-
|
|
313
|
-
|
|
316
|
+
endMasterShell(pty);
|
|
317
|
+
if (hostInfo.masterPty === pty) {
|
|
314
318
|
hostInfo.masterPty = null;
|
|
315
319
|
}
|
|
316
320
|
throw e;
|
|
317
321
|
} finally {
|
|
322
|
+
over = true;
|
|
318
323
|
if (ac !== null) {
|
|
319
324
|
ac.abort();
|
|
320
325
|
}
|
|
@@ -336,9 +341,8 @@ export async function disconnect(hostInfo) {
|
|
|
336
341
|
|
|
337
342
|
try {
|
|
338
343
|
if (await existsMaster(hostInfo)) {
|
|
339
|
-
|
|
340
|
-
args
|
|
341
|
-
args.push("-Oexit");
|
|
344
|
+
//-O in front of the host name, for the same reason as in existsMaster()
|
|
345
|
+
const args = ["-Oexit", ...getSshOption(hostInfo), ...getNoStdinOption()];
|
|
342
346
|
|
|
343
347
|
let output = "";
|
|
344
348
|
const cb = (e)=>{
|
|
@@ -362,7 +366,7 @@ export async function disconnect(hostInfo) {
|
|
|
362
366
|
//the pty must be released even if the master is already gone. It keeps the process alive.
|
|
363
367
|
if (hostInfo.masterPty) {
|
|
364
368
|
debug("exit masterPty");
|
|
365
|
-
|
|
369
|
+
endMasterShell(hostInfo.masterPty);
|
|
366
370
|
hostInfo.masterPty = null;
|
|
367
371
|
}
|
|
368
372
|
}
|