Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BtcNodes: Fix hostname parsing #7309

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/src/main/java/bisq/core/btc/nodes/BtcNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -125,6 +126,10 @@ public static BtcNode fromFullAddress(String fullAddress) {
host = parts[0];
if (parts.length == 2)
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");
Expand Down Expand Up @@ -172,5 +177,11 @@ public String getId() {
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");
}
}
}
67 changes: 67 additions & 0 deletions core/src/test/java/bisq/core/btc/nodes/BtcNodeTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bisq.core.btc.nodes;

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.Matchers.equalTo;
Expand All @@ -18,4 +20,69 @@ void hardcodedAndFilterProvidedNodeShouldBeEqual() {

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