From 16d13d6bee30dddd4b62367d2f5685fb91b4842a Mon Sep 17 00:00:00 2001 From: marco370 <48531002-marco370@users.noreply.replit.com> Date: Fri, 2 Jan 2026 15:46:56 +0000 Subject: [PATCH] Add ability to automatically unblock IPs when added to whitelist Add an endpoint to proxy IP unblocking requests to the ML backend and implement automatic unblocking from routers when an IP is added to the whitelist. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528 Replit-Commit-Checkpoint-Type: intermediate_checkpoint Replit-Commit-Event-Id: 67148eaa-9f6a-42a9-a7bb-a72453425d4c Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/8i4FqXF --- server/routes.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/server/routes.ts b/server/routes.ts index f31007a..3c8ce6e 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -130,11 +130,58 @@ export async function registerRoutes(app: Express): Promise { try { const validatedData = insertWhitelistSchema.parse(req.body); const item = await storage.createWhitelist(validatedData); + + // Auto-unblock from routers when adding to whitelist + const mlBackendUrl = process.env.ML_BACKEND_URL || 'http://localhost:8000'; + try { + const unblockResponse = await fetch(`${mlBackendUrl}/unblock-ip`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ ip_address: validatedData.ipAddress }) + }); + if (unblockResponse.ok) { + const result = await unblockResponse.json(); + console.log(`[WHITELIST] Auto-unblocked ${validatedData.ipAddress} from ${result.unblocked_from} routers`); + } + } catch (unblockError) { + // Don't fail if ML backend is unavailable + console.log(`[WHITELIST] ML backend unavailable for auto-unblock: ${unblockError}`); + } + res.json(item); } catch (error) { res.status(400).json({ error: "Invalid whitelist data" }); } }); + + // Unblock IP from all routers (proxy to ML backend) + app.post("/api/unblock-ip", async (req, res) => { + try { + const { ipAddress, listName = "ddos_blocked" } = req.body; + + if (!ipAddress) { + return res.status(400).json({ error: "IP address is required" }); + } + + const mlBackendUrl = process.env.ML_BACKEND_URL || 'http://localhost:8000'; + const response = await fetch(`${mlBackendUrl}/unblock-ip`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ ip_address: ipAddress, list_name: listName }) + }); + + if (!response.ok) { + const error = await response.text(); + return res.status(response.status).json({ error: error || "Failed to unblock IP" }); + } + + const result = await response.json(); + res.json(result); + } catch (error: any) { + console.error('[UNBLOCK] Error:', error); + res.status(500).json({ error: error.message || "Failed to unblock IP from routers" }); + } + }); app.delete("/api/whitelist/:id", async (req, res) => { try {