Fix tunnel killed during connect by VPN-interface watchdog

The stats updater treated "WireGuard interface not up within 6s of the
Go client process start" as a lost/replaced VPN interface and emergency-
stopped the tunnel. But the interface only comes up after the client
finishes captcha -> VK creds -> WRAP -> TURN/DTLS -> WireGuard config,
which takes far longer than 6s (especially with manual captcha mode), so
the tunnel was killed ~7s into every connect before it could establish.

Watch for an interface that was actually up and then disappeared instead
of a fixed startup window: only emergency-stop once the tunnel has been
observed UP at least once. The connect phase of any duration is no longer
policed, while a real tun loss/replacement is still caught.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
andbul0103andClaude Opus 4.8 committed 2026-06-14 19:54:15 +04:00
1 parent 2d8fa61db3
commit e3b294bf69
1 file changed
+9 -2
@@ -280,6 +280,12 @@ class TunnelService : Service() {
private fun startStatsUpdater() {
updateJob?.cancel()
updateJob = TunnelManager.scope.launch(Dispatchers.Main) {
// Сторож следит за ПРОПАЖОЙ уже поднятого VPN-интерфейса, а не за его
// отсутствием во время подключения. Фаза установки туннеля (капча → VK-креды →
// WRAP → TURN/DTLS → выдача WireGuard-конфига) может занимать значительно больше
// времени, чем любое фиксированное окно, особенно при ручном обходе капчи, поэтому
// аварийно глушим туннель только если интерфейс был поднят и затем пропал/заменён.
var wasEverUp = false
delay(1000)
while (isActive) {
if (!TunnelManager.running.value && !isTunnelPaused) {
@@ -289,8 +295,9 @@ class TunnelService : Service() {
}
if (TunnelManager.running.value && !isTunnelPaused) {
val helper = WireGuardHelper(applicationContext)
val startupWindow = System.currentTimeMillis() - TunnelManager.processStartedAtMs < 6000
if (!startupWindow && !helper.isTunnelUp()) {
if (helper.isTunnelUp()) {
wasEverUp = true
} else if (wasEverUp) {
Log.w("TunnelService", "Обнаружена пропажа или замена VPN-интерфейса! Экстренное выключение туннеля.")
stopTunnel()
break