11package com.v2ray.ang.service
22
3+ import android.annotation.SuppressLint
34import android.app.Service
45import android.content.Context
56import android.content.Intent
@@ -28,6 +29,7 @@ import com.v2ray.ang.util.MyContextWrapper
2829import com.v2ray.ang.util.Utils
2930import java.lang.ref.SoftReference
3031
32+ @SuppressLint(" VpnServicePolicy" )
3133class V2RayVpnService : VpnService (), ServiceControl {
3234 private lateinit var mInterface: ParcelFileDescriptor
3335 private var isRunning = false
@@ -103,7 +105,7 @@ class V2RayVpnService : VpnService(), ServiceControl {
103105 }
104106
105107 override fun startService () {
106- if (mInterface == null ) {
108+ if (! :: mInterface.isInitialized ) {
107109 Log .e(AppConfig .TAG , " Failed to create VPN interface" )
108110 return
109111 }
@@ -136,7 +138,7 @@ class V2RayVpnService : VpnService(), ServiceControl {
136138 private fun setupVpnService () {
137139 val prepare = prepare(this )
138140 if (prepare != null ) {
139- Log .e(AppConfig .TAG , " VPN preparation failed" )
141+ Log .e(AppConfig .TAG , " VPN preparation failed - VPN permission not granted " )
140142 stopSelf()
141143 return
142144 }
@@ -165,9 +167,11 @@ class V2RayVpnService : VpnService(), ServiceControl {
165167
166168 // Close the old interface since the parameters have been changed
167169 try {
168- mInterface.close()
169- } catch (ignored: Exception ) {
170- // ignored
170+ if (::mInterface.isInitialized) {
171+ mInterface.close()
172+ }
173+ } catch (e: Exception ) {
174+ Log .w(AppConfig .TAG , " Failed to close old interface" , e)
171175 }
172176
173177 // Configure platform-specific features
@@ -330,8 +334,8 @@ class V2RayVpnService : VpnService(), ServiceControl {
330334 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
331335 try {
332336 connectivity.unregisterNetworkCallback(defaultNetworkCallback)
333- } catch (ignored : Exception ) {
334- // ignored
337+ } catch (e : Exception ) {
338+ Log .w( AppConfig . TAG , " Failed to unregister network callback " , e)
335339 }
336340 }
337341
@@ -349,7 +353,9 @@ class V2RayVpnService : VpnService(), ServiceControl {
349353 stopSelf()
350354
351355 try {
352- mInterface.close()
356+ if (::mInterface.isInitialized) {
357+ mInterface.close()
358+ }
353359 } catch (e: Exception ) {
354360 Log .e(AppConfig .TAG , " Failed to close VPN interface" , e)
355361 }
0 commit comments