From 3a1daf1888edcfef64371f02df8d14731ba14827 Mon Sep 17 00:00:00 2001 From: "derek.dong" Date: Wed, 23 Nov 2022 16:25:38 +0800 Subject: [PATCH] restart tun2socks process if exited abnormally --- .../kotlin/com/v2ray/ang/service/V2RayVpnService.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index f328a9b9..af17b466 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -36,6 +36,7 @@ class V2RayVpnService : VpnService(), ServiceControl { private val settingsStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SETTING, MMKV.MULTI_PROCESS_MODE) } private lateinit var mInterface: ParcelFileDescriptor + private var isRunning = false //val fd: Int get() = mInterface.fd private lateinit var process: Process @@ -183,6 +184,7 @@ class V2RayVpnService : VpnService(), ServiceControl { // Create a new interface using the builder and save the parameters. try { mInterface = builder.establish()!! + isRunning = true runTun2socks() } catch (e: Exception) { // non-nullable lateinit var @@ -219,6 +221,15 @@ class V2RayVpnService : VpnService(), ServiceControl { process = proBuilder .directory(applicationContext.filesDir) .start() + Thread(Runnable { + Log.d(packageName,"$TUN2SOCKS check") + process.waitFor() + Log.d(packageName,"$TUN2SOCKS exited") + if (isRunning) { + Log.d(packageName,"$TUN2SOCKS restart") + runTun2socks() + } + }).start() Log.d(packageName, process.toString()) sendFd() @@ -262,6 +273,7 @@ class V2RayVpnService : VpnService(), ServiceControl { // val emptyInfo = VpnNetworkInfo() // val info = loadVpnNetworkInfo(configName, emptyInfo)!! + (lastNetworkInfo ?: emptyInfo) // saveVpnNetworkInfo(configName, info) + isRunning = false; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { try { connectivity.unregisterNetworkCallback(defaultNetworkCallback)