Skip to content

Commit

Permalink
Merge pull request #1707 from samthesamman/fix-catch-all-configs
Browse files Browse the repository at this point in the history
Fix for incorrect wg config being retrieved when finding a catch-all config
  • Loading branch information
hussainmohd-a authored Oct 2, 2024
2 parents 52ac57c + f47f53f commit 108d347
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<WgConfigFilesImmutable>) {
Expand Down Expand Up @@ -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}")
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3025,15 +3025,17 @@ 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
Backend.Base
}
} 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 {
Expand Down

0 comments on commit 108d347

Please sign in to comment.