fix crash on close

This commit is contained in:
Kr328 committed 2019-12-15 16:01:48 +08:00
1 parent c3cd2351a5
commit 51acbc75f5
3 files changed
+18 -38

No files matched your search

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