ssh-client-wrapper 2.1.1 → 2.2.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/lib/fork.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  const debug = require("debug")("sshClientWrapper:debug:fork");
3
3
  const debugVerbose = require("debug")("sshClientWrapper:verbose:fork");
4
- const debugMasterVerbose = require("debug")("sshClientWrapper:insecure:fork_masterPty");
5
4
  const debugSendPty = require("debug")("sshClientWrapper:insecure:fork_sendpty");
6
5
  const { spawn } = require("node-pty");
7
6
 
@@ -13,45 +12,53 @@ const nodePtyOpt = {
13
12
  };
14
13
 
15
14
  function sshLoginCallback(output, ptyProcess, pw, ph, logger = debugSendPty) {
16
- if (reNewHostPrompt.test(output)) {
17
- sendPty(ptyProcess, "yes\n", logger, 17);
18
- return;
19
- }
20
- if (rePwPrompt.test(output)) {
21
- if (typeof pw === "string") {
22
- debug("use given password");
23
- sendPty(ptyProcess, `${pw}\n`, logger, 23);
24
- return;
25
- }
26
- if (typeof pw === "function") {
27
- debug("call password callback function");
28
- pw()
29
- .then((v)=>{
30
- sendPty(ptyProcess, `${v}\n`, logger, 30);
31
- });
32
- return;
15
+ return new Promise((resolve, reject)=>{
16
+ if (reNewHostPrompt.test(output)) {
17
+ sendPty(ptyProcess, "yes\n", logger, 17);
18
+ resolve();
33
19
  }
34
- }
35
- if (rePhPrompt.test(output)) {
36
- if (typeof ph === "string") {
37
- debug("use given passphrase");
38
- sendPty(ptyProcess, `${ph}\n`, logger, 38);
39
- return;
20
+ if (rePwPrompt.test(output)) {
21
+ if (typeof pw === "string") {
22
+ debug("use given password");
23
+ sendPty(ptyProcess, `${pw}\n`, logger, 23);
24
+ resolve();
25
+ }
26
+ if (typeof pw === "function") {
27
+ debug("call password callback function");
28
+ pw()
29
+ .then((v)=>{
30
+ sendPty(ptyProcess, `${v}\n`, logger, 30);
31
+ })
32
+ .catch((e)=>{
33
+ reject(e);
34
+ });
35
+ resolve();
36
+ }
40
37
  }
41
- if (typeof ph === "function") {
42
- debug("call passphrase callback function");
43
- ph()
44
- .then((v)=>{
45
- sendPty(ptyProcess, `${v}\n`, logger, 45);
46
- });
38
+ if (rePhPrompt.test(output)) {
39
+ if (typeof ph === "string") {
40
+ debug("use given passphrase");
41
+ sendPty(ptyProcess, `${ph}\n`, logger, 38);
42
+ resolve();
43
+ }
44
+ if (typeof ph === "function") {
45
+ debug("call passphrase callback function");
46
+ ph()
47
+ .then((v)=>{
48
+ sendPty(ptyProcess, `${v}\n`, logger, 45);
49
+ })
50
+ .catch((e)=>{
51
+ reject(e);
52
+ });
53
+ }
47
54
  }
48
- }
55
+ });
49
56
  }
50
57
 
51
58
  /**
52
59
  * create pty for master ssh session and store to hostInfo.masterPty
53
60
  */
54
- function getMasterPty(hostInfo, logger) {
61
+ function createMastePty(hostInfo) {
55
62
  debug("fork pty for master connection");
56
63
 
57
64
  if (hostInfo.masterPty) {
@@ -59,11 +66,6 @@ function getMasterPty(hostInfo, logger) {
59
66
  hostInfo.masterPty.kill();
60
67
  }
61
68
  const ptyProcess = spawn(nodePtyOpt.shell, [], nodePtyOpt);
62
- ptyProcess.onData(buff2String.bind(null, debugMasterVerbose));
63
- ptyProcess.onData((data)=>{
64
- const output = data.toString();
65
- sshLoginCallback(output, ptyProcess, hostInfo.password, hostInfo.passphrase, logger);
66
- });
67
69
  hostInfo.masterPty = ptyProcess;
68
70
  }
69
71
 
@@ -83,9 +85,10 @@ async function fork(hostInfo, cmd, args, timeout, outputCallback) {
83
85
  ptyProcess.onData(buff2String.bind(null, debugVerbose));
84
86
 
85
87
  if (hostInfo !== null) {
86
- ptyProcess.onData((data)=>{
88
+ ptyProcess.onData(async (data)=>{
87
89
  const output = data.toString();
88
- sshLoginCallback(output, ptyProcess, hostInfo.password, hostInfo.passphrase);
90
+ sshLoginCallback(output, ptyProcess, hostInfo.password, hostInfo.passphrase)
91
+ .catch(reject);
89
92
 
90
93
  if (/kex_exchange_identification: Connection closed by remote host/.test(output)) {
91
94
  ptyProcess.kill();
@@ -122,5 +125,6 @@ async function fork(hostInfo, cmd, args, timeout, outputCallback) {
122
125
  }
123
126
  module.exports = {
124
127
  fork,
125
- getMasterPty
128
+ createMastePty,
129
+ sshLoginCallback
126
130
  };
package/lib/sshExec.js CHANGED
@@ -5,7 +5,7 @@ const debug = require("debug")("sshClientWrapper:debug:sshExec");
5
5
  const debugVerbose = require("debug")("sshClientWrapper:verbose:sshExec");
6
6
  const debugSendMasterPty = require("debug")("sshClientWrapper:insecure:sshExec_masterPty");
7
7
  const { sendPty, sshCmd, getSshOption, sanityCheck, watchDogTimer } = require("./util.js");
8
- const { fork, getMasterPty } = require("./fork.js");
8
+ const { fork, createMastePty, sshLoginCallback } = require("./fork.js");
9
9
 
10
10
  /**
11
11
  * Execute command on remote host.
@@ -103,7 +103,7 @@ async function connect(hostInfo, timeout = 60) {
103
103
  }
104
104
 
105
105
  debug(`create master pty for ${hostInfo.host}`);
106
- getMasterPty(hostInfo, debugSendMasterPty);
106
+ createMastePty(hostInfo, debugSendMasterPty);
107
107
 
108
108
  debug(`create master ssh session to ${hostInfo.host}`);
109
109
 
@@ -146,6 +146,11 @@ async function connect(hostInfo, timeout = 60) {
146
146
  reject(new Error("Operation timed out"));
147
147
  }
148
148
  });
149
+ hostInfo.masterPty.onData((data)=>{
150
+ const output = data.toString();
151
+ sshLoginCallback(output, hostInfo.masterPty, hostInfo.password, hostInfo.passphrase, debugSendMasterPty)
152
+ .catch(reject);
153
+ });
149
154
  hostInfo.masterPty.onExit((e)=>{
150
155
  debug("master pty exit", e);
151
156
  resolve(e);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ssh-client-wrapper",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "description": "wrapper library for open ssh client and rsync",
5
5
  "keywords": [
6
6
  "ssh",
@@ -35,7 +35,7 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "debug": "^4.3.4",
38
- "glob": "^10.3.4",
38
+ "glob": "^10.3.10",
39
39
  "node-pty": "^0.10.1",
40
40
  "nyc": "^15.1.0",
41
41
  "promise-retry": "^2.0.1",