From 64f77187d856636ec116dac8f03a1194a45ce6f4 Mon Sep 17 00:00:00 2001 From: ypratama Date: Fri, 4 Sep 2026 11:01:40 +0700 Subject: [PATCH] Feat: Auto cleanup old devices/flows when Subnet Config is updated --- backend/routes/dashboard/agents.js | 36 ++++++++++++++++++++++++++++++ docker-compose.prod.yml | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/backend/routes/dashboard/agents.js b/backend/routes/dashboard/agents.js index 6e6d53f..add0df7 100644 --- a/backend/routes/dashboard/agents.js +++ b/backend/routes/dashboard/agents.js @@ -239,6 +239,27 @@ router.get('/agents/:uuid/subnets', async (req, res) => { // ─── PUT /api/dashboard/agents/:uuid/subnets ───────────────────────────────── // Simpan konfigurasi subnet yang diizinkan untuk agent tertentu // Body: { allowed_subnets: ["192.168.1", "10.21"] } +function ipToLong(ip) { + return ip.split('.').reduce((acc, octet) => (acc << 8) + parseInt(octet, 10), 0) >>> 0; +} + +function ipMatchesSubnets(ip, subnets) { + if (!subnets || subnets.length === 0) return true; + if (!ip) return false; + return subnets.some(subnet => { + if (subnet.includes('/')) { + try { + const [range, bitsStr] = subnet.split('/'); + const bits = parseInt(bitsStr, 10); + if (isNaN(bits) || bits < 0 || bits > 32) return false; + const mask = bits === 0 ? 0 : (~0 << (32 - bits)) >>> 0; + return (ipToLong(ip) & mask) === (ipToLong(range) & mask); + } catch (e) { return false; } + } else { return ip === subnet; } + }); +} + +// PUT /api/dashboard/agents/:uuid/subnets router.put('/agents/:uuid/subnets', async (req, res) => { try { const allowedRoles = ['SUPER_ADMIN', 'TENANT_ADMIN', 'COMPANY_ADMIN']; @@ -251,6 +272,21 @@ router.put('/agents/:uuid/subnets', async (req, res) => { { uuid: req.params.uuid }, { $set: { allowed_subnets: subnets, subnets_updated_at: new Date() } } ); + + // Auto-cleanup background task + if (subnets.length > 0) { + setTimeout(async () => { + try { + const ips = await db.collection('devicestats').distinct('ip_address', { agent_uuid: req.params.uuid }); + const invalidIps = ips.filter(ip => !ipMatchesSubnets(ip, subnets)); + if (invalidIps.length > 0) { + await db.collection('devicestats').deleteMany({ agent_uuid: req.params.uuid, ip_address: { $in: invalidIps } }); + await db.collection('flows').deleteMany({ agent_uuid: req.params.uuid, src_ip: { $in: invalidIps } }); + } + } catch (e) { console.error('Auto-cleanup error:', e); } + }, 100); + } + res.json({ ok: true, data: { allowed_subnets: subnets } }); } catch (err) { res.status(500).json({ ok: false, error: err.message }); diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index d216b7c..1069a61 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -38,7 +38,7 @@ services: environment: - NODE_ENV=production - NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL} - - INTERNAL_API_URL=${INTERNAL_API_URL:-http://backone-inspect-backend-bun-0:3001} + - INTERNAL_API_URL=${INTERNAL_API_URL:-http://backend:3001} - JWT_SECRET=${JWT_SECRET:-super-secret-backone-key} - MONGODB_URI=${MONGODB_URI} - BACKONE_API_KEY=${NETIFY_API_KEY:-sk_db_source2}