iobroker.kisshome-defender
Version:
Collection of information for KISSHome defender
128 lines • 4.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMacForIp = getMacForIp;
exports.validateIpAddress = validateIpAddress;
exports.getVendorForMac = getVendorForMac;
exports.getDefaultGateway = getDefaultGateway;
exports.getTimestamp = getTimestamp;
exports.getDescriptionObject = getDescriptionObject;
exports.getDescriptionFile = getDescriptionFile;
exports.size2text = size2text;
exports.fileNameToDate = fileNameToDate;
exports.normalizeMacAddress = normalizeMacAddress;
// @ts-expect-error no types
const network_1 = require("network");
const arp_lookup_1 = require("@network-utils/arp-lookup");
const vendor_lookup_1 = require("@network-utils/vendor-lookup");
const node_net_1 = require("node:net");
// This function is used trigger the OS to resolve IP to MAC address
async function httpPing(ip) {
// try to open the TCP socket to this IP
const client = new node_net_1.Socket();
return await new Promise(resolve => {
let timeout = setTimeout(() => {
timeout = null;
resolve(false);
}, 200);
client.connect(18001, ip, () => {
client.destroy();
if (timeout) {
clearTimeout(timeout);
timeout = null;
resolve(true);
}
});
client.on('error', () => {
client.destroy();
if (timeout) {
clearTimeout(timeout);
timeout = null;
resolve(false);
}
});
});
}
async function getMacForIp(ip) {
// trigger the OS to resolve IP to MAC address
await httpPing(ip);
const mac = await (0, arp_lookup_1.toMAC)(ip);
if (mac) {
return { mac: mac.toUpperCase(), vendor: (0, vendor_lookup_1.toVendor)(mac), ip };
}
return null;
}
function validateIpAddress(ip) {
if (!ip) {
return true;
}
if (typeof ip !== 'string') {
return false;
}
ip = ip.trim();
if (!ip) {
return true;
}
if (!ip.match(/^\d+\.\d+\.\d+\.\d+$/)) {
return false;
}
const parts = ip
.trim()
.split('.')
.map(part => parseInt(part, 10));
return !parts.find(part => part < 0 || part > 0xff);
}
function getVendorForMac(mac) {
return (0, vendor_lookup_1.toVendor)(mac);
}
function getDefaultGateway() {
return new Promise((resolve, reject) => (0, network_1.get_gateway_ip)((err, ip) => {
if (err) {
return reject(new Error(err));
}
return resolve(ip);
}));
}
function getTimestamp() {
const now = new Date();
return `${now.getUTCFullYear()}-${(now.getUTCMonth() + 1).toString().padStart(2, '0')}-${now.getUTCDate().toString().padStart(2, '0')}_${now.getUTCHours().toString().padStart(2, '0')}-${now.getUTCMinutes().toString().padStart(2, '0')}-${now.getUTCSeconds().toString().padStart(2, '0')}`;
}
function getDescriptionObject(IPs) {
const desc = {};
IPs.sort((a, b) => a.ip.localeCompare(b.ip)).forEach(ip => {
if (ip.mac) {
desc[ip.mac] = { ip: ip.ip, desc: ip.desc };
}
});
return desc;
}
function getDescriptionFile(IPs) {
const desc = getDescriptionObject(IPs);
return JSON.stringify(desc, null, 2);
}
function size2text(size) {
if (size < 1024) {
return `${size} B`;
}
if (size < 1024 * 1024) {
return `${Math.round((size * 10) / 1024) / 10} kB`;
}
return `${Math.round((size * 10) / (1024 * 1024) / 10)} MB`;
}
function fileNameToDate(fileName) {
// File name is in format YYYY-MM-DD_T.json
const datePart = fileName.split('_')[0]; // Get the date part
const [year, month, day] = datePart.split('-').map(Number);
return new Date(year, month - 1, day); // Month is 0-based in JavaScript
}
function normalizeMacAddress(mac) {
if (!mac) {
return '';
}
mac = mac
.toUpperCase()
.trim()
.replace(/[\s:-]/g, '');
// convert to 00:11:22:33:44:55
return mac.replace(/(..)(..)(..)(..)(..)(..)/, '$1:$2:$3:$4:$5:$6');
}
//# sourceMappingURL=utils.js.map