mirror of
https://github.com/bisq-network/bisq.git
synced 2025-03-01 01:32:17 +01:00
BtcNodes: Fix hostname parsing
The fromFullAddress method parsed hostnames as IPV4 addresses instead as hostnames.
This commit is contained in:
parent
a0ff6073ac
commit
8fc4181cd7
2 changed files with 84 additions and 0 deletions
|
@ -23,6 +23,7 @@ import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import lombok.EqualsAndHashCode;
|
import lombok.EqualsAndHashCode;
|
||||||
|
@ -141,6 +142,10 @@ public class BtcNodes {
|
||||||
host = parts[0];
|
host = parts[0];
|
||||||
if (parts.length == 2)
|
if (parts.length == 2)
|
||||||
port = Integer.parseInt(parts[1]);
|
port = Integer.parseInt(parts[1]);
|
||||||
|
|
||||||
|
if (isHostName(host)) {
|
||||||
|
return new BtcNode(host, null, null, port, null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
checkArgument(host.length() > 0, "BtcNode address format not recognised");
|
checkArgument(host.length() > 0, "BtcNode address format not recognised");
|
||||||
|
@ -182,5 +187,17 @@ public class BtcNodes {
|
||||||
", port='" + port + '\'' +
|
", port='" + port + '\'' +
|
||||||
", operator='" + operator;
|
", operator='" + operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
String address = this.address == null ? "" : this.address + ", ";
|
||||||
|
String onionAddress = this.onionAddress == null ? "" : this.onionAddress;
|
||||||
|
return operator + ": [" + address + onionAddress + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isHostName(String hostName) {
|
||||||
|
String ipV4RegEx = "^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
|
||||||
|
boolean isIpV4Address = Pattern.matches(ipV4RegEx, hostName);
|
||||||
|
return !isIpV4Address && !hostName.endsWith(".onion");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package bisq.core.btc.nodes;
|
package bisq.core.btc.nodes;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
@ -18,4 +20,69 @@ public class BtcNodeTest {
|
||||||
|
|
||||||
assertThat(aliceHardcodedBtcNode, equalTo(aliceNodeFromFilter));
|
assertThat(aliceHardcodedBtcNode, equalTo(aliceNodeFromFilter));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {"123.456.890.123", "2001:db8:85a3:8d3:1319:8a2e:370"})
|
||||||
|
void fromFullAddressIpNoPort(String address) {
|
||||||
|
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(address);
|
||||||
|
assertThat(btcNode.getAddress(), equalTo(address));
|
||||||
|
assertThat(btcNode.getPort(), equalTo(BtcNodes.BtcNode.DEFAULT_PORT));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fromFullAddressIpV4() {
|
||||||
|
String address = "123.456.890.123";
|
||||||
|
int port = 4567;
|
||||||
|
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(address + ":" + port);
|
||||||
|
|
||||||
|
assertThat(btcNode.getAddress(), equalTo(address));
|
||||||
|
assertThat(btcNode.getPort(), equalTo(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fromFullAddressIpV6() {
|
||||||
|
String address = "2001:db8:85a3:8d3:1319:8a2e:370";
|
||||||
|
int port = 7348;
|
||||||
|
String fullAddress = "[" + address + "]:" + port;
|
||||||
|
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(fullAddress);
|
||||||
|
|
||||||
|
assertThat(btcNode.getAddress(), equalTo(address));
|
||||||
|
assertThat(btcNode.getPort(), equalTo(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fromFullAddressHostNameNoPort() {
|
||||||
|
String hostname = "btc-node.bisq.network";
|
||||||
|
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(hostname);
|
||||||
|
assertThat(btcNode.getHostName(), equalTo(hostname));
|
||||||
|
assertThat(btcNode.getPort(), equalTo(BtcNodes.BtcNode.DEFAULT_PORT));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fromFullAddressHostName() {
|
||||||
|
String hostname = "btc-node.bisq.network";
|
||||||
|
int port = 4567;
|
||||||
|
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(hostname + ":" + port);
|
||||||
|
|
||||||
|
assertThat(btcNode.getHostName(), equalTo(hostname));
|
||||||
|
assertThat(btcNode.getPort(), equalTo(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fromFullAddressOnionNoPort() {
|
||||||
|
String onionAddress = "alice.onion";
|
||||||
|
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(onionAddress);
|
||||||
|
assertThat(btcNode.getOnionAddress(), equalTo(onionAddress));
|
||||||
|
assertThat(btcNode.getPort(), equalTo(BtcNodes.BtcNode.DEFAULT_PORT));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void fromFullAddressOnion() {
|
||||||
|
String onionAddress = "alice.onion";
|
||||||
|
int port = 4567;
|
||||||
|
BtcNodes.BtcNode btcNode = BtcNodes.BtcNode.fromFullAddress(onionAddress + ":" + port);
|
||||||
|
|
||||||
|
assertThat(btcNode.getOnionAddress(), equalTo(onionAddress));
|
||||||
|
assertThat(btcNode.getPort(), equalTo(port));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue