Don't connect to Tor if it's already running.

Fixes #572, #578.
This commit is contained in:
akwizgran
2016-08-05 13:59:25 +01:00
parent d15d82ccec
commit 36d15358a1

View File

@@ -150,14 +150,6 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
@Override
public boolean start() throws IOException {
if (used.getAndSet(true)) throw new IllegalStateException();
// Try to connect to an existing Tor process if there is one
boolean startProcess = false;
try {
controlSocket = new Socket("127.0.0.1", CONTROL_PORT);
LOG.info("Tor is already running");
} catch (IOException e) {
LOG.info("Tor is not running");
startProcess = true;
// Install the binary, possibly overwriting an older version
if (!installBinary()) {
LOG.warning("Could not install Tor binary");
@@ -169,7 +161,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
return false;
}
LOG.info("Starting Tor");
// Watch for the auth cookie file being created/updated
// Watch for the auth cookie file being updated
cookieFile.getParentFile().mkdirs();
cookieFile.createNewFile();
CountDownLatch latch = new CountDownLatch(1);
@@ -214,10 +206,8 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
Thread.currentThread().interrupt();
return false;
}
// Now we should be able to connect to the new process
controlSocket = new Socket("127.0.0.1", CONTROL_PORT);
}
// Open a control connection and authenticate using the cookie file
controlSocket = new Socket("127.0.0.1", CONTROL_PORT);
controlConnection = new TorControlConnection(controlSocket);
controlConnection.authenticate(read(cookieFile));
// Tell Tor to exit when the control connection is closed
@@ -227,14 +217,12 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
// Register to receive events from the Tor process
controlConnection.setEventHandler(this);
controlConnection.setEvents(Arrays.asList(EVENTS));
// If Tor was already running, find out whether it's bootstrapped
if (!startProcess) {
// Check whether Tor has already bootstrapped
String phase = controlConnection.getInfo("status/bootstrap-phase");
if (phase != null && phase.contains("PROGRESS=100")) {
LOG.info("Tor has already bootstrapped");
connectionStatus.setBootstrapped();
}
}
// Register to receive network status events
networkStateReceiver = new NetworkStateReceiver();
IntentFilter filter = new IntentFilter(CONNECTIVITY_ACTION);
@@ -271,6 +259,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
}
private boolean installConfig() {
LOG.info("Installing Tor config");
InputStream in = null;
OutputStream out = null;
try {
@@ -500,20 +489,16 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
tryToClose(socket);
if (networkStateReceiver != null)
appContext.unregisterReceiver(networkStateReceiver);
if (controlSocket != null && controlConnection != null) {
try {
LOG.info("Stopping Tor");
if (controlSocket == null)
controlSocket = new Socket("127.0.0.1", CONTROL_PORT);
if (controlConnection == null) {
controlConnection = new TorControlConnection(controlSocket);
controlConnection.authenticate(read(cookieFile));
}
controlConnection.setConf("DisableNetwork", "1");
controlConnection.shutdownTor("TERM");
controlSocket.close();
} catch (IOException e) {
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
}
}
wakeLock.release();
}