Skip to content

Commit

Permalink
Merge pull request #1128 from alvasw/tor_implement_TestNetworkTorrcGe…
Browse files Browse the repository at this point in the history
…neratorFactory

Implement TestNetworkTorrcGeneratorFactory
  • Loading branch information
alvasw authored Aug 14, 2023
2 parents 6d8bce3 + 7a9b3e6 commit c87287f
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.network.tor.common.torrc;

import lombok.Builder;

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

@Builder
public class BaseTorrcGenerator implements TorrcConfigGenerator {
public static final String DISABLE_NETWORK_CONFIG_KEY = "DisableNetwork";

private final Path dataDirPath;
private final int controlPort;
private final String hashedControlPassword;

public BaseTorrcGenerator(Path dataDirPath, int controlPort, String hashedControlPassword) {
this.dataDirPath = dataDirPath;
this.controlPort = controlPort;
this.hashedControlPassword = hashedControlPassword;
}

@Override
public Map<String, String> generate() {
Map<String, String> torConfigMap = new HashMap<>();
torConfigMap.put("DataDirectory", dataDirPath.toAbsolutePath().toString());

torConfigMap.put(DISABLE_NETWORK_CONFIG_KEY, "1");
torConfigMap.put("ControlPort", "127.0.0.1:" + controlPort);
torConfigMap.put("HashedControlPassword", hashedControlPassword);
torConfigMap.put("Log",
"debug file " + dataDirPath.resolve("debug.log").toAbsolutePath()
);

torConfigMap.put("SocksPort", "0");
return torConfigMap;
}
}
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.torrc;
package bisq.network.tor.common.torrc;

import bisq.network.tor.common.TorrcConfigGenerator;
import bisq.common.util.NetworkUtils;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,17 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.tor.local_network.torrc;

import bisq.network.tor.common.TorrcConfigGenerator;
import bisq.tor.local_network.TorNode;
package bisq.network.tor.common.torrc;

import java.util.Map;

public class DirectoryAuthorityTorrcGenerator implements TorrcConfigGenerator {
private final TorNode thisTorNode;
private final TorrcConfigGenerator baseTorrcConfigGenerator;
private final String nickname;

public DirectoryAuthorityTorrcGenerator(TorNode thisTorNode) {
this.thisTorNode = thisTorNode;
this.baseTorrcConfigGenerator = new TestNetworkTorrcGenerator(thisTorNode);
public DirectoryAuthorityTorrcGenerator(TorrcConfigGenerator baseTorrcConfigGenerator, String nickname) {
this.baseTorrcConfigGenerator = baseTorrcConfigGenerator;
this.nickname = nickname;
}

@Override
Expand All @@ -37,7 +34,7 @@ public Map<String, String> generate() {

torConfigMap.put("AuthoritativeDirectory", "1");
torConfigMap.put("V3AuthoritativeDirectory", "1");
torConfigMap.put("ContactInfo", "auth-" + thisTorNode.getNickname() + "@test.test\n");
torConfigMap.put("ContactInfo", "auth-" + nickname + "@test.test\n");

torConfigMap.put("AssumeReachable", "1");
torConfigMap.put("TestingV3AuthInitialVotingInterval", "20");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.tor.local_network.torrc;

import bisq.network.tor.common.TorrcConfigGenerator;
package bisq.network.tor.common.torrc;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.tor.local_network.torrc;

import bisq.network.tor.common.TorrcConfigGenerator;
package bisq.network.tor.common.torrc;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,40 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.tor.local_network.torrc;
package bisq.network.tor.common.torrc;

import bisq.network.tor.common.TorrcConfigGenerator;
import bisq.tor.local_network.TorNode;
import lombok.Builder;
import lombok.Getter;

import java.util.HashMap;
import java.util.Map;

/**
* The configuration settings are from the Chutney (<a href="https://gitweb.torproject.org/chutney.git/">project</a>).
*/
@Builder
@Getter
public class TestNetworkTorrcGenerator implements TorrcConfigGenerator {
protected final TorNode thisTorNode;
protected final Map<String, String> torConfigMap = new HashMap<>();

public TestNetworkTorrcGenerator(TorNode thisTorNode) {
this.thisTorNode = thisTorNode;
private final TorrcConfigGenerator baseTorrcConfigGenerator;

private final String nickname;
private final int orPort;
private final int dirPort;

public TestNetworkTorrcGenerator(TorrcConfigGenerator baseTorrcConfigGenerator,
String nickname,
int orPort,
int dirPort) {
this.baseTorrcConfigGenerator = baseTorrcConfigGenerator;
this.nickname = nickname;
this.orPort = orPort;
this.dirPort = dirPort;
}

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

torConfigMap.put("TestingTorNetwork", "1");
torConfigMap.put("TestingDirAuthVoteExit", "*");
torConfigMap.put("TestingDirAuthVoteHSDir", "*");
Expand All @@ -46,37 +57,22 @@ public Map<String, String> generate() {
torConfigMap.put("TestingDirAuthVoteGuard", "*");
torConfigMap.put("TestingMinExitFlagThreshold", "0");

torConfigMap.put("DataDirectory", thisTorNode.getDataDir().toAbsolutePath().toString());

torConfigMap.put("Nickname", thisTorNode.getNickname());
torConfigMap.put("Nickname", nickname);
torConfigMap.put("ShutdownWaitLength", "2");
torConfigMap.put("DisableDebuggerAttachment", "0");
torConfigMap.put("ControlPort", "127.0.0.1:" + thisTorNode.getControlPort());

torConfigMap.put("HashedControlPassword",
thisTorNode.getControlConnectionPassword()
.getHashedPassword()
);

torConfigMap.put("Log",
"debug file " + thisTorNode.getDataDir().resolve("debug.log").toAbsolutePath()
);

torConfigMap.put("ProtocolWarnings", "1");
torConfigMap.put("SafeLogging", "0");
torConfigMap.put("LogTimeGranularity", "1");

if (thisTorNode.getType() != TorNode.Type.CLIENT) {
torConfigMap.put("SocksPort", "0");
}

torConfigMap.put("OrPort", String.valueOf(thisTorNode.getOrPort()));
torConfigMap.put("OrPort", String.valueOf(orPort));
torConfigMap.put("Address", "127.0.0.1");
torConfigMap.put("ServerDNSDetectHijacking", "0");

torConfigMap.put("ServerDNSTestAddresses", "");

torConfigMap.put("DirPort", String.valueOf(thisTorNode.getDirPort()));
torConfigMap.put("DirPort", String.valueOf(dirPort));

return torConfigMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.network.tor.common;
package bisq.network.tor.common.torrc;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
package bisq.tor.local_network;

import bisq.common.util.NetworkUtils;
import bisq.network.tor.common.torrc.TorrcConfigGenerator;
import bisq.tor.local_network.da.DirectoryAuthorityFactory;
import bisq.tor.local_network.torrc.DirectoryAuthorityTorrcGenerator;
import bisq.tor.local_network.torrc.TestNetworkTorrcGeneratorFactory;
import bisq.tor.local_network.torrc.TorrcFileGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -89,10 +90,10 @@ public void createThreeDA(@TempDir Path tempDir) throws IOException, Interrupted

Set<TorNode> allDAs = dirAuthFactory.getAllDirectoryAuthorities();
for (TorNode da : allDAs) {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(da);
TorrcConfigGenerator torDaTorrcGenerator = TestNetworkTorrcGeneratorFactory.directoryTorrcGenerator(da);
Map<String, String> torrcConfigs = torDaTorrcGenerator.generate();
Path torrcPath = da.getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs , allDAs);
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs, allDAs);
torrcFileGenerator.generate();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package bisq.tor.local_network;

import bisq.common.util.NetworkUtils;
import bisq.network.tor.common.torrc.TorrcConfigGenerator;
import bisq.tor.local_network.da.DirectoryAuthorityFactory;
import bisq.tor.local_network.torrc.*;
import bisq.tor.local_network.torrc.TestNetworkTorrcGeneratorFactory;
import bisq.tor.local_network.torrc.TorrcFileGenerator;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -126,28 +128,26 @@ private void createDataDirIfNotPresent(Path nodeDataDirPath) {
private void generateTorrcFiles() throws IOException {
Set<TorNode> allDAs = dirAuthFactory.getAllDirectoryAuthorities();
for (TorNode da : allDAs) {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(da);
TorrcConfigGenerator torDaTorrcGenerator = TestNetworkTorrcGeneratorFactory.directoryTorrcGenerator(da);
Map<String, String> torrcConfigs = torDaTorrcGenerator.generate();
Path torrcPath = da.getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs , allDAs);
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs, allDAs);
generateTorrc(da, torrcFileGenerator);
}

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

for (TorNode client : clients) {
var testNetworkTorrcGenerator = new TestNetworkTorrcGenerator(client);
var clientTorrcGenerator = new ClientTorrcGenerator(testNetworkTorrcGenerator);
TorrcConfigGenerator clientTorrcGenerator = TestNetworkTorrcGeneratorFactory.clientTorrcGenerator(client);
Map<String, String> torrcConfigs = clientTorrcGenerator.generate();
Path torrcPath = client.getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs , allDAs);
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torrcConfigs, allDAs);
generateTorrc(client, torrcFileGenerator);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/

package bisq.tor.local_network.torrc;

import bisq.network.tor.common.torrc.*;
import bisq.tor.local_network.TorNode;

public class TestNetworkTorrcGeneratorFactory {
public static TorrcConfigGenerator directoryTorrcGenerator(TorNode directoryNode) {
var testNetworkTorrcGenerator = testNetworkTorrcGenerator(directoryNode);
return new DirectoryAuthorityTorrcGenerator(testNetworkTorrcGenerator, directoryNode.getNickname());
}

public static TorrcConfigGenerator relayTorrcGenerator(TorNode relayNode) {
var testNetworkTorrcGenerator = testNetworkTorrcGenerator(relayNode);
return new RelayTorrcGenerator(testNetworkTorrcGenerator);
}

public static TorrcConfigGenerator clientTorrcGenerator(TorNode clientNode) {
var testNetworkTorrcGenerator = testNetworkTorrcGenerator(clientNode);
return new ClientTorrcGenerator(testNetworkTorrcGenerator);
}

private static TorrcConfigGenerator testNetworkTorrcGenerator(TorNode torNode) {
return TestNetworkTorrcGenerator.builder()
.baseTorrcConfigGenerator(baseTorrcGenerator(torNode))
.nickname(torNode.getNickname())
.orPort(torNode.getOrPort())
.dirPort(torNode.getDirPort())
.build();
}

private static TorrcConfigGenerator baseTorrcGenerator(TorNode torNode) {
return BaseTorrcGenerator.builder()
.dataDirPath(torNode.getDataDir())
.controlPort(torNode.getControlPort())
.hashedControlPassword(
torNode.getControlConnectionPassword()
.getHashedPassword())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package bisq.tor.local_network;

import bisq.tor.local_network.torrc.DirectoryAuthorityTorrcGenerator;
import bisq.network.tor.common.torrc.TorrcConfigGenerator;
import bisq.tor.local_network.torrc.TestNetworkTorrcGeneratorFactory;
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,12 +81,12 @@ void basicTest(@TempDir Path tempDir) throws IOException {
.when(secondDirAuth)
.getRelayKeyFingerprint();

var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(firstDirAuth);
TorrcConfigGenerator torDaTorrcGenerator = TestNetworkTorrcGeneratorFactory.directoryTorrcGenerator(firstDirAuth);
var allDirAuthorities = Set.of(firstDirAuth, secondDirAuth);

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

torrcFileGenerator.generate();

Expand Down
Loading

0 comments on commit c87287f

Please sign in to comment.