Skip to content

Commit

Permalink
Tor: Throw ControlCommandFailedException when commands fail
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Aug 8, 2023
1 parent d1e8293 commit 22f6c25
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

package bisq.tor.controller;

public class ControlCommandFailedException extends RuntimeException {
public ControlCommandFailedException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,36 @@ public class NativeTorController implements BootstrapEventListener, HsDescUpload
private final CompletableFuture<String> hiddenServiceAddress = new CompletableFuture<>();
private Optional<TorControlConnection> 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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 22f6c25

Please sign in to comment.