mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 11:49:04 +01:00
[plugins/tor/TorPlugin] Adds tor-over-wifi setting
Provides a checkbox in the settings view, as well as an event handler to disable/enable the tor network if the device is not on using the wifi connection. Refactors network-enabling code to a separate function. This function is ran after the network state changes, or the settings change and will update the status accordingly.
This commit is contained in:
@@ -50,11 +50,18 @@ import static android.content.Context.CONNECTIVITY_SERVICE;
|
||||
import static android.content.Context.MODE_PRIVATE;
|
||||
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
|
||||
import static android.net.ConnectivityManager.EXTRA_NO_CONNECTIVITY;
|
||||
import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||
|
||||
import org.briarproject.api.event.EventListener;
|
||||
import org.briarproject.api.event.Event;
|
||||
import org.briarproject.api.event.SettingsUpdatedEvent;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
|
||||
class TorPlugin implements DuplexPlugin, EventHandler {
|
||||
class TorPlugin implements DuplexPlugin, EventHandler,
|
||||
EventListener {
|
||||
|
||||
static final TransportId ID = new TransportId("tor");
|
||||
|
||||
@@ -82,6 +89,9 @@ class TorPlugin implements DuplexPlugin, EventHandler {
|
||||
|
||||
private volatile boolean running = false, networkEnabled = false;
|
||||
private volatile boolean bootstrapped = false;
|
||||
private volatile boolean connectedToWifi = false;
|
||||
private volatile boolean online = false;
|
||||
|
||||
private volatile ServerSocket socket = null;
|
||||
private volatile Socket controlSocket = null;
|
||||
private volatile TorControlConnection controlConnection = null;
|
||||
@@ -585,27 +595,94 @@ class TorPlugin implements DuplexPlugin, EventHandler {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context ctx, Intent i) {
|
||||
|
||||
if (!running) return;
|
||||
boolean online = !i.getBooleanExtra(EXTRA_NO_CONNECTIVITY, false);
|
||||
|
||||
Object o = ctx.getSystemService(CONNECTIVITY_SERVICE);
|
||||
ConnectivityManager cm = (ConnectivityManager) o;
|
||||
NetworkInfo net = cm.getActiveNetworkInfo();
|
||||
|
||||
/* Some devices fail to set EXTRA_NO_CONNECTIVITY, double check */
|
||||
online = !i.getBooleanExtra(EXTRA_NO_CONNECTIVITY, false);
|
||||
if (online) {
|
||||
// Some devices fail to set EXTRA_NO_CONNECTIVITY, double check
|
||||
Object o = ctx.getSystemService(CONNECTIVITY_SERVICE);
|
||||
ConnectivityManager cm = (ConnectivityManager) o;
|
||||
NetworkInfo net = cm.getActiveNetworkInfo();
|
||||
if (net == null || !net.isConnected()) online = false;
|
||||
}
|
||||
String country = locationUtils.getCurrentCountry();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Online: " + online);
|
||||
if ("".equals(country)) LOG.info("Country code unknown");
|
||||
else LOG.info("Country code: " + country);
|
||||
}
|
||||
boolean blocked = TorNetworkMetadata.isTorProbablyBlocked(country);
|
||||
try {
|
||||
enableNetwork(online && !blocked);
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
|
||||
connectedToWifi = (net != null && net.getType() == TYPE_WIFI
|
||||
&& net.isConnected());
|
||||
|
||||
updateConnectionStatus();
|
||||
}
|
||||
}
|
||||
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof SettingsUpdatedEvent) {
|
||||
if (!running) return;
|
||||
|
||||
updateConnectionStatus();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateConnectionStatus() {
|
||||
|
||||
ioExecutor.execute(new Runnable() {
|
||||
|
||||
public void run() {
|
||||
|
||||
boolean wifiOnly = false;
|
||||
boolean blocked = false;
|
||||
|
||||
String country = locationUtils.getCurrentCountry();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Online: " + online);
|
||||
if ("".equals(country)) LOG.info("Country code unknown");
|
||||
else LOG.info("Country code: " + country);
|
||||
}
|
||||
blocked = TorNetworkMetadata.isTorProbablyBlocked(country);
|
||||
TransportConfig c = callback.getConfig();
|
||||
wifiOnly = c.getBoolean("torOverWifi", false);
|
||||
|
||||
try {
|
||||
/*
|
||||
1) Disable network if offline
|
||||
*/
|
||||
if (!online) {
|
||||
LOG.log(WARNING, "Disabling network, network is offline");
|
||||
enableNetwork(false);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
2) Disable network if blocked
|
||||
*/
|
||||
if (blocked) {
|
||||
LOG.log(WARNING, "Disabling network, country is blocked");
|
||||
enableNetwork(false);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
3) Disable network if wifiOnly and not connected to
|
||||
wifi
|
||||
*/
|
||||
if (wifiOnly & !connectedToWifi){
|
||||
LOG.log(WARNING, "Disabling network due to wifi only setting");
|
||||
enableNetwork(false);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
4) Otherwise enable network
|
||||
*/
|
||||
enableNetwork(true);
|
||||
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(WARNING)) LOG.log(WARNING, e.toString(), e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ package org.briarproject.plugins.tor;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.briarproject.api.event.EventBus;
|
||||
|
||||
import org.briarproject.android.util.AndroidUtils;
|
||||
import org.briarproject.api.TransportId;
|
||||
import org.briarproject.api.plugins.duplex.DuplexPlugin;
|
||||
@@ -25,12 +27,14 @@ public class TorPluginFactory implements DuplexPluginFactory {
|
||||
private final Executor ioExecutor;
|
||||
private final Context appContext;
|
||||
private final LocationUtils locationUtils;
|
||||
private final EventBus eventBus;
|
||||
|
||||
public TorPluginFactory(Executor ioExecutor, Context appContext,
|
||||
LocationUtils locationUtils) {
|
||||
LocationUtils locationUtils, EventBus eventBus) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.appContext = appContext;
|
||||
this.locationUtils = locationUtils;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
public TransportId getId() {
|
||||
@@ -38,6 +42,9 @@ public class TorPluginFactory implements DuplexPluginFactory {
|
||||
}
|
||||
|
||||
public DuplexPlugin createPlugin(DuplexPluginCallback callback) {
|
||||
|
||||
TorPlugin thisPlugin = null;
|
||||
|
||||
// Check that we have a Tor binary for this architecture
|
||||
String architecture = null;
|
||||
for (String abi : AndroidUtils.getSupportedArchitectures()) {
|
||||
@@ -55,7 +62,13 @@ public class TorPluginFactory implements DuplexPluginFactory {
|
||||
}
|
||||
// Use position-independent executable for SDK >= 16
|
||||
if (Build.VERSION.SDK_INT >= 16) architecture += "-pie";
|
||||
return new TorPlugin(ioExecutor,appContext, locationUtils, callback,
|
||||
architecture, MAX_LATENCY, MAX_IDLE_TIME, POLLING_INTERVAL);
|
||||
|
||||
thisPlugin = new TorPlugin(ioExecutor,appContext, locationUtils,
|
||||
callback, architecture, MAX_LATENCY, MAX_IDLE_TIME,
|
||||
POLLING_INTERVAL);
|
||||
this.eventBus.addListener(thisPlugin);
|
||||
|
||||
return thisPlugin;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user