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

feat: support ports that are not well known for use with port forwarding #1930

Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 17 additions & 3 deletions sdk/src/main/java/com/hedera/hashgraph/sdk/BaseNodeAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class BaseNodeAddress {
@Nullable
private final String address;
private final int port;
private final boolean secure;

/**
* Constructor.
Expand All @@ -49,9 +50,22 @@ class BaseNodeAddress {
* @param port the port part
*/
public BaseNodeAddress(@Nullable String name, @Nullable String address, int port) {
this(name, address, port, false);
}

/**
* Constructor.
*
* @param name the name part
* @param address the address part
* @param port the port part
* @param secure secure transport
*/
public BaseNodeAddress(@Nullable String name, @Nullable String address, int port, boolean secure) {
this.name = name;
this.address = address;
this.port = port;
this.secure = secure;
}

/**
Expand Down Expand Up @@ -118,7 +132,7 @@ public boolean isInProcess() {
* @return are we secure
*/
public boolean isTransportSecurity() {
return port == PORT_NODE_TLS || port == PORT_MIRROR_TLS;
return port == PORT_NODE_TLS || port == PORT_MIRROR_TLS || secure;
}

/**
Expand All @@ -128,7 +142,7 @@ public boolean isTransportSecurity() {
*/
public BaseNodeAddress toInsecure() {
var newPort = (this.port == PORT_NODE_TLS) ? PORT_NODE_PLAIN : this.port;
return new BaseNodeAddress(name, address, newPort);
return new BaseNodeAddress(name, address, newPort, false);
}

/**
Expand All @@ -138,7 +152,7 @@ public BaseNodeAddress toInsecure() {
*/
public BaseNodeAddress toSecure() {
var newPort = (this.port == PORT_NODE_PLAIN) ? PORT_NODE_TLS : this.port;
return new BaseNodeAddress(name, address, newPort);
return new BaseNodeAddress(name, address, newPort, true);
}

@Override
Expand Down
11 changes: 10 additions & 1 deletion sdk/src/main/java/com/hedera/hashgraph/sdk/Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@
*/
package com.hedera.hashgraph.sdk;

import static com.hedera.hashgraph.sdk.BaseNodeAddress.PORT_NODE_PLAIN;
import static com.hedera.hashgraph.sdk.BaseNodeAddress.PORT_NODE_TLS;

import com.google.common.io.ByteStreams;
import com.google.errorprone.annotations.Var;
import com.google.protobuf.ByteString;

import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -207,9 +211,14 @@ private static Map<AccountId, NodeAddress> getAddressBookForLedger(@Nullable Led

static HashMap<String, AccountId> addressBookToNetwork(Collection<NodeAddress> addressBook, int desiredPort) {
var network = new HashMap<String, AccountId>();
boolean wellKnownPorts = addressBook.stream()
.map(NodeAddress::getAddresses)
.flatMap(Collection::stream)
.map(Endpoint::getPort)
.allMatch(Set.of(PORT_NODE_TLS, PORT_NODE_PLAIN)::contains);
for (var nodeAddress : addressBook) {
for (var endpoint : nodeAddress.addresses) {
if (endpoint.port == desiredPort) {
if (!wellKnownPorts || endpoint.port == desiredPort) {
0xivanov marked this conversation as resolved.
Show resolved Hide resolved
network.put(endpoint.toString(), nodeAddress.accountId);
}
}
Expand Down
Loading