make node parsing and formatting ipv6-friendly

This commit is contained in:
retrnull 2024-04-24 14:48:06 +00:00
parent 48577e46aa
commit 311ad076cb
2 changed files with 11 additions and 17 deletions

View File

@ -145,6 +145,8 @@ dependencies {
implementation 'com.google.android.material:material:1.11.0' 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 'me.dm7.barcodescanner:zxing:1.9.8'
implementation "com.squareup.okhttp3:okhttp:4.12.0" implementation "com.squareup.okhttp3:okhttp:4.12.0"
implementation "io.github.rburgst:okhttp-digest:3.1.0" implementation "io.github.rburgst:okhttp-digest:3.1.0"

View File

@ -16,6 +16,7 @@
package com.m2049r.xmrwallet.data; package com.m2049r.xmrwallet.data;
import com.google.common.net.HostAndPort;
import com.m2049r.xmrwallet.model.NetworkType; import com.m2049r.xmrwallet.model.NetworkType;
import com.m2049r.xmrwallet.model.WalletManager; import com.m2049r.xmrwallet.model.WalletManager;
import com.m2049r.xmrwallet.util.OnionHelper; import com.m2049r.xmrwallet.util.OnionHelper;
@ -169,10 +170,10 @@ public class Node {
throw new IllegalArgumentException("Too many '/' or too few"); throw new IllegalArgumentException("Too many '/' or too few");
daemonAddress = daParts[0]; daemonAddress = daParts[0];
String da[] = daemonAddress.split(":"); HostAndPort hostAndPort = HostAndPort.fromString(daemonAddress)
if ((da.length > 2) || (da.length < 1)) .withDefaultPort(getDefaultRpcPort())
throw new IllegalArgumentException("Too many ':' or too few"); .requireBracketsForIPv6();
String host = da[0]; String host = hostAndPort.getHost();
if (daParts.length == 1) { if (daParts.length == 1) {
networkType = NetworkType.NetworkType_Mainnet; networkType = NetworkType.NetworkType_Mainnet;
@ -204,22 +205,12 @@ public class Node {
} }
this.name = name; 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 { try {
setHost(host); setHost(host);
} catch (UnknownHostException ex) { } catch (UnknownHostException ex) {
throw new IllegalArgumentException("cannot resolve host " + host); throw new IllegalArgumentException("cannot resolve host " + host);
} }
this.rpcPort = port; this.rpcPort = hostAndPort.getPort();
this.levinPort = getDefaultLevinPort(); this.levinPort = getDefaultLevinPort();
} }
@ -233,7 +224,8 @@ public class Node {
if (!username.isEmpty() && !password.isEmpty()) { if (!username.isEmpty() && !password.isEmpty()) {
sb.append(username).append(":").append(password).append("@"); 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("/"); sb.append("/");
switch (networkType) { switch (networkType) {
case NetworkType_Mainnet: case NetworkType_Mainnet:
@ -271,7 +263,7 @@ public class Node {
} }
public String getAddress() { public String getAddress() {
return getHostAddress() + ":" + rpcPort; return HostAndPort.fromParts(getHostAddress(), rpcPort).toString();
} }
public String getHostAddress() { public String getHostAddress() {