Skip to content

Commit

Permalink
TorrcConfigGeneration: Move from inheritance to composition
Browse files Browse the repository at this point in the history
  • Loading branch information
alvasw committed Aug 11, 2023
1 parent c2c6f12 commit 6412f7b
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void createThreeDA(@TempDir Path tempDir) throws IOException, Interrupted
for (TorNode da : allDAs) {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(da);
Map<String, String> torrcConfigs = torDaTorrcGenerator.generate();
Path torrcPath = torDaTorrcGenerator.getThisTorNode().getTorrcPath();
Path torrcPath = da.getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs , allDAs);
torrcFileGenerator.generate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@

import bisq.common.util.NetworkUtils;
import bisq.tor.local_network.da.DirectoryAuthorityFactory;
import bisq.tor.local_network.torrc.ClientTorrcGenerator;
import bisq.tor.local_network.torrc.DirectoryAuthorityTorrcGenerator;
import bisq.tor.local_network.torrc.RelayTorrcGenerator;
import bisq.tor.local_network.torrc.TorrcFileGenerator;
import bisq.tor.local_network.torrc.*;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -131,23 +128,25 @@ private void generateTorrcFiles() throws IOException {
for (TorNode da : allDAs) {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(da);
Map<String, String> torrcConfigs = torDaTorrcGenerator.generate();
Path torrcPath = torDaTorrcGenerator.getThisTorNode().getTorrcPath();
Path torrcPath = da.getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs , allDAs);
generateTorrc(da, torrcFileGenerator);
}

for (TorNode relay : relays) {
var relayTorrcGenerator = new RelayTorrcGenerator(relay);
var testNetworkTorrcGenerator = new TestNetworkTorrcGenerator(relay);
var relayTorrcGenerator = new RelayTorrcGenerator(testNetworkTorrcGenerator);
Map<String, String> torrcConfigs = relayTorrcGenerator.generate();
Path torrcPath = relayTorrcGenerator.getThisTorNode().getTorrcPath();
Path torrcPath = relay.getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs , allDAs);
generateTorrc(relay, torrcFileGenerator);
}

for (TorNode client : clients) {
var clientTorrcGenerator = new ClientTorrcGenerator(client);
var testNetworkTorrcGenerator = new TestNetworkTorrcGenerator(client);
var clientTorrcGenerator = new ClientTorrcGenerator(testNetworkTorrcGenerator);
Map<String, String> torrcConfigs = clientTorrcGenerator.generate();
Path torrcPath = clientTorrcGenerator.getThisTorNode().getTorrcPath();
Path torrcPath = client.getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs , allDAs);
generateTorrc(client, torrcFileGenerator);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
package bisq.tor.local_network.torrc;

import bisq.common.util.NetworkUtils;
import bisq.tor.local_network.TorNode;

import java.util.Map;

public class ClientTorrcGenerator extends TestNetworkTorrcGenerator {
public ClientTorrcGenerator(TorNode thisTorNode) {
super(thisTorNode);
public class ClientTorrcGenerator implements TorrcConfigGenerator {
private final TorrcConfigGenerator baseTorrcConfigGenerator;

public ClientTorrcGenerator(TorrcConfigGenerator baseTorrcConfigGenerator) {
this.baseTorrcConfigGenerator = baseTorrcConfigGenerator;
}

@Override
public Map<String, String> generate() {
super.generate();
Map<String, String> torConfigMap = baseTorrcConfigGenerator.generate();
torConfigMap.put("SocksPort", String.valueOf(NetworkUtils.findFreeSystemPort()));
return torConfigMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@

import java.util.Map;

public class DirectoryAuthorityTorrcGenerator extends TestNetworkTorrcGenerator {
public DirectoryAuthorityTorrcGenerator(TorNode thisDirectoryAuthority) {
super(thisDirectoryAuthority);
public class DirectoryAuthorityTorrcGenerator implements TorrcConfigGenerator {
private final TorNode thisTorNode;
private final TorrcConfigGenerator baseTorrcConfigGenerator;

public DirectoryAuthorityTorrcGenerator(TorNode thisTorNode) {
this.thisTorNode = thisTorNode;
this.baseTorrcConfigGenerator = new TestNetworkTorrcGenerator(thisTorNode);
}

@Override
public Map<String, String> generate() {
super.generate();
Map<String, String> torConfigMap = baseTorrcConfigGenerator.generate();

torConfigMap.put("AuthoritativeDirectory", "1");
torConfigMap.put("V3AuthoritativeDirectory", "1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@

package bisq.tor.local_network.torrc;

import java.nio.file.Path;
import java.util.Map;

public class OverridingTorrcGenerator implements TorrcConfigGenerator {
private final TestNetworkTorrcGenerator template;
private final TorrcConfigGenerator template;
private final Map<String, String> clientTorrcConfig;

public OverridingTorrcGenerator(TestNetworkTorrcGenerator template, Map<String, String> clientTorrcConfig) {
public OverridingTorrcGenerator(TorrcConfigGenerator template, Map<String, String> clientTorrcConfig) {
this.template = template;
this.clientTorrcConfig = clientTorrcConfig;
}
Expand All @@ -35,8 +34,4 @@ public Map<String, String> generate() {
torrcConfigs.putAll(clientTorrcConfig);
return torrcConfigs;
}

public Path getTorrcPath() {
return template.getThisTorNode().getTorrcPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@

package bisq.tor.local_network.torrc;

import bisq.tor.local_network.TorNode;

import java.util.Map;

public class RelayTorrcGenerator extends TestNetworkTorrcGenerator {
public RelayTorrcGenerator(TorNode thisTorNode) {
super(thisTorNode);
public class RelayTorrcGenerator implements TorrcConfigGenerator {
private final TorrcConfigGenerator baseTorrcConfigGenerator;

public RelayTorrcGenerator(TorrcConfigGenerator baseTorrcConfigGenerator) {
this.baseTorrcConfigGenerator = baseTorrcConfigGenerator;
}

@Override
public Map<String, String> generate() {
super.generate();
Map<String, String> torConfigMap = baseTorrcConfigGenerator.generate();

torConfigMap.put("ExitRelay", "1");
torConfigMap.put("ExitPolicy", "accept 127.0.0.0/8:*,accept private:*,accept *:*,reject *:*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* The configuration settings are from the Chutney (<a href="https://gitweb.torproject.org/chutney.git/">project</a>).
*/
@Getter
public abstract class TestNetworkTorrcGenerator implements TorrcConfigGenerator {
public class TestNetworkTorrcGenerator implements TorrcConfigGenerator {
protected final TorNode thisTorNode;
protected final Map<String, String> torConfigMap = new HashMap<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void basicTest(@TempDir Path tempDir) throws IOException {
var allDirAuthorities = Set.of(firstDirAuth, secondDirAuth);

Map<String, String> torrcConfigs = torDaTorrcGenerator.generate();
Path torrcPath = torDaTorrcGenerator.getThisTorNode().getTorrcPath();
Path torrcPath = firstDirAuth.getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs , allDirAuthorities);

torrcFileGenerator.generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package bisq.tor.local_network;

import bisq.tor.local_network.torrc.RelayTorrcGenerator;
import bisq.tor.local_network.torrc.TestNetworkTorrcGenerator;
import bisq.tor.local_network.torrc.TorrcFileGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -80,11 +81,12 @@ void basicTest(@TempDir Path tempDir) throws IOException {
.when(secondRelay)
.getRelayKeyFingerprint();

var relayTorrcGenerator = new RelayTorrcGenerator(firstRelay);
var testNetworkTorrcGenerator = new TestNetworkTorrcGenerator(firstRelay);
var relayTorrcGenerator = new RelayTorrcGenerator(testNetworkTorrcGenerator);
var allDirAuthorities = Set.of(firstRelay, secondRelay);

Map<String, String> torrcConfigs = relayTorrcGenerator.generate();
Path torrcPath = relayTorrcGenerator.getThisTorNode().getTorrcPath();
Path torrcPath = firstRelay.getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs , allDirAuthorities);
torrcFileGenerator.generate();

Expand Down

0 comments on commit 6412f7b

Please sign in to comment.