From b45b810eb947697022542148e9e36351b07779cc Mon Sep 17 00:00:00 2001 From: marco370 <48531002-marco370@users.noreply.replit.com> Date: Mon, 16 Feb 2026 18:35:39 +0000 Subject: [PATCH] Improve IP blocking process by increasing timeouts and adding detailed logging Increase auto-block timeout to 300s, update systemd service timeout to 480s, and reduce individual MikroTik request timeout to 8s. Add per-router logging for blocking operations. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528 Replit-Commit-Checkpoint-Type: intermediate_checkpoint Replit-Commit-Event-Id: 455f4d8c-e90c-45d5-a7f1-e5f98b1345d3 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/cJuycQ5 --- deployment/systemd/ids-auto-block.service | 4 +-- python_ml/auto_block.py | 4 +-- server/mikrotik.ts | 34 ++++++++++++++++++++--- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/deployment/systemd/ids-auto-block.service b/deployment/systemd/ids-auto-block.service index d6bc7e5..922a435 100644 --- a/deployment/systemd/ids-auto-block.service +++ b/deployment/systemd/ids-auto-block.service @@ -22,8 +22,8 @@ SyslogIdentifier=ids-auto-block NoNewPrivileges=true PrivateTmp=true -# Timeout: max 5 minuti per detection+blocking -TimeoutStartSec=300 +# Timeout: max 8 minuti per detection+blocking +TimeoutStartSec=480 [Install] WantedBy=multi-user.target diff --git a/python_ml/auto_block.py b/python_ml/auto_block.py index f496ea2..b2aaf1c 100644 --- a/python_ml/auto_block.py +++ b/python_ml/auto_block.py @@ -58,7 +58,7 @@ def auto_block(): "limit": 200, "list_name": "ddos_blocked" }, - timeout=120 + timeout=300 ) if response.status_code == 200: @@ -85,7 +85,7 @@ def auto_block(): print(f"[{timestamp}] ERRORE: Node.js backend non raggiungibile su {NODE_API_URL}") return 1 except requests.exceptions.Timeout: - print(f"[{timestamp}] ERRORE: Timeout blocco IP (120s)") + print(f"[{timestamp}] ERRORE: Timeout blocco IP (300s)") return 1 except Exception as e: print(f"[{timestamp}] ERRORE imprevisto: {type(e).__name__}: {e}") diff --git a/server/mikrotik.ts b/server/mikrotik.ts index a132014..94ed552 100644 --- a/server/mikrotik.ts +++ b/server/mikrotik.ts @@ -20,7 +20,7 @@ async function mikrotikRequest( method: string, path: string, body?: any, - timeoutMs: number = 10000 + timeoutMs: number = 8000 ): Promise<{ status: number; data: any }> { const useHttps = router.apiPort === 443; const protocol = useHttps ? "https" : "http"; @@ -239,14 +239,21 @@ export async function bulkBlockIps( return { blocked: 0, failed: 0, skipped: 0, details: [] }; } - console.log(`[BULK-BLOCK] Starting: ${ipList.length} IPs on ${enabled.length} routers`); + console.log(`[BULK-BLOCK] Starting: ${ipList.length} IPs on ${enabled.length} routers (${enabled.map(r => r.ipAddress).join(', ')})`); + + const routerStatus = new Map(); + for (const r of enabled) { + routerStatus.set(r.ipAddress, { ok: 0, fail: 0, skip: 0 }); + } const existingCache = new Map>(); await Promise.allSettled( enabled.map(async (router) => { + const start = Date.now(); const existing = await getExistingBlockedIps(router, listName); + const elapsed = Date.now() - start; existingCache.set(router.ipAddress, existing); - console.log(`[BULK-BLOCK] Router ${router.ipAddress}: ${existing.size} IPs already in list`); + console.log(`[BULK-BLOCK] Router ${router.ipAddress}: ${existing.size} IPs already in list (${elapsed}ms)`); }) ); @@ -279,8 +286,23 @@ export async function bulkBlockIps( const routerResults = await Promise.allSettled( enabled.map(async (router) => { const existing = existingCache.get(router.ipAddress) || new Set(); - if (existing.has(ip)) return true; + if (existing.has(ip)) { + const st = routerStatus.get(router.ipAddress); + if (st) st.skip++; + return true; + } + const start = Date.now(); const result = await addToAddressList(router, ip, listName, `${commentPrefix} ${ip}`, timeoutDuration); + const elapsed = Date.now() - start; + const st = routerStatus.get(router.ipAddress); + if (result.success) { + if (st) st.ok++; + } else { + if (st) st.fail++; + if (elapsed > 5000) { + console.warn(`[BULK-BLOCK] SLOW: Router ${router.ipAddress} took ${elapsed}ms for IP ${ip}: ${result.error}`); + } + } return result.success; }) ); @@ -311,6 +333,10 @@ export async function bulkBlockIps( details.push({ ip, status: "already_blocked" }); } + // Report per-router + routerStatus.forEach((st, routerIp) => { + console.log(`[BULK-BLOCK] Router ${routerIp}: ${st.ok} blocked, ${st.fail} failed, ${st.skip} skipped`); + }); console.log(`[BULK-BLOCK] Done: ${blocked} blocked, ${failed} failed, ${skippedIps.length} skipped`); return { blocked, failed, skipped: skippedIps.length, details };