fix sentry error and mac-vendor utils issue

This commit is contained in:
2025-09-23 20:01:19 +02:00
parent dfdfbbdf68
commit bb7fa35496
3 changed files with 14 additions and 29 deletions

View File

@@ -1,9 +1,8 @@
// backend/routes/macLookup.js
const express = require('express'); const express = require('express');
const Sentry = require("@sentry/node"); const Sentry = require("@sentry/node");
const macaddress = require('macaddress'); const macaddress = require('macaddress');
const pino = require('pino'); const pino = require('pino');
const { isValidMacAddress } = require('../utils'); // Wir fügen diese neue Funktion hinzu const { isValidMacAddress } = require('../utils');
const logger = pino({ level: process.env.LOG_LEVEL || 'info' }); const logger = pino({ level: process.env.LOG_LEVEL || 'info' });
const router = express.Router(); const router = express.Router();
@@ -21,7 +20,16 @@ router.get('/', async (req, res) => {
} }
try { try {
const vendor = await macaddress.lookup(mac); // Wrap the callback-based function in a Promise to use it with async/await
const vendor = await new Promise((resolve, reject) => {
macaddress.lookup(mac, (err, vendorString) => {
if (err) {
return reject(err);
}
resolve(vendorString);
});
});
if (vendor) { if (vendor) {
logger.info({ requestIp, mac, vendor }, 'MAC lookup successful'); logger.info({ requestIp, mac, vendor }, 'MAC lookup successful');
res.json({ success: true, mac, vendor }); res.json({ success: true, mac, vendor });

View File

@@ -1,4 +1,3 @@
// backend/routes/portScan.js
const express = require('express'); const express = require('express');
const Sentry = require("@sentry/node"); const Sentry = require("@sentry/node");
const pino = require('pino'); const pino = require('pino');
@@ -27,10 +26,6 @@ router.get('/', (req, res) => {
return res.status(403).json({ success: false, error: 'Operations on private or local IP addresses are not allowed.' }); return res.status(403).json({ success: false, error: 'Operations on private or local IP addresses are not allowed.' });
} }
Sentry.configureScope(scope => {
scope.setContext("port_scan_details", { targetIp, requestIp });
});
res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive'); res.setHeader('Connection', 'keep-alive');
@@ -65,7 +60,8 @@ router.get('/', (req, res) => {
} }
} catch (error) { } catch (error) {
logger.error({ requestIp, targetIp, error: error.message }, 'Error during port scan stream'); logger.error({ requestIp, targetIp, error: error.message }, 'Error during port scan stream');
Sentry.captureException(error); // Add context directly to the captureException call
Sentry.captureException(error, { extra: { requestIp, targetIp } });
if (!isConnectionClosed) { if (!isConnectionClosed) {
sendEvent('error', { error: 'An unexpected error occurred during the scan.' }); sendEvent('error', { error: 'An unexpected error occurred during the scan.' });
} }

View File

@@ -1,4 +1,3 @@
// backend/routes/traceroute.js
const express = require('express'); const express = require('express');
const Sentry = require("@sentry/node"); const Sentry = require("@sentry/node");
const { spawn } = require('child_process'); const { spawn } = require('child_process');
@@ -37,13 +36,6 @@ router.get('/', (req, res) => {
return res.status(403).json({ success: false, error: 'Operations on private or local IP addresses are not allowed.' }); return res.status(403).json({ success: false, error: 'Operations on private or local IP addresses are not allowed.' });
} }
// Add specific context for this request to the current Sentry scope
// Errors/Messages captured later in this request handler will have this context.
Sentry.configureScope(scope => {
scope.setContext("traceroute_details", { targetIp: targetIp, requestIp: requestIp });
});
try { try {
logger.info({ requestIp, targetIp }, `Starting traceroute stream...`); logger.info({ requestIp, targetIp }, `Starting traceroute stream...`);
res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Content-Type', 'text/event-stream');
@@ -77,7 +69,6 @@ router.get('/', (req, res) => {
Sentry.captureException(e, { level: 'warning', extra: { requestIp, targetIp, event } }); Sentry.captureException(e, { level: 'warning', extra: { requestIp, targetIp, event } });
if (proc && !proc.killed) proc.kill(); if (proc && !proc.killed) proc.kill();
if (!res.writableEnded) res.end(); if (!res.writableEnded) res.end();
// No manual transaction finishing needed here
} }
}; };
@@ -88,10 +79,8 @@ router.get('/', (req, res) => {
lines.forEach(line => { lines.forEach(line => {
const parsed = parseTracerouteLine(line); const parsed = parseTracerouteLine(line);
if (parsed) { if (parsed) {
// logger.debug({ requestIp, targetIp, hop: parsed.hop, ip: parsed.ip }, 'Sending hop data');
sendEvent('hop', parsed); sendEvent('hop', parsed);
} else if (line.trim()) { } else if (line.trim()) {
// logger.debug({ requestIp, targetIp, message: line.trim() }, 'Sending info data');
sendEvent('info', { message: line.trim() }); sendEvent('info', { message: line.trim() });
} }
}); });
@@ -110,7 +99,6 @@ router.get('/', (req, res) => {
Sentry.captureException(err, { extra: { requestIp, targetIp } }); // Capture original error Sentry.captureException(err, { extra: { requestIp, targetIp } }); // Capture original error
sendEvent('error', { error: `Failed to start traceroute: ${errorMsg}` }); sendEvent('error', { error: `Failed to start traceroute: ${errorMsg}` });
if (!res.writableEnded) res.end(); if (!res.writableEnded) res.end();
// No manual transaction finishing needed here
}); });
proc.on('close', (code) => { proc.on('close', (code) => {
@@ -125,30 +113,23 @@ router.get('/', (req, res) => {
logger.error({ requestIp, targetIp, exitCode: code }, errorMsg); logger.error({ requestIp, targetIp, exitCode: code }, errorMsg);
Sentry.captureMessage('Traceroute command failed', { level: 'error', extra: { requestIp, targetIp, exitCode: code } }); Sentry.captureMessage('Traceroute command failed', { level: 'error', extra: { requestIp, targetIp, exitCode: code } });
sendEvent('error', { error: errorMsg }); sendEvent('error', { error: errorMsg });
// Transaction status will be inferred by Sentry based on errors captured
} else { } else {
logger.info({ requestIp, targetIp }, `Traceroute stream completed successfully.`); logger.info({ requestIp, targetIp }, `Traceroute stream completed successfully.`);
// Transaction status will likely be 'ok' if no errors were captured
} }
sendEvent('end', { exitCode: code }); sendEvent('end', { exitCode: code });
if (!res.writableEnded) res.end(); if (!res.writableEnded) res.end();
// No manual transaction finishing needed here
}); });
req.on('close', () => { req.on('close', () => {
logger.info({ requestIp, targetIp }, 'Client disconnected from traceroute stream, killing process.'); logger.info({ requestIp, targetIp }, 'Client disconnected from traceroute stream, killing process.');
if (proc && !proc.killed) proc.kill(); if (proc && !proc.killed) proc.kill();
if (!res.writableEnded) res.end(); if (!res.writableEnded) res.end();
// Sentry transaction might be marked as 'cancelled' automatically or based on timeout
// No manual transaction finishing needed here
}); });
} catch (error) { } catch (error) {
// This catch handles errors during the initial setup (e.g., spawn fails immediately)
const errorMsg = getErrorMessage(error, 'Failed to initiate traceroute due to an internal server error.'); const errorMsg = getErrorMessage(error, 'Failed to initiate traceroute due to an internal server error.');
logger.error({ requestIp, targetIp, error: errorMsg, stack: error.stack }, 'Error setting up traceroute stream'); logger.error({ requestIp, targetIp, error: errorMsg, stack: error.stack }, 'Error setting up traceroute stream');
Sentry.captureException(error, { extra: { requestIp, targetIp } }); // Capture original error Sentry.captureException(error, { extra: { requestIp, targetIp } });
// No manual transaction finishing needed here
if (!res.headersSent) { if (!res.headersSent) {
res.status(500).json({ success: false, error: `Failed to initiate traceroute: ${errorMsg}` }); res.status(500).json({ success: false, error: `Failed to initiate traceroute: ${errorMsg}` });