@yousolution/node-red-contrib-you-ftp-sftp 1.11.0 → 1.12.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/README.md CHANGED
@@ -1,52 +1,201 @@
1
1
  # @yousolution/node-red-contrib-you-ftp-sftp
2
2
 
3
- A Node-RED node to FTP and SFTP Client (_fork node-red-contrib-ftp-sftp_).
3
+ [![npm version](https://badge.fury.io/js/@yousolution%2Fnode-red-contrib-you-ftp-sftp.svg)](https://badge.fury.io/js/@yousolution%2Fnode-red-contrib-you-ftp-sftp)
4
+ [![Node-RED](https://img.shields.io/badge/Node--RED-3.x-red)](https://nodered.org)
4
5
 
5
- **Adds FTP/SFTP error handling**
6
-
7
- Please log issues in the repo for assistance.
8
- (https://github.com/yousolution-cloud/node-red-contrib-you-ftp-sftp.git)
6
+ Node-RED nodes for FTP, FTPS and SFTP file transfer operations.
9
7
 
10
8
  ## Install
11
9
 
12
- Run the following command in the root directory of your Node-RED install
10
+ ```bash
11
+ npm install @yousolution/node-red-contrib-you-ftp-sftp
12
+ ```
13
+
14
+ ## Nodes
13
15
 
14
- npm install @yousolution/node-red-contrib-you-ftp-sftp
16
+ This package provides three protocol nodes, each with a **server config node** and an **input node**:
15
17
 
16
- ## Configuration
18
+ | Node | Protocol | Auth | Library |
19
+ |------|----------|------|---------|
20
+ | `ftp` + `ftp in` | FTP (plain) | Password | [basic-ftp](https://github.com/patrickjuchli/basic-ftp) |
21
+ | `ftps` + `ftps in` | FTP over implicit TLS | Password | [basic-ftp](https://github.com/patrickjuchli/basic-ftp) |
22
+ | `sftp` + `sftp in` | SFTP (SSH) | Password, SSH Key, PPK | [ssh2-sftp-client](https://github.com/theophilusx/ssh2-sftp-client) |
17
23
 
18
- process.env.SFTP_SSH_KEY_FILE - If you want to use private SSH key set this environment variable
24
+ ## Operations
19
25
 
20
- ## SFTP & FTP
26
+ All three nodes support the same four operations:
21
27
 
22
- PUT - Set msg.payload.filedata to the file contents you want pushed and will be uploaded to {GUID}.FileExtension. If you need more changes file request to github.
28
+ | Operation | Description |
29
+ |-----------|-------------|
30
+ | `list` | List files in the working directory |
31
+ | `get` | Download a file |
32
+ | `put` | Upload a file |
33
+ | `delete` | Delete a file |
23
34
 
24
- GET - Set msg.payload.filename to get the file or will use Workdir + Filename in configuration. Leave configuration blank to set in code.
35
+ RENAME - Set msg.payload.filename and msg.payload.newfilename Leave configuration blank to set in code.
25
36
 
26
37
  DELETE - Set msg.payload.filename to delete the file or will use Workdir + Filename in configuration. Leave configuration blank to set in code.
27
38
 
28
- LIST - Uses the workdir
39
+ ---
40
+
41
+ ## Server Config Nodes
42
+
43
+ ### FTP / FTPS Config
44
+
45
+ | Field | Type | Default | Description |
46
+ |-------|------|---------|-------------|
47
+ | Host | string | `localhost` | Server hostname or IP |
48
+ | Port | number | `21` | Server port |
49
+ | User | string | `anonymous` | Username |
50
+ | Password | string | `anonymous@` | Password |
51
+ | Connection Timeout | number | `10000` | Connection timeout (ms) |
52
+ | PASV Timeout | number | `10000` | PASV mode timeout (ms) |
53
+ | Keepalive | number | `10000` | Keepalive interval (ms) |
54
+ | Secure | boolean | `false` | Use TLS for data channel (FTP only) |
55
+ | Secure Options | string | — | TLS options (FTP only) |
56
+
57
+ > FTPS forces `secure: true` and `rejectUnauthorized: false`.
58
+
59
+ ### SFTP Config
60
+
61
+ | Field | Type | Default | Description |
62
+ |-------|------|---------|-------------|
63
+ | Host | string | `localhost` | Server hostname or IP |
64
+ | Port | number | `22` | Server port |
65
+ | Username | string | — | SSH username |
66
+ | Password | string | — | SSH password |
67
+ | SSH-Key file | file | — | PEM, OpenSSH, or PPK private key |
68
+ | PPK Passphrase | password | — | Passphrase for encrypted PPK keys |
69
+ | hmac | select | `hmac-sha2-256,hmac-sha2-512,hmac-sha1` | Allowed HMAC algorithms (multi-select) |
70
+ | cipher | select | `aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm` | Allowed ciphers (multi-select) |
71
+
72
+ ---
73
+
74
+ ## Input Node: Message Properties
75
+
76
+ ### Input Messages (`msg`)
77
+
78
+ | Property | Type | Applicable | Description |
79
+ |----------|------|------------|-------------|
80
+ | `msg.host` | string | All | Override server host |
81
+ | `msg.port` | number | All | Override server port |
82
+ | `msg.user` | string | FTP/FTPS | Override username |
83
+ | `msg.password` | string | All | Override password |
84
+ | `msg.workdir` | string | All | Override working directory |
85
+ | `msg.payload.filename` | string | get/delete/put | File path (overrides config) |
86
+ | `msg.payload.filedata` | string/Buffer | put | File content to upload |
87
+ | `msg.ppkpassphrase` | string | SFTP only | Override PPK passphrase at runtime |
88
+
89
+ ### Output Messages (`msg`)
90
+
91
+ | Operation | Output |
92
+ |-----------|--------|
93
+ | `list` | `msg.payload` — Array of file entries |
94
+ | `get` | `msg.payload.filedata` — File content, `msg.payload.filename` — Remote path |
95
+ | `put` | `msg.payload.filename` — Remote path of uploaded file |
96
+ | `delete` | `msg.payload.filename` — Deleted file path |
97
+
98
+ ### Status
99
+
100
+ The node updates its status indicator during operations:
101
+
102
+ | Status | Description |
103
+ |--------|-------------|
104
+ | 🟡 `connection...` | Connecting to server |
105
+ | 🟢 `connected` | Connected successfully |
106
+ | 🟢 `list done` / `get done` / `put done` / `delete done` | Operation completed |
107
+ | 🔴 `connection failed.` | Connection failed |
108
+ | 🔴 `list failed` / ... | Operation failed |
109
+ | 🟡 `converting PPK...` | PPK key conversion in progress (SFTP only) |
110
+
111
+ ---
112
+
113
+ ## Authentication
114
+
115
+ ### FTP / FTPS
116
+
117
+ Username and password only.
118
+
119
+ ### SFTP
120
+
121
+ The SFTP node supports three authentication methods (tried in order):
29
122
 
30
- ## Sample Function Node
123
+ 1. **SSH Private Key** (PEM or OpenSSH format)
124
+ - Upload the key file in the node config, or
125
+ - Set the `SFTP_SSH_KEY_FILE` environment variable
31
126
 
32
- <PRE>
33
- // ----------------------------------------------------
34
- // All functions above use information.
35
- // Sample Javascript Used In Function Node
36
- // ----------------------------------------------------
127
+ 2. **PPK Key** (PuTTY format, v2 and v3)
128
+ - Upload the `.ppk` file in the node config
129
+ - If the PPK is encrypted, provide the passphrase
130
+ - The node automatically converts PPK to PEM using [`ppk-to-openssh`](https://github.com/cartpauj/ppk-to-openssh)
131
+ - Supports all key types: RSA, DSA, ECDSA (P-256/P-384/P-521), Ed25519
132
+ - Supports PPK v3 with Argon2id key derivation
133
+
134
+ 3. **Password** (fallback if no key is provided)
135
+
136
+ > Pass the passphrase for encrypted keys via the config UI or at runtime via `msg.ppkpassphrase`.
137
+
138
+ ---
139
+
140
+ ## Environment Variables
141
+
142
+ | Variable | Description |
143
+ |----------|-------------|
144
+ | `SFTP_SSH_KEY_FILE` | Path to an SSH private key file (PEM/OpenSSH/PPK). Resolved relative to the project root. |
145
+
146
+ ---
147
+
148
+ ## Examples
149
+
150
+ ### Upload a file
151
+
152
+ ```javascript
37
153
  msg.payload = {};
38
- msg.payload.filename="./SAMPLE_FILE.txt"; // Full Path
39
- msg.payload.filedata='{}'; // Needs to be a string
154
+ msg.payload.filename = "/remote/path/file.txt";
155
+ msg.payload.filedata = "file content";
156
+ return msg;
157
+ ```
158
+
159
+ ### Download a file
160
+
161
+ ```javascript
162
+ msg.payload = {};
163
+ msg.payload.filename = "/remote/path/file.txt";
164
+ return msg;
165
+ ```
166
+
167
+ ### List directory
168
+
169
+ ```javascript
170
+ msg.workdir = "/remote/path/";
171
+ return msg;
172
+ ```
173
+
174
+ ### Delete a file
175
+
176
+ ```javascript
177
+ msg.payload = {};
178
+ msg.payload.filename = "/remote/path/file.txt";
179
+ return msg;
180
+ ```
181
+
182
+ ### Override connection at runtime
183
+
184
+ ```javascript
185
+ msg.host = "192.168.1.100";
186
+ msg.port = 22;
187
+ msg.user = "admin";
188
+ msg.password = "secret";
189
+ msg.ppkpassphrase = "my-ppk-passphrase"; // for encrypted PPK keys
40
190
  return msg;
41
- // ----------------------------------------------------
42
- </PRE>
191
+ ```
43
192
 
44
- ## Acknowledgements
193
+ ---
45
194
 
46
- The node-red-contrib-force uses the following open source software:
195
+ ## Changelog
47
196
 
48
- - [node-ftp-sftp](https://github.com/yousolution-cloud/node-red-contrib-you-ftp-sftp): node-ftp is an FTP and SFTP client module for node.js that provides an asynchronous interface for communicating with an FTP and SFTP servers.
197
+ See [history.md](history.md).
49
198
 
50
199
  ## License
51
200
 
52
- See [license](https://github.com/yousolution-cloud/node-red-contrib-you-ftp-sftp/blob/master/LICENSE) (Apache License Version 2.0).
201
+ Apache 2.0 — see [LICENSE](LICENSE).
@@ -41,6 +41,17 @@ services:
41
41
  TLS_ORG: 'YourOrg'
42
42
  TLS_C: 'DE'
43
43
 
44
+ sftp:
45
+ image: atmoz/sftp:alpine
46
+ container_name: sftp
47
+ ports:
48
+ - '2222:22'
49
+ volumes:
50
+ - ./sftp/data:/home/testuser/upload
51
+ - ./sftp/keys/id_rsa.pub:/home/testuser/.ssh/keys/id_rsa.pub:ro
52
+ command: testuser:test123:::upload
53
+ restart: unless-stopped
54
+
44
55
  # pure-ftpd:
45
56
  # container_name: ftp
46
57
  # image: stilliard/pure-ftpd
package/history.md CHANGED
@@ -1,3 +1,15 @@
1
+ # 1.12.0 / 2026-07-21
2
+
3
+ - add PPK file support via `ppk-to-openssh` (v2 and v3 with Argon2id)
4
+ - add PPK passphrase field in SFTP config UI
5
+ - refactor SFTP options to eliminate code duplication
6
+ - improved SFTP config UI: upload button, filename display, clear button, key info badge
7
+ - SFTP key content moved to credentials (encrypted storage)
8
+
9
+ # 1.11.0
10
+
11
+ - add PPK module (internal)
12
+
1
13
  # 1.10.0 / 2023-05-02
2
14
 
3
15
  - add support to file key ssh
package/nodes/ftps.html CHANGED
@@ -81,6 +81,7 @@
81
81
  <option value="list">list</option>
82
82
  <option value="get">get</option>
83
83
  <option value="put">put</option>
84
+ <option value="rename">rename</option>
84
85
  <option value="delete">delete</option>
85
86
  </select>
86
87
  </div>
@@ -92,6 +93,10 @@
92
93
  <label for="node-input-filename"><i class="fa fa-file"></i> Filename</label>
93
94
  <input type="text" id="node-input-filename" placeholder="Filename">
94
95
  </div>
96
+ <div class="form-row input-newfilename-row hidden">
97
+ <label for="node-input-newfilename"><i class="fa fa-file"></i> New Filename</label>
98
+ <input type="text" id="node-input-newfilename" placeholder="New Filename">
99
+ </div>
95
100
  <div class="form-row input-fileExtension-row hidden">
96
101
  <label for="node-input-fileExtension"><i class="fa fa-file"></i> File Extension</label>
97
102
  <input type="text" id="node-input-fileExtension" placeholder="File Extension">
@@ -117,6 +122,7 @@
117
122
  <ul>
118
123
  <li><b>PUT</b> - Set <code>msg.payload.filedata</code> to the file contents you want pushed and will be uploaded to {GUID}.FileExtension. If you need more changes file request to github.</li>
119
124
  <li><b>GET</b> - Set <code>msg.payload.filename</code> to get the file or will use Workdir + Filename in configuration. Leave configuration blank to set in code.</li>
125
+ <li><b>RENAME</b> - Set <code>msg.payload.filename</code> and <code>msg.payload.newfilename</code> Leave configuration blank to set in code.</li>
120
126
  <li><b>DELETE</b> - Set <code>msg.payload.filename</code> to delete the file or will use Workdir + Filename in configuration. Leave configuration blank to set in code.</li>
121
127
  <li><b>LIST</b> - Uses the workdir</li>
122
128
  </ul>
@@ -159,6 +165,9 @@
159
165
  } else if (id == 'put') {
160
166
  filename.show();
161
167
  fileExtension.show();
168
+ } else if (id == 'rename') {
169
+ filename.show();
170
+ newfilename.show();
162
171
  } else if (id == 'get') {
163
172
  filename.show();
164
173
  fileExtension.hide();
package/nodes/ftps.js CHANGED
@@ -199,6 +199,25 @@ module.exports = function (RED) {
199
199
  // }
200
200
  // });
201
201
  break;
202
+ case 'rename':
203
+ try{
204
+ let ftpfilename = node.workdir + node.filename;
205
+ let ftpnewfilename = node.workdir + node.newfilename;
206
+ if (msg.payload.filename) ftpfilename = msg.payload.filename;
207
+ if (msg.payload.newfilename) ftpnewfilename = msg.payload.newfilename;
208
+ console.log('FTPS Renaming File: ' + ftpfilename + ' to ' + ftpnewfilename);
209
+ await client.rename(ftpfilename, ftpnewfilename);
210
+ node.status({ fill: 'green', shape: 'dot', text: 'rename done' });
211
+ await client.close();
212
+ node.send(msg);
213
+ } catch (err) {
214
+ node.status({ fill: 'red', shape: 'ring', text: 'rename failed' });
215
+ await client.close();
216
+ done(err);
217
+ console.error(err.message);
218
+ }
219
+ break;
220
+
202
221
  case 'delete':
203
222
  let delFile = '';
204
223
  if (msg.payload.filename) delFile = msg.payload.filename;
package/nodes/sftp.html CHANGED
@@ -19,27 +19,35 @@
19
19
  <label for="node-config-input-host"><i class="fa fa-bookmark"></i> Host</label>
20
20
  <input type="text" id="node-config-input-host" placeholder="localhost" style="width: 40%;" />
21
21
  <label for="node-config-input-port" style="margin-left: 10px; width: 35px; "> Port</label>
22
- <input type="text" id="node-config-input-port" placeholder="21" style="width:45px">
22
+ <input type="text" id="node-config-input-port" placeholder="22" style="width:45px">
23
23
  </div>
24
24
  <div class="form-row">
25
25
  <label for="node-config-input-username"><i class="fa fa-user"></i> User</label>
26
- <input type="text" id="node-config-input-username" placeholder="anonymous" />
26
+ <input type="text" id="node-config-input-username" placeholder="username" />
27
27
  </div>
28
28
  <div class="form-row">
29
29
  <label for="node-config-input-password"><i class="fa fa-lock"></i> Password</label>
30
30
  <input type="password" id="node-config-input-password">
31
31
  </div>
32
32
  <div class="form-row">
33
- <label for="node-config-input-ssh-key-in"><i class="fa fa-lock"></i> SSH-Key file</label>
34
- <input type="file" id="node-config-input-ssh-key-in">
33
+ <label><i class="fa fa-key"></i> SSH Key</label>
34
+ <span class="ssh-key-input-data">
35
+ <label class="red-ui-button" for="node-config-input-ssh-key-in">
36
+ <i class="fa fa-upload"></i> Upload
37
+ </label>
38
+ <input class="hide" type="file" id="node-config-input-ssh-key-in">
39
+ <span id="ssh-key-name" style="width: calc(100% - 230px); overflow: hidden; line-height:34px; height:34px; text-overflow: ellipsis; display: inline-block; vertical-align: middle;"> </span>
40
+ <button type="button" class="red-ui-button red-ui-button-small" id="ssh-key-clear" style="margin-left: 6px;"><i class="fa fa-times"></i></button>
41
+ </span>
42
+ <input type="hidden" id="node-config-input-sshkey">
35
43
  </div>
36
- <div class="form-row">
37
- <label for="node-config-input-selected-sshkey"><i class="fa fa-lock"></i> Selected SSH-Key file </label>
38
- <input type="checkbox" disabled id="node-config-input-selected-sshkey">
44
+ <div class="form-row" id="ssh-key-info-row" style="display:none;">
45
+ <label><i class="fa fa-info-circle"></i> Key Info</label>
46
+ <span id="ssh-key-info" style="line-height:34px; height:34px;"></span>
39
47
  </div>
40
- <div class="form-row">
41
- <!-- <label for="node-config-input-sshkey"><i class="fa fa-lock"></i> SSH-Key</label> -->
42
- <textarea id="node-config-input-sshkey" style="display:none;"> </textarea>
48
+ <div class="form-row ppk-passphrase-row" style="display:none;">
49
+ <label for="node-config-input-ppkpassphrase"><i class="fa fa-lock"></i> PPK Passphrase</label>
50
+ <input type="password" id="node-config-input-ppkpassphrase" placeholder="Passphrase for encrypted PPK key">
43
51
  </div>
44
52
  <div class="form-row">
45
53
  <label for="node-config-input-hmac"><i class="fa fa-wrench"></i>hmac</label>
@@ -88,33 +96,94 @@
88
96
  username: { value: '', required: true },
89
97
  password: { value: '' },
90
98
  sshkey: { value: '' },
99
+ ppkpassphrase: { value: '' },
91
100
  hmac: { value: 'hmac-sha2-256,hmac-sha2-512,hmac-sha1', required: true },
92
101
  cipher: { value: 'aes128-ctr,aes192-ctr,aes256-ctr,aes128-gcm', required: true },
93
102
  },
103
+ credentials: {
104
+ sshkeydata: { type: 'text' },
105
+ },
94
106
  label: function () {
95
107
  return this.host;
96
108
  },
97
109
  oneditprepare: function () {
98
- const sshKeyFileIn = document.getElementById('node-config-input-ssh-key-in');
99
- const sshKeyFile = document.getElementById('node-config-input-sshkey');
110
+ function updateKeyInfo(content) {
111
+ var infoRow = $('#ssh-key-info-row');
112
+ var infoEl = $('#ssh-key-info');
113
+ var ppkRow = $('.ppk-passphrase-row');
114
+ if (!content) {
115
+ infoRow.hide();
116
+ ppkRow.hide();
117
+ return;
118
+ }
119
+ if (content.indexOf('PuTTY-User-Key-File-') !== -1) {
120
+ var ver = content.indexOf('PuTTY-User-Key-File-3:') !== -1 ? 'v3' : 'v2';
121
+ var algoMatch = content.match(/: (\S+)/);
122
+ var algo = algoMatch ? algoMatch[1] : '?';
123
+ var encrypted = !/Encryption:\s*none/i.test(content);
124
+ var encLabel = encrypted ? 'encrypted' : 'unencrypted';
125
+ infoEl.text('PPK ' + ver + ' / ' + algo + ' / ' + encLabel);
126
+ infoRow.show();
127
+ ppkRow.toggle(encrypted);
128
+ } else {
129
+ var isOpenSSH = content.indexOf('-----BEGIN OPENSSH PRIVATE KEY-----') !== -1;
130
+ var isPEM = content.indexOf('-----BEGIN ') !== -1;
131
+ infoEl.text(isOpenSSH ? 'OpenSSH format' : isPEM ? 'PEM format' : 'Key loaded');
132
+ infoRow.show();
133
+ ppkRow.hide();
134
+ }
135
+ }
100
136
 
101
- if (document.getElementById('node-config-input-sshkey').value) {
102
- $('#node-config-input-selected-sshkey').prop('checked', true);
137
+ function saveFile(file) {
138
+ var reader = new FileReader();
139
+ reader.onload = function (event) {
140
+ var data = event.target.result;
141
+ $('#ssh-key-name').text(file.name);
142
+ $('#node-config-input-sshkey').val(data);
143
+ updateKeyInfo(data);
144
+ };
145
+ reader.readAsText(file, 'UTF-8');
103
146
  }
104
147
 
105
- // node-config-input-selected-sshkey
148
+ function clearKey() {
149
+ $('#ssh-key-name').text('');
150
+ $('#node-config-input-sshkey').val('');
151
+ $('#ssh-key-info-row').hide();
152
+ $('.ppk-passphrase-row').hide();
153
+ }
106
154
 
107
- // let fileOutput = document.getElementById("fileOutput");
108
- sshKeyFileIn.addEventListener('change', function () {
109
- const fileReader = new FileReader();
110
- fileReader.onload = function () {
111
- // console.log(fileReader.result);
112
- sshKeyFile.value = fileReader.result;
113
- };
114
- // console.log({ type: this.files[0].type });
115
- // console.log({ size: this.files[0].size });
116
- fileReader.readAsBinaryString(this.files[0]);
155
+ // Detect if sshkey contains full key content (old format) or just a filename
156
+ var savedKey = this.sshkey || '';
157
+ if (savedKey.indexOf('-----BEGIN ') !== -1 || savedKey.indexOf('PuTTY-User-Key-File-') !== -1) {
158
+ $('#node-config-input-sshkey').val(savedKey);
159
+ $('#ssh-key-name').text('loaded');
160
+ updateKeyInfo(savedKey);
161
+ } else if (savedKey) {
162
+ $('#ssh-key-name').text(savedKey);
163
+ var existingData = $('#node-config-input-sshkey').val();
164
+ if (existingData) {
165
+ updateKeyInfo(existingData);
166
+ } else {
167
+ // Filename senza contenuto — il server proverà a leggerlo dal disco
168
+ $('#ssh-key-info').text('Filename — verrà letto dal disco all\'avvio');
169
+ $('#ssh-key-info-row').show();
170
+ $('.ppk-passphrase-row').show();
171
+ }
172
+ }
173
+
174
+ $('#node-config-input-ssh-key-in').on('change', function () {
175
+ if (this.files && this.files[0]) saveFile(this.files[0]);
117
176
  });
177
+
178
+ $('#ssh-key-clear').on('click', clearKey);
179
+ },
180
+ oneditsave: function () {
181
+ var keyData = $('#node-config-input-sshkey').val();
182
+ if (keyData) {
183
+ this.credentials.sshkeydata = keyData;
184
+ } else {
185
+ this.credentials.sshkeydata = '';
186
+ }
118
187
  },
119
188
  });
120
189
  </script>
@@ -130,6 +199,7 @@
130
199
  <option value="list">list</option>
131
200
  <option value="get">get</option>
132
201
  <option value="put">put</option>
202
+ <option value="rename">rename</option>
133
203
  <option value="delete">delete</option>
134
204
  </select>
135
205
  </div>
@@ -141,6 +211,10 @@
141
211
  <label for="node-input-filename"><i class="fa fa-file"></i> Filename</label>
142
212
  <input type="text" id="node-input-filename" placeholder="Filename">
143
213
  </div>
214
+ <div class="form-row input-newfilename-row hidden">
215
+ <label for="node-input-newfilename"><i class="fa fa-file"></i> New Filename</label>
216
+ <input type="text" id="node-input-newfilename" placeholder="New Filename">
217
+ </div>
144
218
  <div class="form-row input-savedir-row hidden">
145
219
  <label for="node-input-savedir"><i class="fa fa-folder-open"></i> Save Directory</label>
146
220
  <input type="text" id="node-input-savedir" placeholder="Save Directory">
@@ -171,6 +245,7 @@
171
245
  <ul>
172
246
  <li><b>PUT</b> - Set <code>msg.payload.filedata</code> to the file contents you want pushed and will be uploaded to {GUID}.FileExtension. If you need more changes file request to github.</li>
173
247
  <li><b>GET</b> - Set <code>msg.payload.filename</code> to get the file or will use Workdir + Filename in configuration. Leave configuration blank to set in code.</li>
248
+ <li><b>RENAME</b> - Set <code>msg.payload.filename</code> and <code>msg.payload.newfilename</code> Leave configuration blank to set in code.</li>
174
249
  <li><b>DELETE</b> - Set <code>msg.payload.filename</code> to delete the file or will use Workdir + Filename in configuration. Leave configuration blank to set in code.</li>
175
250
  <li><b>LIST</b> - Uses the workdir</li>
176
251
  </ul>
@@ -204,11 +279,13 @@
204
279
  },
205
280
  oneditprepare: function () {
206
281
  var filename = $('.input-filename-row');
282
+ var newfilename = $('.input-newfilename-row');
207
283
  var fileContents = $('.input-fileContents-row');
208
284
  var fileExtension = $('.input-fileExtension-row');
285
+ var savedir = $('.input-savedir-row');
286
+ var localFilename = $('.input-localFilename-row');
209
287
  $('#node-input-operation').change(function () {
210
288
  var id = $('#node-input-operation option:selected').val();
211
- console.log(id);
212
289
  if (id == 'list') {
213
290
  filename.hide();
214
291
  fileExtension.hide();
@@ -216,6 +293,13 @@
216
293
  filename.show();
217
294
  fileContents.hide();
218
295
  fileExtension.show();
296
+ } else if (id == 'rename') {
297
+ filename.show();
298
+ newfilename.show();
299
+ fileContents.hide();
300
+ fileExtension.hide();
301
+ savedir.hide();
302
+ localFilename.hide();
219
303
  } else if (id == 'get') {
220
304
  filename.show();
221
305
  fileExtension.hide();
package/nodes/sftp.js CHANGED
@@ -34,7 +34,9 @@ module.exports = function (RED) {
34
34
  'use strict';
35
35
 
36
36
  // const sftp = require('ssh2').Client;
37
- const fs = require('fs');
37
+ const fs = require('fs');
38
+ const path = require('path');
39
+ const os = require('os');
38
40
 
39
41
  function SFtpNode(n) {
40
42
  RED.nodes.createNode(this, n);
@@ -49,9 +51,50 @@ module.exports = function (RED) {
49
51
  let keyFile = null;
50
52
  let keyData = null;
51
53
 
52
- if (n.sshkey) {
53
- keyFile = 'keyFile';
54
- keyData = n.sshkey;
54
+ // New UI: key content stored in credentials (encrypted)
55
+ if (this.credentials && this.credentials.sshkeydata) {
56
+ keyFile = 'credentials';
57
+ keyData = this.credentials.sshkeydata;
58
+ }
59
+ // Backward compat: old flows store key content directly in n.sshkey
60
+ if (n.sshkey && !keyData) {
61
+ const trimmed = n.sshkey.trim();
62
+ if (trimmed.startsWith('-----BEGIN ') || trimmed.startsWith('PuTTY-User-Key-File-')) {
63
+ keyFile = 'keyFile';
64
+ keyData = n.sshkey;
65
+ } else if (trimmed !== 'loaded') {
66
+ console.log('SFTP - sshkey non contiene una chiave ma "' + trimmed.substring(0, 80) + '", provo a leggerlo come file...');
67
+ const tryPaths = [];
68
+ if (path.isAbsolute(trimmed)) {
69
+ tryPaths.push(path.resolve(trimmed));
70
+ } else {
71
+ tryPaths.push(path.resolve(process.cwd(), trimmed));
72
+ tryPaths.push(path.resolve('/data/', trimmed));
73
+ tryPaths.push(path.resolve(os.homedir(), '.node-red', trimmed));
74
+ tryPaths.push(path.resolve(os.homedir(), trimmed));
75
+ tryPaths.push(path.resolve(os.homedir(), 'Desktop', trimmed));
76
+ tryPaths.push(path.resolve(os.homedir(), 'Downloads', trimmed));
77
+ tryPaths.push(path.resolve(__dirname, '../../' + trimmed));
78
+ }
79
+ console.log('SFTP - Cerco il file in:');
80
+ for (const fp of tryPaths) {
81
+ console.log(' - ' + fp);
82
+ try {
83
+ const content = fs.readFileSync(fp, 'utf8');
84
+ if (content.includes('PuTTY-User-Key-File-') || content.includes('-----BEGIN ')) {
85
+ keyData = content;
86
+ keyFile = fp;
87
+ console.log('SFTP - OK: chiave letta da ' + fp);
88
+ break;
89
+ }
90
+ } catch (_) { /* non trovato */ }
91
+ }
92
+ if (!keyData) {
93
+ console.log('SFTP - WARNING: File "' + trimmed + '" non trovato.');
94
+ console.log('SFTP - Se usi Docker: docker cp /percorso/locale/' + trimmed + ' <container>:/data/' + trimmed);
95
+ console.log('SFTP - Oppure incolla il contenuto della chiave nel campo SSH Key dell\'editor, o usa Upload.');
96
+ }
97
+ }
55
98
  }
56
99
  if (process.env.SFTP_SSH_KEY_FILE) {
57
100
  keyFile = process.env.SFTP_SSH_KEY_FILE;
@@ -60,161 +103,91 @@ module.exports = function (RED) {
60
103
 
61
104
  try {
62
105
  keyData = fs.readFileSync(keyFile).toString();
63
- // console.log("[http://wwww.HardingPoint.com PRIVATE KEY] " + keyData);
64
106
  } catch (e) {
65
107
  keyData = null;
66
108
  console.log('SFTP - Read Key File [' + keyFile + '] Exception : ' + e);
67
109
  }
68
110
  }
69
111
 
112
+ const algorithms = {
113
+ kex: [
114
+ 'curve25519-sha256',
115
+ 'curve25519-sha256@libssh.org',
116
+ 'ecdh-sha2-nistp256',
117
+ 'ecdh-sha2-nistp384',
118
+ 'ecdh-sha2-nistp521',
119
+ 'diffie-hellman-group-exchange-sha256',
120
+ 'diffie-hellman-group14-sha256',
121
+ 'diffie-hellman-group15-sha512',
122
+ 'diffie-hellman-group16-sha512',
123
+ 'diffie-hellman-group17-sha512',
124
+ 'diffie-hellman-group18-sha512',
125
+ 'diffie-hellman-group-exchange-sha1',
126
+ 'diffie-hellman-group14-sha1',
127
+ 'diffie-hellman-group1-sha1',
128
+ ],
129
+ cipher: [
130
+ 'chacha20-poly1305@openssh.com',
131
+ 'aes128-gcm',
132
+ 'aes128-gcm@openssh.com',
133
+ 'aes256-gcm',
134
+ 'aes256-gcm@openssh.com',
135
+ 'aes128-ctr',
136
+ 'aes192-ctr',
137
+ 'aes256-ctr',
138
+ '3des-cbc',
139
+ 'aes256-cbc',
140
+ 'aes192-cbc',
141
+ 'aes128-cbc',
142
+ ],
143
+ serverHostKey: [
144
+ 'ssh-ed25519',
145
+ 'ecdsa-sha2-nistp256',
146
+ 'ecdsa-sha2-nistp384',
147
+ 'ecdsa-sha2-nistp521',
148
+ 'rsa-sha2-512',
149
+ 'rsa-sha2-256',
150
+ 'ssh-rsa',
151
+ 'ssh-dss',
152
+ ],
153
+ hmac: [
154
+ 'hmac-sha2-256-etm@openssh.com',
155
+ 'hmac-sha2-512-etm@openssh.com',
156
+ 'hmac-sha1-etm@openssh.com',
157
+ 'hmac-sha2-256',
158
+ 'hmac-sha2-512',
159
+ 'hmac-sha1',
160
+ 'hmac-md5',
161
+ 'hmac-sha2-256-96',
162
+ 'hmac-sha2-512-96',
163
+ ],
164
+ };
165
+
166
+ this.options = {
167
+ host: n.host || 'localhost',
168
+ port: n.port || 21,
169
+ username: n.username,
170
+ password: n.password,
171
+ algorithms,
172
+ };
173
+
70
174
  if (keyFile && keyData) {
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('*******************');
76
- this.options = {
77
- host: n.host || 'localhost',
78
- port: n.port || 21,
79
- username: n.username,
80
- password: n.password,
81
- privateKey: keyData,
82
- algorithms: {
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
- ],
142
- },
143
- };
175
+ const isPPK = typeof keyData === 'string' && keyData.includes('PuTTY-User-Key-File-');
176
+ if (isPPK) {
177
+ const isEncrypted = !/Encryption:\s*none/i.test(keyData);
178
+ this.ppkPassphrase = n.ppkpassphrase || '';
179
+ if (isEncrypted && !this.ppkPassphrase) {
180
+ console.log('SFTP - WARNING: PPK key is encrypted but no passphrase provided. Set the passphrase in the config or use msg.ppkpassphrase at runtime.');
181
+ }
182
+ console.log('SFTP - Using PPK key file' + (isEncrypted ? ' (encrypted)' : ' (unencrypted)'));
183
+ this.rawKey = keyData;
184
+ this.ppkDetected = true;
185
+ } else {
186
+ console.log('SFTP - Using key file');
187
+ this.options.privateKey = keyData;
188
+ }
144
189
  } else {
145
- // console.log('*******************');
146
190
  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('*******************');
151
- this.options = {
152
- host: n.host || 'localhost',
153
- port: n.port || 21,
154
- username: n.username,
155
- password: n.password,
156
- algorithms: {
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
- ],
216
- },
217
- };
218
191
  }
219
192
  }
220
193
 
@@ -251,12 +224,58 @@ module.exports = function (RED) {
251
224
  // conn.on('ready', async function () {
252
225
  const Client = require('ssh2-sftp-client');
253
226
  const client = new Client();
227
+
228
+ // Lazy PPK -> PEM conversion (supports PPK v2 and v3 with Argon2id)
229
+ console.log('SFTP - PPK state: detected=' + node.sftpConfig.ppkDetected + ' converted=' + node.sftpConfig.ppkConverted);
230
+ if (node.sftpConfig.ppkDetected && !node.sftpConfig.ppkConverted) {
231
+ try {
232
+ node.status({ fill: 'gray', shape: 'ring', text: 'converting PPK...' });
233
+ const ppkPass = msg.ppkpassphrase || node.sftpConfig.ppkPassphrase || '';
234
+ const { PPKParser } = require('ppk-to-openssh');
235
+ const parser = new PPKParser({ outputFormat: 'pem' });
236
+ const result = await parser.parse(node.sftpConfig.rawKey, ppkPass);
237
+ node.sftpConfig.options.privateKey = result.privateKey;
238
+ console.log('SFTP - PPK converted to PEM: ' + result.algorithm);
239
+ } catch (err) {
240
+ const hint = err.message && err.message.includes('Passphrase')
241
+ ? ' - Set the passphrase in the PPK config or pass it via msg.ppkpassphrase'
242
+ : '';
243
+ node.status({ fill: 'red', shape: 'ring', text: 'PPK conversion failed' });
244
+ done(new Error('PPK conversion failed: ' + err.message + hint));
245
+ console.error('SFTP - PPK conversion error: ' + err.message + hint);
246
+ return;
247
+ }
248
+ }
249
+
254
250
  try {
255
251
  node.status({ fill: 'gray', shape: 'ring', text: 'connection...' });
252
+
253
+ const opts = node.sftpConfig.options;
254
+ const hostPort = opts.host + ':' + opts.port;
255
+ const user = opts.username;
256
+ let authInfo = 'none';
257
+ if (opts.privateKey) {
258
+ const pk = opts.privateKey;
259
+ if (pk.includes('BEGIN OPENSSH')) authInfo = 'OpenSSH key';
260
+ else if (pk.includes('BEGIN RSA PRIVATE KEY')) authInfo = 'RSA PEM key';
261
+ else if (pk.includes('BEGIN EC PRIVATE KEY')) authInfo = 'EC PEM key';
262
+ else if (pk.includes('BEGIN DSA PRIVATE KEY')) authInfo = 'DSA PEM key';
263
+ else if (pk.includes('BEGIN PRIVATE KEY')) authInfo = 'PKCS8 PEM key';
264
+ else authInfo = 'private key (unknown format)';
265
+ } else if (opts.password) {
266
+ authInfo = 'password';
267
+ }
268
+ console.log('SFTP - Config: ' + node.sftpConfig.id + ' -> ' + hostPort + ' as ' + user + ' (auth: ' + authInfo + ')');
269
+
256
270
  await client.connect(node.sftpConfig.options);
271
+ node.sftpConfig.ppkConverted = true;
257
272
  node.status({ fill: 'green', shape: 'ring', text: 'connected' });
258
273
  } catch (err) {
259
274
  node.status({ fill: 'red', shape: 'ring', text: 'connection failed.' });
275
+ console.error('SFTP - Connection error: ' + err.message);
276
+ if (err.level) console.error('SFTP - Error level: ' + err.level);
277
+ if (err.description) console.error('SFTP - Error description: ' + err.description);
278
+ if (err.code) console.error('SFTP - Error code: ' + err.code);
260
279
  done(err);
261
280
  console.error(err.message);
262
281
  return;
@@ -328,6 +347,23 @@ module.exports = function (RED) {
328
347
  console.error(err.message);
329
348
  }
330
349
  break;
350
+ case 'rename':
351
+ try{
352
+ let ftpfilename = node.workdir + node.filename;
353
+ let ftpnewfilename = node.workdir + node.newfilename;
354
+ if (msg.payload.filename) ftpfilename = msg.payload.filename;
355
+ if (msg.payload.newfilename) ftpnewfilename = msg.payload.newfilename;
356
+ console.log('SFTP Renaming File: ' + ftpfilename + ' to ' + ftpnewfilename);
357
+ await client.rename(ftpfilename, ftpnewfilename);
358
+ node.status({ fill: 'green', shape: 'dot', text: 'rename done' });
359
+ await client.end();
360
+ node.send(msg);
361
+ } catch (err) {
362
+ node.status({ fill: 'red', shape: 'ring', text: 'rename failed' });
363
+ done(err);
364
+ console.error(err.message);
365
+ }
366
+ break;
331
367
 
332
368
  case 'delete':
333
369
  try {
package/package.json CHANGED
@@ -1,12 +1,20 @@
1
1
  {
2
2
  "name": "@yousolution/node-red-contrib-you-ftp-sftp",
3
- "version": "1.11.0",
3
+ "version": "1.12.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": {
7
7
  "update": "npm pack && mv yousolution-node-red-contrib-you-ftp-sftp-$npm_package_version.tgz ./data && cd data && npm i yousolution-node-red-contrib-you-ftp-sftp-$npm_package_version.tgz && docker-compose restart",
8
8
  "test": "mocha 'test/**/*.spec.js'",
9
- "coverage": "nyc npm run test"
9
+ "coverage": "nyc npm run test",
10
+ "sftp:keygen": "mkdir -p sftp/keys && ssh-keygen -t rsa -b 4096 -m PEM -N \"\" -f sftp/keys/id_rsa",
11
+ "sftp:keygen:force": "rm -f sftp/keys/id_rsa sftp/keys/id_rsa.pub && npm run sftp:keygen",
12
+ "sftp:up": "docker compose up -d sftp",
13
+ "sftp:down": "docker compose stop sftp",
14
+ "sftp:logs": "docker compose logs -f sftp",
15
+ "ftps:up": "docker compose up -d ftps",
16
+ "ftps:down": "docker compose stop ftps",
17
+ "ftps:logs": "docker compose logs -f ftps"
10
18
  },
11
19
  "repository": {
12
20
  "type": "git",
@@ -34,6 +42,7 @@
34
42
  "homepage": "https://github.com/yousolution-cloud/node-red-contrib-you-ftp-sftp.git#readme",
35
43
  "dependencies": {
36
44
  "basic-ftp": "^5.0.3",
45
+ "ppk-to-openssh": "^3.2.0",
37
46
  "ssh2-sftp-client": "^9.1.0"
38
47
  },
39
48
  "devDependencies": {
File without changes
File without changes