Skip to content

Commit

Permalink
Merge pull request #982 from alvasw/tor_support_local_network_restarts
Browse files Browse the repository at this point in the history
Tor: Support local network restart
  • Loading branch information
alvasw authored Jul 7, 2023
2 parents 3f8aad0 + 752a1ff commit db03251
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import bisq.tor.local_network.torrc.RelayTorrcGenerator;
import bisq.tor.local_network.torrc.TorrcFileGenerator;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashSet;
Expand Down Expand Up @@ -51,10 +52,7 @@ public TorNetwork addDirAuth(String passphrase) throws IOException, InterruptedE
String nickname = "da" + dirAuthIndex++;

Path nodeDataDir = rootDataDir.resolve(nickname);
boolean isSuccess = nodeDataDir.toFile().mkdir();
if (!isSuccess) {
throw new IllegalStateException("Couldn't create data directory for " + nickname);
}
createDataDirIfNotPresent(nodeDataDir);

var dirAuth = TorNode.builder()
.type(TorNode.Type.DIRECTORY_AUTHORITY)
Expand All @@ -74,10 +72,7 @@ public TorNetwork addRelay() {
String nickname = "relay" + relayIndex++;

Path nodeDataDir = rootDataDir.resolve(nickname);
boolean isSuccess = nodeDataDir.toFile().mkdir();
if (!isSuccess) {
throw new IllegalStateException("Couldn't create data directory for " + nickname);
}
createDataDirIfNotPresent(nodeDataDir);

TorNode firstRelay = TorNode.builder()
.type(TorNode.Type.RELAY)
Expand All @@ -97,10 +92,7 @@ public TorNetwork addClient() {
String nickname = "client" + clientIndex++;

Path nodeDataDir = rootDataDir.resolve(nickname);
boolean isSuccess = nodeDataDir.toFile().mkdir();
if (!isSuccess) {
throw new IllegalStateException("Couldn't create data directory for " + nickname);
}
createDataDirIfNotPresent(nodeDataDir);

TorNode firstClient = TorNode.builder()
.type(TorNode.Type.CLIENT)
Expand All @@ -121,24 +113,36 @@ public void start() throws IOException {
startProcesses();
}

private void createDataDirIfNotPresent(Path nodeDataDirPath) {
File nodeDataDirFile = nodeDataDirPath.toFile();
if (nodeDataDirFile.exists()) {
return;
}

boolean isSuccess = nodeDataDirPath.toFile().mkdir();
if (!isSuccess) {
throw new IllegalStateException("Couldn't create data directory: " + nodeDataDirPath.toAbsolutePath());
}
}

private void generateTorrcFiles() throws IOException {
Set<TorNode> allDAs = dirAuthFactory.getAllDirectoryAuthorities();
for (TorNode da : allDAs) {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(da);
var torrcFileGenerator = new TorrcFileGenerator(torDaTorrcGenerator, allDAs);
torrcFileGenerator.generate();
generateTorrc(da, torrcFileGenerator);
}

for (TorNode relay : relays) {
var relayTorrcGenerator = new RelayTorrcGenerator(relay);
var torrcFileGenerator = new TorrcFileGenerator(relayTorrcGenerator, allDAs);
torrcFileGenerator.generate();
generateTorrc(relay, torrcFileGenerator);
}

for (TorNode client : clients) {
var clientTorrcGenerator = new ClientTorrcGenerator(client);
var torrcFileGenerator = new TorrcFileGenerator(clientTorrcGenerator, allDAs);
torrcFileGenerator.generate();
generateTorrc(client, torrcFileGenerator);
}
}

Expand Down Expand Up @@ -168,4 +172,11 @@ private Process createAndStartTorProcess(TorNode torNode) throws IOException {
processBuilder.redirectOutput(ProcessBuilder.Redirect.DISCARD);
return processBuilder.start();
}

private void generateTorrc(TorNode torNode, TorrcFileGenerator torrcFileGenerator) throws IOException {
if (torNode.getTorrcPath().toFile().exists()) {
return;
}
torrcFileGenerator.generate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ public void createDirectoryAuthority(TorNode directoryAuthority,
createDataDirIfNotPresent(dataDir);

Path keysPath = dataDir.resolve("keys");
boolean isSuccess = keysPath.toFile().mkdirs();
if (!isSuccess) {
throw new IllegalStateException("Couldn't create keys folder in data directory for directory authority.");
File keysDirFile = keysPath.toFile();
if (!keysDirFile.exists()) {
boolean isSuccess = keysDirFile.mkdirs();
if (!isSuccess) {
throw new IllegalStateException("Couldn't create keys folder in data directory for directory authority.");
}
DirectoryAuthorityKeyGenerator.generate(directoryAuthority, passphrase);
}

DirectoryAuthorityKeyGenerator.generate(directoryAuthority, passphrase);

allDirectoryAuthorities.add(directoryAuthority);
}

Expand Down

0 comments on commit db03251

Please sign in to comment.