mirror of
https://github.com/skydiver/ewelink-api.git
synced 2025-12-21 13:23:05 +01:00
* Added arpTableSolver (#18) * Added arpTableSolver * fix package import * linting class * changed arp library * refactor arp class * using arpping fork * refactor arpTableSolver class * Added Zero Conf functionality (LAN mode) (#46) * added crypto-js * zeroconf helper functions * zeroconf update payload * new method to save devices cache file * class renamed * refactor Zeroconf class * return cached device if exists * moved method to get local ip address * fix mac addresses without leading zeroes * refactor Zeroconf class * using new zeroconf functionality * zeroconf working with single and multichannel devices * save device mixin enhancement * working on zeroconf test cases * catch errors on filesystem methods * zeroconf: added extra test cases * better error handling * zeroconf: 100% code coverage * removed deprecated login method * updates on credentials file * version bump * Docs for v2.0 (#52) * added v1 docs * added zeroconf docs * updated readme * docs updated * removed zeroconf article warning * updated vscode config Co-authored-by: Luis Llamas <luisllamas@hotmail.com>
92 lines
2.2 KiB
JavaScript
92 lines
2.2 KiB
JavaScript
const fs = require('fs');
|
|
const arpping = require('arpping')({});
|
|
|
|
class Zeroconf {
|
|
/**
|
|
* Build the ARP table
|
|
* @param ip
|
|
* @returns {Promise<unknown>}
|
|
*/
|
|
static getArpTable(ip = null) {
|
|
return new Promise((resolve, reject) => {
|
|
arpping.discover(ip, (err, hosts) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
const arpTable = Zeroconf.fixMacAddresses(hosts);
|
|
return resolve(arpTable);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Sometime arp command returns mac addresses without leading zeroes.
|
|
* @param hosts
|
|
*/
|
|
static fixMacAddresses(hosts) {
|
|
return hosts.map(host => {
|
|
const octets = host.mac.split(':');
|
|
|
|
const fixedMac = octets.map(octet => {
|
|
if (octet.length === 1) {
|
|
return `0${octet}`;
|
|
}
|
|
return octet;
|
|
});
|
|
|
|
return {
|
|
ip: host.ip,
|
|
mac: fixedMac.join(':'),
|
|
};
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Save ARP table to local file
|
|
* @param config
|
|
* @returns {Promise<{error: string}|{file: {request: string; resolved: string} | any | string | string, status: string}>}
|
|
*/
|
|
static async saveArpTable(config = {}) {
|
|
const ip = config.ip || null;
|
|
const fileName = config.file || './arp-table.json';
|
|
try {
|
|
const arpTable = await Zeroconf.getArpTable(ip);
|
|
const jsonContent = JSON.stringify(arpTable, null, 2);
|
|
fs.writeFileSync(fileName, jsonContent, 'utf8');
|
|
return { status: 'ok', file: fileName };
|
|
} catch (e) {
|
|
return { error: e.toString() };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Read ARP table file
|
|
* @param fileName
|
|
* @returns {Promise<{error: string}|any>}
|
|
*/
|
|
static async loadArpTable(fileName = './arp-table.json') {
|
|
try {
|
|
const jsonContent = await fs.readFileSync(fileName);
|
|
return JSON.parse(jsonContent);
|
|
} catch (e) {
|
|
return { error: e.toString() };
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Read devices cache file
|
|
* @param fileName
|
|
* @returns {Promise<{error: string}>}
|
|
*/
|
|
static async loadCachedDevices(fileName = './devices-cache.json') {
|
|
try {
|
|
const jsonContent = await fs.readFileSync(fileName);
|
|
return JSON.parse(jsonContent);
|
|
} catch (e) {
|
|
return { error: e.toString() };
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = Zeroconf;
|