From 3d42ac9dba15b19a413c8284290ebd2aa826e989 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Sun, 8 Mar 2026 16:11:33 +0800 Subject: [PATCH] Add logging and safety checks in boot and VPN service https://github.com/2dust/v2rayNG/issues/5346 --- .../com/v2ray/ang/receiver/BootReceiver.kt | 22 +++++++++++++++++-- .../com/v2ray/ang/service/V2RayVpnService.kt | 22 ++++++++++++------- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/BootReceiver.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/BootReceiver.kt index 3b47b634..c4af8b6c 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/BootReceiver.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/receiver/BootReceiver.kt @@ -3,6 +3,8 @@ package com.v2ray.ang.receiver import android.content.BroadcastReceiver import android.content.Context import android.content.Intent +import android.util.Log +import com.v2ray.ang.AppConfig import com.v2ray.ang.handler.MmkvManager import com.v2ray.ang.handler.V2RayServiceManager @@ -16,8 +18,24 @@ class BootReceiver : BroadcastReceiver() { * @param intent The Intent being received. */ override fun onReceive(context: Context?, intent: Intent?) { - if (context == null || intent?.action != Intent.ACTION_BOOT_COMPLETED) return - if (!MmkvManager.decodeStartOnBoot() || MmkvManager.getSelectServer().isNullOrEmpty()) return + Log.i(AppConfig.TAG, "BootReceiver received: ${intent?.action}") + + if (context == null || intent?.action != Intent.ACTION_BOOT_COMPLETED) { + Log.w(AppConfig.TAG, "BootReceiver: Invalid context or action") + return + } + + if (!MmkvManager.decodeStartOnBoot()) { + Log.i(AppConfig.TAG, "BootReceiver: Auto-start on boot is disabled") + return + } + + if (MmkvManager.getSelectServer().isNullOrEmpty()) { + Log.w(AppConfig.TAG, "BootReceiver: No server selected") + return + } + + Log.i(AppConfig.TAG, "BootReceiver: Starting V2Ray service") V2RayServiceManager.startVService(context) } } diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt index 323be01d..38af7a90 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/service/V2RayVpnService.kt @@ -1,5 +1,6 @@ package com.v2ray.ang.service +import android.annotation.SuppressLint import android.app.Service import android.content.Context import android.content.Intent @@ -28,6 +29,7 @@ import com.v2ray.ang.util.MyContextWrapper import com.v2ray.ang.util.Utils import java.lang.ref.SoftReference +@SuppressLint("VpnServicePolicy") class V2RayVpnService : VpnService(), ServiceControl { private lateinit var mInterface: ParcelFileDescriptor private var isRunning = false @@ -103,7 +105,7 @@ class V2RayVpnService : VpnService(), ServiceControl { } override fun startService() { - if (mInterface == null) { + if (!::mInterface.isInitialized) { Log.e(AppConfig.TAG, "Failed to create VPN interface") return } @@ -136,7 +138,7 @@ class V2RayVpnService : VpnService(), ServiceControl { private fun setupVpnService() { val prepare = prepare(this) if (prepare != null) { - Log.e(AppConfig.TAG, "VPN preparation failed") + Log.e(AppConfig.TAG, "VPN preparation failed - VPN permission not granted") stopSelf() return } @@ -165,9 +167,11 @@ class V2RayVpnService : VpnService(), ServiceControl { // Close the old interface since the parameters have been changed try { - mInterface.close() - } catch (ignored: Exception) { - // ignored + if (::mInterface.isInitialized) { + mInterface.close() + } + } catch (e: Exception) { + Log.w(AppConfig.TAG, "Failed to close old interface", e) } // Configure platform-specific features @@ -330,8 +334,8 @@ class V2RayVpnService : VpnService(), ServiceControl { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { try { connectivity.unregisterNetworkCallback(defaultNetworkCallback) - } catch (ignored: Exception) { - // ignored + } catch (e: Exception) { + Log.w(AppConfig.TAG, "Failed to unregister network callback", e) } } @@ -349,7 +353,9 @@ class V2RayVpnService : VpnService(), ServiceControl { stopSelf() try { - mInterface.close() + if (::mInterface.isInitialized) { + mInterface.close() + } } catch (e: Exception) { Log.e(AppConfig.TAG, "Failed to close VPN interface", e) }