From ca782dfc695a65591afc65a93c0d5e9f52b77697 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 10 Aug 2022 12:02:23 +0200 Subject: [PATCH] avoid resolution of onion hosts when creating proxied server socket addresses --- .../java/com/sparrowwallet/sparrow/net/Protocol.java | 6 +++++- .../sparrow/net/ProxySocketFactory.java | 12 ++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/net/Protocol.java b/src/main/java/com/sparrowwallet/sparrow/net/Protocol.java index bb7a22b1..b3780f34 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/Protocol.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/Protocol.java @@ -117,8 +117,12 @@ public enum Protocol { return toUrlString() + hostAndPort.toString(); } + public static boolean isOnionHost(String host) { + return host != null && host.toLowerCase(Locale.ROOT).endsWith(TorService.TOR_ADDRESS_SUFFIX); + } + public static boolean isOnionAddress(HostAndPort server) { - return server.getHost().toLowerCase(Locale.ROOT).endsWith(TorService.TOR_ADDRESS_SUFFIX); + return isOnionHost(server.getHost()); } public static boolean isOnionAddress(String address) { diff --git a/src/main/java/com/sparrowwallet/sparrow/net/ProxySocketFactory.java b/src/main/java/com/sparrowwallet/sparrow/net/ProxySocketFactory.java index 5172e2e1..3a65c450 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/ProxySocketFactory.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/ProxySocketFactory.java @@ -33,12 +33,12 @@ public class ProxySocketFactory extends SocketFactory { @Override public Socket createSocket(String address, int port) throws IOException { - return createSocket(new InetSocketAddress(address, port), null); + return createSocket(createAddress(address, port), null); } @Override public Socket createSocket(String address, int port, InetAddress localAddress, int localPort) throws IOException { - return createSocket(new InetSocketAddress(address, port), new InetSocketAddress(localAddress, localPort)); + return createSocket(createAddress(address, port), new InetSocketAddress(localAddress, localPort)); } @Override @@ -51,6 +51,14 @@ public class ProxySocketFactory extends SocketFactory { return createSocket(new InetSocketAddress(address, port), new InetSocketAddress(localAddress, localPort)); } + private InetSocketAddress createAddress(String address, int port) { + if(Protocol.isOnionHost(address)) { + return InetSocketAddress.createUnresolved(address, port); + } else { + return new InetSocketAddress(address, port); + } + } + private Socket createSocket(InetSocketAddress address, InetSocketAddress bindAddress) throws IOException { Socket socket = new Socket(proxy); if(bindAddress != null) {