Skip to content

Commit

Permalink
Merge pull request #976 from alvasw/make_tor_node_generic
Browse files Browse the repository at this point in the history
Create generic TorNode class
  • Loading branch information
alvasw authored Jul 7, 2023
2 parents 4d88055 + 95a3215 commit 72d1851
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package bisq.tor.local_network;

import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.da.keygen.process.DirectoryAuthorityKeyGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand All @@ -35,7 +34,7 @@ public void generateKeys(@TempDir Path dataDir) throws IOException, InterruptedE
assertThat(keysPath.toFile().mkdirs())
.isTrue();

var directoryAuthority = DirectoryAuthority.builder()
var directoryAuthority = TorNode.builder()
.nickname("Nick")
.dataDir(dataDir)
.controlPort(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package bisq.tor.local_network;

import bisq.common.util.NetworkUtils;
import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.da.DirectoryAuthorityFactory;
import bisq.tor.local_network.torrc.DirectoryAuthorityTorrcGenerator;
import bisq.tor.local_network.torrc.TorrcFileGenerator;
Expand All @@ -36,7 +35,7 @@ public class DirectoryAuthorityTests {

@Test
public void createOneDA(@TempDir Path tempDir) throws IOException, InterruptedException {
var firstDirectoryAuthority = DirectoryAuthority.builder()
var firstDirectoryAuthority = TorNode.builder()
.nickname("DA_1")
.dataDir(tempDir)
.controlPort(NetworkUtils.findFreeSystemPort())
Expand All @@ -54,7 +53,7 @@ public void createThreeDA(@TempDir Path tempDir) throws IOException, Interrupted
var dirAuthFactory = new DirectoryAuthorityFactory();

Path firstDaDataDir = tempDir.resolve("da_1");
var firstDirectoryAuthority = DirectoryAuthority.builder()
var firstDirectoryAuthority = TorNode.builder()
.nickname("DA_1")
.dataDir(firstDaDataDir)
.controlPort(NetworkUtils.findFreeSystemPort())
Expand All @@ -64,7 +63,7 @@ public void createThreeDA(@TempDir Path tempDir) throws IOException, Interrupted
dirAuthFactory.createDirectoryAuthority(firstDirectoryAuthority, PASSPHRASE);

Path secondDaDataDir = tempDir.resolve("da_2");
var secondDirectoryAuthority = DirectoryAuthority.builder()
var secondDirectoryAuthority = TorNode.builder()
.nickname("DA_2")
.dataDir(secondDaDataDir)
.controlPort(NetworkUtils.findFreeSystemPort())
Expand All @@ -74,7 +73,7 @@ public void createThreeDA(@TempDir Path tempDir) throws IOException, Interrupted
dirAuthFactory.createDirectoryAuthority(secondDirectoryAuthority, PASSPHRASE);

Path thirdDaDataDir = tempDir.resolve("da_3");
var thirdDirectoryAuthority = DirectoryAuthority.builder()
var thirdDirectoryAuthority = TorNode.builder()
.nickname("DA_3")
.dataDir(thirdDaDataDir)
.controlPort(NetworkUtils.findFreeSystemPort())
Expand All @@ -83,8 +82,8 @@ public void createThreeDA(@TempDir Path tempDir) throws IOException, Interrupted
.build();
dirAuthFactory.createDirectoryAuthority(thirdDirectoryAuthority, PASSPHRASE);

Set<DirectoryAuthority> allDAs = dirAuthFactory.getAllDirectoryAuthorities();
for (DirectoryAuthority da : allDAs) {
Set<TorNode> allDAs = dirAuthFactory.getAllDirectoryAuthorities();
for (TorNode da : allDAs) {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(da);
var torrcFileGenerator = new TorrcFileGenerator(torDaTorrcGenerator, allDAs);
torrcFileGenerator.generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.tor.local_network.da;
package bisq.tor.local_network;

import bisq.tor.local_network.KeyFingerprintReader;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -29,7 +28,7 @@
import java.util.function.UnaryOperator;

@Getter
public class DirectoryAuthority {
public class TorNode {
private final String nickname;

private final Path dataDir;
Expand All @@ -48,7 +47,7 @@ public class DirectoryAuthority {
private Optional<String> relayKeyFingerprint = Optional.empty();

@Builder
public DirectoryAuthority(String nickname, Path dataDir, int controlPort, int orPort, int dirPort) {
public TorNode(String nickname, Path dataDir, int controlPort, int orPort, int dirPort) {
this.nickname = nickname;
this.dataDir = dataDir;
this.controlPort = controlPort;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package bisq.tor.local_network.da;

import bisq.tor.local_network.TorNode;
import bisq.tor.local_network.da.keygen.process.DirectoryAuthorityKeyGenerator;
import lombok.Getter;

Expand All @@ -29,9 +30,9 @@
public class DirectoryAuthorityFactory {

@Getter
private final Set<DirectoryAuthority> allDirectoryAuthorities = new HashSet<>();
private final Set<TorNode> allDirectoryAuthorities = new HashSet<>();

public void createDirectoryAuthority(DirectoryAuthority directoryAuthority,
public void createDirectoryAuthority(TorNode directoryAuthority,
String passphrase) throws IOException, InterruptedException {
Path dataDir = directoryAuthority.getDataDir();
createDataDirIfNotPresent(dataDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

package bisq.tor.local_network.da.keygen;

import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.TorNode;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class RelayKeyGenProcess {
private final DirectoryAuthority directoryAuthority;
private final TorNode directoryAuthority;

public RelayKeyGenProcess(DirectoryAuthority directoryAuthority) {
public RelayKeyGenProcess(TorNode directoryAuthority) {
this.directoryAuthority = directoryAuthority;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

package bisq.tor.local_network.da.keygen.process;

import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.TorNode;
import bisq.tor.local_network.da.keygen.RelayKeyGenProcess;

import java.io.IOException;

public class DirectoryAuthorityKeyGenerator {
public static void generate(DirectoryAuthority directoryAuthority, String passphrase) throws IOException, InterruptedException {
public static void generate(TorNode directoryAuthority, String passphrase) throws IOException, InterruptedException {
var identityKeyGenProcess = new DirectoryIdentityKeyGenProcess(
directoryAuthority.getKeysPath(),
"127.0.0.1:" + directoryAuthority.getDirPort()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@

package bisq.tor.local_network.torrc;

import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.TorNode;
import lombok.Getter;

/**
* The configuration settings are from the Chutney (<a href="https://gitweb.torproject.org/chutney.git/">project</a>).
*/
@Getter
public abstract class CommonTorrcGenerator {
protected final DirectoryAuthority thisDirectoryAuthority;
protected final TorNode thisTorNode;
protected final StringBuilder torrcStringBuilder = new StringBuilder();

public CommonTorrcGenerator(DirectoryAuthority thisDirectoryAuthority) {
this.thisDirectoryAuthority = thisDirectoryAuthority;
public CommonTorrcGenerator(TorNode thisTorNode) {
this.thisTorNode = thisTorNode;
}

public void generate() {
Expand All @@ -43,28 +43,28 @@ public void generate() {
.append("TestingDirAuthVoteGuard *\n")
.append("TestingMinExitFlagThreshold 0\n")

.append("DataDirectory ").append(thisDirectoryAuthority.getDataDir()).append("\n")
.append("DataDirectory ").append(thisTorNode.getDataDir()).append("\n")
.append("RunAsDaemon 1\n")
.append("Nickname ").append(thisDirectoryAuthority.getNickname()).append("\n")
.append("Nickname ").append(thisTorNode.getNickname()).append("\n")

.append("ShutdownWaitLength 2\n")
.append("DisableDebuggerAttachment 0\n")

.append("ControlPort 127.0.0.1:").append(thisDirectoryAuthority.getControlPort()).append("\n")
.append("ControlPort 127.0.0.1:").append(thisTorNode.getControlPort()).append("\n")
.append("CookieAuthentication 1\n")

.append("Log debug file ").append(thisDirectoryAuthority.getDataDir().resolve("debug.log").toAbsolutePath()).append("\n")
.append("Log debug file ").append(thisTorNode.getDataDir().resolve("debug.log").toAbsolutePath()).append("\n")
.append("ProtocolWarnings 1\n")
.append("SafeLogging 0\n")
.append("LogTimeGranularity 1\n")

.append("SocksPort 0\n")
.append("OrPort ").append(thisDirectoryAuthority.getOrPort()).append("\n")
.append("OrPort ").append(thisTorNode.getOrPort()).append("\n")
.append("Address 127.0.0.1\n")

.append("ServerDNSDetectHijacking 0\n")
.append("ServerDNSTestAddresses\n")

.append("DirPort ").append(thisDirectoryAuthority.getDirPort()).append("\n");
.append("DirPort ").append(thisTorNode.getDirPort()).append("\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

package bisq.tor.local_network.torrc;

import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.TorNode;

public class DirectoryAuthorityTorrcGenerator extends CommonTorrcGenerator {
public DirectoryAuthorityTorrcGenerator(DirectoryAuthority thisDirectoryAuthority) {
public DirectoryAuthorityTorrcGenerator(TorNode thisDirectoryAuthority) {
super(thisDirectoryAuthority);
}

Expand All @@ -31,7 +31,7 @@ public void generate() {
torrcStringBuilder
.append("AuthoritativeDirectory 1\n")
.append("V3AuthoritativeDirectory 1\n")
.append("ContactInfo auth-").append(thisDirectoryAuthority.getNickname()).append("@test.test\n")
.append("ContactInfo auth-").append(thisTorNode.getNickname()).append("@test.test\n")

.append("AssumeReachable 1\n")

Expand All @@ -43,6 +43,6 @@ public void generate() {
.append("V3AuthVoteDelay 4\n")
.append("V3AuthDistDelay 4\n")

.append(thisDirectoryAuthority.getExitPolicy()).append("\n");
.append(thisTorNode.getExitPolicy()).append("\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

package bisq.tor.local_network.torrc;

import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.TorNode;

public class RelayTorrcGenerator extends CommonTorrcGenerator {
public RelayTorrcGenerator(DirectoryAuthority thisDirectoryAuthority) {
super(thisDirectoryAuthority);
public RelayTorrcGenerator(TorNode thisTorNode) {
super(thisTorNode);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@

package bisq.tor.local_network.torrc;

import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.TorNode;

import java.io.IOException;
import java.nio.file.Files;
import java.util.Set;

public class TorrcFileGenerator {
private final CommonTorrcGenerator commonTorrcGenerator;
private final Set<DirectoryAuthority> allDirAuthorities;
private final Set<TorNode> allDirAuthorities;

public TorrcFileGenerator(CommonTorrcGenerator commonTorrcGenerator, Set<DirectoryAuthority> allDirAuthorities) {
public TorrcFileGenerator(CommonTorrcGenerator commonTorrcGenerator, Set<TorNode> allDirAuthorities) {
this.commonTorrcGenerator = commonTorrcGenerator;
this.allDirAuthorities = allDirAuthorities;
}
Expand All @@ -44,7 +44,7 @@ public void generate() throws IOException {
.append(" ").append(dirAuthority.getRelayKeyFingerprint().orElseThrow())
.append("\n"));

DirectoryAuthority thisDirectoryAuthority = commonTorrcGenerator.getThisDirectoryAuthority();
Files.writeString(thisDirectoryAuthority.getTorrcPath(), torrcStringBuilder.toString());
TorNode thisTorNode = commonTorrcGenerator.getThisTorNode();
Files.writeString(thisTorNode.getTorrcPath(), torrcStringBuilder.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package bisq.tor.local_network;

import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.torrc.DirectoryAuthorityTorrcGenerator;
import bisq.tor.local_network.torrc.TorrcFileGenerator;
import org.junit.jupiter.api.Test;
Expand All @@ -38,8 +37,8 @@ void basicTest(@TempDir Path tempDir) throws IOException {
Path daAPath = tempDir.resolve("DA_A");
assertThat(daAPath.toFile().mkdir()).isTrue();

DirectoryAuthority firstDirAuth = spy(
DirectoryAuthority.builder()
TorNode firstDirAuth = spy(
TorNode.builder()
.nickname("A")
.dataDir(daAPath)

Expand All @@ -58,8 +57,8 @@ void basicTest(@TempDir Path tempDir) throws IOException {
.when(firstDirAuth)
.getRelayKeyFingerprint();

DirectoryAuthority secondDirAuth = spy(
DirectoryAuthority.builder()
TorNode secondDirAuth = spy(
TorNode.builder()
.nickname("B")
.dataDir(tempDir.resolve("DA_B"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package bisq.tor.local_network;

import bisq.tor.local_network.da.DirectoryAuthority;
import bisq.tor.local_network.torrc.RelayTorrcGenerator;
import bisq.tor.local_network.torrc.TorrcFileGenerator;
import org.junit.jupiter.api.Test;
Expand All @@ -38,8 +37,8 @@ void basicTest(@TempDir Path tempDir) throws IOException {
Path daAPath = tempDir.resolve("DA_A");
assertThat(daAPath.toFile().mkdir()).isTrue();

DirectoryAuthority firstDirAuth = spy(
DirectoryAuthority.builder()
TorNode firstDirAuth = spy(
TorNode.builder()
.nickname("A")
.dataDir(daAPath)

Expand All @@ -58,8 +57,8 @@ void basicTest(@TempDir Path tempDir) throws IOException {
.when(firstDirAuth)
.getRelayKeyFingerprint();

DirectoryAuthority secondDirAuth = spy(
DirectoryAuthority.builder()
TorNode secondDirAuth = spy(
TorNode.builder()
.nickname("B")
.dataDir(tempDir.resolve("DA_B"))

Expand Down

0 comments on commit 72d1851

Please sign in to comment.