From 46f08da505e234c098626208682f2b12dce21920 Mon Sep 17 00:00:00 2001 From: ignoramous Date: Fri, 24 Sep 2021 00:39:49 +0530 Subject: [PATCH] Do not mistake UnknownApp for NewApp When uid points to missing / unknown app, do not treat it as a new (untracked) app and apply firewall rules applicable to those (untracked / new apps) --- .../bravedns/service/BraveVPNService.kt | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/celzero/bravedns/service/BraveVPNService.kt b/app/src/main/java/com/celzero/bravedns/service/BraveVPNService.kt index 5cf189551..34973bbb9 100644 --- a/app/src/main/java/com/celzero/bravedns/service/BraveVPNService.kt +++ b/app/src/main/java/com/celzero/bravedns/service/BraveVPNService.kt @@ -210,14 +210,6 @@ class BraveVPNService : VpnService(), ConnectionMonitor.NetworkListener, Protect val uid = connInfo.uid val appStatus = FirewallManager.appStatus(uid) - if (settingUpOrbot.get()) { - if (OrbotHelper.ORBOT_PACKAGE_NAME == FirewallManager.getPackageNameByUid(uid)) { - return FirewallRuleset.RULE9B - } else { - // fall-through - } - } - if (allowOrbot(uid)) { return FirewallRuleset.RULE9B } @@ -304,7 +296,7 @@ class BraveVPNService : VpnService(), ConnectionMonitor.NetworkListener, Protect } } - private fun waitAndCheckIfNewAppAllowed(uid: Int): Boolean { + private fun waitAndCheckIfUidBlocked(uid: Int): Boolean { val allowed = testWithBackoff { FirewallManager.hasUid(uid) && !FirewallManager.isUidFirewalled(uid) } @@ -312,10 +304,10 @@ class BraveVPNService : VpnService(), ConnectionMonitor.NetworkListener, Protect } private fun newAppBlocked(uid: Int): Boolean { - return if (!persistentState.blockNewlyInstalledApp) { + return if (!persistentState.blockNewlyInstalledApp || isMissingOrInvalidUid(uid)) { false } else { - waitAndCheckIfNewAppAllowed(uid) + waitAndCheckIfUidBlocked(uid) } } @@ -333,10 +325,10 @@ class BraveVPNService : VpnService(), ConnectionMonitor.NetworkListener, Protect } } - private fun testWithBackoff(stallSec: Long = 20, waitSec: Long = 10, + private fun testWithBackoff(stallSec: Long = 20, durationSec: Long = 10, test: () -> Boolean): Boolean { val minWaitMs = TimeUnit.SECONDS.toMillis(stallSec) - var remainingWaitMs = TimeUnit.SECONDS.toMillis(waitSec) + var remainingWaitMs = TimeUnit.SECONDS.toMillis(durationSec) var attempt = 0 while (remainingWaitMs > 0) { if (test()) return true