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

fix for SocketImpl usage #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 1 addition & 5 deletions src/org/torproject/jtor/connections/ConnectionCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import javax.net.ssl.SSLSocket;

import org.torproject.jtor.circuits.Connection;
import org.torproject.jtor.circuits.ConnectionFailedException;
import org.torproject.jtor.circuits.ConnectionHandshakeException;
Expand All @@ -40,8 +38,7 @@ private class ConnectionTask implements Callable<ConnectionImpl> {
}

public ConnectionImpl call() throws Exception {
SSLSocket socket = factory.createSocket();
ConnectionImpl conn = new ConnectionImpl(socket, router, initializationTracker, isDirectoryConnection);
ConnectionImpl conn = new ConnectionImpl(router, initializationTracker, isDirectoryConnection);
conn.connect();
return conn;
}
Expand All @@ -61,7 +58,6 @@ public void run() {
}

private final ConcurrentMap<Router, Future<ConnectionImpl>> activeConnections = new ConcurrentHashMap<Router, Future<ConnectionImpl>>();
private final ConnectionSocketFactory factory = new ConnectionSocketFactory();
private final ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor();

private TorInitializationTracker initializationTracker;
Expand Down
25 changes: 10 additions & 15 deletions src/org/torproject/jtor/connections/ConnectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public class ConnectionImpl implements Connection, DashboardRenderable {
private final static int DEFAULT_CONNECT_TIMEOUT = 5000;
private final static Cell connectionClosedSentinel = CellImpl.createCell(0, 0);

private final SSLSocket socket;
private final ConnectionSocketFactory factory = new ConnectionSocketFactory();
private SSLSocket socket;
private InputStream input;
private OutputStream output;
private final Router router;
Expand All @@ -55,14 +56,11 @@ public class ConnectionImpl implements Connection, DashboardRenderable {
private final boolean isDirectoryConnection;

private int currentId = 1;
private boolean isConnected;
private volatile boolean isClosed;
private final Thread readCellsThread;
private final Object connectLock = new Object();
private Date lastActivity;

public ConnectionImpl(SSLSocket socket, Router router, TorInitializationTracker tracker, boolean isDirectoryConnection) {
this.socket = socket;
public ConnectionImpl(Router router, TorInitializationTracker tracker, boolean isDirectoryConnection) {
this.router = router;
this.circuitMap = new HashMap<Integer, Circuit>();
this.readCellsThread = new Thread(createReadCellsRunnable());
Expand All @@ -83,7 +81,7 @@ public Router getRouter() {
}

public boolean isClosed() {
return isClosed;
return (socket != null && socket.isClosed());
}

public int allocateCircuitId(Circuit circuit) {
Expand All @@ -102,12 +100,12 @@ private void incrementNextId() {
}

public boolean isConnected() {
return isConnected;
return (socket != null && socket.isConnected());
}

public void connect() throws ConnectionFailedException, ConnectionTimeoutException, ConnectionHandshakeException {
synchronized (connectLock) {
if(isConnected) {
if(isConnected()) {
return;
}
try {
Expand All @@ -124,7 +122,6 @@ public void connect() throws ConnectionFailedException, ConnectionTimeoutExcepti
} catch (ConnectionIOException e) {
throw new ConnectionFailedException(e.getMessage());
}
isConnected = true;
}
}

Expand All @@ -147,8 +144,8 @@ private void connectSocket() throws IOException {
}
}

socket.connect(routerToSocketAddress(router), DEFAULT_CONNECT_TIMEOUT);
socket = factory.createSocket(routerToSocketAddress(router), DEFAULT_CONNECT_TIMEOUT);

if(initializationTracker != null) {
if(isDirectoryConnection) {
initializationTracker.notifyEvent(Tor.BOOTSTRAP_STATUS_HANDSHAKE_DIR);
Expand Down Expand Up @@ -186,7 +183,7 @@ private Cell recvCell() throws ConnectionIOException {
closeSocket();
throw new ConnectionIOException();
} catch (IOException e) {
if(!isClosed) {
if(!isClosed()) {
logger.fine("IOException reading cell from connection "+ this + " : "+ e.getMessage());
closeSocket();
}
Expand All @@ -197,9 +194,7 @@ private Cell recvCell() throws ConnectionIOException {
private void closeSocket() {
try {
logger.fine("Closing connection to "+ this);
isClosed = true;
socket.close();
isConnected = false;
} catch (IOException e) {
logger.warning("Error closing socket: "+ e.getMessage());
}
Expand Down Expand Up @@ -291,7 +286,7 @@ private void processControlCell(Cell cell) {

void idleCloseCheck() {
synchronized (circuitMap) {
final boolean needClose = (!isClosed && circuitMap.isEmpty() && getIdleMilliseconds() > CONNECTION_IDLE_TIMEOUT);
final boolean needClose = (!isClosed() && circuitMap.isEmpty() && getIdleMilliseconds() > CONNECTION_IDLE_TIMEOUT);
if(needClose) {
logger.fine("Closing connection to "+ this +" on idle timeout");
closeSocket();
Expand Down
26 changes: 24 additions & 2 deletions src/org/torproject/jtor/connections/ConnectionSocketFactory.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package org.torproject.jtor.connections;

import java.io.IOException;

import java.lang.reflect.Constructor;

import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketImpl;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
Expand Down Expand Up @@ -63,14 +70,29 @@ private static SSLContext createSSLContext() {
socketFactory = createSSLContext().getSocketFactory();
}

SSLSocket createSocket() {
SSLSocket createSocket(SocketAddress address, int timeout) {
try {
final SSLSocket socket = (SSLSocket) socketFactory.createSocket();
Socket s = createOriginalSocket();
s.connect(address, timeout);
final SSLSocket socket = (SSLSocket) socketFactory.createSocket(s, null, -1, true);
socket.setEnabledCipherSuites(MANDATORY_CIPHERS);
socket.setUseClientMode(true);
return socket;
} catch (IOException e) {
throw new TorException(e);
}
}

Socket createOriginalSocket() throws IOException {
try {
Class<?> clazz = Class.forName("java.net.SocksSocketImpl");
Constructor<?> constructor = clazz.getDeclaredConstructor();
// this maybe does not work because of security restrictions:
constructor.setAccessible(true);
SocketImpl impl = (SocketImpl)constructor.newInstance();
return new Socket(impl) {};
} catch (Throwable t) {
throw new IOException("Cannot create original socket", t);
}
}
}
2 changes: 1 addition & 1 deletion src/org/torproject/jtor/sockets/JTorSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class JTorSocketImpl extends SocketImpl {
}

public void setOption(int optID, Object value) throws SocketException {
throw new UnsupportedOperationException();
// don't throw exception here, this is required for original socket
}

public Object getOption(int optID) throws SocketException {
Expand Down