Skip to content

Commit

Permalink
Clear dnsServers on establish and closeTun
Browse files Browse the repository at this point in the history
Since we know the dnsServers are invalid after having invoked `closeTun` & `establish` this prevents the daemon from using DNS servers that are no longer valid.
  • Loading branch information
Rawa committed Feb 12, 2025
1 parent e70557a commit a7ec806
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.scan
import kotlinx.coroutines.flow.stateIn
Expand All @@ -23,7 +24,10 @@ import net.mullvad.talpid.util.RawNetworkState
import net.mullvad.talpid.util.defaultRawNetworkStateFlow
import net.mullvad.talpid.util.networkEvents

class ConnectivityListener(private val connectivityManager: ConnectivityManager) {
class ConnectivityListener(
private val connectivityManager: ConnectivityManager,
private val resetDnsFlow: Flow<Unit>,
) {
private lateinit var _isConnected: StateFlow<Boolean>
// Used by JNI
val isConnected
Expand All @@ -44,8 +48,7 @@ class ConnectivityListener(private val connectivityManager: ConnectivityManager)
// the default network may fail if the network on Android 11
// https://issuetracker.google.com/issues/175055271?pli=1
_currentNetworkState =
connectivityManager
.defaultRawNetworkStateFlow()
merge(connectivityManager.defaultRawNetworkStateFlow(), resetDnsFlow.map { null })
.map { it?.toNetworkState() }
.onEach { notifyDefaultNetworkChange(it) }
.stateIn(scope, SharingStarted.Eagerly, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
import kotlin.properties.Delegates.observable
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.runBlocking
import net.mullvad.mullvadvpn.lib.common.util.establishSafe
import net.mullvad.mullvadvpn.lib.common.util.prepareVpnSafe
import net.mullvad.mullvadvpn.lib.model.PrepareError
Expand Down Expand Up @@ -42,6 +45,7 @@ open class TalpidVpnService : LifecycleVpnService() {
}
}

private val resetDnsChannel = Channel<Unit>()
private var currentTunConfig: TunConfig? = null

// Used by JNI
Expand All @@ -50,7 +54,11 @@ open class TalpidVpnService : LifecycleVpnService() {
@CallSuper
override fun onCreate() {
super.onCreate()
connectivityListener = ConnectivityListener(getSystemService<ConnectivityManager>()!!)
connectivityListener =
ConnectivityListener(
getSystemService<ConnectivityManager>()!!,
resetDnsChannel.receiveAsFlow(),
)
connectivityListener.register(lifecycleScope)
}

Expand All @@ -71,7 +79,11 @@ open class TalpidVpnService : LifecycleVpnService() {
synchronized(this) { openTunImpl(config) }

// Used by JNI
fun closeTun(): Unit = synchronized(this) { activeTunStatus = null }
fun closeTun(): Unit =
synchronized(this) {
runBlocking { resetDnsChannel.send(Unit) }
activeTunStatus = null
}

// Used by JNI
fun bypass(socket: Int): Boolean = protect(socket)
Expand Down Expand Up @@ -123,6 +135,7 @@ open class TalpidVpnService : LifecycleVpnService() {
builder.addDnsServer(FALLBACK_DUMMY_DNS_SERVER)
}

runBlocking { resetDnsChannel.send(Unit) }
val vpnInterfaceFd =
builder
.establishSafe()
Expand Down

0 comments on commit a7ec806

Please sign in to comment.