Skip to content

Commit 4463ba2

Browse files
fix: исправление переполнения памяти и прав VPN
- Ограничено количество тестируемых серверов за один проход до случайных 20, чтобы предотвратить создание сотен экземпляров Xray Core (Memory / FD exhaustion) - Использован context.startV2Ray() вместо V2RayServiceManager.startVService, чтобы корректно запрашивать у Android разрешение на работу VPN-сервиса (иначе он молча падал без прав) и снимал блокировку UI.
1 parent a39ea8d commit 4463ba2

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

V2rayNG/app/src/main/java/com/kiktor/v2whitelist/handler/SmartConnectManager.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ object SmartConnectManager {
224224
suspend fun smartConnect(context: Context) = withContext(Dispatchers.IO) {
225225
checkAndSetupSubscription(context)
226226
val allServers = MmkvManager.decodeServerList()
227-
val servers = filterServers(allServers)
227+
val servers = filterServers(allServers).shuffled().take(20)
228228

229229
if (servers.isEmpty()) {
230230
Log.e(AppConfig.TAG, "No servers found in hardcoded subscription")
@@ -253,7 +253,13 @@ object SmartConnectManager {
253253
if (V2RayServiceManager.isRunning()) {
254254
MessageUtil.sendMsg2Service(context, AppConfig.MSG_STATE_SWITCH_SERVER, "")
255255
} else {
256-
V2RayServiceManager.startVService(context)
256+
withContext(Dispatchers.Main) {
257+
if (context is com.kiktor.v2whitelist.ui.MainActivity) {
258+
context.startV2Ray()
259+
} else {
260+
V2RayServiceManager.startVService(context)
261+
}
262+
}
257263
}
258264
} else {
259265
Log.e(AppConfig.TAG, "Critical: No servers available to connect")
@@ -267,7 +273,7 @@ object SmartConnectManager {
267273
suspend fun switchServer(context: Context) = withContext(Dispatchers.IO) {
268274
val currentGuid = MmkvManager.getSelectServer()
269275
val allServers = MmkvManager.decodeServerList()
270-
val servers = filterServers(allServers, excludeGuid = currentGuid)
276+
val servers = filterServers(allServers, excludeGuid = currentGuid).shuffled().take(20)
271277

272278
if (servers.isEmpty()) {
273279
return@withContext
@@ -292,7 +298,13 @@ object SmartConnectManager {
292298
if (V2RayServiceManager.isRunning()) {
293299
MessageUtil.sendMsg2Service(context, AppConfig.MSG_STATE_SWITCH_SERVER, "")
294300
} else {
295-
V2RayServiceManager.startVService(context)
301+
withContext(Dispatchers.Main) {
302+
if (context is com.kiktor.v2whitelist.ui.MainActivity) {
303+
context.startV2Ray()
304+
} else {
305+
V2RayServiceManager.startVService(context)
306+
}
307+
}
296308
}
297309
}
298310
}

0 commit comments

Comments
 (0)