ssh-client-wrapper 3.4.6 → 3.4.8
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/README.md +3 -0
- package/lib/agent.js +29 -16
- package/lib/sshExec.js +32 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -206,6 +206,8 @@ Checks if a connection to the remote host can be established.
|
|
|
206
206
|
|
|
207
207
|
### Connection failures
|
|
208
208
|
|
|
209
|
+
Calls made at the same time with one wrapper object share the master connection which is being made, so ssh logs in once and all of them get the same result (the `timeout` of the first call applies).
|
|
210
|
+
|
|
209
211
|
If the login can not be done, the promise is rejected with an `Error` as soon as `ssh` gives up. `err.code` tells the cause, and `err.output` has what `ssh` said (the last 4096 characters). `host`, `user` and `port` tell which login failed.
|
|
210
212
|
|
|
211
213
|
| `err.code` | cause |
|
|
@@ -219,6 +221,7 @@ If the login can not be done, the promise is rejected with an `Error` as soon as
|
|
|
219
221
|
| `BAD_PORT` | invalid port number |
|
|
220
222
|
| `CONTROL_SOCKET_FAILED` | the socket of the master connection can not be created |
|
|
221
223
|
| `TIMEEXPIRE` | `ssh` did not finish within `timeout` |
|
|
224
|
+
| `PTY_EXITED` | the shell which starts the master connection ended before the login succeeded (e.g. `disconnect()` was called meanwhile) |
|
|
222
225
|
| `SSH_FAILED` | any other failure; `message` is the last line which `ssh` said |
|
|
223
226
|
|
|
224
227
|
### Wrong password
|
package/lib/agent.js
CHANGED
|
@@ -6,7 +6,7 @@ import { promisify } from "node:util";
|
|
|
6
6
|
import { setTimeout as setTimeoutPromise } from "node:timers/promises";
|
|
7
7
|
import Debug from "debug";
|
|
8
8
|
import { spawn } from "node-pty";
|
|
9
|
-
import {
|
|
9
|
+
import { resolveExecutable, releasePty } from "./util.js";
|
|
10
10
|
|
|
11
11
|
const debug = Debug("sshClientWrapper:debug:agent");
|
|
12
12
|
const debugVerbose = Debug("sshClientWrapper:verbose:agent");
|
|
@@ -19,6 +19,12 @@ const LOCK_STALE_MS = 60000;
|
|
|
19
19
|
const MAX_BAD_PASSPHRASE = 3;
|
|
20
20
|
const MAX_SOCKET_PATH = 103;
|
|
21
21
|
|
|
22
|
+
//the prompts of ssh-add differ from the one of ssh ("Enter passphrase for key '/k': "):
|
|
23
|
+
//"Enter passphrase for /k: " and, after a wrong one, "Bad passphrase, try again for /k: "
|
|
24
|
+
//both have "(comment)" after the path in some versions of OpenSSH
|
|
25
|
+
const reAddKeyPrompt = /(?:Enter passphrase for|Bad passphrase, try again for) /;
|
|
26
|
+
const reBadPassphrase = /Bad passphrase|incorrect passphrase/i;
|
|
27
|
+
|
|
22
28
|
//sentinel candidate meaning "run ssh-add with the inherited environment and let
|
|
23
29
|
//OpenSSH use its default agent (e.g. the Windows OpenSSH Authentication Agent pipe)"
|
|
24
30
|
const AGENT_DEFAULT = "__scw_default_agent__";
|
|
@@ -289,6 +295,23 @@ function addKey(sock, keyFile, phCallback, ttl) {
|
|
|
289
295
|
fail("ADDKEY_TIMEOUT", `ssh-add did not finish within ${ADDKEY_TIMEOUT_SEC} sec`);
|
|
290
296
|
}, ADDKEY_TIMEOUT_SEC * 1000);
|
|
291
297
|
|
|
298
|
+
const answerPrompt = ()=>{
|
|
299
|
+
resolvePassphrase(phCallback)
|
|
300
|
+
.then((v)=>{
|
|
301
|
+
if (settled) {
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (v === null) {
|
|
305
|
+
fail("NO_PASSPHRASE", "key is encrypted but no passphrase is available");
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
pty.write(`${v}\n`);
|
|
309
|
+
})
|
|
310
|
+
.catch((e)=>{
|
|
311
|
+
finish(reject, e);
|
|
312
|
+
});
|
|
313
|
+
};
|
|
314
|
+
|
|
292
315
|
pty.onData((data)=>{
|
|
293
316
|
const output = data.toString();
|
|
294
317
|
debugVerbose(output);
|
|
@@ -297,25 +320,15 @@ function addKey(sock, keyFile, phCallback, ttl) {
|
|
|
297
320
|
fail("NO_AGENT", "no ssh-agent is reachable");
|
|
298
321
|
return;
|
|
299
322
|
}
|
|
300
|
-
if (
|
|
301
|
-
resolvePassphrase(phCallback)
|
|
302
|
-
.then((v)=>{
|
|
303
|
-
if (v === null) {
|
|
304
|
-
fail("NO_PASSPHRASE", "key is encrypted but no passphrase is available");
|
|
305
|
-
return;
|
|
306
|
-
}
|
|
307
|
-
pty.write(`${v}\n`);
|
|
308
|
-
})
|
|
309
|
-
.catch((e)=>{
|
|
310
|
-
finish(reject, e);
|
|
311
|
-
});
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
if (/Bad passphrase|incorrect passphrase/i.test(output)) {
|
|
323
|
+
if (reBadPassphrase.test(output)) {
|
|
315
324
|
badTries += 1;
|
|
316
325
|
if (badTries >= MAX_BAD_PASSPHRASE || typeof phCallback !== "function") {
|
|
317
326
|
fail("BAD_PASSPHRASE", "bad passphrase for private key");
|
|
327
|
+
return;
|
|
318
328
|
}
|
|
329
|
+
}
|
|
330
|
+
if (reAddKeyPrompt.test(output)) {
|
|
331
|
+
answerPrompt();
|
|
319
332
|
return;
|
|
320
333
|
}
|
|
321
334
|
if (/Identity added/.test(output)) {
|
package/lib/sshExec.js
CHANGED
|
@@ -196,6 +196,9 @@ async function verifyConnection(hostInfo, timeout) {
|
|
|
196
196
|
hostInfo._connectionVerified = true;
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
+
//master connections which are being made, per hostInfo object
|
|
200
|
+
const connectingMasters = new WeakMap();
|
|
201
|
+
|
|
199
202
|
/**
|
|
200
203
|
* create master ssh connection
|
|
201
204
|
* if the master connection is not used, only check that the host can be logged in to
|
|
@@ -224,6 +227,24 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
224
227
|
return verifyConnection(hostInfo, timeout);
|
|
225
228
|
}
|
|
226
229
|
|
|
230
|
+
//calls made at the same time for one hostInfo share the connection which is being made (with the timeout of the first one).
|
|
231
|
+
//Otherwise each of them would replace the master pty of the one before
|
|
232
|
+
let connecting = connectingMasters.get(hostInfo);
|
|
233
|
+
if (!connecting) {
|
|
234
|
+
connecting = connectMaster(hostInfo, timeout).finally(()=>{
|
|
235
|
+
connectingMasters.delete(hostInfo);
|
|
236
|
+
});
|
|
237
|
+
connectingMasters.set(hostInfo, connecting);
|
|
238
|
+
}
|
|
239
|
+
return connecting;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* create the master connection unless there is one
|
|
244
|
+
* @param {object} hostInfo - hostinfo object
|
|
245
|
+
* @param {number} timeout - timeout (in sec.)
|
|
246
|
+
*/
|
|
247
|
+
async function connectMaster(hostInfo, timeout) {
|
|
227
248
|
if (await existsMaster(hostInfo, timeout)) {
|
|
228
249
|
debugVerbose("master connection exists");
|
|
229
250
|
return;
|
|
@@ -300,9 +321,19 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
300
321
|
sshLoginCallback(output, pty, hostInfo.password, hostInfo.passphrase, debugSendMasterPty)
|
|
301
322
|
.catch(reject);
|
|
302
323
|
});
|
|
324
|
+
//the shell never exits by itself. It has been killed, e.g. by disconnect(), or it has crashed
|
|
303
325
|
pty.onExit((e)=>{
|
|
304
326
|
debug("master pty exit", e);
|
|
305
|
-
|
|
327
|
+
if (output.includes(okMark)) {
|
|
328
|
+
resolve();
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const err = failure();
|
|
332
|
+
if (!output.includes(failMark)) {
|
|
333
|
+
err.message = "the shell of the master connection has exited before the login succeeded";
|
|
334
|
+
err.code = "PTY_EXITED";
|
|
335
|
+
}
|
|
336
|
+
reject(err);
|
|
306
337
|
});
|
|
307
338
|
});
|
|
308
339
|
|