@electerm/ssh2 0.8.11 → 1.5.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.
@@ -1,63 +1,84 @@
1
- var HttpAgent = require('http').Agent;
2
- var HttpsAgent = require('https').Agent;
3
- var inherits = require('util').inherits;
1
+ 'use strict';
4
2
 
5
- var Client;
3
+ const { Agent: HttpAgent } = require('http');
4
+ const { Agent: HttpsAgent } = require('https');
5
+ const { connect: tlsConnect } = require('tls');
6
6
 
7
- [HttpAgent, HttpsAgent].forEach((ctor) => {
8
- function SSHAgent(connectCfg, agentOptions) {
9
- if (!(this instanceof SSHAgent))
10
- return new SSHAgent(connectCfg, agentOptions);
7
+ let Client;
11
8
 
12
- ctor.call(this, agentOptions);
9
+ for (const ctor of [HttpAgent, HttpsAgent]) {
10
+ class SSHAgent extends ctor {
11
+ constructor(connectCfg, agentOptions) {
12
+ super(agentOptions);
13
13
 
14
- this._connectCfg = connectCfg;
15
- this._defaultSrcIP = (agentOptions && agentOptions.srcIP) || 'localhost';
16
- }
17
- inherits(SSHAgent, ctor);
14
+ this._connectCfg = connectCfg;
15
+ this._defaultSrcIP = (agentOptions && agentOptions.srcIP) || 'localhost';
16
+ }
17
+
18
+ createConnection(options, cb) {
19
+ const srcIP = (options && options.localAddress) || this._defaultSrcIP;
20
+ const srcPort = (options && options.localPort) || 0;
21
+ const dstIP = options.host;
22
+ const dstPort = options.port;
18
23
 
19
- SSHAgent.prototype.createConnection = createConnection;
24
+ if (Client === undefined)
25
+ Client = require('./client.js');
26
+
27
+ const client = new Client();
28
+ let triedForward = false;
29
+ client.on('ready', () => {
30
+ client.forwardOut(srcIP, srcPort, dstIP, dstPort, (err, stream) => {
31
+ triedForward = true;
32
+ if (err) {
33
+ client.end();
34
+ return cb(err);
35
+ }
36
+ stream.once('close', () => client.end());
37
+ cb(null, decorateStream(stream, ctor, options));
38
+ });
39
+ }).on('error', cb).on('close', () => {
40
+ if (!triedForward)
41
+ cb(new Error('Unexpected connection close'));
42
+ }).connect(this._connectCfg);
43
+ }
44
+ }
20
45
 
21
46
  exports[ctor === HttpAgent ? 'SSHTTPAgent' : 'SSHTTPSAgent'] = SSHAgent;
22
- });
23
-
24
- function createConnection(options, cb) {
25
- var srcIP = (options && options.localAddress) || this._defaultSrcIP;
26
- var srcPort = (options && options.localPort) || 0;
27
- var dstIP = options.host;
28
- var dstPort = options.port;
29
-
30
- if (Client === undefined)
31
- Client = require('./client').Client;
32
-
33
- var client = new Client();
34
- var triedForward = false;
35
- client.on('ready', () => {
36
- client.forwardOut(srcIP, srcPort, dstIP, dstPort, (err, stream) => {
37
- triedForward = true;
38
- if (err) {
39
- client.end();
40
- return cb(err);
41
- }
42
- stream.once('close', () => {
43
- client.end();
44
- });
45
- cb(null, decorateStream(stream));
46
- });
47
- }).on('error', cb).on('close', () => {
48
- if (!triedForward)
49
- cb(new Error('Unexpected connection loss'));
50
- }).connect(this._connectCfg);
51
47
  }
52
48
 
53
49
  function noop() {}
54
50
 
55
- function decorateStream(stream) {
56
- stream.setKeepAlive = noop;
57
- stream.setNoDelay = noop;
58
- stream.setTimeout = noop;
59
- stream.ref = noop;
60
- stream.unref = noop;
61
- stream.destroySoon = stream.destroy;
62
- return stream;
51
+ function decorateStream(stream, ctor, options) {
52
+ if (ctor === HttpAgent) {
53
+ // HTTP
54
+ stream.setKeepAlive = noop;
55
+ stream.setNoDelay = noop;
56
+ stream.setTimeout = noop;
57
+ stream.ref = noop;
58
+ stream.unref = noop;
59
+ stream.destroySoon = stream.destroy;
60
+ return stream;
61
+ }
62
+
63
+ // HTTPS
64
+ options.socket = stream;
65
+ const wrapped = tlsConnect(options);
66
+
67
+ // This is a workaround for a regression in node v12.16.3+
68
+ // https://github.com/nodejs/node/issues/35904
69
+ const onClose = (() => {
70
+ let called = false;
71
+ return () => {
72
+ if (called)
73
+ return;
74
+ called = true;
75
+ if (stream.isPaused())
76
+ stream.resume();
77
+ };
78
+ })();
79
+ // 'end' listener is needed because 'close' is not emitted in some scenarios
80
+ // in node v12.x for some unknown reason
81
+ wrapped.on('end', onClose).on('close', onClose);
82
+
83
+ return wrapped;
63
84
  }
package/lib/index.js ADDED
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ const {
4
+ AgentProtocol,
5
+ BaseAgent,
6
+ createAgent,
7
+ CygwinAgent,
8
+ OpenSSHAgent,
9
+ PageantAgent,
10
+ } = require('./agent.js');
11
+ const {
12
+ SSHTTPAgent: HTTPAgent,
13
+ SSHTTPSAgent: HTTPSAgent,
14
+ } = require('./http-agents.js');
15
+ const { parseKey } = require('./protocol/keyParser.js');
16
+ const {
17
+ flagsToString,
18
+ OPEN_MODE,
19
+ STATUS_CODE,
20
+ stringToFlags,
21
+ } = require('./protocol/SFTP.js');
22
+
23
+ module.exports = {
24
+ AgentProtocol,
25
+ BaseAgent,
26
+ createAgent,
27
+ Client: require('./client.js'),
28
+ CygwinAgent,
29
+ HTTPAgent,
30
+ HTTPSAgent,
31
+ OpenSSHAgent,
32
+ PageantAgent,
33
+ Server: require('./server.js'),
34
+ utils: {
35
+ parseKey,
36
+ sftp: {
37
+ flagsToString,
38
+ OPEN_MODE,
39
+ STATUS_CODE,
40
+ stringToFlags,
41
+ },
42
+ },
43
+ };