diff --git a/build.gradle b/build.gradle index 9b36ae61..65faa187 100644 --- a/build.gradle +++ b/build.gradle @@ -1,7 +1,7 @@ plugins { id 'application' id 'org-openjfx-javafxplugin' - id 'org.beryx.jlink' version '3.0.1' + id 'org.beryx.jlink' version '3.1.1' id 'org.gradlex.extra-java-module-info' version '1.9' } @@ -132,7 +132,7 @@ dependencies { exclude group: 'org.slf4j' } implementation('com.sparrowwallet.bokmakierie:bokmakierie:1.0') - implementation('com.sparrowwallet:tern:1.0.3') + implementation('com.sparrowwallet:tern:1.0.6') implementation('io.reactivex.rxjava2:rxjava:2.2.15') implementation('io.reactivex.rxjava2:rxjavafx:2.2.2') implementation('org.apache.commons:commons-lang3:3.7') diff --git a/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java b/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java index e886f0b2..e000ad92 100644 --- a/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java +++ b/src/main/java/com/sparrowwallet/sparrow/net/HttpClientService.java @@ -6,7 +6,23 @@ import javafx.concurrent.Task; public class HttpClientService extends com.sparrowwallet.tern.http.client.HttpClientService { public HttpClientService(HostAndPort torProxy) { - super(new HttpProxySupplier(torProxy)); + super(new TorHttpProxySupplier(torProxy)); + } + + public HostAndPort getTorProxy() { + if(getHttpProxySupplier() instanceof TorHttpProxySupplier torHttpProxySupplier) { + return torHttpProxySupplier.getTorProxy(); + } + + return null; + } + + public void setTorProxy(HostAndPort torProxy) { + if(getHttpProxySupplier() instanceof TorHttpProxySupplier torHttpProxySupplier) { + //Ensure all http clients are shutdown first + stop(); + torHttpProxySupplier._setTorProxy(torProxy); + } } public static class ShutdownService extends Service { diff --git a/src/main/java/com/sparrowwallet/sparrow/net/HttpProxySupplier.java b/src/main/java/com/sparrowwallet/sparrow/net/HttpProxySupplier.java deleted file mode 100644 index c0c62050..00000000 --- a/src/main/java/com/sparrowwallet/sparrow/net/HttpProxySupplier.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.sparrowwallet.sparrow.net; - -import com.google.common.net.HostAndPort; -import com.sparrowwallet.tern.http.client.TorHttpProxySupplier; - -public class HttpProxySupplier extends TorHttpProxySupplier { - public HttpProxySupplier(HostAndPort torProxy) { - super(torProxy); - } - - @Override - public void changeIdentity() { - HostAndPort torProxy = getTorProxy(); - if(torProxy != null) { - TorUtils.changeIdentity(torProxy); - } - } -} diff --git a/src/main/java/com/sparrowwallet/sparrow/net/TorHttpProxySupplier.java b/src/main/java/com/sparrowwallet/sparrow/net/TorHttpProxySupplier.java new file mode 100644 index 00000000..50158012 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/net/TorHttpProxySupplier.java @@ -0,0 +1,48 @@ +package com.sparrowwallet.sparrow.net; + +import com.google.common.net.HostAndPort; +import com.sparrowwallet.tern.http.client.*; + +import java.util.Optional; + +public class TorHttpProxySupplier implements IHttpProxySupplier { + private HostAndPort torProxy; + private HttpProxy httpProxy; + + public TorHttpProxySupplier(HostAndPort torProxy) { + this.torProxy = torProxy; + this.httpProxy = computeHttpProxy(torProxy); + } + + private HttpProxy computeHttpProxy(HostAndPort hostAndPort) { + if (hostAndPort == null) { + return null; + } + + return new HttpProxy(HttpProxyProtocol.SOCKS, hostAndPort.getHost(), hostAndPort.getPort()); + } + + public HostAndPort getTorProxy() { + return torProxy; + } + + // shouldn't call directly but use httpClientService.setTorProxy() + public void _setTorProxy(HostAndPort hostAndPort) { + // set proxy + this.torProxy = hostAndPort; + this.httpProxy = computeHttpProxy(hostAndPort); + } + + @Override + public Optional getHttpProxy(HttpUsage httpUsage) { + return Optional.ofNullable(httpProxy); + } + + @Override + public void changeIdentity() { + HostAndPort torProxy = getTorProxy(); + if(torProxy != null) { + TorUtils.changeIdentity(torProxy); + } + } +}