@iobroker/socket-classes 0.4.12 → 0.5.2
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 +6 -0
- package/lib/socketCommands.js +325 -154
- package/lib/socketCommandsAdmin.js +344 -217
- package/package.json +1 -1
|
@@ -95,7 +95,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
95
95
|
|
|
96
96
|
_readInstanceConfig(id, user, isTab, configs) {
|
|
97
97
|
return new Promise(resolve =>
|
|
98
|
-
this.adapter.getForeignObject('system.adapter.' + id, {user}, (
|
|
98
|
+
this.adapter.getForeignObject('system.adapter.' + id, {user}, (error, obj) => {
|
|
99
99
|
if (obj && obj.common) {
|
|
100
100
|
const instance = id.split('.').pop();
|
|
101
101
|
const config = {
|
|
@@ -136,25 +136,30 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
138
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
139
|
+
try {
|
|
140
|
+
this.adapter.sendToHost(host, command, message, res => {
|
|
141
|
+
if (!message && SocketCommandsAdmin.ALLOW_CACHE.includes(command)) {
|
|
142
|
+
this.cache[hash] = {ts: Date.now(), res: JSON.stringify(res)};
|
|
143
|
+
|
|
144
|
+
this.cacheGB = this.cacheGB || setInterval(() => {
|
|
145
|
+
const commands = Object.keys(this.cache);
|
|
146
|
+
commands.forEach(cmd => {
|
|
147
|
+
if (Date.now() - this.cache[cmd].ts > 500) {
|
|
148
|
+
delete this.cache[cmd];
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
if (!commands.length) {
|
|
152
|
+
clearInterval(this.cacheGB);
|
|
153
|
+
this.cacheGB = null;
|
|
148
154
|
}
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
});
|
|
155
|
+
}, 2000);
|
|
156
|
+
}
|
|
157
|
+
typeof callback === 'function' && setImmediate(() => callback(res));
|
|
158
|
+
});
|
|
159
|
+
} catch (error) {
|
|
160
|
+
this.adapter.log.error('[sendToHost] ERROR: ' + error.toString());
|
|
161
|
+
typeof callback === 'function' && setImmediate(() => callback({ error }));
|
|
162
|
+
}
|
|
158
163
|
}
|
|
159
164
|
|
|
160
165
|
// remove this function when js.controller 4.x will be mainstream
|
|
@@ -173,13 +178,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
173
178
|
}
|
|
174
179
|
return response.data;
|
|
175
180
|
})
|
|
176
|
-
.catch(
|
|
177
|
-
if (
|
|
178
|
-
throw new Error((
|
|
179
|
-
} else if (
|
|
181
|
+
.catch(error => {
|
|
182
|
+
if (error.response) {
|
|
183
|
+
throw new Error((error.response.data && error.response.data.error) || error.response.data || error.response.status);
|
|
184
|
+
} else if (error.request) {
|
|
180
185
|
throw new Error('no response');
|
|
181
186
|
} else {
|
|
182
|
-
throw
|
|
187
|
+
throw error;
|
|
183
188
|
}
|
|
184
189
|
});
|
|
185
190
|
}
|
|
@@ -229,22 +234,22 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
229
234
|
return this.adapter.setForeignObjectAsync('system.licenses', systemLicenses)
|
|
230
235
|
.then(() => licenses);
|
|
231
236
|
})
|
|
232
|
-
.catch(
|
|
237
|
+
.catch(error => {
|
|
233
238
|
// if password is invalid
|
|
234
|
-
if (
|
|
239
|
+
if (error.message.includes('Authentication required') || error.message.includes('Cannot decode password')) {
|
|
235
240
|
// clear existing licenses if exist
|
|
236
241
|
if (systemLicenses && systemLicenses.native && systemLicenses.native.licenses && systemLicenses.native.licenses.length) {
|
|
237
242
|
systemLicenses.native.licenses = [];
|
|
238
243
|
systemLicenses.native.readTime = new Date().toISOString();
|
|
239
244
|
return this.adapter.setForeignObjectAsync('system.licenses', systemLicenses)
|
|
240
245
|
.then(() => {
|
|
241
|
-
throw
|
|
246
|
+
throw error;
|
|
242
247
|
});
|
|
243
248
|
} else {
|
|
244
|
-
throw
|
|
249
|
+
throw error;
|
|
245
250
|
}
|
|
246
251
|
} else {
|
|
247
|
-
throw
|
|
252
|
+
throw error;
|
|
248
253
|
}
|
|
249
254
|
});
|
|
250
255
|
} else {
|
|
@@ -275,7 +280,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
275
280
|
setTimeout(() => {
|
|
276
281
|
/*
|
|
277
282
|
if (readAll) {
|
|
278
|
-
this.adapter.getForeignStates('*', (
|
|
283
|
+
this.adapter.getForeignStates('*', (error, res) => {
|
|
279
284
|
this.adapter.log.info('received all states');
|
|
280
285
|
for (const id in res) {
|
|
281
286
|
if (res.hasOwnProperty(id) && JSON.stringify(states[id]) !== JSON.stringify(res[id])) {
|
|
@@ -322,50 +327,66 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
322
327
|
}
|
|
323
328
|
|
|
324
329
|
if (!user.match(/^[-.A-Za-züäößÖÄÜа-яА-Я@+$§0-9=?!&# ]+$/)) {
|
|
325
|
-
|
|
326
|
-
callback('Invalid characters in the name. Only following special characters are allowed: -@+$§=?!&# and letters');
|
|
327
|
-
}
|
|
328
|
-
return;
|
|
330
|
+
return SocketCommands._fixCallback(callback, 'Invalid characters in the name. Only following special characters are allowed: -@+$§=?!&# and letters');
|
|
329
331
|
}
|
|
330
332
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
if (
|
|
334
|
-
callback
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
333
|
+
try {
|
|
334
|
+
this.adapter.getForeignObject('system.user.' + user, options, (error, obj) => {
|
|
335
|
+
if (obj) {
|
|
336
|
+
SocketCommands._fixCallback(callback, 'User yet exists');
|
|
337
|
+
} else {
|
|
338
|
+
try {
|
|
339
|
+
this.adapter.setForeignObject('system.user.' + user, {
|
|
340
|
+
type: 'user',
|
|
341
|
+
common: {
|
|
342
|
+
name: user,
|
|
343
|
+
enabled: true,
|
|
344
|
+
groups: []
|
|
345
|
+
}
|
|
346
|
+
}, options, () => {
|
|
347
|
+
try {
|
|
348
|
+
this.adapter.setPassword(user, pw, options, callback);
|
|
349
|
+
} catch (error) {
|
|
350
|
+
this.adapter.log.error('[_addUser] cannot set password: ' + error.toString());
|
|
351
|
+
SocketCommands._fixCallback(callback, error);
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
} catch (error) {
|
|
355
|
+
this.adapter.log.error('[_addUser] cannot save user: ' + error.toString());
|
|
356
|
+
SocketCommands._fixCallback(callback, error);
|
|
343
357
|
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
} catch (error) {
|
|
361
|
+
this.adapter.log.error('[_addUser] cannot read user: ' + error.toString());
|
|
362
|
+
SocketCommands._fixCallback(callback, error);
|
|
363
|
+
}
|
|
349
364
|
}
|
|
350
365
|
|
|
351
366
|
_delUser(user, options, callback) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
if (
|
|
355
|
-
callback
|
|
356
|
-
}
|
|
357
|
-
} else {
|
|
358
|
-
if (obj.common.dontDelete) {
|
|
359
|
-
if (typeof callback === 'function') {
|
|
360
|
-
callback('Cannot delete user, while is system user');
|
|
361
|
-
}
|
|
367
|
+
try {
|
|
368
|
+
this.adapter.getForeignObject('system.user.' + user, options, (error, obj) => {
|
|
369
|
+
if (error || !obj) {
|
|
370
|
+
SocketCommands._fixCallback(callback, 'User does not exist');
|
|
362
371
|
} else {
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
372
|
+
if (obj.common.dontDelete) {
|
|
373
|
+
SocketCommands._fixCallback(callback, 'Cannot delete user, while is system user');
|
|
374
|
+
} else {
|
|
375
|
+
try {
|
|
376
|
+
this.adapter.delForeignObject('system.user.' + user, options, error =>
|
|
377
|
+
// Remove this user from all groups in web client
|
|
378
|
+
SocketCommands._fixCallback(callback, error));
|
|
379
|
+
} catch (error) {
|
|
380
|
+
this.adapter.log.error('[_delUser] cannot delete user: ' + error.toString());
|
|
381
|
+
SocketCommands._fixCallback(callback, error);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
366
384
|
}
|
|
367
|
-
}
|
|
368
|
-
})
|
|
385
|
+
});
|
|
386
|
+
} catch (error) {
|
|
387
|
+
this.adapter.log.error('[_delUser] cannot read user: ' + error.toString());
|
|
388
|
+
SocketCommands._fixCallback(callback, error);
|
|
389
|
+
}
|
|
369
390
|
}
|
|
370
391
|
|
|
371
392
|
_addGroup(group, desc, acl, options, callback) {
|
|
@@ -388,44 +409,63 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
388
409
|
group = group.substring(0, 1).toLowerCase() + group.substring(1);
|
|
389
410
|
|
|
390
411
|
if (!group.match(/^[-.A-Za-züäößÖÄÜа-яА-Я@+$§0-9=?!&#_ ]+$/)) {
|
|
391
|
-
return
|
|
412
|
+
return SocketCommands._fixCallback(callback, 'Invalid characters in the group name. Only following special characters are allowed: -@+$§=?!&# and letters');
|
|
392
413
|
}
|
|
393
414
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
415
|
+
try {
|
|
416
|
+
this.adapter.getForeignObject('system.group.' + group, options, (error, obj) => {
|
|
417
|
+
if (obj) {
|
|
418
|
+
SocketCommands._fixCallback(callback, 'Group yet exists');
|
|
419
|
+
} else {
|
|
420
|
+
obj = {
|
|
421
|
+
_id: 'system.group.' + group,
|
|
422
|
+
type: 'group',
|
|
423
|
+
common: {
|
|
424
|
+
name,
|
|
425
|
+
desc,
|
|
426
|
+
members: [],
|
|
427
|
+
acl
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
try {
|
|
431
|
+
this.adapter.setForeignObject('system.group.' + group, obj, options, error =>
|
|
432
|
+
SocketCommands._fixCallback(callback, error, obj));
|
|
433
|
+
} catch (error) {
|
|
434
|
+
this.adapter.log.error('[_addGroup] cannot write group: ' + error.toString());
|
|
435
|
+
SocketCommands._fixCallback(callback, error);
|
|
406
436
|
}
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
} catch (error) {
|
|
440
|
+
this.adapter.log.error('[_addGroup] cannot read group: ' + error.toString());
|
|
441
|
+
SocketCommands._fixCallback(callback, error);
|
|
442
|
+
}
|
|
412
443
|
}
|
|
413
444
|
|
|
414
445
|
_delGroup(group, options, callback) {
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
if (obj.common.dontDelete) {
|
|
420
|
-
typeof callback === 'function' && callback('Cannot delete group, while is system group');
|
|
446
|
+
try {
|
|
447
|
+
this.adapter.getForeignObject('system.group.' + group, options, (error, obj) => {
|
|
448
|
+
if (error || !obj) {
|
|
449
|
+
SocketCommands._fixCallback(callback, 'Group does not exist');
|
|
421
450
|
} else {
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
451
|
+
if (obj.common.dontDelete) {
|
|
452
|
+
SocketCommands._fixCallback(callback, 'Cannot delete group, while is system group');
|
|
453
|
+
} else {
|
|
454
|
+
try {
|
|
455
|
+
this.adapter.delForeignObject('system.group.' + group, options, error =>
|
|
456
|
+
// Remove this group from all users in web client
|
|
457
|
+
SocketCommands._fixCallback(callback, error));
|
|
458
|
+
} catch (error) {
|
|
459
|
+
this.adapter.log.error('[_delGroup] cannot delete group: ' + error.toString());
|
|
460
|
+
SocketCommands._fixCallback(callback, error);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
426
463
|
}
|
|
427
|
-
}
|
|
428
|
-
})
|
|
464
|
+
});
|
|
465
|
+
} catch (error) {
|
|
466
|
+
this.adapter.log.error('[_delGroup] cannot read group: ' + error.toString());
|
|
467
|
+
SocketCommands._fixCallback(callback, error);
|
|
468
|
+
}
|
|
429
469
|
}
|
|
430
470
|
|
|
431
471
|
static _checkObject(obj, options, flag) {
|
|
@@ -479,27 +519,32 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
479
519
|
callback(null, this.objects);
|
|
480
520
|
}
|
|
481
521
|
} else {
|
|
482
|
-
|
|
483
|
-
this.adapter.
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
socket._acl
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
522
|
+
try {
|
|
523
|
+
this.adapter.getObjectList({include_docs: true}, (error, res) => {
|
|
524
|
+
this.adapter.log.info('received all objects');
|
|
525
|
+
res = res.rows;
|
|
526
|
+
const objects = {};
|
|
527
|
+
|
|
528
|
+
if (socket._acl &&
|
|
529
|
+
socket._acl.user !== 'system.user.admin' &&
|
|
530
|
+
!socket._acl.groups.includes('system.group.administrator')) {
|
|
531
|
+
for (let i = 0; i < res.length; i++) {
|
|
532
|
+
if (SocketCommandsAdmin._checkObject(res[i].doc, socket._acl, 4 /* 'read' */)) {
|
|
533
|
+
objects[res[i].doc._id] = res[i].doc;
|
|
534
|
+
}
|
|
493
535
|
}
|
|
536
|
+
callback(null, objects);
|
|
537
|
+
} else {
|
|
538
|
+
for (let j = 0; j < res.length; j++) {
|
|
539
|
+
objects[res[j].doc._id] = res[j].doc;
|
|
540
|
+
}
|
|
541
|
+
callback(null, objects);
|
|
494
542
|
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
callback(null, objects);
|
|
501
|
-
}
|
|
502
|
-
});
|
|
543
|
+
});
|
|
544
|
+
} catch (error) {
|
|
545
|
+
this.adapter.log.error('[_getAllObjects] ERROR: ' + error.toString());
|
|
546
|
+
SocketCommands._fixCallback(callback, error);
|
|
547
|
+
}
|
|
503
548
|
}
|
|
504
549
|
}
|
|
505
550
|
}
|
|
@@ -507,35 +552,40 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
507
552
|
_initCommandsUser() {
|
|
508
553
|
this.commands['addUser'] = (socket, user, pass, callback) => {
|
|
509
554
|
if (this._checkPermissions(socket, 'addUser', callback, user)) {
|
|
510
|
-
this._addUser(user, pass, {user: socket._acl.user}, (
|
|
511
|
-
SocketCommands._fixCallback(callback,
|
|
555
|
+
this._addUser(user, pass, {user: socket._acl.user}, (error, ...args) =>
|
|
556
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
512
557
|
}
|
|
513
558
|
};
|
|
514
559
|
|
|
515
560
|
this.commands['delUser'] = (socket, user, callback) => {
|
|
516
561
|
if (this._checkPermissions(socket, 'delUser', callback, user)) {
|
|
517
|
-
this._delUser(user, {user: socket._acl.user}, (
|
|
518
|
-
SocketCommands._fixCallback(callback,
|
|
562
|
+
this._delUser(user, {user: socket._acl.user}, (error, ...args) =>
|
|
563
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
519
564
|
}
|
|
520
565
|
};
|
|
521
566
|
|
|
522
567
|
this.commands['addGroup'] = (socket, group, desc, acl, callback) => {
|
|
523
568
|
if (this._checkPermissions(socket, 'addGroup', callback, group)) {
|
|
524
|
-
this._addGroup(group, desc, acl, {user: socket._acl.user}, (
|
|
525
|
-
SocketCommands._fixCallback(callback,
|
|
569
|
+
this._addGroup(group, desc, acl, {user: socket._acl.user}, (error, ...args) =>
|
|
570
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
526
571
|
}
|
|
527
572
|
};
|
|
528
573
|
|
|
529
574
|
this.commands['delGroup'] = (socket, group, callback) => {
|
|
530
575
|
if (this._checkPermissions(socket, 'delGroup', callback, group)) {
|
|
531
|
-
this._delGroup(group, {user: socket._acl.user}, (
|
|
532
|
-
SocketCommands._fixCallback(callback,
|
|
576
|
+
this._delGroup(group, {user: socket._acl.user}, (error, ...args) =>
|
|
577
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
533
578
|
}
|
|
534
579
|
};
|
|
535
580
|
this.commands['changePassword'] = (socket, user, pass, callback) => {
|
|
536
581
|
if (user === socket._acl.user || this._checkPermissions(socket, 'changePassword', callback, user)) {
|
|
537
|
-
|
|
538
|
-
|
|
582
|
+
try {
|
|
583
|
+
this.adapter.setPassword(user, pass, {user: socket._acl.user}, (error, ...args) =>
|
|
584
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
585
|
+
} catch (error) {
|
|
586
|
+
this.adapter.log.error('[_changePassword] ERROR: ' + error.toString());
|
|
587
|
+
SocketCommands._fixCallback(callback, error);
|
|
588
|
+
}
|
|
539
589
|
}
|
|
540
590
|
};
|
|
541
591
|
}
|
|
@@ -546,33 +596,38 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
546
596
|
return this.adapter.log.warn('[getHostByIp] Invalid callback');
|
|
547
597
|
}
|
|
548
598
|
if (this._checkPermissions(socket, 'getHostByIp', ip)) {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
599
|
+
try {
|
|
600
|
+
this.adapter.getObjectView('system', 'host', {}, {user: socket._acl.user}, (error, data) => {
|
|
601
|
+
if (data && data.rows && data.rows.length) {
|
|
602
|
+
for (let i = 0; i < data.rows.length; i++) {
|
|
603
|
+
const obj = data.rows[i].value;
|
|
604
|
+
// if we requested specific name
|
|
605
|
+
if (obj.common.hostname === ip) {
|
|
606
|
+
return callback(ip, obj);
|
|
607
|
+
} else
|
|
608
|
+
// try to find this IP in the list
|
|
609
|
+
if (obj.native.hardware && obj.native.hardware.networkInterfaces) {
|
|
610
|
+
const net = obj.native.hardware.networkInterfaces;
|
|
611
|
+
for (const eth in net) {
|
|
612
|
+
if (!Object.prototype.hasOwnProperty.call(net, eth)) {
|
|
613
|
+
continue;
|
|
614
|
+
}
|
|
615
|
+
for (let j = 0; j < net[eth].length; j++) {
|
|
616
|
+
if (net[eth][j].address === ip) {
|
|
617
|
+
return callback(ip, obj);
|
|
618
|
+
}
|
|
567
619
|
}
|
|
568
620
|
}
|
|
569
621
|
}
|
|
570
622
|
}
|
|
571
623
|
}
|
|
572
|
-
}
|
|
573
624
|
|
|
574
|
-
|
|
575
|
-
|
|
625
|
+
callback(ip, null);
|
|
626
|
+
});
|
|
627
|
+
} catch (error) {
|
|
628
|
+
this.adapter.log.error('[_changePassword] ERROR: ' + error.toString());
|
|
629
|
+
SocketCommands._fixCallback(callback, error);
|
|
630
|
+
}
|
|
576
631
|
}
|
|
577
632
|
};
|
|
578
633
|
|
|
@@ -629,7 +684,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
629
684
|
});
|
|
630
685
|
}
|
|
631
686
|
}
|
|
632
|
-
} catch (
|
|
687
|
+
} catch (_error) {
|
|
633
688
|
// push unchecked
|
|
634
689
|
// result.list.push('log/' + transport + '/' + files[f]);
|
|
635
690
|
this.adapter.log.error(`Cannot check file: ${fileName}/${files[f]}`);
|
|
@@ -641,18 +696,18 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
641
696
|
} else {
|
|
642
697
|
result = {error: 'no file loggers'};
|
|
643
698
|
}
|
|
644
|
-
} catch (
|
|
645
|
-
this.adapter.log.error(
|
|
646
|
-
result = {error
|
|
699
|
+
} catch (error) {
|
|
700
|
+
this.adapter.log.error('Cannot read logs: ' + error);
|
|
701
|
+
result = {error};
|
|
647
702
|
}
|
|
648
|
-
|
|
703
|
+
SocketCommands._fixCallback(callback, result.error, result.list);
|
|
649
704
|
}
|
|
650
705
|
}, 500);
|
|
651
706
|
|
|
652
707
|
this._sendToHost(host, 'getLogFiles', null, result => {
|
|
653
708
|
clearTimeout(timeout);
|
|
654
709
|
timeout = null;
|
|
655
|
-
|
|
710
|
+
SocketCommands._fixCallback(callback, result.error, result.list);
|
|
656
711
|
});
|
|
657
712
|
}
|
|
658
713
|
};
|
|
@@ -663,8 +718,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
663
718
|
if (this.states && this.states[id]) {
|
|
664
719
|
delete this.states[id];
|
|
665
720
|
}
|
|
666
|
-
|
|
667
|
-
|
|
721
|
+
try {
|
|
722
|
+
this.adapter.delForeignState(id, {user: socket._acl.user}, (error, ...args) =>
|
|
723
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
724
|
+
} catch (error) {
|
|
725
|
+
this.adapter.log.error('[delState] ERROR: ' + error.toString());
|
|
726
|
+
SocketCommands._fixCallback(callback, error);
|
|
727
|
+
}
|
|
668
728
|
}
|
|
669
729
|
};
|
|
670
730
|
|
|
@@ -675,7 +735,12 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
675
735
|
this.adapter.log.debug(`cmdExec on ${host}(${id}): ${cmd}`);
|
|
676
736
|
// remember socket for this ID.
|
|
677
737
|
this.cmdSessions[id] = {socket};
|
|
678
|
-
|
|
738
|
+
try {
|
|
739
|
+
this.adapter.sendToHost(host, 'cmdExec', {data: cmd, id});
|
|
740
|
+
} catch (error) {
|
|
741
|
+
this.adapter.log.error('[cmdExec] ERROR: ' + error.toString());
|
|
742
|
+
SocketCommands._fixCallback(callback, error);
|
|
743
|
+
}
|
|
679
744
|
}
|
|
680
745
|
};
|
|
681
746
|
|
|
@@ -690,54 +755,65 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
690
755
|
this.commands['getRatings'] = (socket, update, callback) => {
|
|
691
756
|
if (update || !this.adapter._ratings) {
|
|
692
757
|
this.updateRatings()
|
|
693
|
-
.then(() =>
|
|
758
|
+
.then(() => SocketCommands._fixCallback(callback, null, this.adapter._ratings));
|
|
694
759
|
} else {
|
|
695
|
-
|
|
760
|
+
SocketCommands._fixCallback(callback, null, this.adapter._ratings);
|
|
696
761
|
}
|
|
697
762
|
};
|
|
698
763
|
|
|
699
764
|
this.commands['getCurrentInstance'] = (socket, callback) => {
|
|
700
|
-
|
|
765
|
+
SocketCommands._fixCallback(callback, null, this.adapter.namespace);
|
|
701
766
|
};
|
|
702
767
|
|
|
703
768
|
this.commands['checkFeatureSupported'] = (socket, feature, callback) => {
|
|
704
|
-
|
|
769
|
+
SocketCommands._fixCallback(callback, null, this.adapter.supportsFeature && this.adapter.supportsFeature(feature));
|
|
705
770
|
};
|
|
706
771
|
|
|
707
772
|
this.commands['decrypt'] = (socket, encryptedText, callback) => {
|
|
708
773
|
if (this.secret) {
|
|
709
|
-
|
|
774
|
+
SocketCommands._fixCallback(callback, null, this.adapter.decrypt(this.secret, encryptedText));
|
|
710
775
|
} else {
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
776
|
+
try {
|
|
777
|
+
this.adapter.getForeignObject('system.config', (error, obj) => {
|
|
778
|
+
if (obj && obj.native && obj.native.secret) {
|
|
779
|
+
this.secret = obj.native.secret;
|
|
780
|
+
SocketCommands._fixCallback(callback, null, this.adapter.decrypt(this.secret, encryptedText));
|
|
781
|
+
} else {
|
|
782
|
+
this.adapter.log.error(`No system.config found: ${error}`);
|
|
783
|
+
SocketCommands._fixCallback(callback, error);
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
} catch (error) {
|
|
787
|
+
this.adapter.log.error(`Cannot decrypt: ${error}`);
|
|
788
|
+
SocketCommands._fixCallback(callback, error);
|
|
789
|
+
}
|
|
720
790
|
}
|
|
721
791
|
};
|
|
722
792
|
|
|
723
793
|
this.commands['encrypt'] = (socket, plainText, callback) => {
|
|
724
794
|
if (this.secret) {
|
|
725
|
-
|
|
795
|
+
SocketCommands._fixCallback(callback, null, this.adapter.encrypt(this.secret, plainText));
|
|
726
796
|
} else {
|
|
727
|
-
this.adapter.getForeignObject('system.config', (
|
|
797
|
+
this.adapter.getForeignObject('system.config', (error, obj) => {
|
|
728
798
|
if (obj && obj.native && obj.native.secret) {
|
|
729
799
|
this.secret = obj.native.secret;
|
|
730
|
-
|
|
800
|
+
try {
|
|
801
|
+
const encrypted = this.adapter.encrypt(this.secret, plainText);
|
|
802
|
+
SocketCommands._fixCallback(callback, null, encrypted);
|
|
803
|
+
} catch (error) {
|
|
804
|
+
this.adapter.log.error(`Cannot encrypt: ${error}`);
|
|
805
|
+
SocketCommands._fixCallback(callback, error);
|
|
806
|
+
}
|
|
731
807
|
} else {
|
|
732
|
-
this.adapter.log.error(`No system.config found: ${
|
|
733
|
-
SocketCommands._fixCallback(callback,
|
|
808
|
+
this.adapter.log.error(`No system.config found: ${error}`);
|
|
809
|
+
SocketCommands._fixCallback(callback, error);
|
|
734
810
|
}
|
|
735
811
|
});
|
|
736
812
|
}
|
|
737
813
|
};
|
|
738
814
|
|
|
739
815
|
this.commands['getIsEasyModeStrict'] = (socket, callback) => {
|
|
740
|
-
|
|
816
|
+
SocketCommands._fixCallback(callback, null, this.adapter.config.accessLimit);
|
|
741
817
|
};
|
|
742
818
|
|
|
743
819
|
this.commands['getEasyMode'] = (socket, callback) => {
|
|
@@ -766,10 +842,10 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
766
842
|
});
|
|
767
843
|
});
|
|
768
844
|
} else {
|
|
769
|
-
this.adapter.getObjectView('system', 'instance', {startkey: 'system.adapter.', endkey: 'system.adapter.\u9999'}, {user}, (
|
|
845
|
+
this.adapter.getObjectView('system', 'instance', {startkey: 'system.adapter.', endkey: 'system.adapter.\u9999'}, {user}, (error, doc) => {
|
|
770
846
|
const promises = [];
|
|
771
847
|
const configs = [];
|
|
772
|
-
if (!
|
|
848
|
+
if (!error && doc.rows.length) {
|
|
773
849
|
for (let i = 0; i < doc.rows.length; i++) {
|
|
774
850
|
const obj = doc.rows[i].value;
|
|
775
851
|
if (obj.common.noConfig && !obj.common.adminTab) {
|
|
@@ -799,10 +875,10 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
799
875
|
if (typeof callback === 'function' && this._checkPermissions(socket, 'getObject', callback)) {
|
|
800
876
|
this.adapter.getObjectView('system', 'adapter',
|
|
801
877
|
{startkey: `system.adapter.${adapterName || ''}`, endkey: `system.adapter.${adapterName || '\u9999'}`},
|
|
802
|
-
(
|
|
878
|
+
(error, doc) =>
|
|
803
879
|
{
|
|
804
|
-
if (
|
|
805
|
-
callback(
|
|
880
|
+
if (error) {
|
|
881
|
+
callback(error);
|
|
806
882
|
} else {
|
|
807
883
|
callback(null, doc.rows
|
|
808
884
|
.filter(obj => obj && (!adapterName || (obj.common && obj.common.name === this.adapterName)))
|
|
@@ -826,7 +902,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
826
902
|
let timeout = setTimeout(() => {
|
|
827
903
|
if (timeout) {
|
|
828
904
|
timeout = null;
|
|
829
|
-
|
|
905
|
+
SocketCommands._fixCallback(callback, 'updateLicenses timeout');
|
|
830
906
|
}
|
|
831
907
|
}, 7000);
|
|
832
908
|
|
|
@@ -834,14 +910,14 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
834
910
|
if (timeout) {
|
|
835
911
|
clearTimeout(timeout);
|
|
836
912
|
timeout = null;
|
|
837
|
-
|
|
913
|
+
SocketCommands._fixCallback(callback, result.error, result && result.result);
|
|
838
914
|
}
|
|
839
915
|
});
|
|
840
916
|
} else {
|
|
841
917
|
// remove this branch when js-controller 4.x will be mainstream
|
|
842
918
|
this._updateLicenses(login, password)
|
|
843
|
-
.then(licenses =>
|
|
844
|
-
.catch(
|
|
919
|
+
.then(licenses => SocketCommands._fixCallback(callback, null, licenses))
|
|
920
|
+
.catch(error => SocketCommands._fixCallback(callback, error));
|
|
845
921
|
}
|
|
846
922
|
}
|
|
847
923
|
};
|
|
@@ -851,9 +927,9 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
851
927
|
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
852
928
|
this.adapter.getObjectView('system', 'instance',
|
|
853
929
|
{startkey: `system.adapter.`, endkey: `system.adapter.\u9999`},
|
|
854
|
-
(
|
|
855
|
-
if (
|
|
856
|
-
callback(
|
|
930
|
+
(error, doc) => {
|
|
931
|
+
if (error) {
|
|
932
|
+
callback(error);
|
|
857
933
|
} else {
|
|
858
934
|
// calculate
|
|
859
935
|
const result = {};
|
|
@@ -880,9 +956,9 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
880
956
|
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
881
957
|
this.adapter.getObjectView('system', 'adapter',
|
|
882
958
|
{startkey: `system.adapter.`, endkey: `system.adapter.\u9999`},
|
|
883
|
-
(
|
|
884
|
-
if (
|
|
885
|
-
callback(
|
|
959
|
+
(error, doc) => {
|
|
960
|
+
if (error) {
|
|
961
|
+
callback(error);
|
|
886
962
|
} else {
|
|
887
963
|
// calculate
|
|
888
964
|
const result = {};
|
|
@@ -918,14 +994,30 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
918
994
|
|
|
919
995
|
this.commands['getCompactSystemConfig'] = (socket, callback) => {
|
|
920
996
|
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
921
|
-
this.adapter.getForeignObject('system.config', (
|
|
997
|
+
this.adapter.getForeignObject('system.config', (error, obj) => {
|
|
922
998
|
obj = obj || {};
|
|
923
|
-
const secret = obj.native && obj.native.secret;
|
|
999
|
+
const secret = obj && obj.native && obj.native.secret;
|
|
924
1000
|
delete obj.native;
|
|
925
1001
|
if (secret) {
|
|
926
1002
|
obj.native = {secret};
|
|
927
1003
|
}
|
|
928
|
-
callback(
|
|
1004
|
+
callback(error, obj);
|
|
1005
|
+
});
|
|
1006
|
+
}
|
|
1007
|
+
};
|
|
1008
|
+
|
|
1009
|
+
this.commands['getCompactSystemRepositories'] = (socket, callback) => {
|
|
1010
|
+
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
1011
|
+
this.adapter.getForeignObject('system.repositories', (error, obj) => {
|
|
1012
|
+
obj && obj.native && obj.native.repositories && Object.keys(obj.native.repositories).forEach(name => {
|
|
1013
|
+
if (obj.native.repositories[name].json) {
|
|
1014
|
+
// limit information to _repoInfo
|
|
1015
|
+
obj.native.repositories[name].json = {
|
|
1016
|
+
_repoInfo: obj.native.repositories[name].json._repoInfo
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
});
|
|
1020
|
+
callback(error, obj);
|
|
929
1021
|
});
|
|
930
1022
|
}
|
|
931
1023
|
};
|
|
@@ -937,7 +1029,7 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
937
1029
|
const result = {};
|
|
938
1030
|
data && Object.keys(data).forEach(name => result[name] = {
|
|
939
1031
|
version: data[name].version,
|
|
940
|
-
icon: data[name].extIcon
|
|
1032
|
+
icon: data[name].extIcon,
|
|
941
1033
|
});
|
|
942
1034
|
callback(result);
|
|
943
1035
|
});
|
|
@@ -947,9 +1039,9 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
947
1039
|
this.commands['getCompactHosts'] = (socket, callback) => {
|
|
948
1040
|
if (this._checkPermissions(socket, 'getObject', callback)) {
|
|
949
1041
|
this.adapter.getObjectView('system', 'host',
|
|
950
|
-
{startkey: 'system.host.', endkey: 'system.host.\u9999'}, (
|
|
951
|
-
if (
|
|
952
|
-
callback(
|
|
1042
|
+
{startkey: 'system.host.', endkey: 'system.host.\u9999'}, (error, doc) => {
|
|
1043
|
+
if (error) {
|
|
1044
|
+
callback(error);
|
|
953
1045
|
} else {
|
|
954
1046
|
const result = [];
|
|
955
1047
|
doc.rows.map(item => {
|
|
@@ -992,8 +1084,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
992
1084
|
this.commands['getForeignStates'] = (socket, pattern, callback) => {
|
|
993
1085
|
if (this._checkPermissions(socket, 'getStates', callback)) {
|
|
994
1086
|
if (typeof callback === 'function') {
|
|
995
|
-
|
|
996
|
-
|
|
1087
|
+
try {
|
|
1088
|
+
this.adapter.getForeignStates(pattern, (error, ...args) =>
|
|
1089
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1090
|
+
} catch (error) {
|
|
1091
|
+
this.adapter.log.error(`[getForeignStates] ERROR: ${error}`);
|
|
1092
|
+
SocketCommands._fixCallback(callback, error);
|
|
1093
|
+
}
|
|
997
1094
|
} else {
|
|
998
1095
|
this.adapter.log.warn('[getForeignStates] Invalid callback')
|
|
999
1096
|
}
|
|
@@ -1006,9 +1103,14 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1006
1103
|
options = {};
|
|
1007
1104
|
}
|
|
1008
1105
|
options.user = socket._acl.user;
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1106
|
+
try {
|
|
1107
|
+
// options.recursive = true; // the only difference between delObject and delObjects is this line
|
|
1108
|
+
this.adapter.delForeignObject(id, options, (error, ...args) =>
|
|
1109
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1110
|
+
} catch (error) {
|
|
1111
|
+
this.adapter.log.error(`[delObject] ERROR: ${error}`);
|
|
1112
|
+
SocketCommands._fixCallback(callback, error);
|
|
1113
|
+
}
|
|
1012
1114
|
}
|
|
1013
1115
|
};
|
|
1014
1116
|
|
|
@@ -1019,8 +1121,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1019
1121
|
}
|
|
1020
1122
|
options.user = socket._acl.user;
|
|
1021
1123
|
options.recursive = true; // the only difference between delObject and delObjects is this line
|
|
1022
|
-
|
|
1023
|
-
|
|
1124
|
+
try {
|
|
1125
|
+
this.adapter.delForeignObject(id, options, (error, ...args) =>
|
|
1126
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1127
|
+
} catch (error) {
|
|
1128
|
+
this.adapter.log.error(`[delObjects] ERROR: ${error}`);
|
|
1129
|
+
SocketCommands._fixCallback(callback, error);
|
|
1130
|
+
}
|
|
1024
1131
|
}
|
|
1025
1132
|
};
|
|
1026
1133
|
}
|
|
@@ -1036,9 +1143,15 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1036
1143
|
}
|
|
1037
1144
|
options = options || {};
|
|
1038
1145
|
options.user = socket._acl.user;
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1146
|
+
|
|
1147
|
+
try {
|
|
1148
|
+
const buffer = Buffer.from(data64, 'base64');
|
|
1149
|
+
this.adapter.writeFile(_adapter, fileName, buffer, {user: socket._acl.user}, (error, ...args) =>
|
|
1150
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1151
|
+
} catch (error) {
|
|
1152
|
+
this.adapter.log.error('[writeFile] Cannot convert data: ' + error.toString());
|
|
1153
|
+
callback && callback('Cannot convert data: ' + error.toString());
|
|
1154
|
+
}
|
|
1042
1155
|
}
|
|
1043
1156
|
};
|
|
1044
1157
|
}
|
|
@@ -1053,8 +1166,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1053
1166
|
|
|
1054
1167
|
this.commands['extendObject'] = (socket, id, obj, callback) => {
|
|
1055
1168
|
if (this._checkPermissions(socket, 'extendObject', callback, id)) {
|
|
1056
|
-
|
|
1057
|
-
|
|
1169
|
+
try {
|
|
1170
|
+
this.adapter.extendForeignObject(id, obj, {user: socket._acl.user}, (error, ...args) =>
|
|
1171
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1172
|
+
} catch (error) {
|
|
1173
|
+
this.adapter.log.error(`[extendObject] ERROR: ${error}`);
|
|
1174
|
+
SocketCommands._fixCallback(callback, error);
|
|
1175
|
+
}
|
|
1058
1176
|
}
|
|
1059
1177
|
};
|
|
1060
1178
|
|
|
@@ -1065,13 +1183,17 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1065
1183
|
type = undefined;
|
|
1066
1184
|
}
|
|
1067
1185
|
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1186
|
+
if (typeof callback === 'function') {
|
|
1187
|
+
try {
|
|
1188
|
+
this.adapter.getForeignObjects(pattern, type, {user: socket._acl.user}, (error, ...args) =>
|
|
1189
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1190
|
+
} catch (error) {
|
|
1191
|
+
this.adapter.log.error(`[extendObject] ERROR: ${error}`);
|
|
1192
|
+
SocketCommands._fixCallback(callback, error);
|
|
1073
1193
|
}
|
|
1074
|
-
}
|
|
1194
|
+
} else {
|
|
1195
|
+
this.adapter.log.warn('[getObjects] Invalid callback');
|
|
1196
|
+
}
|
|
1075
1197
|
}
|
|
1076
1198
|
};
|
|
1077
1199
|
|
|
@@ -1085,8 +1207,13 @@ class SocketCommandsAdmin extends SocketCommands {
|
|
|
1085
1207
|
options = {};
|
|
1086
1208
|
}
|
|
1087
1209
|
options.user = socket._acl.user;
|
|
1088
|
-
|
|
1089
|
-
|
|
1210
|
+
try {
|
|
1211
|
+
this.adapter.delForeignObject(id, options, (error, ...args) =>
|
|
1212
|
+
SocketCommands._fixCallback(callback, error, ...args));
|
|
1213
|
+
} catch (error) {
|
|
1214
|
+
this.adapter.log.error(`[delObject] ERROR: ${error}`);
|
|
1215
|
+
SocketCommands._fixCallback(callback, error);
|
|
1216
|
+
}
|
|
1090
1217
|
}
|
|
1091
1218
|
};
|
|
1092
1219
|
}
|