Skip to content

Commit

Permalink
Sanitize the IDs of the clients
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Oct 7, 2024
1 parent 868dcb8 commit 49d99e6
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 116 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ States:
### **WORK IN PROGRESS**
-->
## Changelog
### **WORK IN PROGRESS**

* (bluefox) Sanitize the IDs of the clients

### 3.2.0 (2024-08-28)
* (bluefox) Added information about connected clients in the server mode

Expand Down
29 changes: 17 additions & 12 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ function MQTTServer(adapter) {
setStateImmediate(mappingClients[channelId], stateId, state.val ? 'ON' : 'OFF');
break;
case 'number':
setStateImmediate(mappingClients[channelId], stateId, (state.val === null) ? '' : state.val.toString());
setStateImmediate(mappingClients[channelId], stateId, state.val === null ? '' : state.val.toString());
break;
case 'string':
setStateImmediate(mappingClients[channelId], stateId, (state.val === null) ? '' : state.val.toString());
Expand Down Expand Up @@ -819,7 +819,7 @@ function MQTTServer(adapter) {
const clientIds = [];
if (clients) {
for (const id in clients) {
const oid = `info.clients.${id.replace(/[.\s]+/g, '_')}`;
const oid = `info.clients.${id.replace(/[.\s]+/g, '_').replace(FORBIDDEN_CHARS, '_')}`;
clientIds.push(oid);
const clientObj = await adapter.getObjectAsync(oid);
if (!clientObj?.native) {
Expand All @@ -837,10 +837,15 @@ function MQTTServer(adapter) {
port: clients[id].stream.remotePort,
},
});
} else if (clientObj.native.port !== clients[id].stream.remotePort || clientObj.native.ip !== clients[id].stream.remoteAddress) {
clientObj.native.port = clients[id].stream.remotePort;
clientObj.native.ip = clients[id].stream.remoteAddress;
adapter.setObject(clientObj._id, clientObj);
} else {
if (clients[id] &&
(clientObj.native.port !== clients[id].stream.remotePort ||
clientObj.native.ip !== clients[id].stream.remoteAddress)
) {
clientObj.native.port = clients[id].stream.remotePort;
clientObj.native.ip = clients[id].stream.remoteAddress;
adapter.setObject(clientObj._id, clientObj);
}
}
await adapter.setStateAsync(oid, true, true);
}
Expand Down Expand Up @@ -954,13 +959,13 @@ function MQTTServer(adapter) {
// check for arrays
if (types[attr]) {
if (types[attr].type === 'array') {
// transform to array of attributes
// transform to an array of attributes
for (let i = 1; i <= 10; i++) {
let val = data[attr][i - 1];
if (typeof val === 'undefined') {
break;
}
// define new object
// define a new object
let replaceAttr = attr.replace(FORBIDDEN_CHARS, '_') + i.toString();
let id = `${adapter.namespace}.${client.iobId}.${prefix ? `${prefix}.` : ''}${path.length ? `${path.join('_')}_` : ''}${replaceAttr}`;
let obj = {
Expand Down Expand Up @@ -1337,7 +1342,7 @@ function MQTTServer(adapter) {
// stat/sonoff/POWER

if (types[stateId]) {
let id = `${adapter.namespace}.${client.iobId}.${stateId.replace(/[-.+\s]+/g, '_')}`;
let id = `${adapter.namespace}.${client.iobId}.${stateId.replace(/[-.+\s]+/g, '_').replace(FORBIDDEN_CHARS, '_')}`;
let obj = {
type: 'addObject',
id: id,
Expand Down Expand Up @@ -1485,12 +1490,12 @@ function MQTTServer(adapter) {

// client connected
client.on('connect', options => {
// acknowledge the connect packet
// acknowledge the "connect" packet
client.id = options.clientId;
client.iobId = client.id.replace(FORBIDDEN_CHARS, '_');
mappingClients[client.iobId] = client.id;

// get possible old client
// get possible an old client
let oldClient = clients[client.id];

if (config.user) {
Expand Down Expand Up @@ -1594,7 +1599,7 @@ function MQTTServer(adapter) {
}

if (packet.qos === 1) {
// send PUBACK to client
// send PUBACK to a client
client.puback({messageId: packet.messageId});
} else if (packet.qos === 2) {
const pack = client._messages && client._messages.find(e => e.messageId === packet.messageId);
Expand Down
2 changes: 0 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
/**
*
* ioBroker sonoff Adapter
*
* (c) 2017-2024 bluefox
*
* MIT License
*
*/
'use strict';

Expand Down
Loading

0 comments on commit 49d99e6

Please sign in to comment.