Fix route range

This commit is contained in:
世界 2023-12-09 22:38:00 +08:00
parent c0d2f57ec0
commit 4892c54313
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 56 additions and 25 deletions

View file

@ -8,6 +8,7 @@ import android.os.Build
import android.os.IBinder
import io.nekohasekai.libbox.TunOptions
import io.nekohasekai.sfa.database.Settings
import io.nekohasekai.sfa.ktx.toIpPrefix
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
@ -61,42 +62,64 @@ class VPNService : VpnService(), PlatformInterfaceWrapper {
}
val inet4Address = options.inet4Address
if (inet4Address.hasNext()) {
while (inet4Address.hasNext()) {
val address = inet4Address.next()
builder.addAddress(address.address, address.prefix)
}
while (inet4Address.hasNext()) {
val address = inet4Address.next()
builder.addAddress(address.address(), address.prefix())
}
val inet6Address = options.inet6Address
if (inet6Address.hasNext()) {
while (inet6Address.hasNext()) {
val address = inet6Address.next()
builder.addAddress(address.address, address.prefix)
}
while (inet6Address.hasNext()) {
val address = inet6Address.next()
builder.addAddress(address.address(), address.prefix())
}
if (options.autoRoute) {
builder.addDnsServer(options.dnsServerAddress)
val inet4RouteAddress = options.inet4RouteAddress
if (inet4RouteAddress.hasNext()) {
while (inet4RouteAddress.hasNext()) {
val address = inet4RouteAddress.next()
builder.addRoute(address.address, address.prefix)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
val inet4RouteAddress = options.inet4RouteAddress
if (inet4RouteAddress.hasNext()) {
while (inet4RouteAddress.hasNext()) {
builder.addRoute(inet4RouteAddress.next().toIpPrefix())
}
} else {
builder.addRoute("0.0.0.0", 0)
}
} else {
builder.addRoute("0.0.0.0", 0)
}
val inet6RouteAddress = options.inet6RouteAddress
if (inet6RouteAddress.hasNext()) {
while (inet6RouteAddress.hasNext()) {
val address = inet6RouteAddress.next()
builder.addRoute(address.address, address.prefix)
val inet6RouteAddress = options.inet6RouteAddress
if (inet6RouteAddress.hasNext()) {
while (inet6RouteAddress.hasNext()) {
builder.addRoute(inet6RouteAddress.next().toIpPrefix())
}
} else {
builder.addRoute("::", 0)
}
val inet4RouteExcludeAddress = options.inet4RouteExcludeAddress
while (inet4RouteExcludeAddress.hasNext()) {
builder.excludeRoute(inet4RouteExcludeAddress.next().toIpPrefix())
}
val inet6RouteExcludeAddress = options.inet6RouteExcludeAddress
while (inet6RouteExcludeAddress.hasNext()) {
builder.excludeRoute(inet6RouteExcludeAddress.next().toIpPrefix())
}
} else {
builder.addRoute("::", 0)
val inet4RouteAddress = options.inet4RouteRange
if (inet4RouteAddress.hasNext()) {
while (inet4RouteAddress.hasNext()) {
val address = inet4RouteAddress.next()
builder.addRoute(address.address(), address.prefix())
}
}
val inet6RouteAddress = options.inet6RouteRange
if (inet6RouteAddress.hasNext()) {
while (inet6RouteAddress.hasNext()) {
val address = inet6RouteAddress.next()
builder.addRoute(address.address(), address.prefix())
}
}
}
if (Settings.perAppProxyEnabled) {

View file

@ -1,6 +1,11 @@
package io.nekohasekai.sfa.ktx
import android.net.IpPrefix
import android.os.Build
import androidx.annotation.RequiresApi
import io.nekohasekai.libbox.RoutePrefix
import io.nekohasekai.libbox.StringIterator
import java.net.InetAddress
fun Iterable<String>.toStringIterator(): StringIterator {
return object : StringIterator {
@ -22,4 +27,7 @@ fun StringIterator.toList(): List<String> {
add(next())
}
}
}
}
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
fun RoutePrefix.toIpPrefix() = IpPrefix(InetAddress.getByName(address()), prefix())