Skip to content

Commit

Permalink
tor: Remove lock if not running
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Jun 24, 2024
1 parent 959634f commit 70d6183
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions network/tor/tor/src/main/java/bisq/tor/TorService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import lombok.extern.slf4j.Slf4j;
import net.freehaven.tor.control.PasswordDigest;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
Expand Down Expand Up @@ -76,6 +77,16 @@ public CompletableFuture<Boolean> initialize() {
createTorrcConfigFile(torDataDirPath, hashedControlPassword);

Path torBinaryPath = getTorBinaryPath();
if (!isTorRunning(torBinaryPath.toString())) {
File lockFile = torDataDirPath.resolve("lock").toFile();
if (lockFile.exists()) {
boolean isSuccess = lockFile.delete();
if (!isSuccess) {
throw new IllegalStateException("Couldn't remove tor lock file.");
}
}
}

var nativeTorProcess = new NativeTorProcess(torBinaryPath, torDataDirPath);
torProcess = Optional.of(nativeTorProcess);
nativeTorProcess.start();
Expand Down Expand Up @@ -170,6 +181,14 @@ private Path getTorBinaryPath() {
return torDataDirPath.resolve("tor");
}

private boolean isTorRunning(String absoluteTorBinaryPath) {
return ProcessHandle.allProcesses()
.anyMatch(processHandle -> processHandle.info()
.commandLine()
.orElse("")
.startsWith(absoluteTorBinaryPath));
}

private void installTorIfNotUpToDate() {
Path torDataDirPath = transportConfig.getDataDir();
var torInstaller = new TorInstaller(torDataDirPath);
Expand Down

0 comments on commit 70d6183

Please sign in to comment.