2 files changed
+14
-47
No files matched your search
@@ -13,9 +13,13 @@ import kotlinx.coroutines.launch
|
||||
|
||||
class TunService : VpnService(), CoroutineScope by MainScope() {
|
||||
companion object {
|
||||
private const val TUN_MTU = 9000
|
||||
private const val TUN_DNS = "198.18.0.1"
|
||||
private const val IPV4_ANY = "0.0.0.0/0"
|
||||
// from https://github.com/shadowsocks/shadowsocks-android/blob/master/core/src/main/java/com/github/shadowsocks/bg/VpnService.kt
|
||||
private const val VPN_MTU = 9000
|
||||
private const val PRIVATE_VLAN4_SUBNET = 30
|
||||
private const val PRIVATE_VLAN4_CLIENT = "172.31.255.253"
|
||||
private const val PRIVATE_VLAN4_MIRROR = "172.31.255.254"
|
||||
private const val PRIVATE_VLAN_DNS = "198.18.0.1"
|
||||
private const val VLAN_ANY = "0.0.0.0/0"
|
||||
}
|
||||
|
||||
private val service = this
|
||||
@@ -104,27 +108,23 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
|
||||
}
|
||||
|
||||
private inner class TunConfigure(private val settings: ServiceSettings) : TunModule.Configure {
|
||||
private val network = generateTunNetwork()
|
||||
|
||||
override val builder: Builder
|
||||
get() = Builder()
|
||||
override val mtu: Int
|
||||
get() = TUN_MTU
|
||||
override val gateway: String = network.copyOf().apply {
|
||||
this[3] = (this[3] + 1).toByte()
|
||||
}.asAddressString("/30")
|
||||
override val mirror: String = network.copyOf().apply {
|
||||
this[3] = (this[3] + 2).toByte()
|
||||
}.asAddressString()
|
||||
get() = VPN_MTU
|
||||
override val gateway: String
|
||||
get() = "$PRIVATE_VLAN4_CLIENT/$PRIVATE_VLAN4_SUBNET"
|
||||
override val mirror: String
|
||||
get() = PRIVATE_VLAN4_MIRROR
|
||||
override val route: List<String>
|
||||
get() {
|
||||
return if (settings.get(ServiceSettings.BYPASS_PRIVATE_NETWORK))
|
||||
resources.getStringArray(R.array.bypass_private_route).toList()
|
||||
else
|
||||
listOf(IPV4_ANY)
|
||||
listOf(VLAN_ANY)
|
||||
}
|
||||
override val dnsAddress: String
|
||||
get() = TUN_DNS
|
||||
get() = PRIVATE_VLAN_DNS
|
||||
override val dnsHijacking: Boolean
|
||||
get() = settings.get(ServiceSettings.DNS_HIJACKING)
|
||||
override val allowApplications: Collection<String>
|
||||
@@ -143,29 +143,6 @@ class TunService : VpnService(), CoroutineScope by MainScope() {
|
||||
override fun onCreateTunFailure() {
|
||||
stopSelfForReason("Establish VPN rejected by system")
|
||||
}
|
||||
|
||||
private fun generateTunNetwork(): ByteArray {
|
||||
val result = ByteArray(4)
|
||||
|
||||
// 18 bit namespace
|
||||
val offset = UserUtils.currentUserId % 0x40000
|
||||
val network = (0x3FFFF - offset) shl 2
|
||||
|
||||
result[0] = (172 or (network shr 24) and 0xFF).toByte()
|
||||
result[1] = (16 or (network shr 16) and 0xFF).toByte()
|
||||
result[2] = (0 or (network shr 8) and 0xFF).toByte()
|
||||
result[3] = (0 or (network shr 0) and 0xFF).toByte()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
private fun ByteArray.asAddressString(suffix: String = ""): String {
|
||||
return "${this[0].toUnsignedString()}.${this[1].toUnsignedString()}.${this[2].toUnsignedString()}.${this[3].toUnsignedString()}$suffix"
|
||||
}
|
||||
|
||||
private fun Byte.toUnsignedString(): String {
|
||||
return (this.toInt() and 0xFF).toString()
|
||||
}
|
||||
}
|
||||
|
||||
private fun stopSelfForReason(reason: String?) {
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.github.kr328.clash.service.util
|
||||
|
||||
import android.os.Process
|
||||
|
||||
object UserUtils {
|
||||
private const val PER_USER_RANGE: Int = 100000
|
||||
|
||||
val currentUserId: Int
|
||||
get() = Process.myUid() / PER_USER_RANGE
|
||||
}
|
||||
Reference in new issue
Block a user