From 311ad076cb6e9d5ecba935a1b3bed00bfda897b6 Mon Sep 17 00:00:00 2001 From: retrnull <168001952+retrnull@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:48:06 +0000 Subject: [PATCH] make node parsing and formatting ipv6-friendly --- app/build.gradle | 2 ++ .../java/com/m2049r/xmrwallet/data/Node.java | 26 +++++++------------ 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index ae17246d89..5a9795391b 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -145,6 +145,8 @@ dependencies { implementation 'com.google.android.material:material:1.11.0' + implementation "com.google.guava:guava:33.1.0-android" + implementation 'me.dm7.barcodescanner:zxing:1.9.8' implementation "com.squareup.okhttp3:okhttp:4.12.0" implementation "io.github.rburgst:okhttp-digest:3.1.0" diff --git a/app/src/main/java/com/m2049r/xmrwallet/data/Node.java b/app/src/main/java/com/m2049r/xmrwallet/data/Node.java index 0f0115304e..d11ab5baa1 100644 --- a/app/src/main/java/com/m2049r/xmrwallet/data/Node.java +++ b/app/src/main/java/com/m2049r/xmrwallet/data/Node.java @@ -16,6 +16,7 @@ package com.m2049r.xmrwallet.data; +import com.google.common.net.HostAndPort; import com.m2049r.xmrwallet.model.NetworkType; import com.m2049r.xmrwallet.model.WalletManager; import com.m2049r.xmrwallet.util.OnionHelper; @@ -169,10 +170,10 @@ static public Node fromString(String nodeString) { throw new IllegalArgumentException("Too many '/' or too few"); daemonAddress = daParts[0]; - String da[] = daemonAddress.split(":"); - if ((da.length > 2) || (da.length < 1)) - throw new IllegalArgumentException("Too many ':' or too few"); - String host = da[0]; + HostAndPort hostAndPort = HostAndPort.fromString(daemonAddress) + .withDefaultPort(getDefaultRpcPort()) + .requireBracketsForIPv6(); + String host = hostAndPort.getHost(); if (daParts.length == 1) { networkType = NetworkType.NetworkType_Mainnet; @@ -204,22 +205,12 @@ static public Node fromString(String nodeString) { } this.name = name; - int port; - if (da.length == 2) { - try { - port = Integer.parseInt(da[1]); - } catch (NumberFormatException ex) { - throw new IllegalArgumentException("Port not numeric"); - } - } else { - port = getDefaultRpcPort(); - } try { setHost(host); } catch (UnknownHostException ex) { throw new IllegalArgumentException("cannot resolve host " + host); } - this.rpcPort = port; + this.rpcPort = hostAndPort.getPort(); this.levinPort = getDefaultLevinPort(); } @@ -233,7 +224,8 @@ public String toString() { if (!username.isEmpty() && !password.isEmpty()) { sb.append(username).append(":").append(password).append("@"); } - sb.append(host).append(":").append(rpcPort); + HostAndPort address = HostAndPort.fromParts(host, rpcPort); + sb.append(address.toString()); sb.append("/"); switch (networkType) { case NetworkType_Mainnet: @@ -271,7 +263,7 @@ public Node(InetSocketAddress socketAddress) { } public String getAddress() { - return getHostAddress() + ":" + rpcPort; + return HostAndPort.fromParts(getHostAddress(), rpcPort).toString(); } public String getHostAddress() {