@yousolution/node-red-contrib-you-ftp-sftp 1.10.0 → 1.10.1

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.
Files changed (2) hide show
  1. package/nodes/sftp.js +232 -149
  2. package/package.json +1 -1
package/nodes/sftp.js CHANGED
@@ -28,21 +28,21 @@ TODO
28
28
 
29
29
  // STILL VALIDATING CONNECTIVITY
30
30
  // -- DID validate it works with list/dir
31
- const fs = require('fs');
31
+ // const fs = require('fs');
32
32
 
33
33
  module.exports = function (RED) {
34
34
  'use strict';
35
35
 
36
- const sftp = require('ssh2').Client;
36
+ // const sftp = require('ssh2').Client;
37
37
  const fs = require('fs');
38
38
 
39
39
  function SFtpNode(n) {
40
40
  RED.nodes.createNode(this, n);
41
41
  // var node = this;
42
42
 
43
- console.log('SFTP - Config - username: ' + n.username);
44
- console.log('SFTP - Config - hmac: ' + n.hmac);
45
- console.log('SFTP - Config - cipher: ' + n.cipher);
43
+ // console.log('SFTP - Config - username: ' + n.username);
44
+ // console.log('SFTP - Config - hmac: ' + n.hmac);
45
+ // console.log('SFTP - Config - cipher: ' + n.cipher);
46
46
 
47
47
  // console.log('SFTP - Config - ssh-key: ' + n.sshkey);
48
48
 
@@ -52,9 +52,6 @@ module.exports = function (RED) {
52
52
  if (n.sshkey) {
53
53
  keyFile = 'keyFile';
54
54
  keyData = n.sshkey;
55
- console.log('************************');
56
- console.log(keyData);
57
- console.log('************************');
58
55
  }
59
56
  if (process.env.SFTP_SSH_KEY_FILE) {
60
57
  keyFile = process.env.SFTP_SSH_KEY_FILE;
@@ -71,7 +68,11 @@ module.exports = function (RED) {
71
68
  }
72
69
 
73
70
  if (keyFile && keyData) {
74
- console.log('SFTP - Using privateKey: ' + keyFile + ' Length: ' + keyData.toString().length);
71
+ console.log('SFTP - Using key file');
72
+ // console.log('*******************');
73
+ // console.log('SFTP - Username: ' + n.username);
74
+ // console.log('SFTP - Using privateKey: ' + keyFile + ' Length: ' + keyData.toString().length);
75
+ // console.log('*******************');
75
76
  this.options = {
76
77
  host: n.host || 'localhost',
77
78
  port: n.port || 21,
@@ -79,24 +80,139 @@ module.exports = function (RED) {
79
80
  password: n.password,
80
81
  privateKey: keyData,
81
82
  algorithms: {
82
- // hmac: ['hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1', 'hmac-sha1-96'],
83
- // cipher: ['aes256-cbc']
84
- hmac: n.hmac,
85
- cipher: n.cipher,
83
+ kex: [
84
+ 'curve25519-sha256',
85
+ 'curve25519-sha256@libssh.org',
86
+ 'ecdh-sha2-nistp256',
87
+ 'ecdh-sha2-nistp384',
88
+ 'ecdh-sha2-nistp521',
89
+ 'diffie-hellman-group-exchange-sha256',
90
+ 'diffie-hellman-group14-sha256',
91
+ 'diffie-hellman-group15-sha512',
92
+ 'diffie-hellman-group16-sha512',
93
+ 'diffie-hellman-group17-sha512',
94
+ 'diffie-hellman-group18-sha512',
95
+ 'diffie-hellman-group-exchange-sha1',
96
+ 'diffie-hellman-group14-sha1',
97
+ 'diffie-hellman-group1-sha1',
98
+ ],
99
+ cipher: [
100
+ 'chacha20-poly1305@openssh.com',
101
+ 'aes128-gcm',
102
+ 'aes128-gcm@openssh.com',
103
+ 'aes256-gcm',
104
+ 'aes256-gcm@openssh.com',
105
+ 'aes128-ctr',
106
+ 'aes192-ctr',
107
+ 'aes256-ctr',
108
+ '3des-cbc',
109
+ 'aes256-cbc',
110
+ 'aes192-cbc',
111
+ 'aes128-cbc',
112
+ // 'arcfour256',
113
+ // 'arcfour128',
114
+ // 'arcfour',
115
+ // 'blowfish-cbc',
116
+ // 'ast128-cbc',
117
+ ],
118
+ serverHostKey: [
119
+ 'ssh-ed25519',
120
+ 'ecdsa-sha2-nistp256',
121
+ 'ecdsa-sha2-nistp384',
122
+ 'ecdsa-sha2-nistp521',
123
+ 'rsa-sha2-512',
124
+ 'rsa-sha2-256',
125
+ 'ssh-rsa',
126
+ 'ssh-dss',
127
+ ],
128
+ hmac: [
129
+ 'hmac-sha2-256-etm@openssh.com',
130
+ 'hmac-sha2-512-etm@openssh.com',
131
+ 'hmac-sha1-etm@openssh.com',
132
+ 'hmac-sha2-256',
133
+ 'hmac-sha2-512',
134
+ 'hmac-sha1',
135
+ 'hmac-md5',
136
+ 'hmac-sha2-256-96',
137
+ 'hmac-sha2-512-96',
138
+ // 'hmac-ripemd160',
139
+ // 'hmac-sha1-96',
140
+ // 'hmac-md5-96',
141
+ ],
86
142
  },
87
143
  };
88
144
  } else {
145
+ // console.log('*******************');
89
146
  console.log('SFTP - Using User/Pwd');
147
+ // console.log('SFTP - Username: ' + n.username);
148
+ // console.log('SFTP - Password: ' + n.password);
149
+ // console.log('SFTP - Using privateKey: ' + keyFile + ' Length: ' + keyData?.toString().length);
150
+ // console.log('*******************');
90
151
  this.options = {
91
152
  host: n.host || 'localhost',
92
153
  port: n.port || 21,
93
154
  username: n.username,
94
155
  password: n.password,
95
156
  algorithms: {
96
- // hmac: ['hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1', 'hmac-sha1-96'],
97
- // cipher: ['aes256-cbc']
98
- hmac: n.hmac,
99
- cipher: n.cipher,
157
+ kex: [
158
+ 'curve25519-sha256',
159
+ 'curve25519-sha256@libssh.org',
160
+ 'ecdh-sha2-nistp256',
161
+ 'ecdh-sha2-nistp384',
162
+ 'ecdh-sha2-nistp521',
163
+ 'diffie-hellman-group-exchange-sha256',
164
+ 'diffie-hellman-group14-sha256',
165
+ 'diffie-hellman-group15-sha512',
166
+ 'diffie-hellman-group16-sha512',
167
+ 'diffie-hellman-group17-sha512',
168
+ 'diffie-hellman-group18-sha512',
169
+ 'diffie-hellman-group-exchange-sha1',
170
+ 'diffie-hellman-group14-sha1',
171
+ 'diffie-hellman-group1-sha1',
172
+ ],
173
+ cipher: [
174
+ 'chacha20-poly1305@openssh.com',
175
+ 'aes128-gcm',
176
+ 'aes128-gcm@openssh.com',
177
+ 'aes256-gcm',
178
+ 'aes256-gcm@openssh.com',
179
+ 'aes128-ctr',
180
+ 'aes192-ctr',
181
+ 'aes256-ctr',
182
+ '3des-cbc',
183
+ 'aes256-cbc',
184
+ 'aes192-cbc',
185
+ 'aes128-cbc',
186
+ // 'arcfour256',
187
+ // 'arcfour128',
188
+ // 'arcfour',
189
+ // 'blowfish-cbc',
190
+ // 'ast128-cbc',
191
+ ],
192
+ serverHostKey: [
193
+ 'ssh-ed25519',
194
+ 'ecdsa-sha2-nistp256',
195
+ 'ecdsa-sha2-nistp384',
196
+ 'ecdsa-sha2-nistp521',
197
+ 'rsa-sha2-512',
198
+ 'rsa-sha2-256',
199
+ 'ssh-rsa',
200
+ 'ssh-dss',
201
+ ],
202
+ hmac: [
203
+ 'hmac-sha2-256-etm@openssh.com',
204
+ 'hmac-sha2-512-etm@openssh.com',
205
+ 'hmac-sha1-etm@openssh.com',
206
+ 'hmac-sha2-256',
207
+ 'hmac-sha2-512',
208
+ 'hmac-sha1',
209
+ 'hmac-md5',
210
+ 'hmac-sha2-256-96',
211
+ 'hmac-sha2-512-96',
212
+ // 'hmac-ripemd160',
213
+ // 'hmac-sha1-96',
214
+ // 'hmac-md5-96',
215
+ ],
100
216
  },
101
217
  };
102
218
  }
@@ -115,7 +231,7 @@ module.exports = function (RED) {
115
231
 
116
232
  if (this.sftpConfig) {
117
233
  const node = this;
118
- node.on('input', function (msg, send, done) {
234
+ node.on('input', async function (msg, send, done) {
119
235
  try {
120
236
  node.workdir = node.workdir || msg.workdir || './';
121
237
  node.fileExtension = node.fileExtension || msg.fileExtension || '';
@@ -126,144 +242,111 @@ module.exports = function (RED) {
126
242
  node.sftpConfig.options.username = msg.user || node.sftpConfig.options.username || '';
127
243
  node.sftpConfig.options.password = msg.password || node.sftpConfig.options.password || '';
128
244
 
129
- console.log('========');
130
- console.log(node.sftpConfig.options.privateKey);
131
- console.log('========');
245
+ // console.log('========');
246
+ // console.log(node.sftpConfig.options.privateKey);
247
+ // console.log('========');
132
248
 
133
- let conn = new sftp();
249
+ // let conn = new sftp();
134
250
 
135
- this.sendMsg = function (err, result) {
136
- if (err) {
137
- // node.error(err.toString(), msg);
138
- node.status({ fill: 'red', shape: 'ring', text: 'failed' });
139
- done(err);
140
- return;
141
- }
142
- node.status({});
143
- conn.end();
144
- msg.payload = result;
145
- node.send(msg);
146
- };
147
-
148
- conn.on('ready', function () {
149
- switch (node.operation) {
150
- case 'list':
151
- conn.sftp(function (err, sftp) {
152
- if (err) {
153
- // node.error(err, msg);
154
- done(err);
155
- return;
156
- }
157
- sftp.readdir(node.workdir, node.sendMsg);
158
- });
159
- break;
160
- case 'get':
161
- conn.sftp(function (err, sftp) {
162
- if (err) {
163
- // node.error(err, msg);
164
- done(err);
165
- return;
166
- }
167
- let ftpfilename = node.workdir + node.filename;
168
-
169
- if (msg.payload.filename) ftpfilename = msg.payload.filename;
170
-
171
- let bufferarray = [];
251
+ // conn.on('ready', async function () {
252
+ const Client = require('ssh2-sftp-client');
253
+ const client = new Client();
254
+ try {
255
+ node.status({ fill: 'gray', shape: 'ring', text: 'connection...' });
256
+ await client.connect(node.sftpConfig.options);
257
+ node.status({ fill: 'green', shape: 'ring', text: 'connected' });
258
+ } catch (err) {
259
+ node.status({ fill: 'red', shape: 'ring', text: 'connection failed.' });
260
+ done(err);
261
+ console.error(err.message);
262
+ return;
263
+ }
172
264
 
173
- // Be very careful bufferSize too large causes issues with multi threading
174
- let stream = sftp.createReadStream(ftpfilename, { highWaterMark: 1024, bufferSize: 1024 });
265
+ switch (node.operation) {
266
+ case 'list':
267
+ try {
268
+ const result = await client.list(node.workdir);
269
+ node.status({ fill: 'green', shape: 'dot', text: 'list done' });
270
+ await client.end();
271
+ msg.payload = {};
272
+ msg.payload = result;
273
+ node.send(msg);
274
+ } catch (err) {
275
+ node.status({ fill: 'red', shape: 'ring', text: 'list failed' });
276
+ done(err);
277
+ console.error(err.message);
278
+ }
279
+ break;
175
280
 
176
- let counter = 0;
177
- let buf = '';
178
- let byteSize = 65536;
281
+ case 'get':
282
+ try {
283
+ let ftpfilename = node.workdir + node.filename;
284
+ if (msg.payload.filename) ftpfilename = msg.payload.filename;
179
285
 
180
- stream
181
- .on('data', function (d) {
182
- buf += d;
183
- counter++;
184
- // console.log("Read Chunk ("+ counter + "): " + d.length + " Length: " + buf.length);
185
- })
186
- .on('end', function () {
187
- node.status({});
188
- conn.end();
189
- console.log('SFTP Read Chunks ' + counter + ' Length: ' + buf.length);
190
- msg.payload = {};
191
- msg.payload.filedata = buf;
192
- msg.payload.filename = ftpfilename;
193
- node.send(msg);
194
- });
195
- });
196
- break;
197
- case 'put':
198
- conn.sftp(function (err, sftp) {
199
- if (err) {
200
- // node.error(err, msg);
201
- done(err);
202
- return;
203
- }
204
- let newFile = '';
205
- if (msg.payload.filename) {
206
- newFile = msg.payload.filename;
207
- } else if (node.filename == '') {
208
- let d = new Date();
209
- let guid = d.getTime().toString();
210
- if (node.fileExtension == '') node.fileExtension = '.txt';
211
- newFile = node.workdir + guid + node.fileExtension;
212
- } else {
213
- newFile = node.workdir + node.filename;
214
- }
286
+ const result = await client.get(ftpfilename);
287
+ await client.end();
288
+ node.status({ fill: 'green', shape: 'dot', text: 'get done' });
289
+ // conn.end();
290
+ // console.log('SFTP Read Chunks ' + counter + ' Length: ' + buf.length);
291
+ msg.payload = {};
292
+ msg.payload.filedata = result;
293
+ msg.payload.filename = ftpfilename;
294
+ node.send(msg);
295
+ } catch (err) {
296
+ node.status({ fill: 'red', shape: 'ring', text: 'get failed' });
297
+ done(err);
298
+ console.error(err.message);
299
+ }
300
+ break;
301
+ case 'put':
302
+ let newFile = '';
303
+ if (msg.payload.filename) {
304
+ newFile = msg.payload.filename;
305
+ } else if (node.filename == '') {
306
+ let d = new Date();
307
+ let guid = d.getTime().toString();
308
+ if (node.fileExtension == '') node.fileExtension = '.txt';
309
+ newFile = node.workdir + guid + node.fileExtension;
310
+ } else {
311
+ newFile = node.workdir + node.filename;
312
+ }
215
313
 
216
- let msgData = '';
217
- if (msg.payload.filedata) msgData = msg.payload.filedata;
218
- else msgData = JSON.stringify(msg.payload);
314
+ let msgData = '';
315
+ msgData = msg.payload.filedata ? msg.payload.filedata : JSON.stringify(msg.payload);
316
+ // console.log(newFile);
317
+ node.status({});
318
+ try {
319
+ await client.put(msgData, newFile);
320
+ node.status({ fill: 'green', shape: 'dot', text: 'put done' });
321
+ await client.end();
322
+ msg.payload = {};
323
+ msg.payload.filename = newFile;
324
+ node.send(msg);
325
+ } catch (err) {
326
+ node.status({ fill: 'red', shape: 'ring', text: 'put failed' });
327
+ done(err);
328
+ console.error(err.message);
329
+ }
330
+ break;
219
331
 
220
- console.log('SFTP Put:' + newFile);
221
- let writeStream = sftp.createWriteStream(newFile, { flags: 'w' });
222
- // var payloadBuff = new Buffer(msgData);
223
- // writeStream.write(payloadBuff, node.sendMsg);
224
- writeStream.write(msgData, function (err, result) {
225
- node.status({});
226
- conn.end();
227
- msg.payload = {};
228
- msg.payload.filename = newFile;
229
- node.send(msg);
230
- });
231
- });
232
- break;
233
- case 'delete':
234
- conn.sftp(function (err, sftp) {
235
- if (err) {
236
- // node.error(err, msg);
237
- done(err);
238
- return;
239
- }
240
- let ftpfilename = node.workdir + node.filename;
241
- if (msg.payload.filename) ftpfilename = msg.payload.filename;
242
- console.log('SFTP Deleting File: ' + ftpfilename);
243
- sftp.unlink(ftpfilename, function (err) {
244
- if (err) {
245
- // node.error(err, msg);
246
- done(err);
247
- return;
248
- } else {
249
- console.log('SFTP file unlinked');
250
- node.status({});
251
- msg.payload = {};
252
- msg.payload.filename = ftpfilename;
253
- node.send(msg);
254
- }
255
- conn.end();
256
- });
257
- });
258
- break;
259
- }
260
- });
261
- conn.on('error', function (error) {
262
- // node.error(error, msg);
263
- done(error);
264
- return;
265
- });
266
- conn.connect(node.sftpConfig.options);
332
+ case 'delete':
333
+ try {
334
+ let ftpfilename = node.workdir + node.filename;
335
+ if (msg.payload.filename) ftpfilename = msg.payload.filename;
336
+ console.log('SFTP Deleting File: ' + ftpfilename);
337
+ await client.delete(ftpfilename);
338
+ node.status({ fill: 'green', shape: 'dot', text: 'delete done' });
339
+ await client.end();
340
+ msg.payload = {};
341
+ msg.payload.filename = ftpfilename;
342
+ node.send(msg);
343
+ } catch (err) {
344
+ node.status({ fill: 'red', shape: 'ring', text: 'delete failed' });
345
+ done(err);
346
+ console.error(err.message);
347
+ }
348
+ break;
349
+ }
267
350
  } catch (error) {
268
351
  // node.error(error, msg);
269
352
  done(error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yousolution/node-red-contrib-you-ftp-sftp",
3
- "version": "1.10.0",
3
+ "version": "1.10.1",
4
4
  "description": "A node-red node that support FTP and SFTP file transfer using $() environment variables to control the ftp connection details",
5
5
  "main": "sftp.js",
6
6
  "scripts": {