mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-19 22:29:53 +01:00
Merge branch '578-tor-ports' into 'master'
Don't connect to Tor if it's already running For some time now we've had a reliable way of shutting down the Tor process (the __OwningControllerProcess command line argument combined with the TAKEOWNERSHIP command), but TorPlugin#start() still assumes that Tor may already be running. This allows another app to bind the Tor control and SOCKS ports and collect confidential data from Briar (#578). It also allows two Briar instances running on the same device to try to communicate with the same Tor process, which prevents proper shutdown (#572). This patch prevents the Tor plugin from starting unless it's able to start its own Tor process with the expected control and SOCKS ports. If two Briar instances are running on the same device, only one of them will be able to use Tor. The other should fail to start its Tor plugin and then function normally without Tor access, including normal shutdown. Fixes #572, #578. Open another ticket if you want two Briar instances on the same device to have their own Tor processes. :-) See merge request !272
This commit is contained in:
@@ -150,14 +150,6 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public boolean start() throws IOException {
|
public boolean start() throws IOException {
|
||||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
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
|
// Install the binary, possibly overwriting an older version
|
||||||
if (!installBinary()) {
|
if (!installBinary()) {
|
||||||
LOG.warning("Could not install Tor binary");
|
LOG.warning("Could not install Tor binary");
|
||||||
@@ -169,7 +161,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
LOG.info("Starting Tor");
|
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.getParentFile().mkdirs();
|
||||||
cookieFile.createNewFile();
|
cookieFile.createNewFile();
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
@@ -214,10 +206,8 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
return false;
|
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
|
// Open a control connection and authenticate using the cookie file
|
||||||
|
controlSocket = new Socket("127.0.0.1", CONTROL_PORT);
|
||||||
controlConnection = new TorControlConnection(controlSocket);
|
controlConnection = new TorControlConnection(controlSocket);
|
||||||
controlConnection.authenticate(read(cookieFile));
|
controlConnection.authenticate(read(cookieFile));
|
||||||
// Tell Tor to exit when the control connection is closed
|
// 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
|
// Register to receive events from the Tor process
|
||||||
controlConnection.setEventHandler(this);
|
controlConnection.setEventHandler(this);
|
||||||
controlConnection.setEvents(Arrays.asList(EVENTS));
|
controlConnection.setEvents(Arrays.asList(EVENTS));
|
||||||
// If Tor was already running, find out whether it's bootstrapped
|
// Check whether Tor has already bootstrapped
|
||||||
if (!startProcess) {
|
|
||||||
String phase = controlConnection.getInfo("status/bootstrap-phase");
|
String phase = controlConnection.getInfo("status/bootstrap-phase");
|
||||||
if (phase != null && phase.contains("PROGRESS=100")) {
|
if (phase != null && phase.contains("PROGRESS=100")) {
|
||||||
LOG.info("Tor has already bootstrapped");
|
LOG.info("Tor has already bootstrapped");
|
||||||
connectionStatus.setBootstrapped();
|
connectionStatus.setBootstrapped();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
// Register to receive network status events
|
// Register to receive network status events
|
||||||
networkStateReceiver = new NetworkStateReceiver();
|
networkStateReceiver = new NetworkStateReceiver();
|
||||||
IntentFilter filter = new IntentFilter(CONNECTIVITY_ACTION);
|
IntentFilter filter = new IntentFilter(CONNECTIVITY_ACTION);
|
||||||
@@ -271,6 +259,7 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean installConfig() {
|
private boolean installConfig() {
|
||||||
|
LOG.info("Installing Tor config");
|
||||||
InputStream in = null;
|
InputStream in = null;
|
||||||
OutputStream out = null;
|
OutputStream out = null;
|
||||||
try {
|
try {
|
||||||
@@ -500,20 +489,16 @@ class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
tryToClose(socket);
|
tryToClose(socket);
|
||||||
if (networkStateReceiver != null)
|
if (networkStateReceiver != null)
|
||||||
appContext.unregisterReceiver(networkStateReceiver);
|
appContext.unregisterReceiver(networkStateReceiver);
|
||||||
|
if (controlSocket != null && controlConnection != null) {
|
||||||
try {
|
try {
|
||||||
LOG.info("Stopping Tor");
|
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.setConf("DisableNetwork", "1");
|
||||||
controlConnection.shutdownTor("TERM");
|
controlConnection.shutdownTor("TERM");
|
||||||
controlSocket.close();
|
controlSocket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user