From d17669f131b52a9d6deace950c793522e08c1c2f Mon Sep 17 00:00:00 2001 From: akwizgran <akwizgran@users.sourceforge.net> Date: Tue, 11 Apr 2017 14:53:03 +0100 Subject: [PATCH] Increase socket timeout for Tor sockets. --- .../bramble/api/plugin/TorConstants.java | 1 + .../briarproject/bramble/socks/SocksModule.java | 4 +++- .../briarproject/bramble/socks/SocksSocket.java | 16 +++++++++------- .../bramble/socks/SocksSocketFactory.java | 8 +++++--- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/TorConstants.java b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/TorConstants.java index 5ab9594f8b..639155ac4a 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/TorConstants.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/plugin/TorConstants.java @@ -8,6 +8,7 @@ public interface TorConstants { int CONTROL_PORT = 59051; int CONNECT_TO_PROXY_TIMEOUT = 5000; // Milliseconds + int EXTRA_SOCKET_TIMEOUT = 30000; // Milliseconds String PREF_TOR_NETWORK = "network"; String PREF_TOR_PORT = "port"; diff --git a/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksModule.java b/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksModule.java index 1fe9f31c6f..be342f0110 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksModule.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksModule.java @@ -8,6 +8,7 @@ import dagger.Module; import dagger.Provides; import static org.briarproject.bramble.api.plugin.TorConstants.CONNECT_TO_PROXY_TIMEOUT; +import static org.briarproject.bramble.api.plugin.TorConstants.EXTRA_SOCKET_TIMEOUT; import static org.briarproject.bramble.api.plugin.TorConstants.SOCKS_PORT; @Module @@ -17,6 +18,7 @@ public class SocksModule { SocketFactory provideTorSocketFactory() { InetSocketAddress proxy = new InetSocketAddress("127.0.0.1", SOCKS_PORT); - return new SocksSocketFactory(proxy, CONNECT_TO_PROXY_TIMEOUT); + return new SocksSocketFactory(proxy, CONNECT_TO_PROXY_TIMEOUT, + EXTRA_SOCKET_TIMEOUT); } } diff --git a/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksSocket.java b/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksSocket.java index 9494e62977..7f5cb0090d 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksSocket.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksSocket.java @@ -29,11 +29,13 @@ class SocksSocket extends Socket { private static final byte[] UNSPECIFIED_ADDRESS = new byte[4]; private final SocketAddress proxy; - private final int connectToProxyTimeout; + private final int connectToProxyTimeout, extraSocketTimeout; - SocksSocket(SocketAddress proxy, int connectToProxyTimeout) { + SocksSocket(SocketAddress proxy, int connectToProxyTimeout, + int extraSocketTimeout) { this.proxy = proxy; this.connectToProxyTimeout = connectToProxyTimeout; + this.extraSocketTimeout = extraSocketTimeout; } @Override @@ -47,7 +49,7 @@ class SocksSocket extends Socket { InetAddress address = inet.getAddress(); if (address != null && !Arrays.equals(address.getAddress(), UNSPECIFIED_ADDRESS)) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException(); } String host = inet.getHostName(); if (host.length() > 255) throw new IllegalArgumentException(); @@ -62,16 +64,16 @@ class SocksSocket extends Socket { sendMethodRequest(out); receiveMethodResponse(in); - // Use the supplied timeout temporarily + // Use the supplied timeout temporarily, plus any configured extra int oldTimeout = getSoTimeout(); - setSoTimeout(timeout); + setSoTimeout(timeout + extraSocketTimeout); // Connect to the endpoint via the proxy sendConnectRequest(out, host, port); receiveConnectResponse(in); - // Restore the old timeout - setSoTimeout(oldTimeout); + // Restore the old timeout, plus any configured extra + setSoTimeout(oldTimeout + extraSocketTimeout); } private void sendMethodRequest(OutputStream out) throws IOException { diff --git a/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksSocketFactory.java b/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksSocketFactory.java index adc5265fb9..fb0b1cd916 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksSocketFactory.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/socks/SocksSocketFactory.java @@ -11,16 +11,18 @@ import javax.net.SocketFactory; class SocksSocketFactory extends SocketFactory { private final SocketAddress proxy; - private final int connectToProxyTimeout; + private final int connectToProxyTimeout, extraSocketTimeout; - SocksSocketFactory(SocketAddress proxy, int connectToProxyTimeout) { + SocksSocketFactory(SocketAddress proxy, int connectToProxyTimeout, + int extraSocketTimeout) { this.proxy = proxy; this.connectToProxyTimeout = connectToProxyTimeout; + this.extraSocketTimeout = extraSocketTimeout; } @Override public Socket createSocket() { - return new SocksSocket(proxy, connectToProxyTimeout); + return new SocksSocket(proxy, connectToProxyTimeout, extraSocketTimeout); } @Override -- GitLab