Skip to content

Commit

Permalink
Revert "Implement set underlying networks"
Browse files Browse the repository at this point in the history
This reverts commit 05af5ce.
  • Loading branch information
nekohasekai committed Dec 28, 2024
1 parent 47fe044 commit d17b6b3
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package io.nekohasekai.sfa.bg

import android.annotation.SuppressLint
import android.content.pm.PackageManager
import android.net.Network
import android.net.NetworkCapabilities
import android.os.Build
import android.os.Process
Expand All @@ -13,8 +12,6 @@ import io.nekohasekai.libbox.InterfaceUpdateListener
import io.nekohasekai.libbox.Libbox
import io.nekohasekai.libbox.NetworkInterfaceIterator
import io.nekohasekai.libbox.PlatformInterface
import io.nekohasekai.libbox.RawNetwork
import io.nekohasekai.libbox.RawNetworkIterator
import io.nekohasekai.libbox.StringIterator
import io.nekohasekai.libbox.TunOptions
import io.nekohasekai.libbox.WIFIState
Expand Down Expand Up @@ -147,15 +144,11 @@ interface PlatformInterfaceWrapper : PlatformInterface {
boxInterface.flags = dumpFlags
boxInterface.metered =
!networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
boxInterface.rawNetwork = NetworkWrapper(network)
interfaces.add(boxInterface)
}
return InterfaceArray(interfaces.iterator())
}

override fun setUnderlyingNetworks(networks: RawNetworkIterator) {
}

override fun underNetworkExtension(): Boolean {
return false
}
Expand Down Expand Up @@ -209,8 +202,6 @@ interface PlatformInterfaceWrapper : PlatformInterface {
}
}

data class NetworkWrapper(val network: Network) : RawNetwork

private fun InterfaceAddress.toPrefix(): String {
return if (address is Inet6Address) {
"${Inet6Address.getByAddress(address.address).hostAddress}/${networkPrefixLength}"
Expand Down
51 changes: 4 additions & 47 deletions app/src/main/java/io/nekohasekai/sfa/bg/VPNService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package io.nekohasekai.sfa.bg

import android.content.Intent
import android.content.pm.PackageManager.NameNotFoundException
import android.net.Network
import android.net.ProxyInfo
import android.net.VpnService
import android.os.Build
import android.os.IBinder
import io.nekohasekai.libbox.Notification
import io.nekohasekai.libbox.RawNetworkIterator
import io.nekohasekai.libbox.TunOptions
import io.nekohasekai.sfa.database.Settings
import io.nekohasekai.sfa.ktx.toIpPrefix
Expand Down Expand Up @@ -183,51 +181,10 @@ class VPNService : VpnService(), PlatformInterfaceWrapper {
systemProxyEnabled = false
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
synchronized(this) {
val underlyingNetworks = underlyingNetworksCache
if (underlyingNetworks != null) {
builder.setUnderlyingNetworks(underlyingNetworks)
underlyingNetworksCache = null
}
val pfd =
builder.establish()
?: error("android: the application is not prepared or is revoked")
service.fileDescriptor = pfd
return pfd.fd
}
} else {
val pfd =
builder.establish()
?: error("android: the application is not prepared or is revoked")
service.fileDescriptor = pfd
return pfd.fd
}
}

@Volatile
private var underlyingNetworksCache: Array<Network>? = null

override fun setUnderlyingNetworks(networkIterator: RawNetworkIterator) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
synchronized(this) {
val defaultNetwork = runBlocking { DefaultNetworkListener.get() }
var networks = mutableListOf<Network>()
while (networkIterator.hasNext()) {
val network =
(networkIterator.next() as PlatformInterfaceWrapper.NetworkWrapper).network
if (network == defaultNetwork) {
networks.add(0, network)
} else {
networks.add(network)
}
}
val newNetworks = networks.toTypedArray()
if (!setUnderlyingNetworks(newNetworks)) {
underlyingNetworksCache = newNetworks
}
}
}
val pfd =
builder.establish() ?: error("android: the application is not prepared or is revoked")
service.fileDescriptor = pfd
return pfd.fd
}

override fun writeLog(message: String) = service.writeLog(message)
Expand Down

0 comments on commit d17b6b3

Please sign in to comment.