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 {