knxultimate
Version:
KNX IP protocol implementation for Node. This is the ENGINE of Node-Red KNX-Ultimate node.
76 lines • 3.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLocalAddress = exports.getIPv4Interfaces = void 0;
const utils_1 = require("../utils");
const KnxLog_1 = require("../KnxLog");
const os_1 = __importDefault(require("os"));
const logger = (0, KnxLog_1.module)('ipAddressHelper');
function getIPv4Interfaces() {
const candidateInterfaces = {};
let interfaces = os_1.default.networkInterfaces();
if (process.env.CI) {
interfaces = {
eth0: [
{
address: '192.168.1.58',
netmask: '255.255.255.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: false,
cidr: '192.168.1.58/24',
},
],
};
}
for (const iface in interfaces) {
for (let index = 0; index < interfaces[iface].length; index++) {
let intf;
try {
intf = interfaces[iface][index];
if (intf === undefined) {
logger.debug('intf is null: control point 1');
}
else {
logger.debug('parsing interface: %s (%j)', iface, intf);
if (intf.family !== undefined &&
(intf.family.toString().includes('4') ||
intf.family === 4) &&
!intf.internal) {
logger.debug('Found suitable interface: %s (%j)', iface, intf);
candidateInterfaces[iface] = intf;
}
else {
logger.debug('Found NOT suitable interface: %s (%j)', iface, intf);
}
}
}
catch (error) {
logger.error('getIPv4Interfaces: error parsing the interface %s (%j)', iface, intf);
}
}
}
return candidateInterfaces;
}
exports.getIPv4Interfaces = getIPv4Interfaces;
function getLocalAddress(_interface = '') {
logger.debug('getLocalAddress: getting interfaces');
const candidateInterfaces = getIPv4Interfaces();
if (_interface !== '') {
if (!(0, utils_1.hasProp)(candidateInterfaces, _interface)) {
logger.error(`exports.getLocalAddress: Interface ${_interface} not found or has no useful IPv4 address!`);
throw Error(`Interface ${_interface} not found or has no useful IPv4 address!`);
}
else {
return candidateInterfaces[_interface].address;
}
}
if (Object.keys(candidateInterfaces).length > 0) {
return candidateInterfaces[Object.keys(candidateInterfaces)[0]].address;
}
throw Error('No valid IPv4 interfaces detected');
}
exports.getLocalAddress = getLocalAddress;
//# sourceMappingURL=ipAddressHelper.js.map