Skip to content

Commit

Permalink
Merge pull request #1047 from alvasw/tor_store_configs_in_map
Browse files Browse the repository at this point in the history
Tor: Store torrc configs in map
  • Loading branch information
alvasw committed Jul 30, 2023
2 parents e802b9d + b25093b commit 3576994
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public void createThreeDA(@TempDir Path tempDir) throws IOException, Interrupted
Set<TorNode> allDAs = dirAuthFactory.getAllDirectoryAuthorities();
for (TorNode da : allDAs) {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(da);
var torrcFileGenerator = new TorrcFileGenerator(torDaTorrcGenerator, allDAs);
Path torrcPath = torDaTorrcGenerator.getThisTorNode().getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torDaTorrcGenerator, allDAs);
torrcFileGenerator.generate();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,22 @@ private void generateTorrcFiles() throws IOException {
Set<TorNode> allDAs = dirAuthFactory.getAllDirectoryAuthorities();
for (TorNode da : allDAs) {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(da);
var torrcFileGenerator = new TorrcFileGenerator(torDaTorrcGenerator, allDAs);
Path torrcPath = torDaTorrcGenerator.getThisTorNode().getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torDaTorrcGenerator, allDAs);
generateTorrc(da, torrcFileGenerator);
}

for (TorNode relay : relays) {
var relayTorrcGenerator = new RelayTorrcGenerator(relay);
var torrcFileGenerator = new TorrcFileGenerator(relayTorrcGenerator, allDAs);
Path torrcPath = relayTorrcGenerator.getThisTorNode().getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, relayTorrcGenerator, allDAs);
generateTorrc(relay, torrcFileGenerator);
}

for (TorNode client : clients) {
var clientTorrcGenerator = new ClientTorrcGenerator(client);
var torrcFileGenerator = new TorrcFileGenerator(clientTorrcGenerator, allDAs);
Path torrcPath = clientTorrcGenerator.getThisTorNode().getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, clientTorrcGenerator, allDAs);
generateTorrc(client, torrcFileGenerator);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public enum Type {
private final int orPort;
private final int dirPort;

private final String exitPolicy = "ExitPolicy accept *:*";

private final Path keysPath;

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
import bisq.common.util.NetworkUtils;
import bisq.tor.local_network.TorNode;

public class ClientTorrcGenerator extends CommonTorrcGenerator{
import java.util.Map;

public class ClientTorrcGenerator extends CommonTorrcGenerator {
public ClientTorrcGenerator(TorNode thisTorNode) {
super(thisTorNode);
}

@Override
public void generate() {
public Map<String, String> generate() {
super.generate();
torrcStringBuilder.append("SocksPort ").append(NetworkUtils.findFreeSystemPort()).append("\n");
torConfigMap.put("SocksPort", String.valueOf(NetworkUtils.findFreeSystemPort()));
return torConfigMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,58 +20,64 @@
import bisq.tor.local_network.TorNode;
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>).
*/
@Getter
public abstract class CommonTorrcGenerator {
public abstract class CommonTorrcGenerator implements TorrcConfigGenerator {
protected final TorNode thisTorNode;
protected final StringBuilder torrcStringBuilder = new StringBuilder();
protected final Map<String, String> torConfigMap = new HashMap<>();

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

public void generate() {
torrcStringBuilder.append("TestingTorNetwork 1\n")

.append("PathsNeededToBuildCircuits 0.67\n")
.append("TestingDirAuthVoteExit *\n")
.append("TestingDirAuthVoteHSDir *\n")
.append("V3AuthNIntervalsValid 2\n")
@Override
public Map<String, String> generate() {
torConfigMap.put("TestingTorNetwork", "1");
torConfigMap.put("TestingDirAuthVoteExit", "*");
torConfigMap.put("TestingDirAuthVoteHSDir", "*");

.append("TestingDirAuthVoteGuard *\n")
.append("TestingMinExitFlagThreshold 0\n")
torConfigMap.put("V3AuthNIntervalsValid", "2");
torConfigMap.put("TestingDirAuthVoteGuard", "*");
torConfigMap.put("TestingMinExitFlagThreshold", "0");

.append("DataDirectory ").append(thisTorNode.getDataDir()).append("\n")
.append("RunAsDaemon 1\n")
.append("Nickname ").append(thisTorNode.getNickname()).append("\n")
torConfigMap.put("DataDirectory", thisTorNode.getDataDir().toAbsolutePath().toString());
torConfigMap.put("RunAsDaemon", "1");

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

.append("ControlPort 127.0.0.1:").append(thisTorNode.getControlPort()).append("\n")
torConfigMap.put("HashedControlPassword",
thisTorNode.getControlConnectionPassword()
.getHashedPassword()
);

.append("HashedControlPassword ")
.append(thisTorNode.getControlConnectionPassword().getHashedPassword())
.append("\n")
torConfigMap.put("Log",
"debug file " + thisTorNode.getDataDir().resolve("debug.log").toAbsolutePath()
);

.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");
torConfigMap.put("ProtocolWarnings", "1");
torConfigMap.put("SafeLogging", "0");
torConfigMap.put("LogTimeGranularity", "1");

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

torrcStringBuilder
.append("OrPort ").append(thisTorNode.getOrPort()).append("\n")
.append("Address 127.0.0.1\n")
torConfigMap.put("OrPort", String.valueOf(thisTorNode.getOrPort()));
torConfigMap.put("Address", "127.0.0.1");
torConfigMap.put("ServerDNSDetectHijacking", "0");

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

.append("ServerDNSDetectHijacking 0\n")
.append("ServerDNSTestAddresses\n")
torConfigMap.put("DirPort", String.valueOf(thisTorNode.getDirPort()));

.append("DirPort ").append(thisTorNode.getDirPort()).append("\n");
return torConfigMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,33 @@

import bisq.tor.local_network.TorNode;

import java.util.Map;

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

@Override
public void generate() {
public Map<String, String> generate() {
super.generate();

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

torConfigMap.put("AssumeReachable", "1");
torConfigMap.put("TestingV3AuthInitialVotingInterval", "20");

.append("AssumeReachable 1\n")
torConfigMap.put("TestingV3AuthInitialVoteDelay", "4");
torConfigMap.put("TestingV3AuthInitialDistDelay", "4");

.append("TestingV3AuthInitialVotingInterval 20\n")
.append("TestingV3AuthInitialVoteDelay 4\n")
.append("TestingV3AuthInitialDistDelay 4\n")
torConfigMap.put("V3AuthVotingInterval", "20");
torConfigMap.put("V3AuthVoteDelay", "4");
torConfigMap.put("V3AuthDistDelay", "4");

.append("V3AuthVotingInterval 20\n")
.append("V3AuthVoteDelay 4\n")
.append("V3AuthDistDelay 4\n")
torConfigMap.put("ExitPolicy", "accept *:*");

.append(thisTorNode.getExitPolicy()).append("\n");
return torConfigMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,21 @@

import bisq.tor.local_network.TorNode;

import java.util.Map;

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

@Override
public void generate() {
public Map<String, String> generate() {
super.generate();
torrcStringBuilder
.append("ExitRelay 1\n")
.append("ExitPolicy accept 127.0.0.0/8:*\n")
.append("ExitPolicyRejectPrivate 0\n")
.append("ExitPolicy accept private:*\n")
.append("ExitPolicy accept *:*\n")
.append("ExitPolicy reject *:*\n");

torConfigMap.put("ExitRelay", "1");
torConfigMap.put("ExitPolicy", "accept 127.0.0.0/8:*,accept private:*,accept *:*,reject *:*");
torConfigMap.put("ExitPolicyRejectPrivate", "0");

return torConfigMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 java.util.Map;

public interface TorrcConfigGenerator {
Map<String, String> generate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,31 @@

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

public class TorrcFileGenerator {
private final CommonTorrcGenerator commonTorrcGenerator;
private final Path torrcPath;
private final TorrcConfigGenerator torrcConfigGenerator;
private final Set<TorNode> allDirAuthorities;

public TorrcFileGenerator(CommonTorrcGenerator commonTorrcGenerator, Set<TorNode> allDirAuthorities) {
this.commonTorrcGenerator = commonTorrcGenerator;
public TorrcFileGenerator(Path torrcPath, TorrcConfigGenerator torrcConfigGenerator, Set<TorNode> allDirAuthorities) {
this.torrcPath = torrcPath;
this.torrcConfigGenerator = torrcConfigGenerator;
this.allDirAuthorities = allDirAuthorities;
}

public void generate() throws IOException {
commonTorrcGenerator.generate();
StringBuilder torrcStringBuilder = commonTorrcGenerator.getTorrcStringBuilder();
Map<String, String> torrcConfigs = torrcConfigGenerator.generate();

StringBuilder torrcStringBuilder = new StringBuilder();
torrcConfigs.forEach((key, value) ->
torrcStringBuilder.append(key)
.append(" ")
.append(value)
.append("\n")
);

allDirAuthorities.forEach(dirAuthority ->
torrcStringBuilder.append("DirAuthority ").append(dirAuthority.getNickname())
Expand All @@ -44,7 +55,6 @@ public void generate() throws IOException {
.append(" ").append(dirAuthority.getRelayKeyFingerprint().orElseThrow())
.append("\n"));

TorNode thisTorNode = commonTorrcGenerator.getThisTorNode();
Files.writeString(thisTorNode.getTorrcPath(), torrcStringBuilder.toString());
Files.writeString(torrcPath, torrcStringBuilder.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ void basicTest(@TempDir Path tempDir) throws IOException {
var torDaTorrcGenerator = new DirectoryAuthorityTorrcGenerator(firstDirAuth);
var allDirAuthorities = Set.of(firstDirAuth, secondDirAuth);

var torrcFileGenerator = new TorrcFileGenerator(torDaTorrcGenerator, allDirAuthorities);
Path torrcPath = torDaTorrcGenerator.getThisTorNode().getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, torDaTorrcGenerator, allDirAuthorities);
torrcFileGenerator.generate();

assertThat(firstDirAuth.getTorrcPath())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ void basicTest(@TempDir Path tempDir) throws IOException {
var relayTorrcGenerator = new RelayTorrcGenerator(firstRelay);
var allDirAuthorities = Set.of(firstRelay, secondRelay);

var torrcFileGenerator = new TorrcFileGenerator(relayTorrcGenerator, allDirAuthorities);
Path torrcPath = relayTorrcGenerator.getThisTorNode().getTorrcPath();
var torrcFileGenerator = new TorrcFileGenerator(torrcPath, relayTorrcGenerator, allDirAuthorities);
torrcFileGenerator.generate();

assertThat(firstRelay.getTorrcPath())
Expand Down

0 comments on commit 3576994

Please sign in to comment.