Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tor Testnet Support #1140

Merged
merged 2 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import bisq.network.tor.common.torrc.BaseTorrcGenerator;
import bisq.network.tor.common.torrc.ClientTorrcGenerator;
import bisq.network.tor.common.torrc.TestNetworkTorrcGenerator;
import bisq.network.tor.common.torrc.TorrcConfigGenerator;
import lombok.Builder;
import net.freehaven.tor.control.PasswordDigest;
Expand All @@ -30,12 +31,18 @@
public class TorrcClientConfigFactory {
public static final String DISABLE_NETWORK_CONFIG_KEY = "DisableNetwork";

private final boolean isTestNetwork;
private final Path dataDir;
private final int controlPort;
private final int socksPort;
private final PasswordDigest hashedControlPassword;

public TorrcClientConfigFactory(Path dataDir, int controlPort, int socksPort, PasswordDigest hashedControlPassword) {
public TorrcClientConfigFactory(boolean isTestNetwork,
Path dataDir,
int controlPort,
int socksPort,
PasswordDigest hashedControlPassword) {
this.isTestNetwork = isTestNetwork;
this.dataDir = dataDir;
this.controlPort = controlPort;
this.socksPort = socksPort;
Expand All @@ -50,8 +57,13 @@ public Map<String, String> torrcClientConfigMap(Map<String, String> torrcOverrid
}

private TorrcConfigGenerator clientTorrcGenerator() {
TorrcConfigGenerator baseTorrcGenerator = baseTorrcGenerator();
if (isTestNetwork) {
baseTorrcGenerator = testNetworkTorrcGenerator(baseTorrcGenerator);
}

return ClientTorrcGenerator.builder()
.baseTorrcConfigGenerator(baseTorrcGenerator())
.baseTorrcConfigGenerator(baseTorrcGenerator)
.socksPort(socksPort)
.build();
}
Expand All @@ -63,4 +75,10 @@ private TorrcConfigGenerator baseTorrcGenerator() {
.hashedControlPassword(hashedControlPassword.getHashedPassword())
.build();
}

private static TorrcConfigGenerator testNetworkTorrcGenerator(TorrcConfigGenerator baseTorrcGenerator) {
return TestNetworkTorrcGenerator.builder()
.baseTorrcConfigGenerator(baseTorrcGenerator)
.build();
}
}
15 changes: 14 additions & 1 deletion network/tor/tor/src/main/java/bisq/tor/TorrcFileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.Set;

public class TorrcFileGenerator {
private final Path torrcPath;
private final Map<String, String> torrcConfigMap;
private final Set<DirectoryAuthority> customDirectoryAuthorities;

public TorrcFileGenerator(Path torrcPath, Map<String, String> torrcConfigMap) {
public TorrcFileGenerator(Path torrcPath,
Map<String, String> torrcConfigMap,
Set<DirectoryAuthority> customDirectoryAuthorities) {
this.torrcPath = torrcPath;
this.torrcConfigMap = torrcConfigMap;
this.customDirectoryAuthorities = customDirectoryAuthorities;
}

public void generate() {
Expand All @@ -40,6 +45,14 @@ public void generate() {
.append("\n")
);

customDirectoryAuthorities.forEach(dirAuthority ->
torrcStringBuilder.append("DirAuthority ").append(dirAuthority.getNickname())
.append(" orport=").append(dirAuthority.getOrPort())
.append(" v3ident=").append(dirAuthority.getV3Ident())
.append(" 127.0.0.1:").append(dirAuthority.getDirPort())
.append(" ").append(dirAuthority.getRelayFingerprint())
.append("\n"));

try {
Files.writeString(torrcPath, torrcStringBuilder.toString());
} catch (IOException e) {
Expand Down