From bb7fa354960b36e62b5fa3cae11f7d7e96f8f140 Mon Sep 17 00:00:00 2001 From: MrUnknownDE Date: Tue, 23 Sep 2025 20:01:19 +0200 Subject: [PATCH] fix sentry error and mac-vendor utils issue --- backend/routes/macLookup.js | 14 +++++++++++--- backend/routes/portScan.js | 8 ++------ backend/routes/traceroute.js | 21 +-------------------- 3 files changed, 14 insertions(+), 29 deletions(-) diff --git a/backend/routes/macLookup.js b/backend/routes/macLookup.js index 4a7e77f..1761fb3 100644 --- a/backend/routes/macLookup.js +++ b/backend/routes/macLookup.js @@ -1,9 +1,8 @@ -// backend/routes/macLookup.js const express = require('express'); const Sentry = require("@sentry/node"); const macaddress = require('macaddress'); 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 router = express.Router(); @@ -21,7 +20,16 @@ router.get('/', async (req, res) => { } 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) { logger.info({ requestIp, mac, vendor }, 'MAC lookup successful'); res.json({ success: true, mac, vendor }); diff --git a/backend/routes/portScan.js b/backend/routes/portScan.js index 22c643e..02f1447 100644 --- a/backend/routes/portScan.js +++ b/backend/routes/portScan.js @@ -1,4 +1,3 @@ -// backend/routes/portScan.js const express = require('express'); const Sentry = require("@sentry/node"); 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.' }); } - Sentry.configureScope(scope => { - scope.setContext("port_scan_details", { targetIp, requestIp }); - }); - res.setHeader('Content-Type', 'text/event-stream'); res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Connection', 'keep-alive'); @@ -65,7 +60,8 @@ router.get('/', (req, res) => { } } catch (error) { 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) { sendEvent('error', { error: 'An unexpected error occurred during the scan.' }); } diff --git a/backend/routes/traceroute.js b/backend/routes/traceroute.js index 75c0af1..9068a4e 100644 --- a/backend/routes/traceroute.js +++ b/backend/routes/traceroute.js @@ -1,4 +1,3 @@ -// backend/routes/traceroute.js const express = require('express'); const Sentry = require("@sentry/node"); 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.' }); } - // 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 { logger.info({ requestIp, targetIp }, `Starting traceroute 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 } }); if (proc && !proc.killed) proc.kill(); if (!res.writableEnded) res.end(); - // No manual transaction finishing needed here } }; @@ -88,10 +79,8 @@ router.get('/', (req, res) => { lines.forEach(line => { const parsed = parseTracerouteLine(line); if (parsed) { - // logger.debug({ requestIp, targetIp, hop: parsed.hop, ip: parsed.ip }, 'Sending hop data'); sendEvent('hop', parsed); } else if (line.trim()) { - // logger.debug({ requestIp, targetIp, message: line.trim() }, 'Sending info data'); sendEvent('info', { message: line.trim() }); } }); @@ -110,7 +99,6 @@ router.get('/', (req, res) => { Sentry.captureException(err, { extra: { requestIp, targetIp } }); // Capture original error sendEvent('error', { error: `Failed to start traceroute: ${errorMsg}` }); if (!res.writableEnded) res.end(); - // No manual transaction finishing needed here }); proc.on('close', (code) => { @@ -125,30 +113,23 @@ router.get('/', (req, res) => { logger.error({ requestIp, targetIp, exitCode: code }, errorMsg); Sentry.captureMessage('Traceroute command failed', { level: 'error', extra: { requestIp, targetIp, exitCode: code } }); sendEvent('error', { error: errorMsg }); - // Transaction status will be inferred by Sentry based on errors captured } else { logger.info({ requestIp, targetIp }, `Traceroute stream completed successfully.`); - // Transaction status will likely be 'ok' if no errors were captured } sendEvent('end', { exitCode: code }); if (!res.writableEnded) res.end(); - // No manual transaction finishing needed here }); req.on('close', () => { logger.info({ requestIp, targetIp }, 'Client disconnected from traceroute stream, killing process.'); if (proc && !proc.killed) proc.kill(); 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) { - // 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.'); logger.error({ requestIp, targetIp, error: errorMsg, stack: error.stack }, 'Error setting up traceroute stream'); - Sentry.captureException(error, { extra: { requestIp, targetIp } }); // Capture original error - // No manual transaction finishing needed here + Sentry.captureException(error, { extra: { requestIp, targetIp } }); if (!res.headersSent) { res.status(500).json({ success: false, error: `Failed to initiate traceroute: ${errorMsg}` });