Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function discoverLocalDevices() {
server = dgram.createSocket('udp4');
server.on('listening', function () {
//const address = server.address();
adapter.log.info('Listen for local Tuya devices on port 6666');
});
const normalParser = new MessageParser({version: 3.1});
server.on('message', function (message, remote) {
adapter.log.debug('Discovered device: ' + remote.address + ':' + remote.port + ' - ' + message);
let data;
try {
data = normalParser.parse(message)[0];
} catch (err) {
return;
}
if (!data.payload || !data.payload.gwId || data.commandByte !== CommandType.UDP) return;
if (knownDevices[data.payload.gwId] && knownDevices[data.payload.gwId].device) return;
initDevice(data.payload.gwId, data.payload.productKey, data.payload, ['name']);
});
server.on('error', err => {
adapter.log.warn('Can not Listen for Encrypted UDP packages: ' + err);
});
server.bind(6666);
function checkDiscoveredEncryptedDevices(deviceId, callback) {
const foundIps = Object.keys(discoveredEncryptedDevices);
adapter.log.debug(deviceId + ': Try to initialize encrypted device with received UDP messages (#IPs: ' + foundIps.length + '): version=' + knownDevices[deviceId].version + ', key=' + knownDevices[deviceId].localKey);
const parser = new MessageParser({version: knownDevices[deviceId].version || 3.3, key: knownDevices[deviceId].localKey});
for (let ip of foundIps) {
if (discoveredEncryptedDevices[ip] === true) continue;
let data;
try {
data = parser.parse(discoveredEncryptedDevices[ip])[0];
}
catch (err) {
adapter.log.debug(deviceId + ': Error on decrypt try: ' + err);
continue;
}
if (!data.payload || !data.payload.gwId || (data.commandByte !== CommandType.UDP && data.commandByte !== CommandType.UDP_NEW)) {
adapter.log.debug(deviceId + ': No relevant Data for decrypt try: ' + JSON.stringify(data));
continue;
}