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
This commit is contained in:
parent
a4bf75394a
commit
16d13d6bee
@ -130,12 +130,59 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
||||
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 {
|
||||
const success = await storage.deleteWhitelist(req.params.id);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user