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

TorrcConfigGeneration: Move from inheritance to composition #1122

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 @@ -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 CommonTorrcGenerator {
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 CommonTorrcGenerator {
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 CommonTorrcGenerator template;
private final TorrcConfigGenerator template;
private final Map<String, String> clientTorrcConfig;

public OverridingTorrcGenerator(CommonTorrcGenerator 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 CommonTorrcGenerator {
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,11 +27,11 @@
* The configuration settings are from the Chutney (<a href="https://gitweb.torproject.org/chutney.git/">project</a>).
*/
@Getter
public abstract class CommonTorrcGenerator implements TorrcConfigGenerator {
public class TestNetworkTorrcGenerator implements TorrcConfigGenerator {
protected final TorNode thisTorNode;
protected final Map<String, String> torConfigMap = new HashMap<>();

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

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