Skip to content

Commit 3d42ac9

Browse files
committed
Add logging and safety checks in boot and VPN service
#5346
1 parent d026526 commit 3d42ac9

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

V2rayNG/app/src/main/java/com/v2ray/ang/receiver/BootReceiver.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package com.v2ray.ang.receiver
33
import android.content.BroadcastReceiver
44
import android.content.Context
55
import android.content.Intent
6+
import android.util.Log
7+
import com.v2ray.ang.AppConfig
68
import com.v2ray.ang.handler.MmkvManager
79
import com.v2ray.ang.handler.V2RayServiceManager
810

@@ -16,8 +18,24 @@ class BootReceiver : BroadcastReceiver() {
1618
* @param intent The Intent being received.
1719
*/
1820
override fun onReceive(context: Context?, intent: Intent?) {
19-
if (context == null || intent?.action != Intent.ACTION_BOOT_COMPLETED) return
20-
if (!MmkvManager.decodeStartOnBoot() || MmkvManager.getSelectServer().isNullOrEmpty()) return
21+
Log.i(AppConfig.TAG, "BootReceiver received: ${intent?.action}")
22+
23+
if (context == null || intent?.action != Intent.ACTION_BOOT_COMPLETED) {
24+
Log.w(AppConfig.TAG, "BootReceiver: Invalid context or action")
25+
return
26+
}
27+
28+
if (!MmkvManager.decodeStartOnBoot()) {
29+
Log.i(AppConfig.TAG, "BootReceiver: Auto-start on boot is disabled")
30+
return
31+
}
32+
33+
if (MmkvManager.getSelectServer().isNullOrEmpty()) {
34+
Log.w(AppConfig.TAG, "BootReceiver: No server selected")
35+
return
36+
}
37+
38+
Log.i(AppConfig.TAG, "BootReceiver: Starting V2Ray service")
2139
V2RayServiceManager.startVService(context)
2240
}
2341
}

V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.v2ray.ang.service
22

3+
import android.annotation.SuppressLint
34
import android.app.Service
45
import android.content.Context
56
import android.content.Intent
@@ -28,6 +29,7 @@ import com.v2ray.ang.util.MyContextWrapper
2829
import com.v2ray.ang.util.Utils
2930
import java.lang.ref.SoftReference
3031

32+
@SuppressLint("VpnServicePolicy")
3133
class 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

Comments
 (0)