Skip to content

Commit 6e8f491

Browse files
authored
fix(vpn): handle startTunBridge/startTun errors and clear tunBridgeActive only on success (#40)
1 parent a54792f commit 6e8f491

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

android/app/src/main/java/com/masterdns/vpn/service/MasterDnsVpnService.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,23 @@ class MasterDnsVpnService : VpnService() {
364364

365365
if (globalSettings.fakeDnsEnabled) {
366366
VpnManager.appendLog("Starting DNS-aware TUN bridge...")
367-
mobile.Mobile.startTunBridge(vpnInterface!!.fd.toLong(), 1400L, "127.0.0.1:$socksPort")
368-
tunBridgeActive = true
369-
VpnManager.appendLog("DNS-aware TUN bridge started")
367+
runCatching {
368+
mobile.Mobile.startTunBridge(vpnInterface!!.fd.toLong(), 1400L, "127.0.0.1:$socksPort")
369+
}.onSuccess {
370+
tunBridgeActive = true
371+
VpnManager.appendLog("DNS-aware TUN bridge started")
372+
}.onFailure { e ->
373+
VpnManager.appendLog("DNS-aware TUN bridge failed: ${e.message}")
374+
throw IllegalStateException("TUN bridge start failed: ${e.message}", e)
375+
}
370376
} else {
371377
VpnManager.appendLog("Starting tun2socks bridge: TUN fd -> socks5://127.0.0.1:$socksPort")
372-
mobile.Mobile.startTun(vpnInterface!!.fd.toLong(), "127.0.0.1:$socksPort")
378+
runCatching {
379+
mobile.Mobile.startTun(vpnInterface!!.fd.toLong(), "127.0.0.1:$socksPort")
380+
}.onFailure { e ->
381+
VpnManager.appendLog("tun2socks bridge failed: ${e.message}")
382+
throw IllegalStateException("TUN start failed: ${e.message}", e)
383+
}
373384
}
374385

375386
// Register Network Change detection to bounce TUN bridge
@@ -414,6 +425,10 @@ class MasterDnsVpnService : VpnService() {
414425
VpnManager.appendLog("Connection canceled")
415426
throw e
416427
} catch (e: Exception) {
428+
// The TUN bridge calls (lines 367/372) now re-throw on Go-side
429+
// error so this catch-all fires and stopVpn() runs — previously
430+
// a startTunBridge failure left the UI reporting CONNECTED
431+
// with no traffic flowing.
417432
Log.e(TAG, "Failed to start VPN", e)
418433
VpnManager.appendLog("Error: ${e.message}")
419434
VpnManager.setError(e.message ?: "Unknown error")

0 commit comments

Comments
 (0)