[PATCH android] config: fix wrong Peer endpoint string format

Zhao Gang gang.zhao.42 at gmail.com
Mon Aug 13 17:12:42 CEST 2018


When a tunnel is running, saving the tunnel's config with an IPv6 address endpoint like [::1]:42 would result in the wrong format ::1:42. This patch fixes it.

For endpoints with an IPv6 address(e.g. [::1]:42). Since the default endpoint InetSocketAddress is created unresolved, getEndpointString() returns "[::1]:42" (InetSocketAddress.getHostString() returns the literal hostname). After the endpoint is resolved, getEndpointString() returns "::1:42" (InetSocketAddress.getHostString() returns the IPv6 address without the square brackets). This inconsistent return values caused the above mentioned bug.

With this patch, function getEndpointString would return the right format string whether the endpoint is resolved or not. Also changed the function getResolvedEndpointString to call getEndpointString instead of making the string itself.

Signed-off-by: Zhao Gang <gang.zhao.42 at gmail.com>
---
 app/src/main/java/com/wireguard/config/Peer.java | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/app/src/main/java/com/wireguard/config/Peer.java b/app/src/main/java/com/wireguard/config/Peer.java
index 49c8b70..4c8703c 100644
--- a/app/src/main/java/com/wireguard/config/Peer.java
+++ b/app/src/main/java/com/wireguard/config/Peer.java
@@ -71,7 +71,11 @@ public class Peer {
     private String getEndpointString() {
         if (endpoint == null)
             return null;
-        return String.format("%s:%d", endpoint.getHostString(), endpoint.getPort());
+
+        if (endpoint.getHostString().contains(":") && !endpoint.getHostString().contains("["))
+            return String.format("[%s]:%d", endpoint.getHostString(), endpoint.getPort());
+        else
+            return String.format("%s:%d", endpoint.getHostString(), endpoint.getPort());
     }
 
     public int getPersistentKeepalive() {
@@ -102,13 +106,8 @@ public class Peer {
             endpoint = new InetSocketAddress(endpoint.getHostString(), endpoint.getPort());
         if (endpoint.isUnresolved())
             throw new UnknownHostException(endpoint.getHostString());
-        if (endpoint.getAddress() instanceof Inet6Address)
-            return String.format("[%s]:%d",
-                    endpoint.getAddress().getHostAddress(),
-                    endpoint.getPort());
-        return String.format("%s:%d",
-                endpoint.getAddress().getHostAddress(),
-                endpoint.getPort());
+
+        return getEndpointString();
     }
 
     public void parse(final String line) {
-- 
2.18.0



More information about the WireGuard mailing list