node-red-contrib-ccu 4.7.2 → 4.9.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/CHANGELOG.md +60 -0
- package/README.md +9 -3
- package/nodes/ccu-connection.html +159 -2
- package/nodes/ccu-connection.js +261 -184
- package/nodes/lib/lite.js +20 -4
- package/nodes/lib/metaprovider.js +68 -3
- package/nodes/lib/pairing.js +305 -0
- package/nodes/lib/subscriptions.js +333 -0
- package/package.json +1 -1
|
@@ -24,6 +24,8 @@
|
|
|
24
24
|
const fs = require('fs');
|
|
25
25
|
const http = require('http');
|
|
26
26
|
const https = require('https');
|
|
27
|
+
const net = require('net');
|
|
28
|
+
const tls = require('tls');
|
|
27
29
|
|
|
28
30
|
const API_PATH = '/api/meta/v1';
|
|
29
31
|
|
|
@@ -210,28 +212,86 @@ function readLocalToken(file = LOCAL_TOKEN_FILE) {
|
|
|
210
212
|
}
|
|
211
213
|
}
|
|
212
214
|
|
|
215
|
+
/** a fingerprint as its bare lower-case hex, for comparing */
|
|
216
|
+
const bareFingerprint = (fingerprint) => String(fingerprint).replace(/:/g, '').toLowerCase();
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* A TLS connection that accepts exactly the certificate pinned at pairing (the system's own,
|
|
220
|
+
* usually self-signed): the socket goes to the request only after the handshake and the check, so
|
|
221
|
+
* nothing - the token least of all - is written to another server.
|
|
222
|
+
* @param {string} host
|
|
223
|
+
* @param {number} port
|
|
224
|
+
* @param {string} fingerprint256 colon-separated SHA-256, as pairing returns it
|
|
225
|
+
* @param {number} [timeout] for the connection and the handshake, ms (0: none)
|
|
226
|
+
* @returns {function(object, Function): undefined} for the request's createConnection
|
|
227
|
+
*/
|
|
228
|
+
function pinnedConnection(host, port, fingerprint256, timeout) {
|
|
229
|
+
const pin = bareFingerprint(fingerprint256);
|
|
230
|
+
const name = String(host).replace(/^\[|\]$/g, '');
|
|
231
|
+
return (_options, oncreate) => {
|
|
232
|
+
const socket = tls.connect({
|
|
233
|
+
host: name,
|
|
234
|
+
port,
|
|
235
|
+
servername: net.isIP(name) ? undefined : name,
|
|
236
|
+
rejectUnauthorized: false,
|
|
237
|
+
});
|
|
238
|
+
if (timeout > 0) {
|
|
239
|
+
// the request's own timeout starts only once it has the socket, after the handshake
|
|
240
|
+
socket.setTimeout(timeout, () => socket.destroy(new Error('timeout')));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
socket.once('secureConnect', () => {
|
|
244
|
+
socket.setTimeout(0);
|
|
245
|
+
const seen = socket.getPeerCertificate().fingerprint256;
|
|
246
|
+
if (bareFingerprint(seen) !== pin) {
|
|
247
|
+
socket.destroy();
|
|
248
|
+
oncreate(new Error('the system presents another certificate than the one pinned at pairing'));
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
oncreate(null, socket);
|
|
253
|
+
});
|
|
254
|
+
socket.once('error', (error) => oncreate(error));
|
|
255
|
+
return undefined;
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
213
259
|
/**
|
|
214
260
|
* One HTTP request against the metadata API.
|
|
215
261
|
* @param {object} options
|
|
216
262
|
* @returns {Promise<{status: number, headers: object, res: object, body: Promise<string>}>}
|
|
217
263
|
*/
|
|
218
264
|
function request(options) {
|
|
219
|
-
const {
|
|
265
|
+
const {
|
|
266
|
+
host,
|
|
267
|
+
port,
|
|
268
|
+
tls: useTls,
|
|
269
|
+
insecure,
|
|
270
|
+
fingerprint256,
|
|
271
|
+
path,
|
|
272
|
+
token,
|
|
273
|
+
timeout = 10000,
|
|
274
|
+
idleTimeout,
|
|
275
|
+
accept = 'application/json',
|
|
276
|
+
} = options;
|
|
220
277
|
return new Promise((resolve, reject) => {
|
|
221
278
|
const headers = {accept, 'user-agent': 'node-red-contrib-ccu'};
|
|
222
279
|
if (token) {
|
|
223
280
|
headers.authorization = 'Bearer ' + token;
|
|
224
281
|
}
|
|
225
282
|
|
|
226
|
-
const lib =
|
|
283
|
+
const lib = useTls ? https : http;
|
|
227
284
|
const requestOptions = {
|
|
228
285
|
host,
|
|
229
286
|
port,
|
|
230
287
|
path,
|
|
231
288
|
method: 'GET',
|
|
232
289
|
headers,
|
|
233
|
-
...(
|
|
290
|
+
...(useTls ? {rejectUnauthorized: !insecure} : {}),
|
|
234
291
|
};
|
|
292
|
+
if (useTls && fingerprint256) {
|
|
293
|
+
requestOptions.createConnection = pinnedConnection(host, port, fingerprint256, timeout);
|
|
294
|
+
}
|
|
235
295
|
if (timeout > 0) {
|
|
236
296
|
// as an option, not request.setTimeout(): only this form arms the
|
|
237
297
|
// timer before the socket is connected, so an unreachable box does
|
|
@@ -355,6 +415,7 @@ class MetaProvider {
|
|
|
355
415
|
this.port = options.port || (options.tls ? 443 : 80);
|
|
356
416
|
this.tls = Boolean(options.tls);
|
|
357
417
|
this.insecure = Boolean(options.insecure);
|
|
418
|
+
this.fingerprint256 = options.fingerprint256 || '';
|
|
358
419
|
this.token = options.token || '';
|
|
359
420
|
this.logger = options.logger || SILENT;
|
|
360
421
|
this.onNames = options.onNames || NOOP;
|
|
@@ -452,6 +513,7 @@ class MetaProvider {
|
|
|
452
513
|
port: this.port,
|
|
453
514
|
tls: this.tls,
|
|
454
515
|
insecure: this.insecure,
|
|
516
|
+
fingerprint256: this.fingerprint256,
|
|
455
517
|
logger: this.logger,
|
|
456
518
|
});
|
|
457
519
|
if (info) {
|
|
@@ -495,6 +557,7 @@ class MetaProvider {
|
|
|
495
557
|
port: this.port,
|
|
496
558
|
tls: this.tls,
|
|
497
559
|
insecure: this.insecure,
|
|
560
|
+
fingerprint256: this.fingerprint256,
|
|
498
561
|
path: API_PATH + '/snapshot',
|
|
499
562
|
token: this.token,
|
|
500
563
|
timeout: this.requestTimeout,
|
|
@@ -578,6 +641,7 @@ class MetaProvider {
|
|
|
578
641
|
port: this.port,
|
|
579
642
|
tls: this.tls,
|
|
580
643
|
insecure: this.insecure,
|
|
644
|
+
fingerprint256: this.fingerprint256,
|
|
581
645
|
path: API_PATH + '/events/sse?since=' + this.revision,
|
|
582
646
|
token: this.token,
|
|
583
647
|
accept: 'text/event-stream',
|
|
@@ -738,6 +802,7 @@ module.exports = {
|
|
|
738
802
|
detectOutcome,
|
|
739
803
|
flattenEnum,
|
|
740
804
|
memberNames,
|
|
805
|
+
pinnedConnection,
|
|
741
806
|
readLocalToken,
|
|
742
807
|
refToAddress,
|
|
743
808
|
refToIface,
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/* Pairing with an openccu-lite system from the connection node's dialog.
|
|
2
|
+
|
|
3
|
+
A Node-RED that runs elsewhere asks the system for a token instead of
|
|
4
|
+
having one pasted: the dialog sends the address, the runtime finds out
|
|
5
|
+
what answers there (occulite-client's probe - it follows the system's
|
|
6
|
+
redirect to https), asks for access (occulite-client's pair) and shows
|
|
7
|
+
the six-digit code the system's Status page shows too. An administrator
|
|
8
|
+
approves it there; the dialog then gets the token and the fingerprint of
|
|
9
|
+
the certificate the code was bound to, fills the credentials and the user
|
|
10
|
+
deploys. The connection pins that certificate from then on.
|
|
11
|
+
|
|
12
|
+
Node-RED on the system itself (RedMatic on openccu-lite) needs none of
|
|
13
|
+
this: the connection configures itself from the addon's own token, so a
|
|
14
|
+
pairing request for a local connection is refused.
|
|
15
|
+
|
|
16
|
+
One request per id, held in memory: the dialog polls it, and its token is
|
|
17
|
+
handed out once. A request nobody polls any more is withdrawn. */
|
|
18
|
+
|
|
19
|
+
const crypto = require('crypto');
|
|
20
|
+
const os = require('os');
|
|
21
|
+
|
|
22
|
+
/** what the connection asks for: devices up to pairing and deleting them, the names read */
|
|
23
|
+
const ACCESS = {devices: 'administer', names: 'read'};
|
|
24
|
+
const PURPOSE = {
|
|
25
|
+
devices: 'Node-RED flows read and set datapoints; the rpc node may call any method of the interfaces',
|
|
26
|
+
names: 'channel names, rooms and functions in the nodes and their messages',
|
|
27
|
+
};
|
|
28
|
+
const APP = 'node-red-contrib-ccu';
|
|
29
|
+
|
|
30
|
+
/** the system lets a request expire after five minutes; a little longer here, so its own answer comes first */
|
|
31
|
+
const PAIR_TIMEOUT_MS = 6 * 60 * 1000;
|
|
32
|
+
/** a request nobody polled for this long is withdrawn (the dialog was closed) */
|
|
33
|
+
const ABANDON_MS = 60 * 1000;
|
|
34
|
+
/** a settled request is kept this long for the dialog's last poll */
|
|
35
|
+
const KEEP_MS = 2 * 60 * 1000;
|
|
36
|
+
|
|
37
|
+
/** occulite-client's error codes and what the dialog says about them */
|
|
38
|
+
const MESSAGES = {
|
|
39
|
+
rejected: 'The administrator rejected the request (or chose another code).',
|
|
40
|
+
expired: 'Nobody approved the request within five minutes.',
|
|
41
|
+
timeout: 'Nobody approved the request in time.',
|
|
42
|
+
aborted: 'Pairing was cancelled.',
|
|
43
|
+
'pairing-off': 'Pairing is switched off on the system ("Allow programs to ask for access" on its API tokens page).',
|
|
44
|
+
'not-local':
|
|
45
|
+
'The system accepts pairing requests only from its local networks - this Node-RED is outside them (see the firewall settings of the system).',
|
|
46
|
+
limit: 'The system refuses more pairing requests from this address for now - wait ten minutes and try again.',
|
|
47
|
+
certificate: 'The certificate of the system cannot be checked.',
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The address the probe starts from: the host as entered, with the scheme and port the dialog
|
|
52
|
+
* names; without a port the probe goes to http and follows the system's redirect to https.
|
|
53
|
+
* @param {object} options
|
|
54
|
+
* @param {string} options.host
|
|
55
|
+
* @param {boolean} [options.tls]
|
|
56
|
+
* @param {string|number} [options.port]
|
|
57
|
+
* @returns {string}
|
|
58
|
+
*/
|
|
59
|
+
function addressOf({host, tls, port}) {
|
|
60
|
+
const name = String(host).trim();
|
|
61
|
+
const bracketed = name.includes(':') && !name.startsWith('[') ? '[' + name + ']' : name;
|
|
62
|
+
const portNumber = Number.parseInt(port, 10);
|
|
63
|
+
if (portNumber > 0) {
|
|
64
|
+
return (tls ? 'https' : 'http') + '://' + bracketed + ':' + portNumber;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return tls ? 'https://' + bracketed : bracketed;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The connection node's settings for the system's URL: TLS and the port when it is not the default.
|
|
72
|
+
* @param {string} url scheme and authority
|
|
73
|
+
* @returns {{tls: boolean, port: string}}
|
|
74
|
+
*/
|
|
75
|
+
function settingsOf(url) {
|
|
76
|
+
const parsed = new URL(url);
|
|
77
|
+
return {tls: parsed.protocol === 'https:', port: parsed.port};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The dialog's message for an error of occulite-client.
|
|
82
|
+
* @param {*} error
|
|
83
|
+
* @returns {{error: string, message: string}}
|
|
84
|
+
*/
|
|
85
|
+
function describeError(error) {
|
|
86
|
+
const code = (error && error.name === 'OccuLiteError' && error.code) || (error && error.kind) || 'failed';
|
|
87
|
+
const message = MESSAGES[code] || 'Pairing failed: ' + ((error && error.message) || String(error));
|
|
88
|
+
return {error: code, message};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
class Pairings {
|
|
92
|
+
/**
|
|
93
|
+
* @param {object} options
|
|
94
|
+
* @param {function(): {pair: Function, probe: Function}} options.load occulite-client's pair and probe
|
|
95
|
+
* @param {function(string): boolean} options.isLocal whether a host is the system Node-RED runs on
|
|
96
|
+
* @param {string} [options.appVersion]
|
|
97
|
+
* @param {string} [options.instance] what the system's Status page names this Node-RED by
|
|
98
|
+
* @param {object} [options.logger] {info, warn}
|
|
99
|
+
* @param {number} [options.pairTimeout]
|
|
100
|
+
* @param {number} [options.abandonAfter]
|
|
101
|
+
* @param {number} [options.keepFor]
|
|
102
|
+
*/
|
|
103
|
+
constructor(options) {
|
|
104
|
+
this.load = options.load;
|
|
105
|
+
this.isLocal = options.isLocal;
|
|
106
|
+
this.appVersion = options.appVersion;
|
|
107
|
+
this.instance = options.instance || os.hostname();
|
|
108
|
+
this.logger = options.logger || {info() {}, warn() {}};
|
|
109
|
+
this.pairTimeout = options.pairTimeout || PAIR_TIMEOUT_MS;
|
|
110
|
+
this.abandonAfter = options.abandonAfter || ABANDON_MS;
|
|
111
|
+
this.keepFor = options.keepFor || KEEP_MS;
|
|
112
|
+
this.requests = new Map();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Starts a pairing. Throws (with `status`) on what is refused at once: no address, a local
|
|
117
|
+
* connection, occulite-client not loadable.
|
|
118
|
+
* @param {object} options
|
|
119
|
+
* @param {string} options.host the address in the dialog
|
|
120
|
+
* @param {boolean} [options.tls]
|
|
121
|
+
* @param {string|number} [options.port] the openccu-lite port of the dialog
|
|
122
|
+
* @returns {string} the request's id
|
|
123
|
+
*/
|
|
124
|
+
start({host, tls, port}) {
|
|
125
|
+
if (!host || !String(host).trim()) {
|
|
126
|
+
throw Object.assign(new Error('Enter the address of the system first.'), {status: 400, code: 'host'});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (this.isLocal(String(host).trim())) {
|
|
130
|
+
throw Object.assign(
|
|
131
|
+
new Error(
|
|
132
|
+
'Node-RED runs on the system itself: the connection configures itself, no pairing is needed.',
|
|
133
|
+
),
|
|
134
|
+
{status: 409, code: 'local'},
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let modules;
|
|
139
|
+
try {
|
|
140
|
+
modules = this.load();
|
|
141
|
+
} catch (error) {
|
|
142
|
+
throw Object.assign(new Error(error.message), {status: 500, code: 'unavailable'});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const id = crypto.randomBytes(12).toString('hex');
|
|
146
|
+
const request = {
|
|
147
|
+
id,
|
|
148
|
+
state: 'probing',
|
|
149
|
+
controller: new AbortController(),
|
|
150
|
+
polled: Date.now(),
|
|
151
|
+
timer: null,
|
|
152
|
+
};
|
|
153
|
+
this.requests.set(id, request);
|
|
154
|
+
this.watch(request);
|
|
155
|
+
this.run(request, modules, addressOf({host, tls, port}));
|
|
156
|
+
return id;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The probe, then the pairing; the request's state follows.
|
|
161
|
+
* @param {object} request
|
|
162
|
+
* @param {{pair: Function, probe: Function}} modules
|
|
163
|
+
* @param {string} address
|
|
164
|
+
* @returns {Promise<void>}
|
|
165
|
+
*/
|
|
166
|
+
async run(request, {pair, probe}, address) {
|
|
167
|
+
const {signal} = request.controller;
|
|
168
|
+
try {
|
|
169
|
+
const found = await probe(address, {timeout: 5000, signal});
|
|
170
|
+
if (found.kind !== 'openccu-lite') {
|
|
171
|
+
const message =
|
|
172
|
+
found.kind === 'ccu'
|
|
173
|
+
? 'This is not an openccu-lite system. A CCU pairs no programs - enter its credentials under Authentication instead.'
|
|
174
|
+
: found.reason === 'certificate'
|
|
175
|
+
? MESSAGES.certificate
|
|
176
|
+
: 'The system does not answer' + (found.reason ? ' (' + found.reason + ').' : '.');
|
|
177
|
+
this.settle(request, {
|
|
178
|
+
state: 'failed',
|
|
179
|
+
error: found.kind === 'ccu' ? 'not-openccu-lite' : 'unreachable',
|
|
180
|
+
message,
|
|
181
|
+
});
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const capabilities = (found.info && found.info.capabilities) || {};
|
|
186
|
+
if (capabilities.pairing === false) {
|
|
187
|
+
this.settle(request, {state: 'failed', error: 'pairing-off', message: MESSAGES['pairing-off']});
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
request.url = found.url;
|
|
192
|
+
request.state = 'requesting';
|
|
193
|
+
const paired = await pair({
|
|
194
|
+
url: found.url,
|
|
195
|
+
app: APP,
|
|
196
|
+
appVersion: this.appVersion,
|
|
197
|
+
instance: this.instance,
|
|
198
|
+
access: ACCESS,
|
|
199
|
+
purpose: PURPOSE,
|
|
200
|
+
signal,
|
|
201
|
+
timeout: this.pairTimeout,
|
|
202
|
+
onCode: (code) => {
|
|
203
|
+
request.code = code;
|
|
204
|
+
request.state = 'waiting';
|
|
205
|
+
this.logger.info('pairing with ' + found.url + ': code ' + code + ', waiting for the approval');
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
this.logger.info('paired with ' + found.url + ' as ' + paired.name);
|
|
209
|
+
this.settle(request, {
|
|
210
|
+
state: 'approved',
|
|
211
|
+
token: paired.token,
|
|
212
|
+
fingerprint256: paired.fingerprint256 || '',
|
|
213
|
+
name: paired.name,
|
|
214
|
+
scopes: paired.scopes,
|
|
215
|
+
...settingsOf(found.url),
|
|
216
|
+
});
|
|
217
|
+
} catch (error) {
|
|
218
|
+
const {error: code, message} = describeError(error);
|
|
219
|
+
if (code !== 'aborted') {
|
|
220
|
+
this.logger.warn('pairing with ' + (request.url || address) + ' failed: ' + message);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
this.settle(request, {state: code === 'aborted' ? 'cancelled' : 'failed', error: code, message});
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Withdraws a request the dialog stopped polling.
|
|
229
|
+
* @param {object} request
|
|
230
|
+
*/
|
|
231
|
+
watch(request) {
|
|
232
|
+
request.timer = setInterval(
|
|
233
|
+
() => {
|
|
234
|
+
if (Date.now() - request.polled > this.abandonAfter) {
|
|
235
|
+
this.cancel(request.id);
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
Math.min(this.abandonAfter, 10000),
|
|
239
|
+
);
|
|
240
|
+
request.timer.unref();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The outcome; the request stays for the dialog's last poll.
|
|
245
|
+
* @param {object} request
|
|
246
|
+
* @param {object} outcome
|
|
247
|
+
*/
|
|
248
|
+
settle(request, outcome) {
|
|
249
|
+
clearInterval(request.timer);
|
|
250
|
+
Object.assign(request, outcome);
|
|
251
|
+
request.timer = setTimeout(() => this.requests.delete(request.id), this.keepFor);
|
|
252
|
+
request.timer.unref();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* The dialog's view of a request; the token is in it once.
|
|
257
|
+
* @param {string} id
|
|
258
|
+
* @returns {object|undefined}
|
|
259
|
+
*/
|
|
260
|
+
get(id) {
|
|
261
|
+
const request = this.requests.get(id);
|
|
262
|
+
if (!request) {
|
|
263
|
+
return undefined;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
request.polled = Date.now();
|
|
267
|
+
const view = {state: request.state};
|
|
268
|
+
['code', 'url', 'error', 'message', 'name', 'fingerprint256', 'tls', 'port', 'token'].forEach((key) => {
|
|
269
|
+
if (request[key] !== undefined) {
|
|
270
|
+
view[key] = request[key];
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
if (request.state === 'approved' || request.state === 'failed' || request.state === 'cancelled') {
|
|
274
|
+
clearTimeout(request.timer);
|
|
275
|
+
this.requests.delete(request.id);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return view;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Withdraws a request (the dialog was closed or its button pressed again).
|
|
283
|
+
* @param {string} id
|
|
284
|
+
* @returns {boolean} whether there was one
|
|
285
|
+
*/
|
|
286
|
+
cancel(id) {
|
|
287
|
+
const request = this.requests.get(id);
|
|
288
|
+
if (!request) {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
clearInterval(request.timer);
|
|
293
|
+
clearTimeout(request.timer);
|
|
294
|
+
this.requests.delete(id);
|
|
295
|
+
request.controller.abort();
|
|
296
|
+
return true;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Withdraws every request (Node-RED stops). */
|
|
300
|
+
close() {
|
|
301
|
+
[...this.requests.keys()].forEach((id) => this.cancel(id));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
module.exports = {Pairings, addressOf, settingsOf, describeError, ACCESS, APP, MESSAGES};
|