FederatedBtcNodeProvider: Compare banned node address and port

This commit is contained in:
Alva Swanson 2024-11-18 22:22:22 +00:00
parent 0da22ab3b0
commit 17d48d989a
No known key found for this signature in database
GPG Key ID: 004760E77F753090
2 changed files with 116 additions and 10 deletions

View File

@ -25,18 +25,29 @@ public class FederatedBtcNodeProvider {
.collect(Collectors.toSet());
hardcodedBtcNodes.addAll(filterProvidedBtcNodes);
Set<String> bannedBtcNodeHostNames = bannedBtcNodesConfig.stream()
Set<NodeAddress> bannedBtcNodeHostNames = bannedBtcNodesConfig.stream()
.filter(n -> !n.isEmpty())
.map(FederatedBtcNodeProvider::getNodeAddress)
.filter(Objects::nonNull)
.map(NodeAddress::getHostName)
.collect(Collectors.toSet());
return hardcodedBtcNodes.stream()
.filter(btcNode -> {
String nodeAddress = btcNode.hasOnionAddress() ? btcNode.getOnionAddress() :
btcNode.getHostNameOrAddress();
return !bannedBtcNodeHostNames.contains(nodeAddress);
Objects.requireNonNull(nodeAddress);
int port = btcNode.getPort();
for (NodeAddress bannedAddress : bannedBtcNodeHostNames) {
boolean isBanned = nodeAddress.equals(bannedAddress.getHostName()) &&
port == bannedAddress.getPort();
if (isBanned) {
return false;
}
}
return true;
})
.collect(Collectors.toList());
}

View File

@ -60,6 +60,29 @@ public class FederatedBtcNodeProviderTest {
assertIterableEquals(expected, selectedNodes);
}
@Test
void bannedIpV4NodeWrongPort() {
String bannedAddress = "123.456.890.123";
var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, null, bannedAddress, 4567, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
String bannedFullAddress = bannedAddress + ":" + 1234;
List<String> bannedBtcNodes = List.of(bannedFullAddress);
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
assertIterableEquals(hardcodedNodes, selectedNodes);
}
@Test
void bannedIpV6Node() {
String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370";
@ -91,19 +114,44 @@ public class FederatedBtcNodeProviderTest {
}
@Test
void bannedHostNameNode() {
void bannedIpV6NodeWrongPort() {
String bannedAddress = "2001:db8:85a3:8d3:1319:8a2e:370";
var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, "btc1.dnsalias.net", null,
5678, "@bob"),
new BtcNodes.BtcNode(null, null, bannedAddress, 7348, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of("btc1.dnsalias.net:5678");
String bannedFullAddress = "[" + bannedAddress + "]" + ":" + 1234;
List<String> bannedBtcNodes = List.of(bannedFullAddress);
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
assertIterableEquals(hardcodedNodes, selectedNodes);
}
@Test
void bannedHostNameNode() {
String bannedHostName = "btc1.dnsalias.net";
int port = 5678;
var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, bannedHostName, null, port, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of(bannedHostName + ":" + port);
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
@ -118,11 +166,35 @@ public class FederatedBtcNodeProviderTest {
}
@Test
void bannedOnionNode() {
void bannedHostNameNodeWrongPort() {
String bannedHostName = "btc1.dnsalias.net";
var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, "bob.onion", null,
new BtcNodes.BtcNode(null, bannedHostName, null, 5678, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of(bannedHostName + ":" + 1234);
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
assertIterableEquals(hardcodedNodes, selectedNodes);
}
@Test
void bannedOnionNode() {
String bannedOnionAddress = "bob.onion";
var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, bannedOnionAddress, null,
BtcNodes.BtcNode.DEFAULT_PORT, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
@ -130,7 +202,7 @@ public class FederatedBtcNodeProviderTest {
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of("bob.onion:8333");
List<String> bannedBtcNodes = List.of(bannedOnionAddress + ":" + BtcNodes.BtcNode.DEFAULT_PORT);
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
@ -143,4 +215,27 @@ public class FederatedBtcNodeProviderTest {
);
assertIterableEquals(expected, selectedNodes);
}
@Test
void bannedOnionNodeWrongPort() {
String bannedOnionAddress = "bob.onion";
var hardcodedNodes = List.of(
new BtcNodes.BtcNode(null, "alice.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@alice"),
new BtcNodes.BtcNode(null, bannedOnionAddress, null,
BtcNodes.BtcNode.DEFAULT_PORT, "@bob"),
new BtcNodes.BtcNode(null, "charlie.onion", null,
BtcNodes.BtcNode.DEFAULT_PORT, "@charlie")
);
List<BtcNodes.BtcNode> mutableHardcodedList = new ArrayList<>(hardcodedNodes);
List<String> filterProvidedBtcNodes = Collections.emptyList();
List<String> bannedBtcNodes = List.of(bannedOnionAddress + ":" + 1234);
List<BtcNodes.BtcNode> selectedNodes = FederatedBtcNodeProvider
.getNodes(mutableHardcodedList, filterProvidedBtcNodes, bannedBtcNodes);
assertIterableEquals(hardcodedNodes, selectedNodes);
}
}