Skip to content

Commit 2987824

Browse files
Merge pull request #1329 from hussainmohd-a/v055e
v055e
2 parents 8cae280 + 14f186a commit 2987824

File tree

7 files changed

+47
-23
lines changed

7 files changed

+47
-23
lines changed

app/src/full/java/com/celzero/bravedns/ui/HomeScreenActivity.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import com.celzero.bravedns.backup.BackupHelper.Companion.BACKUP_FILE_EXTN
5151
import com.celzero.bravedns.backup.BackupHelper.Companion.INTENT_RESTART_APP
5252
import com.celzero.bravedns.backup.BackupHelper.Companion.INTENT_SCHEME
5353
import com.celzero.bravedns.backup.RestoreAgent
54-
import com.celzero.bravedns.data.AppConfig
5554
import com.celzero.bravedns.database.RefreshDatabase
5655
import com.celzero.bravedns.databinding.ActivityHomeScreenBinding
5756
import com.celzero.bravedns.service.AppUpdater
@@ -90,7 +89,6 @@ class HomeScreenActivity : AppCompatActivity(R.layout.activity_home_screen) {
9089
private val b by viewBinding(ActivityHomeScreenBinding::bind)
9190

9291
private val persistentState by inject<PersistentState>()
93-
private val appConfig by inject<AppConfig>()
9492
private val appUpdateManager by inject<AppUpdater>()
9593
private val rdb by inject<RefreshDatabase>()
9694

@@ -101,8 +99,10 @@ class HomeScreenActivity : AppCompatActivity(R.layout.activity_home_screen) {
10199

102100
private var biometricPromptRetryCount = 1
103101
private var onResumeCalledAlready = false
104-
private val ON_RESUME_CALLED_PREFERENCE_KEY = "onResumeCalled"
105-
102+
companion object {
103+
private const val ON_RESUME_CALLED_PREFERENCE_KEY = "onResumeCalled"
104+
}
105+
106106
// TODO - #324 - Usage of isDarkTheme() in all activities.
107107
private fun Context.isDarkThemeOn(): Boolean {
108108
return resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK ==

app/src/main/AndroidManifest.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools"
44
package="com.celzero.bravedns"
5-
android:versionCode="35"
6-
android:versionName="v055d"> <!-- 35(v055d) -->
5+
android:versionCode="36"
6+
android:versionName="v055e"> <!-- 36(v055e) -->
77

88
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
99
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />

app/src/main/java/com/celzero/bravedns/data/AppConfig.kt

+19-4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ internal constructor(
6969
) {
7070
private var braveModeObserver: MutableLiveData<Int> = MutableLiveData()
7171
private var pcapFilePath: String = ""
72+
private var customSocks5Endpoint: ProxyEndpoint? = null
73+
private var customHttpEndpoint: ProxyEndpoint? = null
74+
private var orbotEndpoint: ProxyEndpoint? = null
7275

7376
companion object {
7477
private var connectedDns: MutableLiveData<String> = MutableLiveData()
@@ -381,7 +384,7 @@ internal constructor(
381384
DnsType.DOT.type -> DnsType.DOT
382385
DnsType.ODOH.type -> DnsType.ODOH
383386
else -> {
384-
Log.wtf(LOG_TAG_VPN, "Invalid dns type mode: ${persistentState.dnsType}")
387+
Log.w(LOG_TAG_VPN, "Invalid dns type mode: ${persistentState.dnsType}")
385388
DnsType.DOH
386389
}
387390
}
@@ -442,15 +445,24 @@ internal constructor(
442445
}
443446

444447
suspend fun getSocks5ProxyDetails(): ProxyEndpoint {
445-
return proxyEndpointRepository.getCustomSocks5Endpoint()
448+
if (customSocks5Endpoint == null) {
449+
customSocks5Endpoint = proxyEndpointRepository.getCustomSocks5Endpoint()
450+
}
451+
return customSocks5Endpoint!!
446452
}
447453

448454
suspend fun getHttpProxyDetails(): ProxyEndpoint {
449-
return proxyEndpointRepository.getHttpProxyDetails()
455+
if (customHttpEndpoint == null) {
456+
customHttpEndpoint = proxyEndpointRepository.getHttpProxyDetails()
457+
}
458+
return customHttpEndpoint!!
450459
}
451460

452461
suspend fun getConnectedOrbotProxy(): ProxyEndpoint? {
453-
return proxyEndpointRepository.getConnectedOrbotProxy()
462+
if (orbotEndpoint == null) {
463+
orbotEndpoint = proxyEndpointRepository.getConnectedOrbotProxy()
464+
}
465+
return orbotEndpoint
454466
}
455467

456468
suspend fun getOrbotSocks5Endpoint(): ProxyEndpoint {
@@ -1039,15 +1051,18 @@ internal constructor(
10391051

10401052
suspend fun updateCustomSocks5Proxy(proxyEndpoint: ProxyEndpoint) {
10411053
proxyEndpointRepository.update(proxyEndpoint)
1054+
customSocks5Endpoint = proxyEndpoint
10421055
addProxy(ProxyType.SOCKS5, ProxyProvider.CUSTOM)
10431056
}
10441057

10451058
suspend fun updateOrbotProxy(proxyEndpoint: ProxyEndpoint) {
10461059
proxyEndpointRepository.update(proxyEndpoint)
1060+
orbotEndpoint = proxyEndpoint
10471061
}
10481062

10491063
suspend fun updateCustomHttpProxy(proxyEndpoint: ProxyEndpoint) {
10501064
proxyEndpointRepository.update(proxyEndpoint)
1065+
customHttpEndpoint = proxyEndpoint
10511066
addProxy(ProxyType.HTTP, ProxyProvider.CUSTOM)
10521067
}
10531068

app/src/main/java/com/celzero/bravedns/net/go/GoVpnAdapter.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ class GoVpnAdapter : KoinComponent {
572572
}
573573

574574
private suspend fun setSocks5TunnelModeIfNeeded(tunProxyMode: AppConfig.TunProxyMode) {
575-
val socksEnabled = AppConfig.ProxyType.of(persistentState.proxyType).isSocks5Enabled()
575+
val socksEnabled = AppConfig.ProxyType.of(appConfig.getProxyType()).isSocks5Enabled()
576576
if (!socksEnabled) return
577577

578578
val socks5: ProxyEndpoint? =

app/src/main/java/com/celzero/bravedns/service/BraveVPNService.kt

+15-9
Original file line numberDiff line numberDiff line change
@@ -1443,7 +1443,13 @@ class BraveVPNService :
14431443
logd("on pref change, key: $key")
14441444
when (key) {
14451445
PersistentState.BRAVE_MODE -> {
1446-
io("braveModeChange") { setTunMode() }
1446+
io("braveModeChange") {
1447+
// change in brave mode, requires restart of the vpn (to set routes in vpn),
1448+
// tunMode (to set the tun mode), and dnsAlg (to update the dns alg) in go
1449+
restartVpnWithNewAppConfig(reason = "braveMode")
1450+
setTunMode()
1451+
updateDnsAlg()
1452+
}
14471453
notificationManager.notify(SERVICE_ID, updateNotificationBuilder())
14481454
}
14491455
PersistentState.LOCAL_BLOCK_LIST -> {
@@ -2995,7 +3001,7 @@ class BraveVPNService :
29953001
}
29963002

29973003
if (trapVpnDns) {
2998-
logd("flow: dns-request, returning Ipn.Base, $uid")
3004+
logd("flow: dns-request, returning ${Backend.Base}, $uid, $connId")
29993005
return@runBlocking persistAndConstructFlowResponse(null, Backend.Base, connId, uid)
30003006
}
30013007

@@ -3043,22 +3049,22 @@ class BraveVPNService :
30433049
val proxyId = "${ProxyManager.ID_WG_BASE}${wgConfig.id}"
30443050
// even if inactive, route connections to wg if lockdown/catch-all is enabled to
30453051
// avoid leaks
3046-
return if (wgConfig.isActive || wgConfig.isLockdown || wgConfig.isCatchAll) {
3052+
if (wgConfig.isActive || wgConfig.isLockdown || wgConfig.isCatchAll) {
30473053
val canRoute = vpnAdapter?.canRouteIp(proxyId, connTracker.destIP, true)
30483054
if (canRoute == true) {
30493055
logd("flow: wg is active/lockdown/catch-all; $proxyId, $connId, $uid")
3050-
persistAndConstructFlowResponse(connTracker, proxyId, connId, uid)
3056+
return persistAndConstructFlowResponse(connTracker, proxyId, connId, uid)
30513057
} else {
30523058
logd("flow: wg is active/lockdown/catch-all, but no route, $connId, $uid")
3053-
persistAndConstructFlowResponse(connTracker, Backend.Base, connId, uid)
3059+
return persistAndConstructFlowResponse(connTracker, Backend.Base, connId, uid)
30543060
}
30553061
} else {
3056-
logd("flow: wg is not active; using base, $connId, $uid")
3057-
persistAndConstructFlowResponse(connTracker, Backend.Base, connId, uid)
3062+
// fall-through, no lockdown/catch-all/active wg found, so proceed with other checks
30583063
}
30593064
}
30603065

30613066
// no need to check for other proxies if the protocol is not TCP or UDP
3067+
// fixme: is this even needed?
30623068
if (
30633069
connTracker.protocol != Protocol.TCP.protocolType &&
30643070
connTracker.protocol != Protocol.UDP.protocolType
@@ -3132,7 +3138,7 @@ class BraveVPNService :
31323138

31333139
// chose socks5 proxy over http proxy
31343140
if (appConfig.isCustomSocks5Enabled()) {
3135-
val endpoint = runBlocking { appConfig.getSocks5ProxyDetails() }
3141+
val endpoint = appConfig.getSocks5ProxyDetails()
31363142
val packageName = FirewallManager.getPackageNameByUid(uid)
31373143
logd("flow: socks5 proxy is enabled, $packageName, ${endpoint.proxyAppName}")
31383144
// do not block the app if the app is set to forward the traffic via socks5 proxy
@@ -3153,7 +3159,7 @@ class BraveVPNService :
31533159
}
31543160

31553161
if (appConfig.isCustomHttpProxyEnabled()) {
3156-
val endpoint = runBlocking { appConfig.getHttpProxyDetails() }
3162+
val endpoint = appConfig.getHttpProxyDetails()
31573163
val packageName = FirewallManager.getPackageNameByUid(uid)
31583164
// do not block the app if the app is set to forward the traffic via http proxy
31593165
if (endpoint.proxyAppName == packageName) {

app/src/main/java/com/celzero/bravedns/service/ConnectionMonitor.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -787,14 +787,15 @@ class ConnectionMonitor(context: Context, networkListener: NetworkListener) :
787787
}
788788

789789
private suspend fun tcp53(nw: Network?, host: String): Boolean {
790+
val timeout = 500 // ms
790791
val port53 = 53 // port
791792
var socket: Socket? = null
792793

793794
try {
794795
socket = Socket()
795796
val s = InetSocketAddress(host, port53)
796797
nw?.bindSocket(socket)
797-
socket.connect(s)
798+
socket.connect(s, timeout)
798799
val c = socket.isConnected
799800
val b = socket.isBound
800801
if (DEBUG)
@@ -816,13 +817,15 @@ class ConnectionMonitor(context: Context, networkListener: NetworkListener) :
816817
}
817818

818819
private suspend fun udp53(nw: Network?, host: String): Boolean {
820+
val timeout = 500 // ms
819821
val port53 = 53 // port
820822
var socket: DatagramSocket? = null
821823

822824
try {
823825
socket = DatagramSocket()
824826
val s = InetSocketAddress(host, port53)
825827
nw?.bindSocket(socket)
828+
socket.soTimeout = timeout
826829
socket.connect(s)
827830
val c = socket.isConnected
828831
val b = socket.isBound

gradle.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ android.nonTransitiveRClass=true
2424
# Enable configuration cache
2525
org.gradle.unsafe.configuration-cache=true
2626
android.nonFinalResIds=true
27-
# Version code for this module (35 for v055d)
28-
VERSION_CODE=35
27+
# Version code for this module (36 for v055e)
28+
VERSION_CODE=36

0 commit comments

Comments
 (0)