-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathssh.js
executable file
·177 lines (140 loc) · 3.8 KB
/
ssh.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
const { Client } = require('ssh2');
const { Mutex } = require('async-mutex');
module.exports = function (RED) {
async function _connectClient(node, callback, failed = undefined){
const release = await node.connectMutex.acquire();
if(node.isConnected) {
release();
callback(node.client);
return;
};
node.client = new Client();
node.client.on('ready', () => {
node.isConnected = true;
node.log("Ssh client ready");
node.status({ fill: "green", shape: "dot", text: 'Connected' });
release();
callback(node.client);
});
node.client.on('close', () => {
node.isConnected = false;
node.status({ fill: "red", shape: "dot", text: 'Disconnected' });
});
node.client.on('end', () => {
node.isConnected = false;
node.status({ fill: "red", shape: "dot", text: "Disconnected" });
});
node.client.on('error', (err) => {
node.status({fill: "red", shape: "dot", text: err});
node.log(err);
release();
node.isConnected = false;
if(failed) {
failed(err);
};
});
node.client.on('continue', () => {
if(node.continue) {
node.continue();
};
});
node.client.connect(node.conf.options);
};
function SshConf(n) {
RED.nodes.createNode(this,n);
const node = this;
node.options = {
host: node.credentials.hostname ? node.credentials.hostname : undefined,
port: node.credentials.port ? node.credentials.port : undefined,
username: node.credentials.username ? node.credentials.username : undefined,
password: node.credentials.password ? node.credentials.password : undefined,
privateKey: n.ssh ? require('fs').readFileSync(n.ssh) : undefined
// TODO privatekey with passphrase will fail
// passphrase: "passphraseofprivatekey"
};
node.connectMutex = new Mutex();
node.sendMutex = new Mutex();
};
RED.nodes.registerType("ssh-conf",SshConf,{
credentials: {
username: { type: "text" },
password: { type: "password" },
hostname: { value: "" },
port: { value: "" },
}
});
function SshV3(config) {
RED.nodes.createNode(this,config);
this.conf = RED.nodes.getNode(config.conf);
this.name = config.name;
let node = this;
node.connectMutex = new Mutex();
node.sendMutex = new Mutex();
node.status({ fill: "yellow", shape: "dot", text: "waiting for input" });
node.on('input', async (msg, send, done) => {
if (!msg.payload) {
node.warn("msg.payload cant be empty");
return;
};
if (typeof msg.payload != "string"){
node.warn("msg.payload must be string");
return;
};
const release = await node.sendMutex.acquire();
const session = {
code: 0,
stdout: [],
stderr: []
};
const notify = (type, data) => {
switch (type) {
case 0:
session.code = data;
msg.session = session;
send(msg);
done();
break;
case 1:
session.stdout.push(data.toString());
break;
case 2:
session.stderr.push(data.toString());
break;
}
};
await _connectClient(node, (conn) => {
node.continue = () => {
release();
};
const wait = conn.exec(msg.payload, (err, stream) => {
if (err) {
node.log("Ssh client error in input.");
throw err;
}
stream.on('close', function (code, signal) {
notify(0, code);
}).on('data', (data) => {
notify(1, data);
}).stderr.on('data', (data) => {
notify(2, data);
});
});
if(wait) {
node.continue = null;
release();
}
}, (err) => {
release();
node.error(err, msg);
});
_connectClient(node, (conn) => { node.debug("SSH-CLI initial connection succeeded."); });
});
node.on('close', function () {
node.client && node.client.end();
node.client && node.client.destroy();
});
node.debug("SSH-CLI setup done.");
}
RED.nodes.registerType("ssh-v3",SshV3);
};