@@ -27,9 +27,12 @@ class NetworkAnalyzerRepositoryImpl @Inject constructor(
2727
2828 companion object {
2929 private const val TAG = " NetworkAnalyzerRepo"
30- // 7-day and 30-day retention in milliseconds
30+ // Retention windows
3131 private const val TRAFFIC_RETENTION_MS = 7L * 24 * 60 * 60 * 1000
3232 private const val BLACKLIST_RETENTION_MS = 30L * 24 * 60 * 60 * 1000
33+ // Auto-blacklist rule: MALICIOUS_THRESHOLD hits within WINDOW_MS → permanent block
34+ private const val AUTO_BLACKLIST_THRESHOLD = 3
35+ private const val AUTO_BLACKLIST_WINDOW_MS = 24L * 60 * 60 * 1000 // 24 hours
3336 }
3437
3538 // ── Traffic Logging ────────────────────────────────────────────────────────
@@ -49,6 +52,36 @@ class NetworkAnalyzerRepositoryImpl @Inject constructor(
4952 blocked = result.shouldBlock
5053 )
5154 )
55+
56+ // Auto-blacklist: if this domain has been MALICIOUS 3+ times in 24h, block permanently
57+ if (result.threatLevel == ThreatLevel .MALICIOUS ) {
58+ val target = packet.destinationDomain ? : packet.destinationIp
59+ checkAutoBlacklist(target)
60+ }
61+ }
62+
63+ /* *
64+ * Checks if [domain] has been flagged MALICIOUS [AUTO_BLACKLIST_THRESHOLD]+ times
65+ * within the last [AUTO_BLACKLIST_WINDOW_MS]. If so, permanently blacklists it.
66+ * This enforces the plan's "3× MALICIOUS in 24h → auto-blacklist" rule.
67+ */
68+ private suspend fun checkAutoBlacklist (domain : String ) {
69+ val since = System .currentTimeMillis() - AUTO_BLACKLIST_WINDOW_MS
70+ val hitCount = trafficDao.countMaliciousForDomainSince(domain, since)
71+
72+ if (hitCount >= AUTO_BLACKLIST_THRESHOLD ) {
73+ val alreadyBlocked = blacklistDao.getByDomain(domain) != null
74+ if (! alreadyBlocked) {
75+ blacklistDao.insert(
76+ NetworkBlacklistEntity (
77+ domain = domain,
78+ reason = " Auto-blacklisted: flagged MALICIOUS $hitCount times in 24h" ,
79+ blockPermanently = true
80+ )
81+ )
82+ Log .w(TAG , " 🚫 AUTO-BLACKLISTED: $domain ($hitCount MALICIOUS hits in 24h)" )
83+ }
84+ }
5285 }
5386
5487 override fun getRecentTraffic (limit : Int ): Flow <List <NetworkTrafficEntity >> =
0 commit comments