Modern Apps git

← Modern-Apps.git

vpn: fix bracketed-IPv6 endpoint host parsing

Commit
704447e6b0031f23738fd1ec6e678e3d1b482189
Author
Vayun Mathur <[email protected]>
When
2026-11-08 02:42 UTC (28 days ago)
Parents
05b6fca
VpnConfig.endpointHost() used peerEndpoint.substringBefore(':'), so a
bracketed IPv6 endpoint such as [2001:db8::1]:51820 parsed as the host
"[". VpnTunnelService.resolveEndpoint() then built
InetSocketAddress("[", port), which never resolves; DatagramChannel
connect threw UnresolvedAddressException (caught as
IllegalArgumentException in openUdpChannel), returned null, and the
service stopped the tunnel. Any config with an IPv6-literal endpoint
could never connect: tapping Connect showed the spinner briefly and
dropped back to disconnected.

Extract the host parsing into endpointHostOf(), which returns the
address inside [...] for bracketed literals and keeps the old
before-the-first-colon behaviour otherwise (unbracketed IPv6 without
brackets stays ambiguous and is out of scope). endpointPort() already
used substringAfterLast(':') and was correct for the bracketed form.

Apply the same fix to the import fallback name in
VpnViewModel.importFromUri, which had the identical substringBefore(':')
expression and would have named an imported IPv6 tunnel "[".

Update WgConfigParserTest.endpointHostAndPort, which had pinned the
buggy "[" result as a known limitation, to assert the fixed parsing
plus whitespace and malformed-bracket cases.

Verified with :vpn:testDevUnitTest (BUILD SUCCESSFUL).

Diff

vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.kt b/vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.ktindex 093e860477..5e934d9e0a 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/data/VpnConfig.kt@@ -111,7 +111,21 @@ object WgConfigParser {     } } -fun VpnConfig.endpointHost(): String = peerEndpoint.substringBefore(':').trim()+fun VpnConfig.endpointHost(): String = endpointHostOf(peerEndpoint)++/**+ * Host part of a wg-quick `Endpoint` value (`host:port`). Bracketed IPv6+ * literals (`[2001:db8::1]:51820`) yield the address inside the brackets;+ * anything else yields the text before the first `:`.+ */+fun endpointHostOf(peerEndpoint: String): String {+    val ep = peerEndpoint.trim()+    if (ep.startsWith("[")) {+        val end = ep.indexOf(']')+        if (end > 1) return ep.substring(1, end).trim()+    }+    return ep.substringBefore(':').trim()+} fun VpnConfig.endpointPort(): Int = peerEndpoint.substringAfterLast(':').toIntOrNull() ?: DEFAULT_WG_PORT  private const val DEFAULT_WG_PORT = 51820
vpn/src/main/java/com/vayunmathur/vpn/platform/VpnViewModel.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/platform/VpnViewModel.kt b/vpn/src/main/java/com/vayunmathur/vpn/platform/VpnViewModel.ktindex f7c1431ab2..1a5f1af3b7 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/platform/VpnViewModel.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/platform/VpnViewModel.kt@@ -21,6 +21,7 @@ import com.vayunmathur.vpn.data.DomainCountSummary import com.vayunmathur.vpn.data.VpnConfig import com.vayunmathur.vpn.data.VpnConfigDao import com.vayunmathur.vpn.data.WgConfigParser+import com.vayunmathur.vpn.data.endpointHostOf import com.vayunmathur.vpn.data.toEntity import com.vayunmathur.vpn.data.toModel import com.vayunmathur.vpn.service.AppResolver@@ -166,7 +167,7 @@ class VpnViewModel(                 val nameFromFile = uri.lastPathSegment                     ?.substringBeforeLast('.')                     ?.takeIf { it.isNotBlank() }-                    ?: imp.peerEndpoint.substringBefore(':').ifBlank { "Imported Tunnel" }+                    ?: endpointHostOf(imp.peerEndpoint).ifBlank { "Imported Tunnel" }                 val model = VpnConfig(                     name = nameFromFile,                     privateKey = imp.privateKey,
vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.kt
diff --git a/vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.kt b/vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.ktindex 605f6961f8..e891b0a1d6 100644--- a/vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.kt+++ b/vpn/src/test/java/com/vayunmathur/vpn/data/WgConfigParserTest.kt@@ -88,9 +88,14 @@ class WgConfigParserTest {         assertEquals(51820, VpnConfig(peerEndpoint = "vpn.example.com:abc").endpointPort())         assertEquals("", VpnConfig(peerEndpoint = "").endpointHost())         assertEquals(51820, VpnConfig(peerEndpoint = "").endpointPort())-        // Known limitation pinned as-is: a bracketed IPv6 endpoint host parses as "["-        // (substringBefore(':')), so IPv6-literal endpoints do not resolve. Recorded, not fixed here.-        assertEquals("[", VpnConfig(peerEndpoint = "[::1]:51820").endpointHost())+        // Bracketed IPv6 literals resolve to the address inside the brackets,+        // so IPv6 endpoints connect instead of resolving to "[".+        assertEquals("::1", VpnConfig(peerEndpoint = "[::1]:51820").endpointHost())+        assertEquals("2001:db8::1", VpnConfig(peerEndpoint = "[2001:db8::1]:51820").endpointHost())+        assertEquals(51820, VpnConfig(peerEndpoint = "[2001:db8::1]:51820").endpointPort())+        assertEquals("2001:db8::1", endpointHostOf("  [2001:db8::1]:51820  "))+        // Malformed brackets fall back to the pre-colon text, as before.+        assertEquals("[", VpnConfig(peerEndpoint = "[:51820").endpointHost())         assertEquals(1234, VpnConfig(peerEndpoint = "10.0.0.1:1234").endpointPort())     }