diff --git a/service/src/main/aidl/com/github/kr328/clash/service/IClashService.aidl b/service/src/main/aidl/com/github/kr328/clash/service/IClashService.aidl index be56221..8b2c4ba 100644 --- a/service/src/main/aidl/com/github/kr328/clash/service/IClashService.aidl +++ b/service/src/main/aidl/com/github/kr328/clash/service/IClashService.aidl @@ -19,8 +19,6 @@ interface IClashService { // Control void setSelectProxy(String proxy, String selected); - void startTunDevice(in ParcelFileDescriptor fd, int mtu, boolean dnsHijacking); - void stopTunDevice(); void start(); void stop(); diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt index cc60f5a..7ff9d13 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt @@ -33,10 +33,6 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, private lateinit var notification: ClashNotification private val clashService = object : IClashService.Stub() { - override fun stopTunDevice() { - notification.setVpn(false) - } - override fun setSelectProxy(proxy: String?, selected: String?) { require(proxy != null && selected != null) @@ -73,25 +69,14 @@ class ClashService : Service(), IClashEventObserver, ClashEventService.Master, } override fun stop() { + if ( processStatus == ProcessEvent.STOPPED ) + return + processStatus = ProcessEvent.STOPPED this@ClashService.eventService.performProcessEvent(processStatus) } - override fun startTunDevice(fd: ParcelFileDescriptor, mtu: Int, dnsHijacking: Boolean) { - try { - notification.setVpn(true) - } catch (e: Exception) { - Log.e("Start tun failure", e) - - this@ClashService.eventService.performErrorEvent( - ErrorEvent(ErrorEvent.Type.START_FAILURE, e.toString()) - ) - } finally { - fd.close() - } - } - override fun getEventService(): IClashEventService { return this@ClashService.eventService } diff --git a/service/src/main/java/com/github/kr328/clash/service/TunService.kt b/service/src/main/java/com/github/kr328/clash/service/TunService.kt index e6d4c5c..d3c6063 100644 --- a/service/src/main/java/com/github/kr328/clash/service/TunService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/TunService.kt @@ -8,7 +8,6 @@ import android.content.ServiceConnection import android.net.VpnService import android.os.* import bridge.Bridge -import bridge.TunCallback import com.github.kr328.clash.core.event.* import com.github.kr328.clash.core.utils.Log import com.github.kr328.clash.service.net.DefaultNetworkObserver @@ -19,7 +18,6 @@ class TunService : VpnService(), IClashEventObserver { private const val VPN_MTU = 1500 private const val PRIVATE_VLAN_DNS = "172.19.0.2" // sync with tun/tun.go/dnsServerAddress private const val PRIVATE_VLAN4_CLIENT = "172.19.0.1" - private const val PRIVATE_VLAN6_CLIENT = "fdfe:dcba:9876::1" } private var start = true @@ -87,9 +85,6 @@ class TunService : VpnService(), IClashEventObserver { override fun onDestroy() { super.onDestroy() - fileDescriptor.close() - - clash.stopTunDevice() clash.stop() clash.eventService.unregisterEventObserver(TunService::class.java.simpleName) @@ -117,11 +112,22 @@ class TunService : VpnService(), IClashEventObserver { ProcessEvent.STARTED -> { start = false - Bridge.startTunDevice(fileDescriptor.fd.toLong(), VPN_MTU.toLong(), - "$PRIVATE_VLAN4_CLIENT/30", PRIVATE_VLAN_DNS - ) { - protect(it.toInt()) + if ( settings.isDnsHijackingEnabled ) { + Bridge.startTunDevice(fileDescriptor.fd.toLong(), VPN_MTU.toLong(), + "$PRIVATE_VLAN4_CLIENT/30", "0.0.0.0" + ) { + protect(it.toInt()) + } } + else { + Bridge.startTunDevice(fileDescriptor.fd.toLong(), VPN_MTU.toLong(), + "$PRIVATE_VLAN4_CLIENT/30", PRIVATE_VLAN_DNS + ) { + protect(it.toInt()) + } + } + + fileDescriptor.close() Log.i("STARTED") } @@ -146,12 +152,6 @@ class TunService : VpnService(), IClashEventObserver { addRoute("0.0.0.0", 0) } - // IPv6 - if ( settings.isIPv6Enabled ) { - - addRoute("::", 0) - } - return this } @@ -194,9 +194,6 @@ class TunService : VpnService(), IClashEventObserver { private fun Builder.addAddress(): Builder { addAddress(PRIVATE_VLAN4_CLIENT, 30) - if ( settings.isIPv6Enabled ) - addAddress(PRIVATE_VLAN6_CLIENT, 126) - return this }