ssh-client-wrapper 3.4.5 → 3.4.7

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 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/index.js CHANGED
@@ -287,7 +287,7 @@ class SshClientWrapper {
287
287
  */
288
288
  async disconnect() {
289
289
  debug(`disconnect from ${this.hostInfo.host} called`);
290
- this.deps.disconnect(this.hostInfo);
290
+ return this.deps.disconnect(this.hostInfo);
291
291
  }
292
292
 
293
293
  /**
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 shell prints the mark only if ssh fails. The mark is written with "" in it,
60
- * so that the line which the terminal echoes back does not contain the mark itself.
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 the remote host echoes if the login succeeds
65
- * @param {string} failId - string which makes the mark unique. It must differ from words
66
- * @returns {object} - { line: command line, failMark: string which is printed if ssh fails }
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
@@ -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;
@@ -237,9 +258,7 @@ export async function connect(hostInfo, timeout = 60) {
237
258
  let over = false;
238
259
 
239
260
  debug(`create master ssh session to ${hostInfo.host}`);
240
- const words = crypto.randomUUID();
241
- const re = new RegExp(`^${words}`);
242
- const { line, failMark } = makeMasterCommand(sshCmd, getSshOption(hostInfo), words, crypto.randomUUID());
261
+ const { line, okMark, failMark } = makeMasterCommand(sshCmd, getSshOption(hostInfo), crypto.randomUUID(), crypto.randomUUID());
243
262
 
244
263
  let ac = null;
245
264
  let output = "";
@@ -264,7 +283,7 @@ export async function connect(hostInfo, timeout = 60) {
264
283
  output = `${output}${chunk}`.slice(-65536);
265
284
  debugVerbose(chunk);
266
285
 
267
- if (re.test(chunk)) {
286
+ if (output.includes(okMark)) {
268
287
  debug("test done. call exit on remotehost");
269
288
  done = true;
270
289
  resolve();
@@ -302,9 +321,19 @@ export async function connect(hostInfo, timeout = 60) {
302
321
  sshLoginCallback(output, pty, hostInfo.password, hostInfo.passphrase, debugSendMasterPty)
303
322
  .catch(reject);
304
323
  });
324
+ //the shell never exits by itself. It has been killed, e.g. by disconnect(), or it has crashed
305
325
  pty.onExit((e)=>{
306
326
  debug("master pty exit", e);
307
- resolve(e);
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);
308
337
  });
309
338
  });
310
339
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ssh-client-wrapper",
3
- "version": "3.4.5",
3
+ "version": "3.4.7",
4
4
  "description": "wrapper library for open ssh client and rsync",
5
5
  "keywords": [
6
6
  "ssh",