add ipv6 support for tun

This commit is contained in:
wwqgtxx
2024-09-11 09:11:48 +08:00
parent b661d94278
commit 9edf35c4bc
7 changed files with 91 additions and 6 deletions

View File

@@ -132,17 +132,31 @@ class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.De
val device = with(Builder()) {
// Interface address
addAddress(TUN_GATEWAY, TUN_SUBNET_PREFIX)
if (store.allowIpv6) {
addAddress(TUN_GATEWAY6, TUN_SUBNET_PREFIX6)
}
// Route
if (store.bypassPrivateNetwork) {
resources.getStringArray(R.array.bypass_private_route).map(::parseCIDR).forEach {
addRoute(it.ip, it.prefix)
}
if (store.allowIpv6) {
resources.getStringArray(R.array.bypass_private_route6).map(::parseCIDR).forEach {
addRoute(it.ip, it.prefix)
}
}
// Route of virtual DNS
addRoute(TUN_DNS, 32)
if (store.allowIpv6) {
addRoute(TUN_DNS6, 128)
}
} else {
addRoute(NET_ANY, 0)
if (store.allowIpv6) {
addRoute(NET_ANY6, 0)
}
}
// Access Control
@@ -171,6 +185,9 @@ class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.De
// Virtual Dns Server
addDnsServer(TUN_DNS)
if (store.allowIpv6) {
addDnsServer(TUN_DNS6)
}
// Open MainActivity
setConfigureIntent(
@@ -207,9 +224,9 @@ class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.De
TunModule.TunDevice(
fd = establish()?.detachFd()
?: throw NullPointerException("Establish VPN rejected by system"),
gateway = "$TUN_GATEWAY/$TUN_SUBNET_PREFIX",
portal = TUN_PORTAL,
dns = if (store.dnsHijacking) NET_ANY else TUN_DNS,
gateway = "$TUN_GATEWAY/$TUN_SUBNET_PREFIX" + if (store.allowIpv6) ",$TUN_GATEWAY6/$TUN_SUBNET_PREFIX6" else "",
portal = TUN_PORTAL + if (store.allowIpv6) ",$TUN_PORTAL6" else "",
dns = if (store.dnsHijacking) NET_ANY else (TUN_DNS + if (store.allowIpv6) ",$TUN_DNS6" else ""),
)
}
@@ -220,9 +237,14 @@ class TunService : VpnService(), CoroutineScope by CoroutineScope(Dispatchers.De
private const val TUN_MTU = 9000
private const val TUN_SUBNET_PREFIX = 30
private const val TUN_GATEWAY = "172.19.0.1"
private const val TUN_SUBNET_PREFIX6 = 126
private const val TUN_GATEWAY6 = "fdfe:dcba:9876::1"
private const val TUN_PORTAL = "172.19.0.2"
private const val TUN_PORTAL6 = "fdfe:dcba:9876::2"
private const val TUN_DNS = TUN_PORTAL
private const val TUN_DNS6 = TUN_PORTAL6
private const val NET_ANY = "0.0.0.0"
private const val NET_ANY6 = "::"
private val HTTP_PROXY_LOCAL_LIST: List<String> = listOf(
"localhost",

View File

@@ -51,6 +51,11 @@ class ServiceStore(context: Context) {
defaultValue = true
)
var allowIpv6 by store.boolean(
key = "allow_ipv6",
defaultValue = false
)
var dynamicNotification by store.boolean(
key = "dynamic_notification",
defaultValue = true

View File

@@ -77,4 +77,15 @@
<item>255.255.255.252/31</item>
<item>255.255.255.254/32</item>
</string-array>
<!-- exclude fc00::/7, fe80::/10, ff00::/8 -->
<string-array name="bypass_private_route6" translatable="false">
<item>::/1</item>
<item>8000::/2</item>
<item>c000::/3</item>
<item>e000::/4</item>
<item>f000::/5</item>
<item>f800::/6</item>
<item>fe00::/9</item>
<item>fec0::/10</item>
</string-array>
</resources>