Skip to content

Commit d04b5ee

Browse files
committed
Add detailed logging and validations to services
1 parent 405cd7f commit d04b5ee

File tree

3 files changed

+85
-31
lines changed

3 files changed

+85
-31
lines changed

V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2RayServiceManager.kt

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ object V2RayServiceManager {
5757
* @param guid The GUID of the server configuration to use (optional).
5858
*/
5959
fun startVService(context: Context, guid: String? = null) {
60+
Log.i(AppConfig.TAG, "StartCore-Manager: startVService from ${context::class.java.simpleName}")
61+
6062
if (guid != null) {
6163
MmkvManager.setSelectServer(guid)
6264
}
65+
6366
startContextService(context)
6467
}
6568

@@ -91,15 +94,30 @@ object V2RayServiceManager {
9194
*/
9295
private fun startContextService(context: Context) {
9396
if (coreController.isRunning) {
97+
Log.w(AppConfig.TAG, "StartCore-Manager: Core already running")
98+
return
99+
}
100+
101+
val guid = MmkvManager.getSelectServer()
102+
if (guid == null) {
103+
Log.e(AppConfig.TAG, "StartCore-Manager: No server selected")
104+
return
105+
}
106+
107+
val config = MmkvManager.decodeServerConfig(guid)
108+
if (config == null) {
109+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to decode server config")
94110
return
95111
}
96-
val guid = MmkvManager.getSelectServer() ?: return
97-
val config = MmkvManager.decodeServerConfig(guid) ?: return
112+
98113
if (config.configType != EConfigType.CUSTOM
99114
&& config.configType != EConfigType.POLICYGROUP
100115
&& !Utils.isValidUrl(config.server)
101116
&& !Utils.isPureIpAddress(config.server.orEmpty())
102-
) return
117+
) {
118+
Log.e(AppConfig.TAG, "StartCore-Manager: Invalid server configuration")
119+
return
120+
}
103121
// val result = V2rayConfigUtil.getV2rayConfig(context, guid)
104122
// if (!result.status) return
105123

@@ -108,12 +126,21 @@ object V2RayServiceManager {
108126
} else {
109127
context.toast(R.string.toast_services_start)
110128
}
111-
val intent = if (SettingsManager.isVpnMode()) {
129+
130+
val isVpnMode = SettingsManager.isVpnMode()
131+
val intent = if (isVpnMode) {
132+
Log.i(AppConfig.TAG, "StartCore-Manager: Starting VPN service")
112133
Intent(context.applicationContext, V2RayVpnService::class.java)
113134
} else {
135+
Log.i(AppConfig.TAG, "StartCore-Manager: Starting Proxy service")
114136
Intent(context.applicationContext, V2RayProxyOnlyService::class.java)
115137
}
116-
ContextCompat.startForegroundService(context, intent)
138+
139+
try {
140+
ContextCompat.startForegroundService(context, intent)
141+
} catch (e: Exception) {
142+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to start service", e)
143+
}
117144
}
118145

119146
/**
@@ -123,15 +150,34 @@ object V2RayServiceManager {
123150
*/
124151
fun startCoreLoop(vpnInterface: ParcelFileDescriptor?): Boolean {
125152
if (coreController.isRunning) {
153+
Log.w(AppConfig.TAG, "StartCore-Manager: Core already running")
126154
return false
127155
}
128156

129-
val service = getService() ?: return false
130-
val guid = MmkvManager.getSelectServer() ?: return false
131-
val config = MmkvManager.decodeServerConfig(guid) ?: return false
157+
val service = getService()
158+
if (service == null) {
159+
Log.e(AppConfig.TAG, "StartCore-Manager: Service is null")
160+
return false
161+
}
162+
163+
val guid = MmkvManager.getSelectServer()
164+
if (guid == null) {
165+
Log.e(AppConfig.TAG, "StartCore-Manager: No server selected")
166+
return false
167+
}
168+
169+
val config = MmkvManager.decodeServerConfig(guid)
170+
if (config == null) {
171+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to decode server config")
172+
return false
173+
}
174+
175+
Log.i(AppConfig.TAG, "StartCore-Manager: Starting core loop for ${config.remarks}")
132176
val result = V2rayConfigManager.getV2rayConfig(service, guid)
133-
if (!result.status)
177+
if (!result.status) {
178+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to get V2Ray config")
134179
return false
180+
}
135181

136182
try {
137183
val mFilter = IntentFilter(AppConfig.BROADCAST_ACTION_SERVICE)
@@ -140,7 +186,7 @@ object V2RayServiceManager {
140186
mFilter.addAction(Intent.ACTION_USER_PRESENT)
141187
ContextCompat.registerReceiver(service, mMsgReceive, mFilter, Utils.receiverFlags())
142188
} catch (e: Exception) {
143-
Log.e(AppConfig.TAG, "Failed to register broadcast receiver", e)
189+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to register receiver", e)
144190
return false
145191
}
146192

@@ -154,23 +200,23 @@ object V2RayServiceManager {
154200
NotificationManager.showNotification(currentConfig)
155201
coreController.startLoop(result.content, tunFd)
156202
} catch (e: Exception) {
157-
Log.e(AppConfig.TAG, "Failed to start Core loop", e)
203+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to start core loop", e)
158204
return false
159205
}
160206

161207
if (coreController.isRunning == false) {
208+
Log.e(AppConfig.TAG, "StartCore-Manager: Core failed to start")
162209
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_FAILURE, "")
163210
NotificationManager.cancelNotification()
164211
return false
165212
}
166213

167214
try {
168215
MessageUtil.sendMsg2UI(service, AppConfig.MSG_STATE_START_SUCCESS, "")
169-
//NotificationManager.showNotification(currentConfig)
170216
NotificationManager.startSpeedNotification(currentConfig)
171-
217+
Log.i(AppConfig.TAG, "StartCore-Manager: Core started successfully")
172218
} catch (e: Exception) {
173-
Log.e(AppConfig.TAG, "Failed to startup service", e)
219+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to complete startup", e)
174220
return false
175221
}
176222
return true
@@ -189,7 +235,7 @@ object V2RayServiceManager {
189235
try {
190236
coreController.stopLoop()
191237
} catch (e: Exception) {
192-
Log.e(AppConfig.TAG, "Failed to stop V2Ray loop", e)
238+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to stop V2Ray loop", e)
193239
}
194240
}
195241
}
@@ -200,7 +246,7 @@ object V2RayServiceManager {
200246
try {
201247
service.unregisterReceiver(mMsgReceive)
202248
} catch (e: Exception) {
203-
Log.e(AppConfig.TAG, "Failed to unregister broadcast receiver", e)
249+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to unregister receiver", e)
204250
}
205251

206252
return true
@@ -234,14 +280,14 @@ object V2RayServiceManager {
234280
try {
235281
time = coreController.measureDelay(SettingsManager.getDelayTestUrl())
236282
} catch (e: Exception) {
237-
Log.e(AppConfig.TAG, "Failed to measure delay with primary URL", e)
283+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to measure delay", e)
238284
errorStr = e.message?.substringAfter("\":") ?: "empty message"
239285
}
240286
if (time == -1L) {
241287
try {
242288
time = coreController.measureDelay(SettingsManager.getDelayTestUrl(true))
243289
} catch (e: Exception) {
244-
Log.e(AppConfig.TAG, "Failed to measure delay with alternative URL", e)
290+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to measure delay", e)
245291
errorStr = e.message?.substringAfter("\":") ?: "empty message"
246292
}
247293
}
@@ -293,7 +339,7 @@ object V2RayServiceManager {
293339
serviceControl.stopService()
294340
0
295341
} catch (e: Exception) {
296-
Log.e(AppConfig.TAG, "Failed to stop service in callback", e)
342+
Log.e(AppConfig.TAG, "StartCore-Manager: Failed to stop service", e)
297343
-1
298344
}
299345
}
@@ -340,12 +386,12 @@ object V2RayServiceManager {
340386
}
341387

342388
AppConfig.MSG_STATE_STOP -> {
343-
Log.i(AppConfig.TAG, "Stop Service")
389+
Log.i(AppConfig.TAG, "StartCore-Manager: Stop service")
344390
serviceControl.stopService()
345391
}
346392

347393
AppConfig.MSG_STATE_RESTART -> {
348-
Log.i(AppConfig.TAG, "Restart Service")
394+
Log.i(AppConfig.TAG, "StartCore-Manager: Restart service")
349395
serviceControl.stopService()
350396
Thread.sleep(500L)
351397
startVService(serviceControl.getService())
@@ -358,12 +404,12 @@ object V2RayServiceManager {
358404

359405
when (intent?.action) {
360406
Intent.ACTION_SCREEN_OFF -> {
361-
Log.i(AppConfig.TAG, "SCREEN_OFF, stop querying stats")
407+
Log.i(AppConfig.TAG, "StartCore-Manager: Screen off")
362408
NotificationManager.stopSpeedNotification(currentConfig)
363409
}
364410

365411
Intent.ACTION_SCREEN_ON -> {
366-
Log.i(AppConfig.TAG, "SCREEN_ON, start querying stats")
412+
Log.i(AppConfig.TAG, "StartCore-Manager: Screen on")
367413
NotificationManager.startSpeedNotification(currentConfig)
368414
}
369415
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import android.app.Service
44
import android.content.Context
55
import android.content.Intent
66
import android.os.IBinder
7+
import android.util.Log
8+
import com.v2ray.ang.AppConfig
79
import com.v2ray.ang.contracts.ServiceControl
810
import com.v2ray.ang.handler.SettingsManager
911
import com.v2ray.ang.handler.V2RayServiceManager
@@ -16,6 +18,7 @@ class V2RayProxyOnlyService : Service(), ServiceControl {
1618
*/
1719
override fun onCreate() {
1820
super.onCreate()
21+
Log.i(AppConfig.TAG, "StartCore-Proxy: Service created")
1922
V2RayServiceManager.serviceControl = SoftReference(this)
2023
}
2124

@@ -27,6 +30,7 @@ class V2RayProxyOnlyService : Service(), ServiceControl {
2730
* @return The start mode.
2831
*/
2932
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
33+
Log.i(AppConfig.TAG, "StartCore-Proxy: Service command received")
3034
V2RayServiceManager.startCoreLoop(null)
3135
return START_STICKY
3236
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,14 @@ class V2RayVpnService : VpnService(), ServiceControl {
7474

7575
override fun onCreate() {
7676
super.onCreate()
77+
Log.i(AppConfig.TAG, "StartCore-VPN: Service created")
7778
val policy = StrictMode.ThreadPolicy.Builder().permitAll().build()
7879
StrictMode.setThreadPolicy(policy)
7980
V2RayServiceManager.serviceControl = SoftReference(this)
8081
}
8182

8283
override fun onRevoke() {
84+
Log.w(AppConfig.TAG, "StartCore-VPN: Permission revoked")
8385
stopAllService()
8486
}
8587

@@ -90,10 +92,12 @@ class V2RayVpnService : VpnService(), ServiceControl {
9092

9193
override fun onDestroy() {
9294
super.onDestroy()
95+
Log.i(AppConfig.TAG, "StartCore-VPN: Service destroyed")
9396
NotificationManager.cancelNotification()
9497
}
9598

9699
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
100+
Log.i(AppConfig.TAG, "StartCore-VPN: Service command received")
97101
setupVpnService()
98102
startService()
99103
return START_STICKY
@@ -106,11 +110,11 @@ class V2RayVpnService : VpnService(), ServiceControl {
106110

107111
override fun startService() {
108112
if (!::mInterface.isInitialized) {
109-
Log.e(AppConfig.TAG, "Failed to create VPN interface")
113+
Log.e(AppConfig.TAG, "StartCore-VPN: Interface not initialized")
110114
return
111115
}
112116
if (!V2RayServiceManager.startCoreLoop(mInterface)) {
113-
Log.e(AppConfig.TAG, "Failed to start V2Ray core loop")
117+
Log.e(AppConfig.TAG, "StartCore-VPN: Failed to start core loop")
114118
stopAllService()
115119
return
116120
}
@@ -138,13 +142,13 @@ class V2RayVpnService : VpnService(), ServiceControl {
138142
private fun setupVpnService() {
139143
val prepare = prepare(this)
140144
if (prepare != null) {
141-
Log.e(AppConfig.TAG, "VPN preparation failed - VPN permission not granted")
145+
Log.e(AppConfig.TAG, "StartCore-VPN: Permission not granted")
142146
stopSelf()
143147
return
144148
}
145149

146150
if (configureVpnService() != true) {
147-
Log.e(AppConfig.TAG, "VPN configuration failed")
151+
Log.e(AppConfig.TAG, "StartCore-VPN: Configuration failed")
148152
stopSelf()
149153
return
150154
}
@@ -248,7 +252,7 @@ class V2RayVpnService : VpnService(), ServiceControl {
248252
try {
249253
connectivity.requestNetwork(defaultNetworkRequest, defaultNetworkCallback)
250254
} catch (e: Exception) {
251-
Log.e(AppConfig.TAG, "Failed to request default network", e)
255+
Log.e(AppConfig.TAG, "StartCore-VPN: Failed to request network", e)
252256
}
253257
}
254258

@@ -301,7 +305,7 @@ class V2RayVpnService : VpnService(), ServiceControl {
301305
builder.addAllowedApplication(it)
302306
}
303307
} catch (e: PackageManager.NameNotFoundException) {
304-
Log.e(AppConfig.TAG, "Failed to configure app in VPN: ${e.localizedMessage}", e)
308+
Log.e(AppConfig.TAG, "StartCore-VPN: Failed to configure app", e)
305309
}
306310
}
307311
}
@@ -335,7 +339,7 @@ class V2RayVpnService : VpnService(), ServiceControl {
335339
try {
336340
connectivity.unregisterNetworkCallback(defaultNetworkCallback)
337341
} catch (e: Exception) {
338-
Log.w(AppConfig.TAG, "Failed to unregister network callback", e)
342+
Log.w(AppConfig.TAG, "StartCore-VPN: Failed to unregister callback", e)
339343
}
340344
}
341345

@@ -357,7 +361,7 @@ class V2RayVpnService : VpnService(), ServiceControl {
357361
mInterface.close()
358362
}
359363
} catch (e: Exception) {
360-
Log.e(AppConfig.TAG, "Failed to close VPN interface", e)
364+
Log.e(AppConfig.TAG, "StartCore-VPN: Failed to close interface", e)
361365
}
362366
}
363367
}

0 commit comments

Comments
 (0)