cleanup code

This commit is contained in:
Kr328
2020-04-16 22:40:27 +08:00
parent a573631068
commit f0b61c9e1f
2 changed files with 25 additions and 29 deletions

View File

@@ -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()
}
}

View File

@@ -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()
}
}