ssh-client-wrapper 3.1.4 → 3.3.0
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 +1 -0
- package/lib/agent.js +1 -1
- package/lib/fork.js +3 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/sshExec.js +58 -1
- package/lib/util.js +20 -5
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -64,6 +64,7 @@ Creates a new SSH client instance.
|
|
|
64
64
|
* `retryDuration` (number, optional): Time (msec) to wait between each retry. Defaults to `1000`.
|
|
65
65
|
* `retryableExitCodes` (number[], optional): Additional rsync exit codes to treat as retryable for `.send()`/`.recv()`. See [Retry behavior for `.send()` / `.recv()`](#retry-behavior-for-send--recv) below.
|
|
66
66
|
* `replaceRetryableExitCodes` (boolean, optional): If `true`, `retryableExitCodes` replaces the built-in retryable exit code list instead of adding to it.
|
|
67
|
+
* `useControlMaster` (boolean, optional): Keep an ssh master connection (`ControlMaster`) and share it between the ssh/rsync calls. Defaults to `true`, but to `false` on Windows because its ssh clients can not do it. If `false`, every call logs in by itself (use the ssh-agent to avoid answering prompts every time), and `ControlPersist` and `ControlPersistDir` are ignored.
|
|
67
68
|
* `useAgent` (boolean, optional): Authenticate key-based hosts through an ssh-agent instead of replaying the passphrase on every connection. Defaults to `true` when `keyFile` is set. Set `false` to force the legacy behavior. See [SSH agent authentication](#ssh-agent-authentication) below.
|
|
68
69
|
* `identityAgent` (string, optional): Path to an existing ssh-agent socket to authenticate through (emitted as `-oIdentityAgent`). When set, the wrapper uses that agent as-is and manages nothing.
|
|
69
70
|
* `agentKeyTTL` (number, optional): Lifetime in seconds applied to keys the wrapper adds to an agent (`ssh-add -t`). Defaults to `3600`.
|
package/lib/agent.js
CHANGED
|
@@ -5,7 +5,7 @@ import { execFile } from "node:child_process";
|
|
|
5
5
|
import { promisify } from "node:util";
|
|
6
6
|
import { setTimeout as setTimeoutPromise } from "node:timers/promises";
|
|
7
7
|
import Debug from "debug";
|
|
8
|
-
import { spawn } from "node-pty
|
|
8
|
+
import { spawn } from "node-pty";
|
|
9
9
|
import { rePhPrompt } from "./util.js";
|
|
10
10
|
|
|
11
11
|
const debug = Debug("sshClientWrapper:debug:agent");
|
package/lib/fork.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Debug from "debug";
|
|
2
|
-
import { spawn } from "node-pty
|
|
2
|
+
import { spawn } from "node-pty";
|
|
3
3
|
|
|
4
4
|
import { rePwPrompt, rePhPrompt, reNewHostPrompt, reRemoteHostIdentificationHasChanged, buff2String, sendPty, watchDogTimer } from "./util.js";
|
|
5
5
|
|
|
@@ -85,7 +85,8 @@ export function createMasterPty(hostInfo) {
|
|
|
85
85
|
export async function fork(hostInfo, cmd, args, timeout, outputCallback, retryableExitCodes = [], expects = []) {
|
|
86
86
|
return new Promise((resolve, reject)=>{
|
|
87
87
|
debug(`cmd=${cmd}: args=${args}`);
|
|
88
|
-
|
|
88
|
+
//node-pty accepts only string arguments (e.g. the port number is a number)
|
|
89
|
+
const ptyProcess = spawn(cmd, args.map(String), nodePtyOpt);
|
|
89
90
|
let ac = null;
|
|
90
91
|
if (timeout > 0) {
|
|
91
92
|
ac = watchDogTimer(timeout, cmd, ()=>{
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -28,6 +28,7 @@ const debugVerbose = Debug("sshClientWrapper:verbose:interface");
|
|
|
28
28
|
* @param {string} hostInfo.keyFile - private key filename
|
|
29
29
|
* @param {boolean} hostInfo.noStrictHostKeyChecking - bypass host key checking if true
|
|
30
30
|
* @param {Integer} hostInfo.ControlPersist - how long keep master connection after last client connection has been closed (sec).
|
|
31
|
+
* @param {boolean} hostInfo.useControlMaster - keep a master connection (ssh ControlMaster) and share it between ssh/rsync calls. default: true, but false on win32 because its ssh clients do not support it. If false, every call logs in by itself and ControlPersist and ControlPersistDir are ignored.
|
|
31
32
|
* @param {Integer} hostInfo.ConnectTimeout - timeout used when connectiong to ths SSH server
|
|
32
33
|
* @param {Integer} hostInfo.maxRetry - max number of retry
|
|
33
34
|
* @param {Integer} hostInfo.retryDuration - duration time (msec) between each retry
|
package/lib/sshExec.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import crypto from "crypto";
|
|
2
2
|
import { setTimeout as setTimeoutPromise } from "timers/promises";
|
|
3
3
|
import Debug from "debug";
|
|
4
|
-
import { sendPty, sshCmd, getSshOption, sanityCheck, watchDogTimer, rePwPrompt, rePhPrompt } from "./util.js";
|
|
4
|
+
import { sendPty, sshCmd, getSshOption, isControlMasterEnabled, sanityCheck, watchDogTimer, rePwPrompt, rePhPrompt } from "./util.js";
|
|
5
5
|
import { fork, createMasterPty, sshLoginCallback } from "./fork.js";
|
|
6
6
|
import { shouldUseAgent, ensureAgentForHost } from "./agent.js";
|
|
7
7
|
|
|
@@ -142,8 +142,55 @@ async function existsMaster(hostInfo, argTimeout) {
|
|
|
142
142
|
return !/Control socket connect.*: (No such file or directory|Connection refused)/.test(output);
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
+
/**
|
|
146
|
+
* check once that the host can be logged in to, for the case that no master connection is used
|
|
147
|
+
* @param {object} hostInfo - hostinfo object
|
|
148
|
+
* @param {number} timeout - timeout (in sec.)
|
|
149
|
+
*/
|
|
150
|
+
async function verifyConnection(hostInfo, timeout) {
|
|
151
|
+
if (hostInfo._connectionVerified) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
const args = getSshOption(hostInfo);
|
|
155
|
+
args.push(`echo ${crypto.randomUUID()}`);
|
|
156
|
+
|
|
157
|
+
let output = "";
|
|
158
|
+
const cb = (e)=>{
|
|
159
|
+
output += e;
|
|
160
|
+
};
|
|
161
|
+
debug(`verify connection: ${sshCmd} ${args.join(" ")}`);
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
await fork(hostInfo, sshCmd, args, timeout, cb);
|
|
165
|
+
} catch (e) {
|
|
166
|
+
const err = new Error(e.message);
|
|
167
|
+
err.host = hostInfo.host;
|
|
168
|
+
err.user = hostInfo.user;
|
|
169
|
+
err.port = hostInfo.port;
|
|
170
|
+
err.output = output;
|
|
171
|
+
if (e.code === "TIMEEXPIRE") {
|
|
172
|
+
err.message = `can not connect within ${timeout} sec`;
|
|
173
|
+
} else if (/Permission denied \(/.test(output)) {
|
|
174
|
+
err.message = "Permission denied";
|
|
175
|
+
err.code = "AUTH_FAILED";
|
|
176
|
+
} else if (/Could not resolve hostname/.test(output)) {
|
|
177
|
+
err.message = "Could not resolve hostname";
|
|
178
|
+
} else if (/Bad port/.test(output)) {
|
|
179
|
+
err.message = "Bad port";
|
|
180
|
+
} else if (/Operation|Connection timed out/.test(output)) {
|
|
181
|
+
err.message = "Operation timed out";
|
|
182
|
+
} else {
|
|
183
|
+
throw e;
|
|
184
|
+
}
|
|
185
|
+
throw err;
|
|
186
|
+
}
|
|
187
|
+
debug(`connected to ${hostInfo.host}`);
|
|
188
|
+
hostInfo._connectionVerified = true;
|
|
189
|
+
}
|
|
190
|
+
|
|
145
191
|
/**
|
|
146
192
|
* create master ssh connection
|
|
193
|
+
* if the master connection is not used, only check that the host can be logged in to
|
|
147
194
|
* @param {object} hostInfo - hostinfo object
|
|
148
195
|
*/
|
|
149
196
|
export async function connect(hostInfo, timeout = 60) {
|
|
@@ -165,6 +212,10 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
165
212
|
delete hostInfo._agentEnsuredAt;
|
|
166
213
|
}
|
|
167
214
|
|
|
215
|
+
if (!isControlMasterEnabled(hostInfo)) {
|
|
216
|
+
return verifyConnection(hostInfo, timeout);
|
|
217
|
+
}
|
|
218
|
+
|
|
168
219
|
if (await existsMaster(hostInfo, timeout)) {
|
|
169
220
|
debugVerbose("master connection exists");
|
|
170
221
|
return;
|
|
@@ -279,6 +330,12 @@ export async function connect(hostInfo, timeout = 60) {
|
|
|
279
330
|
export async function disconnect(hostInfo) {
|
|
280
331
|
debug("disconnect called");
|
|
281
332
|
|
|
333
|
+
if (!isControlMasterEnabled(hostInfo)) {
|
|
334
|
+
debug(`disconnect: master connection is not used for ${hostInfo.host}`);
|
|
335
|
+
delete hostInfo._connectionVerified;
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
282
339
|
try {
|
|
283
340
|
if (await existsMaster(hostInfo)) {
|
|
284
341
|
const args = getSshOption(hostInfo);
|
package/lib/util.js
CHANGED
|
@@ -40,6 +40,7 @@ const hostInfoSchema = {
|
|
|
40
40
|
ControlPersist: { type: "number", minimum: 0, default: 180 },
|
|
41
41
|
ConnectTimeout: { type: "number", minimum: 0 },
|
|
42
42
|
ControlPersistDir: { type: "string", pattern: "\\S+", transform: ["trim"] },
|
|
43
|
+
useControlMaster: { type: "boolean" },
|
|
43
44
|
maxRetry: { type: "number", minimum: 0, default: 3 },
|
|
44
45
|
retryDuration: { type: "number", minimum: 0, default: 1000 },
|
|
45
46
|
retryableExitCodes: {
|
|
@@ -181,6 +182,18 @@ function getControlPersistDir(hostInfo) {
|
|
|
181
182
|
return "~/.ssh";
|
|
182
183
|
}
|
|
183
184
|
|
|
185
|
+
/**
|
|
186
|
+
* check if the ssh master connection (ControlMaster) should be used
|
|
187
|
+
* @param {object} hostInfo - host information object defined in index.js
|
|
188
|
+
* @returns {boolean} - hostInfo.useControlMaster if it is set, otherwise false on win32 (its ssh clients can not do it) and true elsewhere
|
|
189
|
+
*/
|
|
190
|
+
export const isControlMasterEnabled = (hostInfo)=>{
|
|
191
|
+
if (typeof hostInfo.useControlMaster === "boolean") {
|
|
192
|
+
return hostInfo.useControlMaster;
|
|
193
|
+
}
|
|
194
|
+
return process.platform !== "win32";
|
|
195
|
+
};
|
|
196
|
+
|
|
184
197
|
/**
|
|
185
198
|
* return ssh option with some hard corded options
|
|
186
199
|
* @param {object} hostInfo - host information object defined in index.js
|
|
@@ -242,11 +255,13 @@ export const getSshOption = (hostInfo, withoutDestination)=>{
|
|
|
242
255
|
args.push(`-oIdentityAgent=${agentSock}`);
|
|
243
256
|
}
|
|
244
257
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
258
|
+
if (isControlMasterEnabled(hostInfo)) {
|
|
259
|
+
const controlPersist = Number.isInteger(hostInfo.ControlPersist) && hostInfo.ControlPersist >= 0 ? hostInfo.ControlPersist : "180";
|
|
260
|
+
const controlPersistDir = getControlPersistDir(hostInfo);
|
|
261
|
+
args.push("-oControlMaster=auto");
|
|
262
|
+
args.push(`-oControlPath=${controlPersistDir}/scw-%C`);
|
|
263
|
+
args.push(`-oControlPersist=${controlPersist}`);
|
|
264
|
+
}
|
|
250
265
|
|
|
251
266
|
if (Number.isInteger(hostInfo.ConnectTimeout) && hostInfo.ConnectTimeout > 0) {
|
|
252
267
|
args.push(`-oConnectTimeout=${hostInfo.ConnectTimeout}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ssh-client-wrapper",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
4
4
|
"description": "wrapper library for open ssh client and rsync",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ssh",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"ajv": "^8.17.1",
|
|
44
44
|
"ajv-keywords": "^5.1.0",
|
|
45
45
|
"debug": "^4.4.3",
|
|
46
|
-
"glob": "^
|
|
47
|
-
"node-pty
|
|
46
|
+
"glob": "^14.0.2",
|
|
47
|
+
"node-pty": "^1.1.0"
|
|
48
48
|
},
|
|
49
49
|
"devDependencies": {
|
|
50
50
|
"@eslint/js": "^9.33.0",
|