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

Stream isolation for Tor #2081

Merged
merged 6 commits into from Dec 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 12 additions & 6 deletions p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java
Expand Up @@ -47,6 +47,7 @@
import java.net.Socket;
import java.security.SecureRandom;
import java.io.IOException;
import java.util.Base64;
import java.util.Date;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -78,6 +79,8 @@ public class TorNetworkNode extends NetworkNode {

private boolean streamIsolation = false;

private Socks5Proxy socksProxy;

///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -118,18 +121,21 @@ protected Socket createSocket(NodeAddress peerNodeAddress) throws IOException {
// TODO handle failure more cleanly
public Socks5Proxy getSocksProxy() {
try {
tor = Tor.getDefault();

String stream = "";
String stream = null;
if (streamIsolation) {
// create a random string
byte[] bytes = new byte[512]; // note that getProxy does Sha256 that string anyways
new SecureRandom().nextBytes(bytes);
stream = new String(bytes);
stream = Base64.getEncoder().encodeToString(bytes);
}

// ask for the connection
return tor != null ? tor.getProxy(stream) : null;
if (socksProxy == null || streamIsolation) {
tor = Tor.getDefault();

// ask for the connection
socksProxy = tor != null ? tor.getProxy(stream) : null;
}
return socksProxy;
} catch (TorCtlException e) {
log.error("TorCtlException at getSocksProxy: " + e.toString());
e.printStackTrace();
Expand Down
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, TestUtils.getNetworkProtoResolver(),
TorNetworkNode node1 = new TorNetworkNode(port, TestUtils.getNetworkProtoResolver(), false,
new NewTor(new File("torNode_" + port), "", "", new ArrayList<String>()));
node1.start(new SetupListener() {
@Override
Expand All @@ -80,7 +80,7 @@ public void onRequestCustomBridges() {

latch = new CountDownLatch(1);
int port2 = 9002;
TorNetworkNode node2 = new TorNetworkNode(port2, TestUtils.getNetworkProtoResolver(),
TorNetworkNode node2 = new TorNetworkNode(port2, TestUtils.getNetworkProtoResolver(), false,
new NewTor(new File("torNode_" + port), "", "", new ArrayList<String>()));
node2.start(new SetupListener() {
@Override
Expand Down Expand Up @@ -138,7 +138,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, TestUtils.getNetworkProtoResolver(),
TorNetworkNode node1 = new TorNetworkNode(port, TestUtils.getNetworkProtoResolver(), false,
new NewTor(new File("torNode_" + port), "", "", new ArrayList<String>()));
node1.start(new SetupListener() {
@Override
Expand All @@ -164,7 +164,7 @@ public void onRequestCustomBridges() {
});

int port2 = 9002;
TorNetworkNode node2 = new TorNetworkNode(port2, TestUtils.getNetworkProtoResolver(),
TorNetworkNode node2 = new TorNetworkNode(port2, TestUtils.getNetworkProtoResolver(), false,
new NewTor(new File("torNode_" + port), "", "", new ArrayList<String>()));
node2.start(new SetupListener() {
@Override
Expand Down