diff --git a/app/src/main/java/io/nekohasekai/sfa/bg/VPNService.kt b/app/src/main/java/io/nekohasekai/sfa/bg/VPNService.kt index 175d963..5b7df3e 100644 --- a/app/src/main/java/io/nekohasekai/sfa/bg/VPNService.kt +++ b/app/src/main/java/io/nekohasekai/sfa/bg/VPNService.kt @@ -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) { diff --git a/app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt b/app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt index 8badf84..bc817ab 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ktx/Wrappers.kt @@ -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.toStringIterator(): StringIterator { return object : StringIterator { @@ -22,4 +27,7 @@ fun StringIterator.toList(): List { add(next()) } } -} \ No newline at end of file +} + +@RequiresApi(Build.VERSION_CODES.TIRAMISU) +fun RoutePrefix.toIpPrefix() = IpPrefix(InetAddress.getByName(address()), prefix()) \ No newline at end of file