Skip to content

Commit

Permalink
Connect bitcoinj to hidden services This commit adds a few small modi…
Browse files Browse the repository at this point in the history
…fications to PeerAddress to make it easier for bitcoinj to connect to Tor hidden services when using the Orchid TorClient.
  • Loading branch information
cpacia authored and dan-da committed Jan 10, 2017
1 parent 3f5c74b commit 5cfebe6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/src/main/java/org/bitcoinj/core/Peer.java
Expand Up @@ -440,7 +440,7 @@ private void processVersionMessage(VersionMessage m) throws ProtocolException {
PeerAddress peerAddress = getAddress();
long peerTime = vPeerVersionMessage.time * 1000;
log.info("Connected to {}: version={}, subVer='{}', services=0x{}, time={}, blocks={}",
peerAddress == null ? "Peer" : peerAddress.getAddr().getHostAddress(),
peerAddress == null ? "Peer" : (peerAddress.getAddr() == null ? peerAddress.getHostname() : peerAddress.getAddr().getHostAddress()),
peerVersion,
vPeerVersionMessage.subVer,
vPeerVersionMessage.localServices,
Expand Down
30 changes: 28 additions & 2 deletions core/src/main/java/org/bitcoinj/core/PeerAddress.java
Expand Up @@ -39,6 +39,7 @@ public class PeerAddress extends ChildMessage {
static final int MESSAGE_SIZE = 30;

private InetAddress addr;
private String hostname; // Used for .onion addresses
private int port;
private BigInteger services;
private long time;
Expand Down Expand Up @@ -96,8 +97,20 @@ public PeerAddress(InetAddress addr) {
this(addr, MainNetParams.get().getPort());
}

/**
* Constructs a peer address from an {@link InetSocketAddress}. An InetSocketAddress can take in as parameters an
* InetAddress or a String hostname. If you want to connect to a .onion, set the hostname to the .onion address.
*/
public PeerAddress(InetSocketAddress addr) {
this(addr.getAddress(), addr.getPort());
if (addr.getHostName() == null || !addr.getHostName().toLowerCase().endsWith(".onion")) {
this.addr = checkNotNull(addr.getAddress());
} else {
this.hostname = addr.getHostName();
}
this.port = addr.getPort();
this.protocolVersion = NetworkParameters.PROTOCOL_VERSION;
this.services = BigInteger.ZERO;
length = protocolVersion > 31402 ? MESSAGE_SIZE : MESSAGE_SIZE - 4;
}

public static PeerAddress localhost(NetworkParameters params) {
Expand Down Expand Up @@ -162,6 +175,11 @@ public int getMessageSize() {
return length;
}

public String getHostname() {
maybeParse();
return hostname;
}

public InetAddress getAddr() {
maybeParse();
return addr;
Expand Down Expand Up @@ -215,6 +233,9 @@ public void setTime(long time) {

@Override
public String toString() {
if (hostname != null) {
return "[" + hostname + "]:" + port;
}
return "[" + addr.getHostAddress() + "]:" + port;
}

Expand All @@ -236,6 +257,11 @@ public int hashCode() {
}

public InetSocketAddress toSocketAddress() {
return new InetSocketAddress(addr, port);
// Reconstruct the InetSocketAddress properly
if (hostname != null) {
return InetSocketAddress.createUnresolved(hostname, port);
} else {
return new InetSocketAddress(addr, port);
}
}
}

0 comments on commit 5cfebe6

Please sign in to comment.