From 5e6348676cc517450ee7f27a9d2ad5d7b9aecb5a Mon Sep 17 00:00:00 2001 From: Rurirei <72071920+rurirei@users.noreply.github.com> Date: Thu, 1 Oct 2020 15:32:08 +0800 Subject: [PATCH 1/4] ignore err of registerNetworkConnectivity --- .../com/v2ray/ang/service/V2RayVpnService.kt | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) 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 823776a1..da4836bd 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 @@ -153,8 +153,12 @@ class V2RayVpnService : VpnService(), ServiceControl { } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - connectivity.requestNetwork(defaultNetworkRequest, defaultNetworkCallback) - listeningForDefaultNetwork = true + try { + connectivity.requestNetwork(defaultNetworkRequest, defaultNetworkCallback) + listeningForDefaultNetwork = true + } catch (ignored: Exception) { + // ignored + } } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { @@ -200,9 +204,13 @@ class V2RayVpnService : VpnService(), ServiceControl { // val emptyInfo = VpnNetworkInfo() // val info = loadVpnNetworkInfo(configName, emptyInfo)!! + (lastNetworkInfo ?: emptyInfo) // saveVpnNetworkInfo(configName, info) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && listeningForDefaultNetwork) { - connectivity.unregisterNetworkCallback(defaultNetworkCallback) - listeningForDefaultNetwork = false + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + try { + connectivity.unregisterNetworkCallback(defaultNetworkCallback) + listeningForDefaultNetwork = false + } catch (ignored: Exception) { + // ignored + } } V2RayServiceManager.stopV2rayPoint() From 3ae0777d7f327f2c132b0e5aa1e929081a94fc04 Mon Sep 17 00:00:00 2001 From: Rurirei <72071920+rurirei@users.noreply.github.com> Date: Thu, 1 Oct 2020 17:07:51 +0800 Subject: [PATCH 2/4] useless bool --- .../src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt | 4 ---- 1 file changed, 4 deletions(-) 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 da4836bd..53b1ea6c 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 @@ -58,8 +58,6 @@ class V2RayVpnService : VpnService(), ServiceControl { } } - private var listeningForDefaultNetwork = false - override fun onCreate() { super.onCreate() @@ -155,7 +153,6 @@ class V2RayVpnService : VpnService(), ServiceControl { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { try { connectivity.requestNetwork(defaultNetworkRequest, defaultNetworkCallback) - listeningForDefaultNetwork = true } catch (ignored: Exception) { // ignored } @@ -207,7 +204,6 @@ class V2RayVpnService : VpnService(), ServiceControl { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { try { connectivity.unregisterNetworkCallback(defaultNetworkCallback) - listeningForDefaultNetwork = false } catch (ignored: Exception) { // ignored } From e2d667e0bb4d3190a0c8cd213cd0c1210841b7e0 Mon Sep 17 00:00:00 2001 From: Rurirei <72071920+rurirei@users.noreply.github.com> Date: Thu, 1 Oct 2020 17:36:07 +0800 Subject: [PATCH 3/4] sendFd() should not called twice --- .../app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt | 1 - 1 file changed, 1 deletion(-) 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 53b1ea6c..e64176aa 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 @@ -164,7 +164,6 @@ class V2RayVpnService : VpnService(), ServiceControl { // Create a new interface using the builder and save the parameters. mInterface = builder.establish() - sendFd() } private fun sendFd() { From 3ffb2e8e05af60a2c71c4b2e9ae078f1452eab6a Mon Sep 17 00:00:00 2001 From: Rurirei <72071920+rurirei@users.noreply.github.com> Date: Sat, 3 Oct 2020 20:23:43 +0800 Subject: [PATCH 4/4] catch exception for lateinit PropertyNotInitialized exception needs catch for lateinit var --- .../main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 e64176aa..0e803c85 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 @@ -163,7 +163,13 @@ class V2RayVpnService : VpnService(), ServiceControl { } // Create a new interface using the builder and save the parameters. - mInterface = builder.establish() + try { + mInterface = builder.establish()!! + } catch (e: Exception) { + // non-nullable lateinit var + e.printStackTrace() + stopV2Ray() + } } private fun sendFd() {