Merge branch '871-increase-socket-timeout' into 'master'

Increase socket timeout for Tor sockets

See merge request !519
This commit is contained in:
akwizgran
2017-04-19 16:53:59 +00:00
4 changed files with 18 additions and 11 deletions

View File

@@ -8,6 +8,7 @@ public interface TorConstants {
int CONTROL_PORT = 59051; int CONTROL_PORT = 59051;
int CONNECT_TO_PROXY_TIMEOUT = 5000; // Milliseconds int CONNECT_TO_PROXY_TIMEOUT = 5000; // Milliseconds
int EXTRA_SOCKET_TIMEOUT = 30000; // Milliseconds
String PREF_TOR_NETWORK = "network"; String PREF_TOR_NETWORK = "network";
String PREF_TOR_PORT = "port"; String PREF_TOR_PORT = "port";

View File

@@ -8,6 +8,7 @@ import dagger.Module;
import dagger.Provides; import dagger.Provides;
import static org.briarproject.bramble.api.plugin.TorConstants.CONNECT_TO_PROXY_TIMEOUT; 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; import static org.briarproject.bramble.api.plugin.TorConstants.SOCKS_PORT;
@Module @Module
@@ -17,6 +18,7 @@ public class SocksModule {
SocketFactory provideTorSocketFactory() { SocketFactory provideTorSocketFactory() {
InetSocketAddress proxy = new InetSocketAddress("127.0.0.1", InetSocketAddress proxy = new InetSocketAddress("127.0.0.1",
SOCKS_PORT); SOCKS_PORT);
return new SocksSocketFactory(proxy, CONNECT_TO_PROXY_TIMEOUT); return new SocksSocketFactory(proxy, CONNECT_TO_PROXY_TIMEOUT,
EXTRA_SOCKET_TIMEOUT);
} }
} }

View File

@@ -29,11 +29,13 @@ class SocksSocket extends Socket {
private static final byte[] UNSPECIFIED_ADDRESS = new byte[4]; private static final byte[] UNSPECIFIED_ADDRESS = new byte[4];
private final SocketAddress proxy; 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.proxy = proxy;
this.connectToProxyTimeout = connectToProxyTimeout; this.connectToProxyTimeout = connectToProxyTimeout;
this.extraSocketTimeout = extraSocketTimeout;
} }
@Override @Override
@@ -47,7 +49,7 @@ class SocksSocket extends Socket {
InetAddress address = inet.getAddress(); InetAddress address = inet.getAddress();
if (address != null if (address != null
&& !Arrays.equals(address.getAddress(), UNSPECIFIED_ADDRESS)) { && !Arrays.equals(address.getAddress(), UNSPECIFIED_ADDRESS)) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
String host = inet.getHostName(); String host = inet.getHostName();
if (host.length() > 255) throw new IllegalArgumentException(); if (host.length() > 255) throw new IllegalArgumentException();
@@ -62,16 +64,16 @@ class SocksSocket extends Socket {
sendMethodRequest(out); sendMethodRequest(out);
receiveMethodResponse(in); receiveMethodResponse(in);
// Use the supplied timeout temporarily // Use the supplied timeout temporarily, plus any configured extra
int oldTimeout = getSoTimeout(); int oldTimeout = getSoTimeout();
setSoTimeout(timeout); setSoTimeout(timeout + extraSocketTimeout);
// Connect to the endpoint via the proxy // Connect to the endpoint via the proxy
sendConnectRequest(out, host, port); sendConnectRequest(out, host, port);
receiveConnectResponse(in); receiveConnectResponse(in);
// Restore the old timeout // Restore the old timeout, plus any configured extra
setSoTimeout(oldTimeout); setSoTimeout(oldTimeout + extraSocketTimeout);
} }
private void sendMethodRequest(OutputStream out) throws IOException { private void sendMethodRequest(OutputStream out) throws IOException {

View File

@@ -11,16 +11,18 @@ import javax.net.SocketFactory;
class SocksSocketFactory extends SocketFactory { class SocksSocketFactory extends SocketFactory {
private final SocketAddress proxy; 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.proxy = proxy;
this.connectToProxyTimeout = connectToProxyTimeout; this.connectToProxyTimeout = connectToProxyTimeout;
this.extraSocketTimeout = extraSocketTimeout;
} }
@Override @Override
public Socket createSocket() { public Socket createSocket() {
return new SocksSocket(proxy, connectToProxyTimeout); return new SocksSocket(proxy, connectToProxyTimeout, extraSocketTimeout);
} }
@Override @Override