@iobroker/socket-classes 1.4.2 → 1.4.4
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 +7 -1
- package/lib/socketCommands.js +327 -278
- package/lib/socketCommandsAdmin.js +388 -242
- package/lib/socketCommon.js +76 -52
- package/package.json +6 -5
package/lib/socketCommon.js
CHANGED
|
@@ -11,13 +11,13 @@ class SocketCommon {
|
|
|
11
11
|
static COMMAND_RE_AUTHENTICATE = 'reauthenticate';
|
|
12
12
|
|
|
13
13
|
constructor(settings, adapter) {
|
|
14
|
-
this.settings
|
|
15
|
-
this.adapter
|
|
16
|
-
this.commands
|
|
14
|
+
this.settings = settings || {};
|
|
15
|
+
this.adapter = adapter;
|
|
16
|
+
this.commands = null;
|
|
17
17
|
this.noDisconnect = this.__getIsNoDisconnect();
|
|
18
|
-
this.infoTimeout
|
|
18
|
+
this.infoTimeout = null;
|
|
19
19
|
this.eventHandlers = {};
|
|
20
|
-
this.store
|
|
20
|
+
this.store = null; // will be set in __initAuthentication
|
|
21
21
|
this.adapter._language = this.settings.language;
|
|
22
22
|
}
|
|
23
23
|
|
|
@@ -81,7 +81,9 @@ class SocketCommon {
|
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
server.__inited = true;
|
|
84
|
-
this.adapter.log.info(
|
|
84
|
+
this.adapter.log.info(
|
|
85
|
+
`${this.settings.secure ? 'Secure ' : ''}socket.io server listening on port ${this.settings.port}`
|
|
86
|
+
);
|
|
85
87
|
}
|
|
86
88
|
|
|
87
89
|
if (this.settings.auth && this.server) {
|
|
@@ -99,36 +101,51 @@ class SocketCommon {
|
|
|
99
101
|
this._initSocket(socket, cb);
|
|
100
102
|
});
|
|
101
103
|
// support of dynamic namespaces (because of reverse proxy)
|
|
102
|
-
this.allNamespaces &&
|
|
103
|
-
this.
|
|
104
|
-
|
|
105
|
-
|
|
104
|
+
this.allNamespaces &&
|
|
105
|
+
this.allNamespaces.on('connection', (socket, cb) => {
|
|
106
|
+
this.eventHandlers.connect && this.eventHandlers.connect(socket);
|
|
107
|
+
this._initSocket(socket, cb);
|
|
108
|
+
});
|
|
106
109
|
}
|
|
107
110
|
|
|
108
111
|
this.server.on('error', (error, details) => {
|
|
109
112
|
// ignore "failed connection" as it already shown
|
|
110
113
|
if (!error || !error.message || !error.message.includes('failed connection')) {
|
|
111
|
-
if (
|
|
112
|
-
(
|
|
114
|
+
if (
|
|
115
|
+
(error && error.message && error.message.includes('authentication failed')) ||
|
|
116
|
+
(details && details.toString().includes('authentication failed'))
|
|
113
117
|
) {
|
|
114
|
-
this.adapter.log.debug(
|
|
118
|
+
this.adapter.log.debug(
|
|
119
|
+
`Error: ${(error && error.message) || JSON.stringify(error)}${details ? ` - ${details}` : ''}`
|
|
120
|
+
);
|
|
115
121
|
} else {
|
|
116
|
-
this.adapter.log.error(
|
|
122
|
+
this.adapter.log.error(
|
|
123
|
+
`Error: ${(error && error.message) || JSON.stringify(error)}${details ? ` - ${details}` : ''}`
|
|
124
|
+
);
|
|
117
125
|
}
|
|
118
126
|
}
|
|
119
127
|
});
|
|
120
128
|
|
|
121
129
|
// support of dynamic namespaces (because of reverse proxy)
|
|
122
|
-
this.allNamespaces &&
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
if (error
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
130
|
+
this.allNamespaces &&
|
|
131
|
+
this.allNamespaces.on('error', (error, details) => {
|
|
132
|
+
// ignore "failed connection" as it already shown
|
|
133
|
+
if (!error || !error.message || !error.message.includes('failed connection')) {
|
|
134
|
+
if (error && error.message && error.message.includes('authentication failed')) {
|
|
135
|
+
this.adapter.log.debug(
|
|
136
|
+
`Error: ${(error && error.message) || JSON.stringify(error)}${
|
|
137
|
+
details ? ` - ${details}` : ''
|
|
138
|
+
}`
|
|
139
|
+
);
|
|
140
|
+
} else {
|
|
141
|
+
this.adapter.log.error(
|
|
142
|
+
`Error: ${(error && error.message) || JSON.stringify(error)}${
|
|
143
|
+
details ? ` - ${details}` : ''
|
|
144
|
+
}`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
129
147
|
}
|
|
130
|
-
}
|
|
131
|
-
});
|
|
148
|
+
});
|
|
132
149
|
|
|
133
150
|
this._updateConnectedInfo();
|
|
134
151
|
}
|
|
@@ -160,10 +177,14 @@ class SocketCommon {
|
|
|
160
177
|
}
|
|
161
178
|
});
|
|
162
179
|
} else {
|
|
163
|
-
this.adapter.calculatePermissions(
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
180
|
+
this.adapter.calculatePermissions(
|
|
181
|
+
this.settings.defaultUser,
|
|
182
|
+
SocketCommands.COMMANDS_PERMISSIONS,
|
|
183
|
+
acl => {
|
|
184
|
+
socket._acl = SocketCommon._mergeACLs(address, acl, this.settings.whiteListSettings);
|
|
185
|
+
this._socketEvents(socket, address, cb);
|
|
186
|
+
}
|
|
187
|
+
);
|
|
167
188
|
}
|
|
168
189
|
} else {
|
|
169
190
|
this._socketEvents(socket, address, cb);
|
|
@@ -179,17 +200,15 @@ class SocketCommon {
|
|
|
179
200
|
// this could be an object or array
|
|
180
201
|
const sockets = this.server.sockets.sockets || this.server.sockets.connected;
|
|
181
202
|
|
|
182
|
-
Object.keys(sockets).forEach(i =>
|
|
183
|
-
|
|
184
|
-
} else
|
|
185
|
-
if (this.server && this.server.sockets) {
|
|
203
|
+
Object.keys(sockets).forEach(i => this.commands.unsubscribeSocket(sockets[i]));
|
|
204
|
+
} else if (this.server && this.server.sockets) {
|
|
186
205
|
for (const socket in this.server.sockets) {
|
|
187
206
|
if (Object.prototype.hasOwnProperty.call(this.server.sockets, socket)) {
|
|
188
207
|
this.commands.unsubscribeSocket(socket);
|
|
189
208
|
}
|
|
190
209
|
}
|
|
191
210
|
}
|
|
192
|
-
}
|
|
211
|
+
}
|
|
193
212
|
|
|
194
213
|
static getWhiteListIpForAddress(address, whiteList) {
|
|
195
214
|
if (!whiteList) {
|
|
@@ -243,7 +262,10 @@ class SocketCommon {
|
|
|
243
262
|
const whiteListAcl = SocketCommon._getPermissionsForIp(address, whiteList);
|
|
244
263
|
if (whiteListAcl) {
|
|
245
264
|
['object', 'state', 'file'].forEach(key => {
|
|
246
|
-
if (
|
|
265
|
+
if (
|
|
266
|
+
Object.prototype.hasOwnProperty.call(acl, key) &&
|
|
267
|
+
Object.prototype.hasOwnProperty.call(whiteListAcl, key)
|
|
268
|
+
) {
|
|
247
269
|
Object.keys(acl[key]).forEach(permission => {
|
|
248
270
|
if (Object.prototype.hasOwnProperty.call(whiteListAcl[key], permission)) {
|
|
249
271
|
acl[key][permission] = acl[key][permission] && whiteListAcl[key][permission];
|
|
@@ -309,7 +331,9 @@ class SocketCommon {
|
|
|
309
331
|
if (this.eventHandlers.disconnect) {
|
|
310
332
|
this.eventHandlers.disconnect(socket, error);
|
|
311
333
|
} else {
|
|
312
|
-
this.adapter.log.info(
|
|
334
|
+
this.adapter.log.info(
|
|
335
|
+
`<== Disconnect ${socket._acl.user} from ${this.__getClientAddress(socket)} ${socket._name || ''}`
|
|
336
|
+
);
|
|
313
337
|
}
|
|
314
338
|
});
|
|
315
339
|
|
|
@@ -321,23 +345,24 @@ class SocketCommon {
|
|
|
321
345
|
if (this.serverMode) {
|
|
322
346
|
const sessionId = this.__getSessionID(socket);
|
|
323
347
|
if (sessionId) {
|
|
324
|
-
socket._secure
|
|
348
|
+
socket._secure = true;
|
|
325
349
|
socket._sessionID = sessionId;
|
|
326
350
|
// Get user for session
|
|
327
|
-
this.store &&
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
351
|
+
this.store &&
|
|
352
|
+
this.store.get(socket._sessionID, (err, obj) => {
|
|
353
|
+
if (!obj || !obj.passport) {
|
|
354
|
+
socket._acl.user = '';
|
|
355
|
+
socket.emit(SocketCommon.COMMAND_RE_AUTHENTICATE);
|
|
356
|
+
// ws does not require disconnect
|
|
357
|
+
if (!this.noDisconnect) {
|
|
358
|
+
socket.disconnect();
|
|
359
|
+
}
|
|
334
360
|
}
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
}
|
|
340
|
-
});
|
|
361
|
+
if (socket._authPending) {
|
|
362
|
+
socket._authPending(!!socket._acl.user, true);
|
|
363
|
+
delete socket._authPending;
|
|
364
|
+
}
|
|
365
|
+
});
|
|
341
366
|
}
|
|
342
367
|
}
|
|
343
368
|
|
|
@@ -357,13 +382,12 @@ class SocketCommon {
|
|
|
357
382
|
this.infoTimeout = null;
|
|
358
383
|
|
|
359
384
|
if (this.server) {
|
|
360
|
-
|
|
385
|
+
const clientsArray = [];
|
|
361
386
|
if (this.server.sockets) {
|
|
362
387
|
// this could be an object or array
|
|
363
388
|
const sockets = this.server.sockets.sockets || this.server.sockets.connected;
|
|
364
389
|
|
|
365
|
-
Object.keys(sockets).forEach(i =>
|
|
366
|
-
clientsArray.push(sockets[i]._name || 'noname'));
|
|
390
|
+
Object.keys(sockets).forEach(i => clientsArray.push(sockets[i]._name || 'noname'));
|
|
367
391
|
}
|
|
368
392
|
const text = `[${clientsArray.length}]${clientsArray.join(', ')}`;
|
|
369
393
|
this.adapter.setState('info.connected', text, true);
|
|
@@ -421,7 +445,7 @@ class SocketCommon {
|
|
|
421
445
|
try {
|
|
422
446
|
this.server && this.server.close && this.server.close();
|
|
423
447
|
this.server = null;
|
|
424
|
-
} catch
|
|
448
|
+
} catch {
|
|
425
449
|
// ignore
|
|
426
450
|
}
|
|
427
451
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@iobroker/socket-classes",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.4",
|
|
4
4
|
"description": "ioBroker server-side web sockets",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "bluefox",
|
|
@@ -22,16 +22,17 @@
|
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@iobroker/adapter-core": "^3.0.3",
|
|
25
|
-
"
|
|
25
|
+
"axios": "^1.5.1",
|
|
26
26
|
"cookie-parser": "^1.4.6",
|
|
27
|
-
"
|
|
27
|
+
"passport": "^0.6.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@alcalzone/release-script": "^3.6.0",
|
|
31
31
|
"@alcalzone/release-script-plugin-license": "^3.5.9",
|
|
32
|
-
"
|
|
33
|
-
"gulp": "^4.0.2",
|
|
32
|
+
"@foxriver76/eslint-config": "^1.0.1",
|
|
34
33
|
"chai": "^4.3.10",
|
|
34
|
+
"gulp": "^4.0.2",
|
|
35
|
+
"iobroker.js-controller": "dev",
|
|
35
36
|
"mocha": "^10.2.0"
|
|
36
37
|
},
|
|
37
38
|
"bugs": {
|