Skip to content

Commit

Permalink
Use embedded binary for local Tor network
Browse files Browse the repository at this point in the history
Developers can run a local Tor network to test changes and simulate
network conditions. This change enforces that all local Tor networks
have the same behavior.
  • Loading branch information
alvasw committed May 21, 2024
1 parent 3546d6f commit 89f0b5a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions network/tor/tor-local-network/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ application {

dependencies {
implementation(project(":tor-common"))
implementation(project(":tor"))
implementation(libs.chimp.jtorctl)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import bisq.network.tor.common.torrc.DirectoryAuthority;
import bisq.network.tor.common.torrc.TorrcConfigGenerator;
import bisq.network.tor.common.torrc.TorrcFileGenerator;
import bisq.tor.installer.TorInstaller;
import bisq.tor.local_network.da.DirectoryAuthorityFactory;
import bisq.tor.local_network.torrc.TestNetworkTorrcGeneratorFactory;
import bisq.tor.process.LdPreload;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -109,6 +111,7 @@ public TorNetwork addClient() {

public void start() throws IOException {
generateTorrcFiles();
installTor();
startProcesses();
}

Expand Down Expand Up @@ -145,6 +148,12 @@ private void generateTorrcFiles() {
}
}

private void installTor() {
Path torBinaryDirPath = rootDataDir.resolve("tor_binary");
var torInstaller = new TorInstaller(torBinaryDirPath);
torInstaller.installIfNotUpToDate();
}

private void startProcesses() throws IOException {
for (TorNode directoryAuthority : directoryAuthorities) {
Process process = createAndStartTorProcess(directoryAuthority);
Expand All @@ -163,10 +172,19 @@ private void startProcesses() throws IOException {
}

private Process createAndStartTorProcess(TorNode torNode) throws IOException {
String absoluteTorBinaryPath = rootDataDir.resolve("tor_binary")
.resolve("tor")
.toAbsolutePath()
.toString();
String absoluteTorrcPathAsString = torNode.getTorrcPath()
.toAbsolutePath()
.toString();
var processBuilder = new ProcessBuilder("tor", "-f", absoluteTorrcPathAsString);
var processBuilder = new ProcessBuilder(absoluteTorBinaryPath, "-f", absoluteTorrcPathAsString);

Map<String, String> environment = processBuilder.environment();
Path torDataDirPath = rootDataDir.resolve("tor_binary");
environment.put("LD_PRELOAD", LdPreload.computeLdPreloadVariable(torDataDirPath));

processBuilder.redirectError(ProcessBuilder.Redirect.DISCARD);
processBuilder.redirectOutput(ProcessBuilder.Redirect.DISCARD);
return processBuilder.start();
Expand Down

0 comments on commit 89f0b5a

Please sign in to comment.