try again oui ^^

This commit is contained in:
2025-03-29 17:17:35 +01:00
parent 33e8400d51
commit 95b83f060b
3 changed files with 38 additions and 21 deletions

View File

@@ -15,6 +15,7 @@
"express": "^4.21.2", "express": "^4.21.2",
"express-rate-limit": "^7.5.0", "express-rate-limit": "^7.5.0",
"macaddress": "^0.5.3", "macaddress": "^0.5.3",
"oui": "^13.0.5",
"pino": "^9.6.0", "pino": "^9.6.0",
"pino-pretty": "^13.0.0", "pino-pretty": "^13.0.0",
"whois-json": "^2.0.4" "whois-json": "^2.0.4"
@@ -904,6 +905,22 @@
"wrappy": "1" "wrappy": "1"
} }
}, },
"node_modules/oui": {
"version": "13.0.5",
"resolved": "https://registry.npmjs.org/oui/-/oui-13.0.5.tgz",
"integrity": "sha512-bAXoXcLrl3SweuEuh8aWpdTsfHka6B7Gh1SxsTZMXtWi9QSxQk7CupxWlP2QQ4olTDifaWAiQbROMxfarAQZlQ==",
"dependencies": {
"oui-data": "^1.1.76"
},
"bin": {
"oui": "bin/oui.js"
}
},
"node_modules/oui-data": {
"version": "1.1.314",
"resolved": "https://registry.npmjs.org/oui-data/-/oui-data-1.1.314.tgz",
"integrity": "sha512-F8r1LwdAucn6Fgq1KECZPMCpMgXPdhCblFOY/eQ6S83x5lfQQK9aRRIpz7dH6iQeAeqoSP1b4I8z4lqUaTwAvg=="
},
"node_modules/p-limit": { "node_modules/p-limit": {
"version": "2.3.0", "version": "2.3.0",
"resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.3.0.tgz",

View File

@@ -16,6 +16,7 @@
"express": "^4.21.2", "express": "^4.21.2",
"express-rate-limit": "^7.5.0", "express-rate-limit": "^7.5.0",
"macaddress": "^0.5.3", "macaddress": "^0.5.3",
"oui": "^13.0.5",
"pino": "^9.6.0", "pino": "^9.6.0",
"pino-pretty": "^13.0.0", "pino-pretty": "^13.0.0",
"whois-json": "^2.0.4" "whois-json": "^2.0.4"

View File

@@ -9,7 +9,7 @@ const dns = require('dns').promises;
const pino = require('pino'); // Logging library const pino = require('pino'); // Logging library
const rateLimit = require('express-rate-limit'); // Rate limiting middleware const rateLimit = require('express-rate-limit'); // Rate limiting middleware
const whois = require('whois-json'); // Hinzugefügt für WHOIS const whois = require('whois-json'); // Hinzugefügt für WHOIS
const macaddress = require('macaddress'); // Ersetzt oui const oui = require('oui'); // Ersetzt macaddress für OUI Lookup
// --- Logger Initialisierung --- // --- Logger Initialisierung ---
const logger = pino({ const logger = pino({
@@ -95,8 +95,9 @@ function isValidMac(mac) {
if (!mac || typeof mac !== 'string') { if (!mac || typeof mac !== 'string') {
return false; return false;
} }
// Erlaubt Formate wie 00:1A:2B:3C:4D:5E, 00-1A-2B-3C-4D-5E, 001A.2B3C.4D5E // Erlaubt Formate wie 00:1A:2B:3C:4D:5E, 00-1A-2B-3C-4D-5E, 001A.2B3C.4D5E, 001a2b3c4d5e
const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$|^([0-9A-Fa-f]{4}\.){2}([0-9A-Fa-f]{4})$|^([0-9A-Fa-f]{12})$/; // Auch ohne Trennzeichen erlaubt // Die oui Bibliothek ist tolerant gegenüber verschiedenen Formaten.
const macRegex = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$|^([0-9A-Fa-f]{4}\.){2}([0-9A-Fa-f]{4})$|^([0-9A-Fa-f]{12})$/;
return macRegex.test(mac.trim()); return macRegex.test(mac.trim());
} }
@@ -258,7 +259,7 @@ function parseTracerouteLine(line) {
} }
// --- Initialisierung (MaxMind DBs laden) --- // --- Initialisierung (MaxMind DBs laden & OUI DB laden) ---
async function initialize() { async function initialize() {
try { try {
logger.info('Loading MaxMind databases...'); logger.info('Loading MaxMind databases...');
@@ -269,11 +270,13 @@ async function initialize() {
asnReader = await geoip.Reader.open(asnDbPath); asnReader = await geoip.Reader.open(asnDbPath);
logger.info('MaxMind databases loaded successfully.'); logger.info('MaxMind databases loaded successfully.');
// Kein explizites Laden für 'macaddress' nötig. // OUI Datenbank laden (kann beim ersten Aufruf etwas dauern)
logger.info('MAC address lookup data (macaddress) will be loaded on first use.'); logger.info('Loading OUI database...');
await oui.update(); // Sicherstellen, dass die DB aktuell ist (optional, aber empfohlen)
logger.info('OUI database loaded/updated.');
} catch (error) { } catch (error) {
logger.fatal({ error: error.message, stack: error.stack }, 'Could not initialize MaxMind databases. Exiting.'); logger.fatal({ error: error.message, stack: error.stack }, 'Could not initialize databases. Exiting.');
process.exit(1); process.exit(1);
} }
} }
@@ -684,7 +687,7 @@ app.get('/api/whois-lookup', async (req, res) => {
} }
}); });
// MAC Address Lookup Endpunkt (mit 'macaddress' Bibliothek) // MAC Address Lookup Endpunkt (mit 'oui' Bibliothek)
app.get('/api/mac-lookup', async (req, res) => { app.get('/api/mac-lookup', async (req, res) => {
const macRaw = req.query.mac; const macRaw = req.query.mac;
const mac = typeof macRaw === 'string' ? macRaw.trim() : macRaw; const mac = typeof macRaw === 'string' ? macRaw.trim() : macRaw;
@@ -698,24 +701,20 @@ app.get('/api/mac-lookup', async (req, res) => {
} }
try { try {
// Bereinige die MAC-Adresse: Entferne alle nicht-hexadezimalen Zeichen // Verwende die oui Bibliothek für den Lookup. Sie sollte verschiedene Formate verarbeiten können.
const cleanedMac = mac.replace(/[^0-9a-fA-F]/g, ''); // oui() gibt den Vendor-String oder null zurück. Es kann beim ersten Mal die DB laden.
logger.debug({ requestIp, originalMac: mac, cleanedMac }, 'Cleaned MAC address for lookup'); const vendor = await oui(mac); // Verwende await, falls oui.update() im Hintergrund läuft
// Verwende die bereinigte MAC-Adresse für den Lookup if (vendor) {
const result = await macaddress.one(cleanedMac); logger.info({ requestIp, mac, vendor }, 'MAC lookup successful');
res.json({ success: true, mac: mac, vendor: vendor });
if (result && result.vendor) { // Prüfe, ob Ergebnis und Vendor existieren
logger.info({ requestIp, mac: cleanedMac, vendor: result.vendor }, 'MAC lookup successful');
res.json({ success: true, mac: mac, vendor: result.vendor }); // Gib die originale MAC zurück
} else { } else {
logger.info({ requestIp, mac: cleanedMac }, 'MAC lookup successful, but no vendor found'); logger.info({ requestIp, mac }, 'MAC lookup successful, but no vendor found');
res.json({ success: true, mac: mac, vendor: null, message: 'Vendor not found for this MAC address prefix.' }); // Gib die originale MAC zurück res.json({ success: true, mac: mac, vendor: null, message: 'Vendor not found for this MAC address prefix.' });
} }
} catch (error) { } catch (error) {
// Fehler können auftreten, wenn die interne DB nicht geladen werden kann // Fehler können auftreten, wenn die interne DB nicht geladen werden kann oder andere Probleme auftreten
// oder die Eingabe ungültig ist (trotz Bereinigung, falls die Bibliothek weitere Prüfungen macht)
logger.error({ requestIp, mac, error: error.message }, 'MAC lookup failed'); logger.error({ requestIp, mac, error: error.message }, 'MAC lookup failed');
res.status(500).json({ success: false, error: `MAC lookup failed: ${error.message}` }); res.status(500).json({ success: false, error: `MAC lookup failed: ${error.message}` });
} }