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 0cc119f..cea8fc7 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 @@ -4,10 +4,7 @@ import android.content.Intent import android.os.Binder import android.os.IBinder import com.github.kr328.clash.service.clash.ClashRuntime -import com.github.kr328.clash.service.clash.module.CloseModule -import com.github.kr328.clash.service.clash.module.DynamicNotificationModule -import com.github.kr328.clash.service.clash.module.ReloadModule -import com.github.kr328.clash.service.clash.module.StaticNotificationModule +import com.github.kr328.clash.service.clash.module.* import com.github.kr328.clash.service.settings.ServiceSettings import com.github.kr328.clash.service.util.broadcastClashStarted import com.github.kr328.clash.service.util.broadcastClashStopped @@ -36,9 +33,7 @@ class ClashService : BaseService() { runtime.install(ReloadModule(service)) { onLoaded { if (it != null) { - reason = it.message - - stopSelf() + service.stopSelfForReason(it.message) } else { service.broadcastProfileLoaded() } @@ -46,9 +41,7 @@ class ClashService : BaseService() { } runtime.install(CloseModule()) { onClosed { - reason = null - - stopSelf() + service.stopSelfForReason(null) } } @@ -78,4 +71,10 @@ class ClashService : BaseService() { super.onDestroy() } + + private fun stopSelfForReason(reason: String?) { + this.reason = reason + + stopSelf() + } } \ No newline at end of file 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 1b48ab1..5faa13b 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 @@ -19,6 +19,7 @@ class TunService : VpnService(), CoroutineScope by MainScope() { 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 @@ -47,25 +48,15 @@ class TunService : VpnService(), CoroutineScope by MainScope() { runtime.install(ReloadModule(service)) { onLoaded { if (it != null) { - reason = it.message - - stopSelf() - - TunModule.requestStop() + service.stopSelfForReason(it.message) } else { - broadcastProfileLoaded() + service.broadcastProfileLoaded() } } } runtime.install(CloseModule()) { onClosed { - launch { - reason = null - - stopSelf() - - TunModule.requestStop() - } + service.stopSelfForReason(null) } } @@ -99,7 +90,7 @@ class TunService : VpnService(), CoroutineScope by MainScope() { } override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - broadcastClashStarted() + service.broadcastClashStarted() return super.onStartCommand(intent, flags, startId) } @@ -107,7 +98,7 @@ class TunService : VpnService(), CoroutineScope by MainScope() { override fun onDestroy() { ServiceStatusProvider.serviceRunning = false - broadcastClashStopped(reason) + service.broadcastClashStopped(reason) cancel() @@ -128,7 +119,7 @@ class TunService : VpnService(), CoroutineScope by MainScope() { return if (settings.get(ServiceSettings.BYPASS_PRIVATE_NETWORK)) resources.getStringArray(R.array.bypass_private_route).toList() else - listOf("0.0.0.0/0") + listOf(VLAN_ANY) } override val dnsAddress: String get() = PRIVATE_VLAN_DNS @@ -148,9 +139,15 @@ class TunService : VpnService(), CoroutineScope by MainScope() { } override fun onCreateTunFailure() { - reason = "Start VPN is rejected by the system" - - stopSelf() + stopSelfForReason("Start VPN rejected by the system") } } + + private fun stopSelfForReason(reason: String?) { + this.reason = reason + + stopSelf() + + TunModule.requestStop() + } } \ No newline at end of file