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

Torrc #1893

Merged
merged 7 commits into from
Nov 8, 2018
Merged

Torrc #1893

Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions core/src/main/java/bisq/core/app/BisqEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ private static String appDataDir(String userDataDir, String appName) {
protected final String btcNodes, seedNodes, ignoreDevMsg, useDevPrivilegeKeys, useDevMode, useTorForBtc, rpcUser, rpcPassword,
rpcPort, rpcBlockNotificationPort, dumpBlockchainData, fullDaoNode,
myAddress, banList, dumpStatistics, maxMemory, socks5ProxyBtcAddress,
torRcFile, torRcOptions,
socks5ProxyHttpAddress, useAllProvidedNodes, numConnectionForBtc, genesisTxId, genesisBlockHeight, referralId, daoActivated;


Expand Down Expand Up @@ -267,6 +268,12 @@ public BisqEnvironment(PropertySource commandLineProperties) {
socks5ProxyHttpAddress = commandLineProperties.containsProperty(NetworkOptionKeys.SOCKS_5_PROXY_HTTP_ADDRESS) ?
(String) commandLineProperties.getProperty(NetworkOptionKeys.SOCKS_5_PROXY_HTTP_ADDRESS) :
"";
torRcFile = commandLineProperties.containsProperty(NetworkOptionKeys.TORRC_FILE) ?
(String) commandLineProperties.getProperty(NetworkOptionKeys.TORRC_FILE) :
"";
torRcOptions = commandLineProperties.containsProperty(NetworkOptionKeys.TORRC_OPTIONS) ?
(String) commandLineProperties.getProperty(NetworkOptionKeys.TORRC_OPTIONS) :
"";

//RpcOptionKeys
rpcUser = commandLineProperties.containsProperty(DaoOptionKeys.RPC_USER) ?
Expand Down Expand Up @@ -435,6 +442,8 @@ private PropertySource<?> defaultProperties() {
setProperty(NetworkOptionKeys.NETWORK_ID, String.valueOf(baseCurrencyNetwork.ordinal()));
setProperty(NetworkOptionKeys.SOCKS_5_PROXY_BTC_ADDRESS, socks5ProxyBtcAddress);
setProperty(NetworkOptionKeys.SOCKS_5_PROXY_HTTP_ADDRESS, socks5ProxyHttpAddress);
setProperty(NetworkOptionKeys.TORRC_FILE, torRcFile);
setProperty(NetworkOptionKeys.TORRC_OPTIONS, torRcOptions);

setProperty(AppOptionKeys.APP_DATA_DIR_KEY, appDataDir);
setProperty(AppOptionKeys.DESKTOP_WITH_HTTP_API, desktopWithHttpApi);
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/bisq/core/app/BisqExecutable.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
import joptsimple.OptionException;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.util.PathConverter;
import joptsimple.util.PathProperties;
import joptsimple.util.RegexMatcher;

import com.google.inject.Guice;
import com.google.inject.Injector;
Expand Down Expand Up @@ -330,6 +333,14 @@ protected void customizeOptionParsing(OptionParser parser) {
parser.accepts(NetworkOptionKeys.SOCKS_5_PROXY_HTTP_ADDRESS,
description("A proxy address to be used for Http requests (should be non-Tor). [host:port]", ""))
.withRequiredArg();
parser.accepts(NetworkOptionKeys.TORRC_FILE,
description("An existing torrc-file to be sourced for Tor. Note that torrc-entries, which are critical to Bisqs flawless operation, cannot be overwritten.", ""))
.withRequiredArg();
// .withValuesConvertedBy(new PathConverter(PathProperties.FILE_EXISTING, PathProperties.READABLE));
parser.accepts(NetworkOptionKeys.TORRC_OPTIONS,
description("A list of torrc-entries to amend to Bisqs torrc. Note that torrc-entries, which are critical to Bisqs flawless operation, cannot be overwritten. [torrc options line, torrc option, ...]", ""))
.withRequiredArg();
// .withValuesConvertedBy(RegexMatcher.regex("^([^\\s,]+\\s[^,]+,?\\s*)+$"));

//AppOptionKeys
parser.accepts(AppOptionKeys.USER_DATA_DIR_KEY,
Expand Down
2 changes: 2 additions & 0 deletions p2p/src/main/java/bisq/network/NetworkOptionKeys.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public class NetworkOptionKeys {
//SOCKS_5_PROXY_BTC_ADDRESS used in network module so dont move it to BtcOptionKeys
public static final String SOCKS_5_PROXY_BTC_ADDRESS = "socks5ProxyBtcAddress";
public static final String SOCKS_5_PROXY_HTTP_ADDRESS = "socks5ProxyHttpAddress";
public static final String TORRC_OPTIONS = "torrcOptions";
public static final String TORRC_FILE = "torrcFile";
}
6 changes: 4 additions & 2 deletions p2p/src/main/java/bisq/network/p2p/NetworkNodeProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ public NetworkNodeProvider(NetworkProtoResolver networkProtoResolver,
@Named(NetworkOptionKeys.USE_LOCALHOST_FOR_P2P) boolean useLocalhostForP2P,
@Named(NetworkOptionKeys.MY_ADDRESS) String address,
@Named(NetworkOptionKeys.PORT_KEY) int port,
@Named(NetworkOptionKeys.TOR_DIR) File torDir) {
@Named(NetworkOptionKeys.TOR_DIR) File torDir,
@Named(NetworkOptionKeys.TORRC_FILE) String torrcFile,
@Named(NetworkOptionKeys.TORRC_OPTIONS) String torrcOptions) {
networkNode = useLocalhostForP2P ?
new LocalhostNetworkNode(address, port, networkProtoResolver) :
new TorNetworkNode(port, torDir, networkProtoResolver, bridgeAddressProvider);
new TorNetworkNode(port, torDir, networkProtoResolver, bridgeAddressProvider, torrcFile, torrcOptions);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions p2p/src/main/java/bisq/network/p2p/P2PModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,7 @@ protected void configure() {
bindConstant().annotatedWith(named(NetworkOptionKeys.BAN_LIST)).to(environment.getRequiredProperty(NetworkOptionKeys.BAN_LIST));
bindConstant().annotatedWith(named(NetworkOptionKeys.SOCKS_5_PROXY_BTC_ADDRESS)).to(environment.getRequiredProperty(NetworkOptionKeys.SOCKS_5_PROXY_BTC_ADDRESS));
bindConstant().annotatedWith(named(NetworkOptionKeys.SOCKS_5_PROXY_HTTP_ADDRESS)).to(environment.getRequiredProperty(NetworkOptionKeys.SOCKS_5_PROXY_HTTP_ADDRESS));
bindConstant().annotatedWith(named(NetworkOptionKeys.TORRC_FILE)).to(environment.getRequiredProperty(NetworkOptionKeys.TORRC_FILE));
bindConstant().annotatedWith(named(NetworkOptionKeys.TORRC_OPTIONS)).to(environment.getRequiredProperty(NetworkOptionKeys.TORRC_OPTIONS));
}
}
45 changes: 29 additions & 16 deletions p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
Expand All @@ -77,8 +77,6 @@ public class TorNetworkNode extends NetworkNode {
private static final int MAX_RESTART_ATTEMPTS = 5;
private static final long SHUT_DOWN_TIMEOUT = 5;

private static final String CONFIG_TORRCPREFIX = "torrc:";
private static final String CONFIG_TORRCFILE = "torrcfile";

private HiddenServiceSocket hiddenServiceSocket;
private final File torDir;
Expand All @@ -89,15 +87,20 @@ public class TorNetworkNode extends NetworkNode {
private MonadicBinding<Boolean> allShutDown;
private Tor tor;

private String torrcFile = "";
private String torrcOptions = "";


///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////

public TorNetworkNode(int servicePort, File torDir, NetworkProtoResolver networkProtoResolver, BridgeAddressProvider bridgeAddressProvider) {
public TorNetworkNode(int servicePort, File torDir, NetworkProtoResolver networkProtoResolver, BridgeAddressProvider bridgeAddressProvider, String torrcFile, String torrcOptions) {
super(servicePort, networkProtoResolver);
this.torDir = torDir;
this.bridgeAddressProvider = bridgeAddressProvider;
this.torrcFile = torrcFile;
this.torrcOptions = torrcOptions;
}


Expand Down Expand Up @@ -253,28 +256,38 @@ private void createTorAndHiddenService(File torDir, int localPort, int servicePo
Torrc override = null;

// check if the user wants to provide his own torrc file
String torrcFile = System.getProperty(CONFIG_TORRCFILE);
if(torrcFile != null) {
if(!"".equals(torrcFile)) {
try {
override = new Torrc(new FileInputStream(new File(torrcFile)));
} catch(IOException e) {
log.error("custom torrc file not found (" + torrcFile + "). Proceeding with defaults.");
log.error("custom torrc file not found ('{}'). Proceeding with defaults.", torrcFile);
}
}

// check if the user wants to temporarily add to the default torrc file
LinkedHashMap<String, String> tmp = new LinkedHashMap<>();
System.getProperties().forEach((k, v) -> {
if(((String) k).startsWith(CONFIG_TORRCPREFIX))
tmp.put(((String) k).substring(CONFIG_TORRCPREFIX.length()), (String) v);
});
if(!tmp.isEmpty())
LinkedHashMap<String, String> torrcOptionsMap = new LinkedHashMap<>();
if(!"".equals(torrcOptions)) {
Arrays.asList(torrcOptions.split(",")).forEach(line -> {
line = line.trim();
if(line.matches("^[^\\s]+\\s.+")) {
String[] tmp = line.split("\\s", 2);
torrcOptionsMap.put(tmp[0].trim(), tmp[1].trim());
}
else {
log.error("custom torrc override parse error ('{}'). Proceeding without custom overrides.", line);
torrcOptionsMap.clear();
}
});
}

// assemble final override options
if(!torrcOptionsMap.isEmpty())
// check for custom torrcFile
if(null != override)
if(override != null)
// and merge the contents
override = new Torrc(override.getInputStream$tor(), tmp);
override = new Torrc(override.getInputStream$tor(), torrcOptionsMap);
else
override = new Torrc(tmp);
override = new Torrc(torrcOptionsMap);

log.info("Starting tor");
Tor.setDefault(new NativeTor(torDir, bridgeEntries, override));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class TorNetworkNodeTest {
public void testTorNodeBeforeSecondReady() throws InterruptedException, IOException {
latch = new CountDownLatch(1);
int port = 9001;
TorNetworkNode node1 = new TorNetworkNode(port, new File("torNode_" + port), TestUtils.getNetworkProtoResolver(), null);
TorNetworkNode node1 = new TorNetworkNode(port, new File("torNode_" + port), TestUtils.getNetworkProtoResolver(), null, "", "");
node1.start(new SetupListener() {
@Override
public void onTorNodeReady() {
Expand All @@ -79,7 +79,7 @@ public void onRequestCustomBridges() {

latch = new CountDownLatch(1);
int port2 = 9002;
TorNetworkNode node2 = new TorNetworkNode(port2, new File("torNode_" + port2), TestUtils.getNetworkProtoResolver(), null);
TorNetworkNode node2 = new TorNetworkNode(port2, new File("torNode_" + port2), TestUtils.getNetworkProtoResolver(), null, "", "");
node2.start(new SetupListener() {
@Override
public void onTorNodeReady() {
Expand Down Expand Up @@ -136,7 +136,7 @@ public void onFailure(@NotNull Throwable throwable) {
public void testTorNodeAfterBothReady() throws InterruptedException, IOException {
latch = new CountDownLatch(2);
int port = 9001;
TorNetworkNode node1 = new TorNetworkNode(port, new File("torNode_" + port), TestUtils.getNetworkProtoResolver(), null);
TorNetworkNode node1 = new TorNetworkNode(port, new File("torNode_" + port), TestUtils.getNetworkProtoResolver(), null, "", "");
node1.start(new SetupListener() {
@Override
public void onTorNodeReady() {
Expand All @@ -161,7 +161,7 @@ public void onRequestCustomBridges() {
});

int port2 = 9002;
TorNetworkNode node2 = new TorNetworkNode(port2, new File("torNode_" + port), TestUtils.getNetworkProtoResolver(), null);
TorNetworkNode node2 = new TorNetworkNode(port2, new File("torNode_" + port), TestUtils.getNetworkProtoResolver(), null, "", "");
node2.start(new SetupListener() {
@Override
public void onTorNodeReady() {
Expand Down