From 73fc35413b7ae5291b08fe8751b3f31a4da70b22 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Tue, 6 Nov 2018 14:15:00 +0100 Subject: [PATCH 1/7] bumped netlayer to 0.4.7 --- build.gradle | 2 +- gradle/witness/gradle-witness.gradle | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 1f75f98c454..0e310a4e30d 100644 --- a/build.gradle +++ b/build.gradle @@ -125,7 +125,7 @@ configure(project(':common')) { configure(project(':p2p')) { dependencies { compile project(':common') - compile('com.github.JesusMcCloud.netlayer:tor.native:0.4.2') { + compile('com.github.JesusMcCloud.netlayer:tor.native:0.4.7') { exclude(module: 'slf4j-api') } compile('org.apache.httpcomponents:httpclient:4.5.3') { diff --git a/gradle/witness/gradle-witness.gradle b/gradle/witness/gradle-witness.gradle index 35725cd5582..376b0a8cef5 100644 --- a/gradle/witness/gradle-witness.gradle +++ b/gradle/witness/gradle-witness.gradle @@ -20,7 +20,7 @@ dependencyVerification { 'de.jensd:fontawesomefx-commons:5539bb3335ecb822dbf928546f57766eeb9f1516cc1417a064b5709629612149', 'com.googlecode.jcsv:jcsv:73ca7d715e90c8d2c2635cc284543b038245a34f70790660ed590e157b8714a2', 'com.github.sarxos:webcam-capture:d960b7ea8ec3ddf2df0725ef214c3fccc9699ea7772df37f544e1f8e4fd665f6', - 'com.github.JesusMcCloud.netlayer:tor.native:de44e782b21838d3426dbff99abbfd1cbb8e5d3f6d5e997441ff4fd8354934fa', + 'com.github.JesusMcCloud.netlayer:tor.native:c4c1ca96e6fbef3325db5d1cf9d6593ede4e6799b9242959fcce6f322cf5713c', 'org.apache.httpcomponents:httpclient:db3d1b6c2d6a5e5ad47577ad61854e2f0e0936199b8e05eb541ed52349263135', 'net.sf.jopt-simple:jopt-simple:6f45c00908265947c39221035250024f2caec9a15c1c8cf553ebeecee289f342', 'org.fxmisc.easybind:easybind:666af296dda6de68751668a62661571b5238ac6f1c07c8a204fc6f902b222aaf', @@ -37,11 +37,11 @@ dependencyVerification { 'com.google.code.findbugs:jsr305:c885ce34249682bc0236b4a7d56efcc12048e6135a5baf7a9cde8ad8cda13fcd', 'com.google.guava:guava:36a666e3b71ae7f0f0dca23654b67e086e6c93d192f60ba5dfd5519db6c288c8', 'com.google.inject:guice:9b9df27a5b8c7864112b4137fd92b36c3f1395bfe57be42fedf2f520ead1a93e', - 'com.github.JesusMcCloud.netlayer:tor:3896950c56a41985f901ff9475524ac162cba18b2d5a0ed39810b20ddaf5128a', - 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:841b021d62fc007ce2883963ff9440d5393fb1f6a0604ed68cd016afcaf02967', - 'com.github.MicroUtils:kotlin-logging:7dbd501cc210d721f730d480c53ee2a6e3c154ae89b07dc7dee224b9c5aca9eb', - 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:bd8cd4e3ef391cd468434747b7939c49e80f8bf4ae10355a65382bfb54fd633c', - 'org.jetbrains.kotlin:kotlin-stdlib:e1c39d27f23a7fe2d3e4ac65e80a53e98fdcf60e07de9d53b8b841c5944fc810', + 'com.github.JesusMcCloud.netlayer:tor:6341e4097534d500218320ba5e9dc1e926df0d913b7c949adc842db815cc8e1f', + 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:b306e0e6735841e31e320bf3260c71d60fc35057cfa87895f23251ee260a64a8', + 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:169ee5879cba8444499243ceea5e6a2cb6ecea5424211cc819f0704501154b35', + 'io.github.microutils:kotlin-logging:4992504fd3c6ecdf9ed10874b9508e758bb908af9e9d7af19a61e9afb6b7e27a', + 'org.jetbrains.kotlin:kotlin-stdlib:f0595b9ed88ddc6fd66bddf68c56c6f2f6c4b17faa51e43e478acad32b05303e', 'org.jetbrains:annotations:ace2a10dc8e2d5fd34925ecac03e4988b2c0f851650c94b8cef49ba1bd111478', 'org.bouncycastle:bcpg-jdk15on:de3355b821fc81dd32e1f3f560d5b3eca1c678fd2400011d0bfc69fb91bcde85', 'commons-io:commons-io:cc6a41dc3eaacc9e440a6bd0d2890b20d36b4ee408fe2d67122f328bb6e01581', From a46328fa4151c091b84ca53ea19174671765072c Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Wed, 7 Nov 2018 10:07:28 +0100 Subject: [PATCH 2/7] custom torrc file available --- .../network/p2p/network/TorNetworkNode.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java index 16adbecdb4d..fcd8a08e184 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java @@ -32,6 +32,7 @@ import org.berndpruenster.netlayer.tor.Tor; import org.berndpruenster.netlayer.tor.TorCtlException; import org.berndpruenster.netlayer.tor.TorSocket; +import org.berndpruenster.netlayer.tor.Torrc; import com.runjva.sourceforge.jsocks.protocol.Socks5Proxy; @@ -51,6 +52,7 @@ import java.nio.file.Paths; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.util.Date; @@ -242,8 +244,21 @@ private void createTorAndHiddenService(File torDir, int localPort, int servicePo ListenableFuture future = executorService.submit(() -> { try { long ts1 = new Date().getTime(); + + Torrc override = null; + + // check if the user wants to provide his own torrc file + String torrcfile = System.getProperty("torrcfile"); + if(null != 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.info("Starting tor"); - Tor.setDefault(new NativeTor(torDir, bridgeEntries)); + Tor.setDefault(new NativeTor(torDir, bridgeEntries, override)); log.info("\n################################################################\n" + "Tor started after {} ms. Start publishing hidden service.\n" + "################################################################", From 7b07a1585b266a55bfd9796429c8c5d734bbf70e Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Wed, 7 Nov 2018 10:51:14 +0100 Subject: [PATCH 3/7] custom override of torrc --- .../bisq/network/p2p/network/TorNetworkNode.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java index fcd8a08e184..e9040cd6347 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java @@ -56,6 +56,7 @@ import java.io.IOException; import java.util.Date; +import java.util.LinkedHashMap; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -257,6 +258,20 @@ private void createTorAndHiddenService(File torDir, int localPort, int servicePo } } + // check if the user wants to temporarily add to the default torrc file + LinkedHashMap tmp = new LinkedHashMap<>(); + System.getProperties().forEach((k, v) -> { + if(((String) k).startsWith("torrc:")) + tmp.put(((String) k).substring(6), (String) v); + }); + if(!tmp.isEmpty()) + // check for custom torrcfile + if(null != override) + // and merge the contents + override = new Torrc(override.getInputStream$tor(), tmp); + else + override = new Torrc(tmp); + log.info("Starting tor"); Tor.setDefault(new NativeTor(torDir, bridgeEntries, override)); log.info("\n################################################################\n" + From 03683a7ecd678baa27c2b8438936cd67076fe70d Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Wed, 7 Nov 2018 10:53:18 +0100 Subject: [PATCH 4/7] got rid of magic strings --- .../java/bisq/network/p2p/network/TorNetworkNode.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java index e9040cd6347..a6cd8757fa5 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java @@ -71,11 +71,15 @@ // Run in UserThread public class TorNetworkNode extends NetworkNode { + private static final Logger log = LoggerFactory.getLogger(TorNetworkNode.class); 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; private final BridgeAddressProvider bridgeAddressProvider; @@ -249,7 +253,7 @@ 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("torrcfile"); + String torrcfile = System.getProperty(CONFIG_TORRCFILE); if(null != torrcfile) { try { override = new Torrc(new FileInputStream(new File(torrcfile))); @@ -261,8 +265,8 @@ private void createTorAndHiddenService(File torDir, int localPort, int servicePo // check if the user wants to temporarily add to the default torrc file LinkedHashMap tmp = new LinkedHashMap<>(); System.getProperties().forEach((k, v) -> { - if(((String) k).startsWith("torrc:")) - tmp.put(((String) k).substring(6), (String) v); + if(((String) k).startsWith(CONFIG_TORRCPREFIX)) + tmp.put(((String) k).substring(CONFIG_TORRCPREFIX.length()), (String) v); }); if(!tmp.isEmpty()) // check for custom torrcfile From 3d53c2584a9159667c96bdea081a667b62cb82f9 Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Wed, 7 Nov 2018 19:44:17 +0100 Subject: [PATCH 5/7] comply to coding conventions --- .../java/bisq/network/p2p/network/TorNetworkNode.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java index a6cd8757fa5..3ec67b59228 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java @@ -253,12 +253,12 @@ 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(null != torrcfile) { + String torrcFile = System.getProperty(CONFIG_TORRCFILE); + if(null != torrcFile) { try { - override = new Torrc(new FileInputStream(new File(torrcfile))); + 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 (" + torrcFile + "). Proceeding with defaults."); } } @@ -269,7 +269,7 @@ private void createTorAndHiddenService(File torDir, int localPort, int servicePo tmp.put(((String) k).substring(CONFIG_TORRCPREFIX.length()), (String) v); }); if(!tmp.isEmpty()) - // check for custom torrcfile + // check for custom torrcFile if(null != override) // and merge the contents override = new Torrc(override.getInputStream$tor(), tmp); From 5ab801cad2b2058d42a9fe7352866b1eed5efcbd Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Thu, 8 Nov 2018 10:13:29 +0100 Subject: [PATCH 6/7] readability --- p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java index 3ec67b59228..220cfd8c125 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java @@ -254,7 +254,7 @@ private void createTorAndHiddenService(File torDir, int localPort, int servicePo // check if the user wants to provide his own torrc file String torrcFile = System.getProperty(CONFIG_TORRCFILE); - if(null != torrcFile) { + if(torrcFile != null) { try { override = new Torrc(new FileInputStream(new File(torrcFile))); } catch(IOException e) { From 7b98445eb982af53e8a6856131cb49914d9bdedd Mon Sep 17 00:00:00 2001 From: Florian Reimair Date: Thu, 8 Nov 2018 14:56:44 +0100 Subject: [PATCH 7/7] use program arguments instead of system properties --- .../java/bisq/core/app/BisqEnvironment.java | 9 ++++ .../java/bisq/core/app/BisqExecutable.java | 11 +++++ .../java/bisq/network/NetworkOptionKeys.java | 2 + .../bisq/network/p2p/NetworkNodeProvider.java | 6 ++- .../main/java/bisq/network/p2p/P2PModule.java | 2 + .../network/p2p/network/TorNetworkNode.java | 45 ++++++++++++------- .../p2p/network/TorNetworkNodeTest.java | 8 ++-- 7 files changed, 61 insertions(+), 22 deletions(-) diff --git a/core/src/main/java/bisq/core/app/BisqEnvironment.java b/core/src/main/java/bisq/core/app/BisqEnvironment.java index b75074c5b51..96846c5167c 100644 --- a/core/src/main/java/bisq/core/app/BisqEnvironment.java +++ b/core/src/main/java/bisq/core/app/BisqEnvironment.java @@ -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; @@ -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) ? @@ -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); diff --git a/core/src/main/java/bisq/core/app/BisqExecutable.java b/core/src/main/java/bisq/core/app/BisqExecutable.java index 57c9dd4cd43..63db8bd4071 100644 --- a/core/src/main/java/bisq/core/app/BisqExecutable.java +++ b/core/src/main/java/bisq/core/app/BisqExecutable.java @@ -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; @@ -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, diff --git a/p2p/src/main/java/bisq/network/NetworkOptionKeys.java b/p2p/src/main/java/bisq/network/NetworkOptionKeys.java index c152441e9b8..2d2bb0f641c 100644 --- a/p2p/src/main/java/bisq/network/NetworkOptionKeys.java +++ b/p2p/src/main/java/bisq/network/NetworkOptionKeys.java @@ -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"; } diff --git a/p2p/src/main/java/bisq/network/p2p/NetworkNodeProvider.java b/p2p/src/main/java/bisq/network/p2p/NetworkNodeProvider.java index 53493d1e826..20938753aed 100644 --- a/p2p/src/main/java/bisq/network/p2p/NetworkNodeProvider.java +++ b/p2p/src/main/java/bisq/network/p2p/NetworkNodeProvider.java @@ -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 diff --git a/p2p/src/main/java/bisq/network/p2p/P2PModule.java b/p2p/src/main/java/bisq/network/p2p/P2PModule.java index dbac9d03df8..4e0d0903345 100644 --- a/p2p/src/main/java/bisq/network/p2p/P2PModule.java +++ b/p2p/src/main/java/bisq/network/p2p/P2PModule.java @@ -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)); } } diff --git a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java index 220cfd8c125..1ea39456184 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java @@ -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; @@ -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; @@ -89,15 +87,20 @@ public class TorNetworkNode extends NetworkNode { private MonadicBinding 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; } @@ -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 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 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)); diff --git a/p2p/src/test/java/bisq/network/p2p/network/TorNetworkNodeTest.java b/p2p/src/test/java/bisq/network/p2p/network/TorNetworkNodeTest.java index 2245a3ebd99..720c5ceb323 100644 --- a/p2p/src/test/java/bisq/network/p2p/network/TorNetworkNodeTest.java +++ b/p2p/src/test/java/bisq/network/p2p/network/TorNetworkNodeTest.java @@ -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() { @@ -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() { @@ -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() { @@ -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() {