From 22f6c25443be81ea86c627065cb540372a28fa2c Mon Sep 17 00:00:00 2001 From: Alva Swanson Date: Mon, 31 Jul 2023 13:02:41 +0200 Subject: [PATCH] Tor: Throw ControlCommandFailedException when commands fail --- .../ControlCommandFailedException.java | 24 +++++++++ .../tor/controller/NativeTorController.java | 50 ++++++++++++------- 2 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 network/tor/src/main/java/bisq/tor/controller/ControlCommandFailedException.java diff --git a/network/tor/src/main/java/bisq/tor/controller/ControlCommandFailedException.java b/network/tor/src/main/java/bisq/tor/controller/ControlCommandFailedException.java new file mode 100644 index 0000000000..67476d2fe3 --- /dev/null +++ b/network/tor/src/main/java/bisq/tor/controller/ControlCommandFailedException.java @@ -0,0 +1,24 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.tor.controller; + +public class ControlCommandFailedException extends RuntimeException { + public ControlCommandFailedException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/network/tor/src/main/java/bisq/tor/controller/NativeTorController.java b/network/tor/src/main/java/bisq/tor/controller/NativeTorController.java index 45089288a5..1efd27449d 100644 --- a/network/tor/src/main/java/bisq/tor/controller/NativeTorController.java +++ b/network/tor/src/main/java/bisq/tor/controller/NativeTorController.java @@ -46,24 +46,36 @@ public class NativeTorController implements BootstrapEventListener, HsDescUpload private final CompletableFuture hiddenServiceAddress = new CompletableFuture<>(); private Optional torControlConnection = Optional.empty(); - public void connect(int controlPort, PasswordDigest controlConnectionSecret) throws IOException { - var controlSocket = new Socket("127.0.0.1", controlPort); - var controlConnection = new TorControlConnection(controlSocket); - controlConnection.launchThread(true); - controlConnection.authenticate(controlConnectionSecret.getSecret()); - torControlConnection = Optional.of(controlConnection); + public synchronized void connect(int controlPort, PasswordDigest controlConnectionSecret) { + try { + var controlSocket = new Socket("127.0.0.1", controlPort); + var controlConnection = new TorControlConnection(controlSocket); + controlConnection.launchThread(true); + controlConnection.authenticate(controlConnectionSecret.getSecret()); + torControlConnection = Optional.of(controlConnection); + } catch (IOException e) { + throw new ControlCommandFailedException("Couldn't connect to control port.", e); + } } - public void bindTorToConnection() throws IOException { - TorControlConnection controlConnection = torControlConnection.orElseThrow(); - controlConnection.takeOwnership(); - controlConnection.resetConf(NativeTorProcess.ARG_OWNER_PID); + public void bindTorToConnection() { + try { + TorControlConnection controlConnection = torControlConnection.orElseThrow(); + controlConnection.takeOwnership(); + controlConnection.resetConf(NativeTorProcess.ARG_OWNER_PID); + } catch (IOException e) { + throw new ControlCommandFailedException("Couldn't bind Tor to control connection.", e); + } } - public void enableTorNetworking() throws IOException { - TorControlConnection controlConnection = torControlConnection.orElseThrow(); - addBootstrapEventListener(controlConnection); - controlConnection.setConf(ClientTorrcGenerator.DISABLE_NETWORK_CONFIG_KEY, "0"); + public void enableTorNetworking() { + try { + TorControlConnection controlConnection = torControlConnection.orElseThrow(); + addBootstrapEventListener(controlConnection); + controlConnection.setConf(ClientTorrcGenerator.DISABLE_NETWORK_CONFIG_KEY, "0"); + } catch (IOException e) { + throw new ControlCommandFailedException("Couldn't enable Tor networking.", e); + } } public TorControlConnection.CreateHiddenServiceResult createHiddenService( @@ -117,9 +129,13 @@ public void waitUntilBootstrapped() { } } - public void shutdown() throws IOException { - TorControlConnection controlConnection = torControlConnection.orElseThrow(); - controlConnection.shutdownTor("SHUTDOWN"); + public synchronized void shutdown() { + try { + TorControlConnection controlConnection = torControlConnection.orElseThrow(); + controlConnection.shutdownTor("SHUTDOWN"); + } catch (IOException e) { + throw new ControlCommandFailedException("Couldn't send shutdown command to Tor.", e); + } } @Override