Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

Commit

Permalink
Disable fd cleanup (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
zaneschepke committed Apr 11, 2024
2 parents ea9e719 + 006836c commit d651616
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions nym_vpn_client/src/main/java/net/nymtech/vpn/NymVpnService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package net.nymtech.vpn
import android.content.Intent
import android.net.VpnService
import android.os.Build
import android.os.ParcelFileDescriptor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
Expand All @@ -22,7 +21,6 @@ import java.net.Inet4Address
import java.net.Inet6Address
import java.net.InetAddress
import java.util.concurrent.Executors
import kotlin.properties.Delegates.observable

class NymVpnService : VpnService() {
companion object {
Expand All @@ -39,18 +37,22 @@ class NymVpnService : VpnService() {

private val scope = CoroutineScope(Dispatchers.Default)

private var activeTunStatus by observable<CreateTunResult?>(null) { _, oldTunStatus, _ ->
val oldTunFd =
when (oldTunStatus) {
is CreateTunResult.Success -> oldTunStatus.tunFd
is CreateTunResult.InvalidDnsServers -> oldTunStatus.tunFd
else -> null
}
if (oldTunFd != null) {
Timber.i("Closing file descriptor $oldTunFd")
ParcelFileDescriptor.adoptFd(oldTunFd).close()
}
}
private var activeTunStatus: CreateTunResult? = null

// Once we make sure Rust library doesn't close the fd first, we should re-use this code for closing fd,
// as it's more general, including for wireguard tunnels
// private var activeTunStatus by observable<CreateTunResult?>(null) { _, oldTunStatus, _ ->
// val oldTunFd =
// when (oldTunStatus) {
// is CreateTunResult.Success -> oldTunStatus.tunFd
// is CreateTunResult.InvalidDnsServers -> oldTunStatus.tunFd
// else -> null
// }
// if (oldTunFd != null) {
// Timber.i("Closing file descriptor $oldTunFd")
// ParcelFileDescriptor.adoptFd(oldTunFd).close()
// }
// }

private val tunIsOpen
get() = activeTunStatus?.isOpen ?: false
Expand Down

0 comments on commit d651616

Please sign in to comment.