From f47f53fd22f832f902c053157dea3828f38e8745 Mon Sep 17 00:00:00 2001 From: Sam Chan Sam Date: Wed, 18 Sep 2024 22:07:48 -0700 Subject: [PATCH] - when looking for a catch all config, only consider configs that can route to the given ip --- .../celzero/bravedns/service/WireguardManager.kt | 14 +++++++++----- .../celzero/bravedns/service/BraveVPNService.kt | 6 ++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/src/full/java/com/celzero/bravedns/service/WireguardManager.kt b/app/src/full/java/com/celzero/bravedns/service/WireguardManager.kt index f56e40b91..442a080b1 100644 --- a/app/src/full/java/com/celzero/bravedns/service/WireguardManager.kt +++ b/app/src/full/java/com/celzero/bravedns/service/WireguardManager.kt @@ -495,7 +495,9 @@ object WireguardManager : KoinComponent { } } Logger.d(LOG_TAG_PROXY, "optimalCatchAllConfig: fetching new wgId for uid: $uid") - val catchAllList = mappings.filter { it.isActive && it.isCatchAll } + val catchAllList = mappings.filter { + val id = ProxyManager.ID_WG_BASE + it.id + it.isActive && it.isCatchAll && VpnController.canRouteIp(id, ip, false) } catchAllList.forEach { if (isProxyConnectionValid(it.id, ip)) { // note the uid and wgid in a cache, so that we can use it for further requests @@ -507,7 +509,7 @@ object WireguardManager : KoinComponent { // none of the catch-all has valid connection, send ping to all catch-all configs pingCatchAllConfigs(catchAllList) // return any catch-all config - return catchAllList.random().id + return catchAllList.randomOrNull()?.id } private fun pingCatchAllConfigs(catchAllConfigs: List) { @@ -934,8 +936,10 @@ object WireguardManager : KoinComponent { return mappings.find { it.oneWireGuard && it.isActive }?.id } - suspend fun getOptimalCatchAllConfigId(): Int? { - val configs = mappings.filter { it.isCatchAll && it.isActive } + suspend fun getOptimalCatchAllConfigId(ip: String?): Int? { + val configs = mappings.filter { + val id = ProxyManager.ID_WG_BASE + it.id + it.isCatchAll && it.isActive && ((ip == null) || VpnController.canRouteIp(id, ip, false)) } configs.forEach { if (isValidLastOk(it.id)) { Logger.d(LOG_TAG_PROXY, "found optimal catch all config: ${it.id}") @@ -944,7 +948,7 @@ object WireguardManager : KoinComponent { } Logger.d(LOG_TAG_PROXY, "no optimal catch all config found, returning any catchall") // if no catch-all config is active, return any catch-all config - return configs.random()?.id + return configs.randomOrNull()?.id } private fun io(f: suspend () -> Unit) { 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 8b562658a..ee930c27d 100644 --- a/app/src/main/java/com/celzero/bravedns/service/BraveVPNService.kt +++ b/app/src/main/java/com/celzero/bravedns/service/BraveVPNService.kt @@ -3025,7 +3025,8 @@ class BraveVPNService : ProxyManager.ID_WG_BASE + id } else if (WireguardManager.catchAllEnabled()) { // if the enabled wireguard is catchall-wireguard, then return wireguard id - val id = WireguardManager.getOptimalCatchAllConfigId() ?: return Backend.Base + val endpoint = appConfig.getSelectedDnsProxyDetails() + val id = WireguardManager.getOptimalCatchAllConfigId(endpoint?.proxyIP) ?: return Backend.Base ProxyManager.ID_WG_BASE + id } else { // if the enabled wireguard is not one-wireguard, then return base @@ -3033,7 +3034,8 @@ class BraveVPNService : } } else if (WireguardManager.catchAllEnabled()) { // check even if wireguard is not enabled // if the enabled wireguard is catchall-wireguard, then return wireguard id - val id = WireguardManager.getOptimalCatchAllConfigId() ?: return Backend.Base + val endpoint = appConfig.getSelectedDnsProxyDetails() + val id = WireguardManager.getOptimalCatchAllConfigId(endpoint?.proxyIP) ?: return Backend.Base // in this case, no need to check if the proxy is available ProxyManager.ID_WG_BASE + id } else {