@yousolution/node-red-contrib-you-ftp-sftp 1.10.0 → 1.11.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/docker-compose.yml +22 -2
- package/nodes/ftp.js +71 -56
- package/nodes/ftps.js +4 -2
- package/nodes/sftp.js +232 -149
- package/package.json +12 -13
package/docker-compose.yml
CHANGED
|
@@ -10,8 +10,16 @@ services:
|
|
|
10
10
|
- ./data:/data
|
|
11
11
|
ports:
|
|
12
12
|
- '1880:1880'
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
|
|
14
|
+
# deploy:
|
|
15
|
+
# resources:
|
|
16
|
+
# limits:
|
|
17
|
+
# # cpus: 0.50
|
|
18
|
+
# memory: 45M
|
|
19
|
+
|
|
20
|
+
# environment:
|
|
21
|
+
# NODE_ENV: 'dev'
|
|
22
|
+
# NODE_OPTIONS: '--max-old-space-size=40'
|
|
15
23
|
|
|
16
24
|
ftps:
|
|
17
25
|
image: stilliard/pure-ftpd
|
|
@@ -32,3 +40,15 @@ services:
|
|
|
32
40
|
TLS_CN: 'localhost'
|
|
33
41
|
TLS_ORG: 'YourOrg'
|
|
34
42
|
TLS_C: 'DE'
|
|
43
|
+
|
|
44
|
+
# pure-ftpd:
|
|
45
|
+
# container_name: ftp
|
|
46
|
+
# image: stilliard/pure-ftpd
|
|
47
|
+
# ports:
|
|
48
|
+
# - 21:21
|
|
49
|
+
# - 30000-30009:30000-30009
|
|
50
|
+
# environment:
|
|
51
|
+
# PUBLICHOST: '0.0.0.0'
|
|
52
|
+
# FTP_USER_NAME: foo
|
|
53
|
+
# FTP_USER_PASS: pass
|
|
54
|
+
# FTP_USER_HOME: /home/foo
|
package/nodes/ftp.js
CHANGED
|
@@ -14,6 +14,8 @@
|
|
|
14
14
|
* limitations under the License.
|
|
15
15
|
**/
|
|
16
16
|
|
|
17
|
+
const ftp = require('basic-ftp');
|
|
18
|
+
|
|
17
19
|
module.exports = function (RED) {
|
|
18
20
|
'use strict';
|
|
19
21
|
// var ftp = require('ftp');
|
|
@@ -50,11 +52,14 @@ module.exports = function (RED) {
|
|
|
50
52
|
this.workdir = n.workdir;
|
|
51
53
|
this.ftpConfig = RED.nodes.getNode(this.ftp);
|
|
52
54
|
|
|
55
|
+
const client = new ftp.Client();
|
|
56
|
+
|
|
53
57
|
if (this.ftpConfig) {
|
|
54
58
|
const node = this;
|
|
55
59
|
// console.log("FTP ftpConfig: " + JSON.stringify(this.ftpConfig));
|
|
56
|
-
node.on('input', (msg, send, done) => {
|
|
60
|
+
node.on('input', async (msg, send, done) => {
|
|
57
61
|
try {
|
|
62
|
+
// const client = new ftp.Client();
|
|
58
63
|
node.workdir = node.workdir || msg.workdir || './';
|
|
59
64
|
node.fileExtension = node.fileExtension || msg.fileExtension || '';
|
|
60
65
|
|
|
@@ -64,49 +69,59 @@ module.exports = function (RED) {
|
|
|
64
69
|
node.ftpConfig.options.user = msg.user || node.ftpConfig.options.user;
|
|
65
70
|
node.ftpConfig.options.password = msg.password || node.ftpConfig.options.password;
|
|
66
71
|
node.ftpConfig.options.pass = msg.pass || msg.password || node.ftpConfig.options.pass;
|
|
72
|
+
node.ftpConfig.options.secure = false;
|
|
67
73
|
|
|
68
|
-
|
|
74
|
+
try {
|
|
75
|
+
await client.access(node.ftpConfig.options);
|
|
76
|
+
// client.ftp.verbose = true;
|
|
77
|
+
} catch (err) {
|
|
78
|
+
console.log(err);
|
|
79
|
+
}
|
|
69
80
|
|
|
70
|
-
const
|
|
81
|
+
// const JSFtp = require('jsftp');
|
|
82
|
+
|
|
83
|
+
// const Ftp = new JSFtp(node.ftpConfig.options);
|
|
71
84
|
switch (node.operation) {
|
|
72
85
|
case 'list':
|
|
73
86
|
console.log('FTP List:' + node.workdir.toString());
|
|
74
|
-
|
|
75
|
-
|
|
87
|
+
try {
|
|
88
|
+
let data = await client.list(node.workdir);
|
|
76
89
|
msg.payload = data;
|
|
90
|
+
client.close();
|
|
77
91
|
node.send(msg);
|
|
78
|
-
})
|
|
92
|
+
} catch (err) {
|
|
93
|
+
client.close();
|
|
94
|
+
done(err);
|
|
95
|
+
}
|
|
79
96
|
break;
|
|
97
|
+
|
|
80
98
|
case 'get':
|
|
81
99
|
let ftpfilename = node.workdir + node.filename;
|
|
82
100
|
if (msg.payload.filename) ftpfilename = msg.payload.filename;
|
|
83
|
-
|
|
101
|
+
let str = '';
|
|
84
102
|
console.log('FTP Get:' + ftpfilename);
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
socket.on('close', (err) => {
|
|
95
|
-
if (err) {
|
|
96
|
-
// node.error(err, msg);
|
|
97
|
-
done(err);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
node.status({});
|
|
101
|
-
msg.payload = {};
|
|
102
|
-
msg.payload.filedata = str;
|
|
103
|
-
msg.payload.filename = ftpfilename;
|
|
104
|
-
node.send(msg);
|
|
105
|
-
});
|
|
106
|
-
socket.resume();
|
|
107
|
-
}
|
|
103
|
+
|
|
104
|
+
const stream = require('stream');
|
|
105
|
+
const dataStream = new stream.Writable({
|
|
106
|
+
write: function (chunk, encoding, next) {
|
|
107
|
+
str += chunk.toString();
|
|
108
|
+
next();
|
|
109
|
+
},
|
|
108
110
|
});
|
|
111
|
+
try {
|
|
112
|
+
await client.downloadTo(dataStream, ftpfilename);
|
|
113
|
+
node.status({});
|
|
114
|
+
msg.payload = {};
|
|
115
|
+
msg.payload.filedata = str;
|
|
116
|
+
msg.payload.filename = ftpfilename;
|
|
117
|
+
client.close();
|
|
118
|
+
node.send(msg);
|
|
119
|
+
} catch (err) {
|
|
120
|
+
client.close();
|
|
121
|
+
done(err);
|
|
122
|
+
}
|
|
109
123
|
break;
|
|
124
|
+
|
|
110
125
|
case 'put':
|
|
111
126
|
let newFile = '';
|
|
112
127
|
if (msg.payload.filename) {
|
|
@@ -126,39 +141,39 @@ module.exports = function (RED) {
|
|
|
126
141
|
|
|
127
142
|
console.log('FTP Put:' + newFile);
|
|
128
143
|
|
|
129
|
-
// let Ftp = new JSFtp(node.
|
|
130
|
-
|
|
131
|
-
const buffer = new Buffer(msgData);
|
|
144
|
+
// let Ftp = new JSFtp(node.ftpsConfig.options);
|
|
132
145
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
146
|
+
const { Readable } = require('stream');
|
|
147
|
+
const buffer = new Buffer.from(msgData);
|
|
148
|
+
try {
|
|
149
|
+
await client.uploadFrom(Readable.from(buffer), newFile);
|
|
150
|
+
node.status({});
|
|
151
|
+
msg.payload = {};
|
|
152
|
+
msg.payload.filename = newFile;
|
|
153
|
+
client.close();
|
|
154
|
+
node.send(msg);
|
|
155
|
+
} catch (err) {
|
|
156
|
+
client.close();
|
|
157
|
+
done(err);
|
|
158
|
+
}
|
|
144
159
|
break;
|
|
160
|
+
|
|
145
161
|
case 'delete':
|
|
146
162
|
let delFile = '';
|
|
147
163
|
if (msg.payload.filename) delFile = msg.payload.filename;
|
|
148
164
|
else delFile = node.workdir + node.filename;
|
|
149
165
|
console.log('FTP Delete:' + delFile);
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
});
|
|
166
|
+
try {
|
|
167
|
+
await client.remove(delFile);
|
|
168
|
+
node.status({});
|
|
169
|
+
msg.payload = {};
|
|
170
|
+
msg.payload.filename = delFile;
|
|
171
|
+
client.close();
|
|
172
|
+
node.send(msg);
|
|
173
|
+
} catch (err) {
|
|
174
|
+
client.close();
|
|
175
|
+
done(err);
|
|
176
|
+
}
|
|
162
177
|
break;
|
|
163
178
|
}
|
|
164
179
|
} catch (error) {
|
package/nodes/ftps.js
CHANGED
|
@@ -53,6 +53,8 @@ module.exports = function (RED) {
|
|
|
53
53
|
this.workdir = n.workdir;
|
|
54
54
|
this.ftpsConfig = RED.nodes.getNode(this.ftps);
|
|
55
55
|
|
|
56
|
+
const client = new ftps.Client();
|
|
57
|
+
|
|
56
58
|
if (this.ftpsConfig) {
|
|
57
59
|
const node = this;
|
|
58
60
|
// console.log("FTP ftpsConfig: " + JSON.stringify(this.ftpsConfig));
|
|
@@ -72,8 +74,7 @@ module.exports = function (RED) {
|
|
|
72
74
|
rejectUnauthorized: false,
|
|
73
75
|
};
|
|
74
76
|
|
|
75
|
-
|
|
76
|
-
client.ftp.verbose = true;
|
|
77
|
+
// client.ftp.verbose = true;
|
|
77
78
|
|
|
78
79
|
try {
|
|
79
80
|
await client.access(node.ftpsConfig.options);
|
|
@@ -93,6 +94,7 @@ module.exports = function (RED) {
|
|
|
93
94
|
let data = await client.list(node.workdir);
|
|
94
95
|
msg.payload = data;
|
|
95
96
|
client.close();
|
|
97
|
+
// client = null;
|
|
96
98
|
node.send(msg);
|
|
97
99
|
} catch (err) {
|
|
98
100
|
client.close();
|
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
|
|
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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
174
|
-
|
|
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
|
-
|
|
177
|
-
|
|
178
|
-
|
|
281
|
+
case 'get':
|
|
282
|
+
try {
|
|
283
|
+
let ftpfilename = node.workdir + node.filename;
|
|
284
|
+
if (msg.payload.filename) ftpfilename = msg.payload.filename;
|
|
179
285
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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.
|
|
3
|
+
"version": "1.11.0",
|
|
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": {
|
|
@@ -33,20 +33,19 @@
|
|
|
33
33
|
},
|
|
34
34
|
"homepage": "https://github.com/yousolution-cloud/node-red-contrib-you-ftp-sftp.git#readme",
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"ftp": "^0.3
|
|
37
|
-
"
|
|
38
|
-
"ssh2-sftp-client": "^9.0.4",
|
|
39
|
-
"basic-ftp": "^5.0.2"
|
|
36
|
+
"basic-ftp": "^5.0.3",
|
|
37
|
+
"ssh2-sftp-client": "^9.1.0"
|
|
40
38
|
},
|
|
41
39
|
"devDependencies": {
|
|
42
|
-
"@types/chai": "^4.3.
|
|
43
|
-
"@types/node-red
|
|
44
|
-
"@types/
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"node-red
|
|
40
|
+
"@types/chai": "^4.3.11",
|
|
41
|
+
"@types/node-red": "^1.3.4",
|
|
42
|
+
"@types/node-red-node-test-helper": "^0.3.3",
|
|
43
|
+
"@types/sinon": "^17.0.2",
|
|
44
|
+
"chai": "^4.3.10",
|
|
45
|
+
"mocha": "^10.0.2",
|
|
46
|
+
"node-red": "^3.1.0",
|
|
47
|
+
"node-red-node-test-helper": "^0.3.2",
|
|
49
48
|
"nyc": "^15.1.0",
|
|
50
|
-
"sinon": "^
|
|
49
|
+
"sinon": "^17.0.1"
|
|
51
50
|
}
|
|
52
51
|
}
|