From d0f21eafd1abbefc95ceb2b75256849d0e954309 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Wed, 8 Nov 2023 17:08:44 +0200 Subject: [PATCH] schedule regular check to indicate proxy status in sparrow terminal --- .../sparrow/terminal/ProxyStatusLabel.java | 65 +++++++++++++++++++ .../sparrow/terminal/SparrowTextGui.java | 29 ++++++++- .../sparrow/transaction/headers.fxml | 2 +- 3 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/sparrowwallet/sparrow/terminal/ProxyStatusLabel.java diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/ProxyStatusLabel.java b/src/main/java/com/sparrowwallet/sparrow/terminal/ProxyStatusLabel.java new file mode 100644 index 00000000..cb983842 --- /dev/null +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/ProxyStatusLabel.java @@ -0,0 +1,65 @@ +package com.sparrowwallet.sparrow.terminal; + +import com.google.common.net.HostAndPort; +import com.googlecode.lanterna.gui2.Label; +import com.sparrowwallet.sparrow.AppServices; +import com.sparrowwallet.sparrow.io.Config; +import javafx.concurrent.ScheduledService; +import javafx.concurrent.Task; +import javafx.concurrent.Worker; +import javafx.util.Duration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.Socket; + +public class ProxyStatusLabel extends Label { + private static final Logger log = LoggerFactory.getLogger(ProxyStatusLabel.class); + + private final ProxyConnectionTest proxyConnectionTest = new ProxyConnectionTest(); + + public ProxyStatusLabel() { + super(""); + } + + public void update() { + if(!AppServices.isUsingProxy()) { + getTextGUI().getGUIThread().invokeLater(() -> setText("")); + } else if(!Config.get().isUseProxy()) { + proxyConnectionTest.cancel(); + if(AppServices.isTorRunning()) { + getTextGUI().getGUIThread().invokeLater(() -> setText("Proxy enabled")); + } + } else if(!proxyConnectionTest.isRunning()) { + if(proxyConnectionTest.getState() == Worker.State.CANCELLED || proxyConnectionTest.getState() == Worker.State.FAILED) { + proxyConnectionTest.reset(); + } + proxyConnectionTest.setPeriod(Duration.seconds(20.0)); + proxyConnectionTest.setBackoffStrategy(null); + proxyConnectionTest.setOnSucceeded(workerStateEvent -> { + getTextGUI().getGUIThread().invokeLater(() -> setText("Proxy enabled")); + }); + proxyConnectionTest.setOnFailed(workerStateEvent -> { + getTextGUI().getGUIThread().invokeLater(() -> setText("Proxy error!")); + log.warn("Failed to connect to external Tor proxy: " + workerStateEvent.getSource().getException().getMessage()); + }); + proxyConnectionTest.start(); + } + } + + private static class ProxyConnectionTest extends ScheduledService { + @Override + protected Task createTask() { + return new Task<>() { + protected Void call() throws IOException { + HostAndPort proxyHostAndPort = HostAndPort.fromString(Config.get().getProxyServer()); + Socket socket = new Socket(proxyHostAndPort.getHost(), proxyHostAndPort.getPort()); + socket.close(); + + return null; + } + }; + } + } +} diff --git a/src/main/java/com/sparrowwallet/sparrow/terminal/SparrowTextGui.java b/src/main/java/com/sparrowwallet/sparrow/terminal/SparrowTextGui.java index 81425d5d..2851bc7b 100644 --- a/src/main/java/com/sparrowwallet/sparrow/terminal/SparrowTextGui.java +++ b/src/main/java/com/sparrowwallet/sparrow/terminal/SparrowTextGui.java @@ -10,6 +10,7 @@ import com.sparrowwallet.sparrow.event.*; import com.sparrowwallet.sparrow.io.Config; import com.sparrowwallet.sparrow.net.ServerType; import javafx.animation.*; +import javafx.application.Platform; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.util.Duration; @@ -20,6 +21,7 @@ public class SparrowTextGui extends MultiWindowTextGUI { private final Panel titleBar; private final Panel statusBar; + private final ProxyStatusLabel proxyStatusLabel; private final Label connectedLabel; private final Label statusLabel; private final ProgressBar statusProgress; @@ -36,8 +38,10 @@ public class SparrowTextGui extends MultiWindowTextGUI { Panel panel = new Panel(new BorderLayout()); - titleBar = new Panel(new GridLayout(2)); + titleBar = new Panel(new GridLayout(3)); new Label("Sparrow Terminal").addTo(titleBar); + this.proxyStatusLabel = new ProxyStatusLabel(); + titleBar.addComponent(proxyStatusLabel, GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER, true, false)); this.connectedLabel = new Label("Disconnected"); titleBar.addComponent(connectedLabel, GridLayout.createLayoutData(GridLayout.Alignment.END, GridLayout.Alignment.CENTER, true, false)); panel.addComponent(titleBar, BorderLayout.Location.TOP); @@ -73,10 +77,14 @@ public class SparrowTextGui extends MultiWindowTextGUI { private void setConnectedLabel(Integer height) { getGUIThread().invokeLater(() -> { - connectedLabel.setText(height == null ? "Disconnected" : "Connected " + (AppServices.isUsingProxy() ? "with proxy " : "") + "at " + height); + connectedLabel.setText(height == null ? "Disconnected" : "Connected at " + height); }); } + private void updateProxyStatusLabel() { + Platform.runLater(proxyStatusLabel::update); + } + @Subscribe public void connectionStart(ConnectionStartEvent event) { statusUpdated(new StatusEvent(event.getStatus(), 120)); @@ -85,12 +93,14 @@ public class SparrowTextGui extends MultiWindowTextGUI { @Subscribe public void connectionFailed(ConnectionFailedEvent event) { setDisconnectedLabel(); + updateProxyStatusLabel(); statusUpdated(new StatusEvent("Connection failed: " + event.getMessage())); } @Subscribe public void connection(ConnectionEvent event) { setConnectedLabel(event.getBlockHeight()); + updateProxyStatusLabel(); statusUpdated(new StatusEvent("Connected to " + Config.get().getServerDisplayName() + " at height " + event.getBlockHeight())); } @@ -107,6 +117,21 @@ public class SparrowTextGui extends MultiWindowTextGUI { setConnectedLabel(event.getHeight()); } + @Subscribe + public void torBootStatus(TorBootStatusEvent event) { + updateProxyStatusLabel(); + } + + @Subscribe + public void torFailedStatus(TorFailedStatusEvent event) { + updateProxyStatusLabel(); + } + + @Subscribe + public void torReadyStatus(TorReadyStatusEvent event) { + updateProxyStatusLabel(); + } + @Subscribe public void statusUpdated(StatusEvent event) { getGUIThread().invokeLater(() -> statusLabel.setText(event.getStatus())); diff --git a/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml b/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml index cba45b00..ad01fe8e 100644 --- a/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml +++ b/src/main/resources/com/sparrowwallet/sparrow/transaction/headers.fxml @@ -191,7 +191,7 @@
-
+