mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 10:49:06 +01:00
Compare commits
62 Commits
recently-o
...
peer-disco
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
085987febd | ||
|
|
65c96514b5 | ||
|
|
174e678304 | ||
|
|
14d7abc823 | ||
|
|
52fca33d0b | ||
|
|
472d560dda | ||
|
|
2522034397 | ||
|
|
3e6b65b1a0 | ||
|
|
155746b24e | ||
|
|
91caff183f | ||
|
|
249dcda34e | ||
|
|
c0a82f96a3 | ||
|
|
79f5229230 | ||
|
|
02b4925609 | ||
|
|
0664720680 | ||
|
|
f04d32f7f2 | ||
|
|
dfa05fc473 | ||
|
|
cb936d95c5 | ||
|
|
1b402ba0c2 | ||
|
|
2c6f81a120 | ||
|
|
b69eb8f203 | ||
|
|
e956f073ae | ||
|
|
f4b6389163 | ||
|
|
82bfb4d95e | ||
|
|
93ec646634 | ||
|
|
2420456f25 | ||
|
|
b32417e7d3 | ||
|
|
9efa3cc44e | ||
|
|
90c8603d3a | ||
|
|
1ae9750c13 | ||
|
|
b0b87fc0db | ||
|
|
62cb6095ca | ||
|
|
d4a64f4ee3 | ||
|
|
6886551895 | ||
|
|
b50b9f8088 | ||
|
|
c1aade221a | ||
|
|
40f2c1923b | ||
|
|
cfc640f4ce | ||
|
|
c865b90c6c | ||
|
|
4db2d0fda2 | ||
|
|
719debc36a | ||
|
|
ce1b5eb0d9 | ||
|
|
5bd9a29eab | ||
|
|
e6d093c52f | ||
|
|
fe5bbfdd17 | ||
|
|
e6ac6913a7 | ||
|
|
54068a9e24 | ||
|
|
4bb14f51d2 | ||
|
|
37ea59a89e | ||
|
|
f19dbf144a | ||
|
|
4b94bd0f1b | ||
|
|
0b29e3ce11 | ||
|
|
6a9dbcf482 | ||
|
|
f5a21d8c07 | ||
|
|
b6a73f2c98 | ||
|
|
d084f6dd8d | ||
|
|
0259c23cb4 | ||
|
|
341382cfa8 | ||
|
|
49baf1020b | ||
|
|
6b33c5b913 | ||
|
|
53889436fc | ||
|
|
e35d1763bc |
@@ -38,7 +38,7 @@ configurations {
|
||||
|
||||
dependencies {
|
||||
implementation project(path: ':bramble-core', configuration: 'default')
|
||||
tor 'org.briarproject:tor-android:0.3.5.10@zip'
|
||||
tor 'org.briarproject:tor-android:0.3.5.9@zip'
|
||||
tor 'org.briarproject:obfs4proxy-android:0.0.11-2@zip'
|
||||
|
||||
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
||||
|
||||
@@ -8,23 +8,34 @@ import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.ParcelUuid;
|
||||
import android.os.Parcelable;
|
||||
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.DiscoveryHandler;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.PluginException;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.util.AndroidUtils;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@@ -46,15 +57,25 @@ import static android.bluetooth.BluetoothAdapter.SCAN_MODE_NONE;
|
||||
import static android.bluetooth.BluetoothAdapter.STATE_OFF;
|
||||
import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
||||
import static android.bluetooth.BluetoothDevice.ACTION_FOUND;
|
||||
import static android.bluetooth.BluetoothDevice.ACTION_UUID;
|
||||
import static android.bluetooth.BluetoothDevice.DEVICE_TYPE_LE;
|
||||
import static android.bluetooth.BluetoothDevice.EXTRA_DEVICE;
|
||||
import static android.bluetooth.BluetoothDevice.EXTRA_UUID;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static java.nio.ByteOrder.LITTLE_ENDIAN;
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.shuffle;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_UUID;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
@@ -63,7 +84,9 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
private static final Logger LOG =
|
||||
getLogger(AndroidBluetoothPlugin.class.getName());
|
||||
|
||||
private static final int MAX_DISCOVERY_MS = 10_000;
|
||||
private static final int MIN_DEVICE_DISCOVERY_MS = 2_000;
|
||||
private static final int MAX_DEVICE_DISCOVERY_MS = 30_000;
|
||||
private static final int MAX_SERVICE_DISCOVERY_MS = 15_000;
|
||||
|
||||
private final AndroidExecutor androidExecutor;
|
||||
private final Context appContext;
|
||||
@@ -137,17 +160,42 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
@Override
|
||||
void disableAdapterIfEnabledByUs() {
|
||||
if (isAdapterEnabled() && wasEnabledByUs) {
|
||||
cancelDiscoverability();
|
||||
if (adapter.disable()) LOG.info("Disabling Bluetooth");
|
||||
else LOG.info("Could not disable Bluetooth");
|
||||
wasEnabledByUs = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void cancelDiscoverability() {
|
||||
if (adapter.getScanMode() == SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
||||
try {
|
||||
Method setDiscoverableTimeout = BluetoothAdapter.class
|
||||
.getDeclaredMethod("setDiscoverableTimeout", int.class);
|
||||
setDiscoverableTimeout.setAccessible(true);
|
||||
setDiscoverableTimeout.invoke(adapter, 1);
|
||||
LOG.info("Cancelled discoverability");
|
||||
} catch (NoSuchMethodException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
} catch (IllegalAccessException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
} catch (InvocationTargetException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
void setEnabledByUs() {
|
||||
wasEnabledByUs = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
void onAdapterDisabled() {
|
||||
super.onAdapterDisabled();
|
||||
wasEnabledByUs = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
String getBluetoothAddress() {
|
||||
@@ -202,7 +250,8 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
@Nullable
|
||||
DuplexTransportConnection discoverAndConnect(String uuid) {
|
||||
if (adapter == null) return null;
|
||||
for (String address : discoverDevices()) {
|
||||
for (BluetoothDevice d : discoverDevices()) {
|
||||
String address = d.getAddress();
|
||||
try {
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Connecting to " + scrubMacAddress(address));
|
||||
@@ -218,10 +267,184 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
return null;
|
||||
}
|
||||
|
||||
private Collection<String> discoverDevices() {
|
||||
List<String> addresses = new ArrayList<>();
|
||||
@Override
|
||||
public boolean supportsDiscovery() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discoverPeers(
|
||||
List<Pair<TransportProperties, DiscoveryHandler>> properties) {
|
||||
// Discover all nearby devices
|
||||
List<BluetoothDevice> devices = discoverDevices();
|
||||
if (devices.isEmpty()) {
|
||||
LOG.info("No devices discovered");
|
||||
return;
|
||||
}
|
||||
|
||||
List<Pair<TransportProperties, DiscoveryHandler>> discovered =
|
||||
new ArrayList<>();
|
||||
Map<String, Pair<TransportProperties, DiscoveryHandler>> byUuid =
|
||||
new HashMap<>();
|
||||
Map<String, Pair<TransportProperties, DiscoveryHandler>> byAddress =
|
||||
new HashMap<>();
|
||||
for (Pair<TransportProperties, DiscoveryHandler> pair : properties) {
|
||||
TransportProperties p = pair.getFirst();
|
||||
String uuid = p.get(PROP_UUID);
|
||||
if (!isNullOrEmpty(uuid)) {
|
||||
byUuid.put(uuid, pair);
|
||||
String address = p.get(PROP_ADDRESS);
|
||||
if (!isNullOrEmpty(address)) byAddress.put(address, pair);
|
||||
}
|
||||
}
|
||||
|
||||
List<BluetoothDevice> unknown = new ArrayList<>(devices);
|
||||
for (BluetoothDevice d : devices) {
|
||||
Pair<TransportProperties, DiscoveryHandler> pair =
|
||||
byAddress.remove(d.getAddress());
|
||||
if (pair == null) {
|
||||
// Try cached UUIDs
|
||||
for (String uuid : getUuids(d)) {
|
||||
pair = byUuid.remove(uuid);
|
||||
if (pair != null) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Matched "
|
||||
+ scrubMacAddress(d.getAddress())
|
||||
+ " by cached UUID");
|
||||
}
|
||||
TransportProperties p =
|
||||
new TransportProperties(pair.getFirst());
|
||||
p.put(PROP_ADDRESS, d.getAddress());
|
||||
discovered.add(new Pair<>(p, pair.getSecond()));
|
||||
unknown.remove(d);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Matched " + scrubMacAddress(d.getAddress())
|
||||
+ " by address");
|
||||
}
|
||||
discovered.add(pair);
|
||||
unknown.remove(d);
|
||||
}
|
||||
}
|
||||
if (unknown.isEmpty()) {
|
||||
LOG.info("All discovered devices are known, not fetching UUIDs");
|
||||
return;
|
||||
}
|
||||
// Fetch up-to-date UUIDs
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Fetching UUIDs for " + unknown.size() + " devices");
|
||||
BlockingQueue<Intent> intents = new LinkedBlockingQueue<>();
|
||||
DiscoveryReceiver receiver = new DiscoveryReceiver(intents);
|
||||
QueueingReceiver receiver = new QueueingReceiver(intents);
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(ACTION_UUID);
|
||||
appContext.registerReceiver(receiver, filter);
|
||||
try {
|
||||
List<BluetoothDevice> pending = new ArrayList<>();
|
||||
for (BluetoothDevice d : unknown) {
|
||||
if (d.fetchUuidsWithSdp()) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Fetching UUIDs for "
|
||||
+ scrubMacAddress(d.getAddress()));
|
||||
}
|
||||
pending.add(d);
|
||||
} else {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Failed to fetch UUIDs for "
|
||||
+ scrubMacAddress(d.getAddress()));
|
||||
}
|
||||
}
|
||||
}
|
||||
long now = clock.currentTimeMillis();
|
||||
long end = now + MAX_SERVICE_DISCOVERY_MS;
|
||||
while (now < end && !pending.isEmpty()) {
|
||||
Intent i = intents.poll(end - now, MILLISECONDS);
|
||||
if (i == null) break;
|
||||
BluetoothDevice d = requireNonNull(
|
||||
i.getParcelableExtra(EXTRA_DEVICE));
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Fetched UUIDs for "
|
||||
+ scrubMacAddress(d.getAddress()));
|
||||
}
|
||||
Set<String> uuids = getUuids(d);
|
||||
Parcelable[] extra = i.getParcelableArrayExtra(EXTRA_UUID);
|
||||
if (extra != null) {
|
||||
for (Parcelable p : extra) {
|
||||
uuids.addAll(getUuidStrings((ParcelUuid) p));
|
||||
}
|
||||
}
|
||||
for (String uuid : uuids) {
|
||||
Pair<TransportProperties, DiscoveryHandler> pair =
|
||||
byUuid.remove(uuid);
|
||||
if (pair != null) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Matched "
|
||||
+ scrubMacAddress(d.getAddress())
|
||||
+ " by fetched UUID");
|
||||
}
|
||||
TransportProperties p =
|
||||
new TransportProperties(pair.getFirst());
|
||||
p.put(PROP_ADDRESS, d.getAddress());
|
||||
discovered.add(new Pair<>(p, pair.getSecond()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
pending.remove(d);
|
||||
now = clock.currentTimeMillis();
|
||||
}
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
if (pending.isEmpty()) {
|
||||
LOG.info("Finished fetching UUIDs");
|
||||
} else {
|
||||
LOG.info("Failed to fetch UUIDs for " + pending.size()
|
||||
+ " devices");
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
LOG.info("Interrupted while fetching UUIDs");
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
appContext.unregisterReceiver(receiver);
|
||||
}
|
||||
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Discovered " + discovered.size() + " contacts");
|
||||
}
|
||||
for (Pair<TransportProperties, DiscoveryHandler> pair : discovered) {
|
||||
pair.getSecond().handleDevice(pair.getFirst());
|
||||
}
|
||||
}
|
||||
|
||||
private Set<String> getUuids(BluetoothDevice d) {
|
||||
Set<String> strings = new TreeSet<>();
|
||||
ParcelUuid[] uuids = d.getUuids();
|
||||
if (uuids == null) return strings;
|
||||
for (ParcelUuid u : uuids) strings.addAll(getUuidStrings(u));
|
||||
return strings;
|
||||
}
|
||||
|
||||
// Workaround for https://code.google.com/p/android/issues/detail?id=197341
|
||||
private List<String> getUuidStrings(ParcelUuid u) {
|
||||
UUID forwards = u.getUuid();
|
||||
ByteBuffer buf = ByteBuffer.allocate(16);
|
||||
buf.putLong(forwards.getLeastSignificantBits());
|
||||
buf.putLong(forwards.getMostSignificantBits());
|
||||
buf.rewind();
|
||||
buf.order(LITTLE_ENDIAN);
|
||||
UUID backwards = new UUID(buf.getLong(), buf.getLong());
|
||||
return asList(forwards.toString(), backwards.toString());
|
||||
}
|
||||
|
||||
private List<BluetoothDevice> discoverDevices() {
|
||||
if (adapter.isDiscovering()) {
|
||||
LOG.info("Already discovering");
|
||||
return emptyList();
|
||||
}
|
||||
List<BluetoothDevice> devices = new ArrayList<>();
|
||||
BlockingQueue<Intent> intents = new LinkedBlockingQueue<>();
|
||||
QueueingReceiver receiver = new QueueingReceiver(intents);
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(ACTION_DISCOVERY_STARTED);
|
||||
filter.addAction(ACTION_DISCOVERY_FINISHED);
|
||||
@@ -229,8 +452,9 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
appContext.registerReceiver(receiver, filter);
|
||||
try {
|
||||
if (adapter.startDiscovery()) {
|
||||
long now = clock.currentTimeMillis();
|
||||
long end = now + MAX_DISCOVERY_MS;
|
||||
long start = clock.currentTimeMillis();
|
||||
long end = start + MAX_DEVICE_DISCOVERY_MS;
|
||||
long now = start;
|
||||
while (now < end) {
|
||||
Intent i = intents.poll(end - now, MILLISECONDS);
|
||||
if (i == null) break;
|
||||
@@ -239,17 +463,26 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
LOG.info("Discovery started");
|
||||
} else if (ACTION_DISCOVERY_FINISHED.equals(action)) {
|
||||
LOG.info("Discovery finished");
|
||||
break;
|
||||
now = clock.currentTimeMillis();
|
||||
if (now - start < MIN_DEVICE_DISCOVERY_MS) {
|
||||
LOG.info("Discovery finished quickly, retrying");
|
||||
if (!adapter.startDiscovery()) {
|
||||
LOG.info("Could not restart discovery");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else if (ACTION_FOUND.equals(action)) {
|
||||
BluetoothDevice d = i.getParcelableExtra(EXTRA_DEVICE);
|
||||
BluetoothDevice d = requireNonNull(
|
||||
i.getParcelableExtra(EXTRA_DEVICE));
|
||||
// Ignore Bluetooth LE devices
|
||||
if (SDK_INT < 18 || d.getType() != DEVICE_TYPE_LE) {
|
||||
String address = d.getAddress();
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Discovered " +
|
||||
scrubMacAddress(address));
|
||||
if (!addresses.contains(address))
|
||||
addresses.add(address);
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Discovered "
|
||||
+ scrubMacAddress(d.getAddress()));
|
||||
}
|
||||
if (!devices.contains(d)) devices.add(d);
|
||||
}
|
||||
}
|
||||
now = clock.currentTimeMillis();
|
||||
@@ -265,9 +498,9 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
adapter.cancelDiscovery();
|
||||
appContext.unregisterReceiver(receiver);
|
||||
}
|
||||
// Shuffle the addresses so we don't always try the same one first
|
||||
shuffle(addresses);
|
||||
return addresses;
|
||||
// Shuffle the devices so we don't always try the same one first
|
||||
shuffle(devices);
|
||||
return devices;
|
||||
}
|
||||
|
||||
private class BluetoothStateReceiver extends BroadcastReceiver {
|
||||
@@ -288,11 +521,11 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
||||
}
|
||||
}
|
||||
|
||||
private static class DiscoveryReceiver extends BroadcastReceiver {
|
||||
private static class QueueingReceiver extends BroadcastReceiver {
|
||||
|
||||
private final BlockingQueue<Intent> intents;
|
||||
|
||||
private DiscoveryReceiver(BlockingQueue<Intent> intents) {
|
||||
private QueueingReceiver(BlockingQueue<Intent> intents) {
|
||||
this.intents = intents;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,9 +10,6 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
||||
import static org.briarproject.bramble.util.AndroidUtils.isValidBluetoothAddress;
|
||||
|
||||
@NotNullByDefault
|
||||
class AndroidBluetoothTransportConnection
|
||||
extends AbstractDuplexTransportConnection {
|
||||
@@ -26,8 +23,6 @@ class AndroidBluetoothTransportConnection
|
||||
super(plugin);
|
||||
this.connectionManager = connectionManager;
|
||||
this.socket = socket;
|
||||
String address = socket.getRemoteDevice().getAddress();
|
||||
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -9,11 +9,11 @@ import android.net.wifi.WifiManager;
|
||||
|
||||
import org.briarproject.bramble.PoliteExecutor;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.network.event.NetworkStatusEvent;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
@@ -32,10 +32,14 @@ import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
|
||||
@NotNullByDefault
|
||||
class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
||||
class AndroidLanTcpPlugin extends LanTcpPlugin {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(AndroidLanTcpPlugin.class.getName());
|
||||
@@ -68,16 +72,11 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
||||
public void start() {
|
||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||
initialisePortProperty();
|
||||
running = true;
|
||||
Settings settings = callback.getSettings();
|
||||
state.setStarted(settings.getBoolean(PREF_PLUGIN_ENABLE, false));
|
||||
updateConnectionStatus();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
running = false;
|
||||
tryToClose(socket);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Socket createSocket() throws IOException {
|
||||
return socketFactory.createSocket();
|
||||
@@ -130,13 +129,15 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
super.eventOccurred(e);
|
||||
if (e instanceof NetworkStatusEvent) updateConnectionStatus();
|
||||
}
|
||||
|
||||
private void updateConnectionStatus() {
|
||||
connectionStatusExecutor.execute(() -> {
|
||||
if (!running) return;
|
||||
List<InetAddress> addrs = getUsableLocalInetAddresses();
|
||||
State s = getState();
|
||||
if (s != ACTIVE && s != INACTIVE) return;
|
||||
List<InetAddress> addrs = getLocalInetAddresses();
|
||||
if (addrs.contains(WIFI_AP_ADDRESS)
|
||||
|| addrs.contains(WIFI_DIRECT_AP_ADDRESS)) {
|
||||
LOG.info("Providing wifi hotspot");
|
||||
@@ -145,15 +146,21 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
||||
// make outgoing connections on API 21+ if another network
|
||||
// has internet access
|
||||
socketFactory = SocketFactory.getDefault();
|
||||
if (socket == null || socket.isClosed()) bind();
|
||||
if (s == INACTIVE) bind();
|
||||
} else if (addrs.isEmpty()) {
|
||||
LOG.info("Not connected to wifi");
|
||||
socketFactory = SocketFactory.getDefault();
|
||||
tryToClose(socket);
|
||||
// Server socket may not have been closed automatically when
|
||||
// interface was taken down. Socket will be cleared and state
|
||||
// updated in acceptContactConnections()
|
||||
if (s == ACTIVE) {
|
||||
LOG.info("Closing server socket");
|
||||
tryToClose(state.getServerSocket(), LOG, WARNING);
|
||||
}
|
||||
} else {
|
||||
LOG.info("Connected to wifi");
|
||||
socketFactory = getSocketFactory();
|
||||
if (socket == null || socket.isClosed()) bind();
|
||||
if (s == INACTIVE) bind();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -74,7 +74,6 @@ class AndroidTorPlugin extends TorPlugin {
|
||||
|
||||
@Override
|
||||
protected void enableNetwork(boolean enable) throws IOException {
|
||||
if (!running) return;
|
||||
if (enable) wakeLock.acquire();
|
||||
super.enableNetwork(enable);
|
||||
if (!enable) wakeLock.release();
|
||||
|
||||
@@ -61,12 +61,12 @@ class AndroidLocationUtils implements LocationUtils {
|
||||
private String getCountryFromPhoneNetwork() {
|
||||
Object o = appContext.getSystemService(TELEPHONY_SERVICE);
|
||||
TelephonyManager tm = (TelephonyManager) o;
|
||||
return tm.getNetworkCountryIso();
|
||||
return tm == null ? "" : tm.getNetworkCountryIso();
|
||||
}
|
||||
|
||||
private String getCountryFromSimCard() {
|
||||
Object o = appContext.getSystemService(TELEPHONY_SERVICE);
|
||||
TelephonyManager tm = (TelephonyManager) o;
|
||||
return tm.getSimCountryIso();
|
||||
return tm == null ? "" : tm.getSimCountryIso();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ public class AndroidUtils {
|
||||
return new Pair<>("", "");
|
||||
}
|
||||
|
||||
public static boolean isValidBluetoothAddress(@Nullable String address) {
|
||||
private static boolean isValidBluetoothAddress(@Nullable String address) {
|
||||
return !StringUtils.isNullOrEmpty(address)
|
||||
&& BluetoothAdapter.checkBluetoothAddress(address)
|
||||
&& !address.equals(FAKE_BLUETOOTH_ADDRESS);
|
||||
|
||||
@@ -70,7 +70,7 @@ dependencyVerification {
|
||||
'org.bouncycastle:bcpkix-jdk15on:1.56:bcpkix-jdk15on-1.56.jar:7043dee4e9e7175e93e0b36f45b1ec1ecb893c5f755667e8b916eb8dd201c6ca',
|
||||
'org.bouncycastle:bcprov-jdk15on:1.56:bcprov-jdk15on-1.56.jar:963e1ee14f808ffb99897d848ddcdb28fa91ddda867eb18d303e82728f878349',
|
||||
'org.briarproject:obfs4proxy-android:0.0.11-2:obfs4proxy-android-0.0.11-2.zip:57e55cbe87aa2aac210fdbb6cd8cdeafe15f825406a08ebf77a8b787aa2c6a8a',
|
||||
'org.briarproject:tor-android:0.3.5.10:tor-android-0.3.5.10.zip:edd83bf557fcff2105eaa0bdb3f607a6852ebe7360920929ae3039dd5f4774c5',
|
||||
'org.briarproject:tor-android:0.3.5.9:tor-android-0.3.5.9.zip:853b0440feccd6904bd03e6b2de53a62ebcde1d58068beeadc447a7dff950bc8',
|
||||
'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d',
|
||||
'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a',
|
||||
'org.codehaus.groovy:groovy-all:2.4.15:groovy-all-2.4.15.jar:51d6c4e71782e85674239189499854359d380fb75e1a703756e3aaa5b98a5af0',
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.briarproject.bramble.api.account;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
@@ -14,8 +13,7 @@ public interface AccountManager {
|
||||
* Returns true if the manager has the database key. This will be false
|
||||
* before {@link #createAccount(String, String)} or {@link #signIn(String)}
|
||||
* has been called, and true after {@link #createAccount(String, String)}
|
||||
* or {@link #signIn(String)} has returned true, until
|
||||
* {@link #deleteAccount()} is called or the process exits.
|
||||
* or {@link #signIn(String)} has returned true, until the process exits.
|
||||
*/
|
||||
boolean hasDatabaseKey();
|
||||
|
||||
@@ -24,22 +22,25 @@ public interface AccountManager {
|
||||
* before {@link #createAccount(String, String)} or {@link #signIn(String)}
|
||||
* has been called, and non-null after
|
||||
* {@link #createAccount(String, String)} or {@link #signIn(String)} has
|
||||
* returned true, until {@link #deleteAccount()} is called or the process
|
||||
* exits.
|
||||
* returned true, until the process exits.
|
||||
*/
|
||||
@Nullable
|
||||
SecretKey getDatabaseKey();
|
||||
|
||||
/**
|
||||
* Returns true if the encrypted database key can be loaded from disk.
|
||||
* Returns true if the encrypted database key can be loaded from disk, and
|
||||
* the database directory exists and is a directory.
|
||||
*/
|
||||
boolean accountExists();
|
||||
|
||||
/**
|
||||
* Creates an identity with the given name and registers it with the
|
||||
* {@link IdentityManager}. Creates a database key, encrypts it with the
|
||||
* given password and stores it on disk. {@link #accountExists()} will
|
||||
* return true after this method returns true.
|
||||
* given password and stores it on disk.
|
||||
* <p/>
|
||||
* This method does not create the database directory, so
|
||||
* {@link #accountExists()} will continue to return false until the
|
||||
* database directory is created.
|
||||
*/
|
||||
boolean createAccount(String name, String password);
|
||||
|
||||
@@ -53,19 +54,17 @@ public interface AccountManager {
|
||||
* Loads the encrypted database key from disk and decrypts it with the
|
||||
* given password.
|
||||
*
|
||||
* @throws DecryptionException If the database key could not be loaded and
|
||||
* decrypted.
|
||||
* @return true if the database key was successfully loaded and decrypted.
|
||||
*/
|
||||
void signIn(String password) throws DecryptionException;
|
||||
boolean signIn(String password);
|
||||
|
||||
/**
|
||||
* Loads the encrypted database key from disk, decrypts it with the old
|
||||
* password, encrypts it with the new password, and stores it on disk,
|
||||
* replacing the old key.
|
||||
*
|
||||
* @throws DecryptionException If the database key could not be loaded and
|
||||
* decrypted.
|
||||
* @return true if the database key was successfully loaded, re-encrypted
|
||||
* and stored.
|
||||
*/
|
||||
void changePassword(String oldPassword, String newPassword)
|
||||
throws DecryptionException;
|
||||
boolean changePassword(String oldPassword, String newPassword);
|
||||
}
|
||||
|
||||
@@ -142,17 +142,16 @@ public interface CryptoComponent {
|
||||
/**
|
||||
* Decrypts and authenticates the given ciphertext that has been read from
|
||||
* storage. The encryption and authentication keys are derived from the
|
||||
* given password.
|
||||
* given password. Returns null if the ciphertext cannot be decrypted and
|
||||
* authenticated (for example, if the password is wrong).
|
||||
*
|
||||
* @param keyStrengthener Used to strengthen the password-based key. If
|
||||
* null, or if strengthening was not used when encrypting the ciphertext,
|
||||
* the password-based key will not be strengthened
|
||||
* @throws DecryptionException If the ciphertext cannot be decrypted and
|
||||
* authenticated (for example, if the password is wrong).
|
||||
*/
|
||||
@Nullable
|
||||
byte[] decryptWithPassword(byte[] ciphertext, String password,
|
||||
@Nullable KeyStrengthener keyStrengthener)
|
||||
throws DecryptionException;
|
||||
@Nullable KeyStrengthener keyStrengthener);
|
||||
|
||||
/**
|
||||
* Returns true if the given ciphertext was encrypted using a strengthened
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
@NotNullByDefault
|
||||
public class DecryptionException extends Exception {
|
||||
|
||||
private final DecryptionResult result;
|
||||
|
||||
public DecryptionException(DecryptionResult result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public DecryptionResult getDecryptionResult() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package org.briarproject.bramble.api.crypto;
|
||||
|
||||
/**
|
||||
* The result of a password-based decryption operation.
|
||||
*/
|
||||
public enum DecryptionResult {
|
||||
|
||||
/**
|
||||
* Decryption succeeded.
|
||||
*/
|
||||
SUCCESS,
|
||||
|
||||
/**
|
||||
* Decryption failed because the format of the ciphertext was invalid.
|
||||
*/
|
||||
INVALID_CIPHERTEXT,
|
||||
|
||||
/**
|
||||
* Decryption failed because the {@link KeyStrengthener} used for
|
||||
* encryption was not available for decryption.
|
||||
*/
|
||||
KEY_STRENGTHENER_ERROR,
|
||||
|
||||
/**
|
||||
* Decryption failed because the password used for decryption did not match
|
||||
* the password used for encryption.
|
||||
*/
|
||||
INVALID_PASSWORD
|
||||
}
|
||||
@@ -18,6 +18,8 @@ public interface EventBus {
|
||||
/**
|
||||
* Asynchronously notifies all listeners of an event. Listeners are
|
||||
* notified on the {@link EventExecutor}.
|
||||
* <p>
|
||||
* This method can safely be called while holding a lock.
|
||||
*/
|
||||
void broadcast(Event e);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,4 @@ public interface BluetoothConstants {
|
||||
|
||||
String PROP_ADDRESS = "address";
|
||||
String PROP_UUID = "uuid";
|
||||
|
||||
String PREF_BT_ENABLE = "enable";
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@ import org.briarproject.bramble.api.contact.PendingContactId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
||||
|
||||
@@ -20,15 +21,15 @@ public interface ConnectionRegistry {
|
||||
/**
|
||||
* Registers a connection with the given contact over the given transport.
|
||||
* Broadcasts {@link ConnectionOpenedEvent}. Also broadcasts
|
||||
* {@link ConnectionStatusChangedEvent} if this is the only connection with
|
||||
* the contact.
|
||||
* {@link ContactConnectedEvent} if this is the only connection with the
|
||||
* contact.
|
||||
*/
|
||||
void registerConnection(ContactId c, TransportId t, boolean incoming);
|
||||
|
||||
/**
|
||||
* Unregisters a connection with the given contact over the given transport.
|
||||
* Broadcasts {@link ConnectionClosedEvent}. Also broadcasts
|
||||
* {@link ConnectionStatusChangedEvent} if this is the only connection with
|
||||
* {@link ContactDisconnectedEvent} if this is the only connection with
|
||||
* the contact.
|
||||
*/
|
||||
void unregisterConnection(ContactId c, TransportId t, boolean incoming);
|
||||
@@ -44,9 +45,9 @@ public interface ConnectionRegistry {
|
||||
boolean isConnected(ContactId c, TransportId t);
|
||||
|
||||
/**
|
||||
* Returns the connection status of the given contact via all transports.
|
||||
* Returns true if the given contact is connected via any transport.
|
||||
*/
|
||||
ConnectionStatus getConnectionStatus(ContactId c);
|
||||
boolean isConnected(ContactId c);
|
||||
|
||||
/**
|
||||
* Registers a connection with the given pending contact. Broadcasts
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.briarproject.bramble.api.plugin;
|
||||
|
||||
public enum ConnectionStatus {
|
||||
CONNECTED, RECENTLY_CONNECTED, DISCONNECTED
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package org.briarproject.bramble.api.plugin;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
|
||||
/**
|
||||
* An interface for handling peers discovered by transport plugins.
|
||||
*/
|
||||
@NotNullByDefault
|
||||
public interface DiscoveryHandler {
|
||||
|
||||
/**
|
||||
* Handles a peer discovered by a transport plugin.
|
||||
*
|
||||
* @param p A set of properties describing the discovered peer.
|
||||
*/
|
||||
void handleDevice(TransportProperties p);
|
||||
}
|
||||
@@ -3,12 +3,55 @@ package org.briarproject.bramble.api.plugin;
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface Plugin {
|
||||
|
||||
enum State {
|
||||
|
||||
/**
|
||||
* The plugin has not finished starting or has been stopped.
|
||||
*/
|
||||
STARTING_STOPPING,
|
||||
|
||||
/**
|
||||
* The plugin is disabled by settings. Use {@link #getReasonsDisabled()}
|
||||
* to find out which settings are responsible.
|
||||
*/
|
||||
DISABLED,
|
||||
|
||||
/**
|
||||
* The plugin is being enabled and can't yet make or receive
|
||||
* connections.
|
||||
*/
|
||||
ENABLING,
|
||||
|
||||
/**
|
||||
* The plugin is enabled and can make or receive connections.
|
||||
*/
|
||||
ACTIVE,
|
||||
|
||||
/**
|
||||
* The plugin is enabled but can't make or receive connections
|
||||
*/
|
||||
INACTIVE
|
||||
}
|
||||
|
||||
/**
|
||||
* The string for the boolean preference
|
||||
* to use with the {@link SettingsManager} to enable or disable the plugin.
|
||||
*/
|
||||
String PREF_PLUGIN_ENABLE = "enable";
|
||||
|
||||
/**
|
||||
* Reason flag returned by {@link #getReasonsDisabled()} to indicate that
|
||||
* the plugin has been disabled by the user.
|
||||
*/
|
||||
int REASON_USER = 1;
|
||||
|
||||
/**
|
||||
* Returns the plugin's transport identifier.
|
||||
*/
|
||||
@@ -35,9 +78,18 @@ public interface Plugin {
|
||||
void stop() throws PluginException;
|
||||
|
||||
/**
|
||||
* Returns true if the plugin is running.
|
||||
* Returns the current state of the plugin.
|
||||
*/
|
||||
boolean isRunning();
|
||||
State getState();
|
||||
|
||||
/**
|
||||
* Returns a set of flags indicating why the plugin is
|
||||
* {@link State#DISABLED disabled}, or 0 if the plugin is not disabled.
|
||||
* <p>
|
||||
* The flags used are plugin-specific, except the generic flag
|
||||
* {@link #REASON_USER}, which may be used by any plugin.
|
||||
*/
|
||||
int getReasonsDisabled();
|
||||
|
||||
/**
|
||||
* Returns true if the plugin should be polled periodically to attempt to
|
||||
@@ -54,6 +106,5 @@ public interface Plugin {
|
||||
* Attempts to create connections using the given transport properties,
|
||||
* passing any created connections to the corresponding handlers.
|
||||
*/
|
||||
void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||
properties);
|
||||
void poll(List<Pair<TransportProperties, ConnectionHandler>> properties);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package org.briarproject.bramble.api.plugin;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
|
||||
@@ -32,12 +36,17 @@ public interface PluginCallback extends ConnectionHandler {
|
||||
void mergeLocalProperties(TransportProperties p);
|
||||
|
||||
/**
|
||||
* Signals that the transport is enabled.
|
||||
* Informs the callback of the plugin's current state.
|
||||
* <p>
|
||||
* If the current state is different from the previous state, the callback
|
||||
* will broadcast a {@link TransportStateEvent}. If the current state is
|
||||
* {@link State#ACTIVE} and the previous state was not
|
||||
* {@link State#ACTIVE}, the callback will broadcast a
|
||||
* {@link TransportActiveEvent}. If the current state is not
|
||||
* {@link State#ACTIVE} and the previous state was {@link State#ACTIVE},
|
||||
* the callback will broadcast a {@link TransportInactiveEvent}.
|
||||
* <p>
|
||||
* This method can safely be called while holding a lock.
|
||||
*/
|
||||
void transportEnabled();
|
||||
|
||||
/**
|
||||
* Signals that the transport is disabled.
|
||||
*/
|
||||
void transportDisabled();
|
||||
void pluginStateChanged(State state);
|
||||
}
|
||||
|
||||
@@ -41,4 +41,17 @@ public interface PluginManager {
|
||||
* Returns any duplex plugins that support rendezvous.
|
||||
*/
|
||||
Collection<DuplexPlugin> getRendezvousPlugins();
|
||||
|
||||
/**
|
||||
* Enables or disables the plugin
|
||||
* identified by the given {@link TransportId}.
|
||||
* <p>
|
||||
* Note that this applies the change asynchronously
|
||||
* and there are no order guarantees.
|
||||
* <p>
|
||||
* If no plugin with the given {@link TransportId} is registered,
|
||||
* this is a no-op.
|
||||
*/
|
||||
void setPluginEnabled(TransportId t, boolean enabled);
|
||||
|
||||
}
|
||||
|
||||
@@ -21,6 +21,21 @@ public interface TorConstants {
|
||||
int PREF_TOR_NETWORK_AUTOMATIC = 0;
|
||||
int PREF_TOR_NETWORK_WITHOUT_BRIDGES = 1;
|
||||
int PREF_TOR_NETWORK_WITH_BRIDGES = 2;
|
||||
// TODO: Remove when settings migration code is removed
|
||||
int PREF_TOR_NETWORK_NEVER = 3;
|
||||
|
||||
/**
|
||||
* Reason flag returned by {@link Plugin#getReasonsDisabled()}.
|
||||
*/
|
||||
int REASON_BATTERY = 2;
|
||||
|
||||
/**
|
||||
* Reason flag returned by {@link Plugin#getReasonsDisabled()}.
|
||||
*/
|
||||
int REASON_MOBILE_DATA = 4;
|
||||
|
||||
/**
|
||||
* Reason flag returned by {@link Plugin#getReasonsDisabled()}.
|
||||
*/
|
||||
int REASON_COUNTRY_BLOCKED = 8;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Plugin;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -15,8 +14,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
||||
public abstract class AbstractDuplexTransportConnection
|
||||
implements DuplexTransportConnection {
|
||||
|
||||
protected final TransportProperties remote = new TransportProperties();
|
||||
|
||||
private final Plugin plugin;
|
||||
private final Reader reader;
|
||||
private final Writer writer;
|
||||
@@ -47,11 +44,6 @@ public abstract class AbstractDuplexTransportConnection
|
||||
return writer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProperties getRemoteProperties() {
|
||||
return remote;
|
||||
}
|
||||
|
||||
private class Reader implements TransportConnectionReader {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
package org.briarproject.bramble.api.plugin.duplex;
|
||||
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||
import org.briarproject.bramble.api.plugin.DiscoveryHandler;
|
||||
import org.briarproject.bramble.api.plugin.Plugin;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
@@ -58,4 +62,16 @@ public interface DuplexPlugin extends Plugin {
|
||||
@Nullable
|
||||
RendezvousEndpoint createRendezvousEndpoint(KeyMaterialSource k,
|
||||
boolean alice, ConnectionHandler incoming);
|
||||
|
||||
/**
|
||||
* Returns true if the plugin supports peer discovery.
|
||||
*/
|
||||
boolean supportsDiscovery();
|
||||
|
||||
/**
|
||||
* Attempts to discover peers using the given transport properties, passing
|
||||
* any discovered peers to the corresponding handlers.
|
||||
*/
|
||||
void discoverPeers(List<Pair<TransportProperties, DiscoveryHandler>>
|
||||
properties);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.briarproject.bramble.api.plugin.duplex;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
|
||||
/**
|
||||
* An interface for reading and writing data over a duplex transport. The
|
||||
@@ -24,10 +23,4 @@ public interface DuplexTransportConnection {
|
||||
* for writing to the connection.
|
||||
*/
|
||||
TransportConnectionWriter getWriter();
|
||||
|
||||
/**
|
||||
* Returns a possibly empty set of {@link TransportProperties} describing
|
||||
* the remote peer.
|
||||
*/
|
||||
TransportProperties getRemoteProperties();
|
||||
}
|
||||
|
||||
@@ -3,31 +3,24 @@ package org.briarproject.bramble.api.plugin.event;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a contact's connection status changes.
|
||||
* An event that is broadcast when a contact connects that was not previously
|
||||
* connected via any transport.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class ConnectionStatusChangedEvent extends Event {
|
||||
public class ContactConnectedEvent extends Event {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final ConnectionStatus status;
|
||||
|
||||
public ConnectionStatusChangedEvent(ContactId contactId,
|
||||
ConnectionStatus status) {
|
||||
public ContactConnectedEvent(ContactId contactId) {
|
||||
this.contactId = contactId;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public ContactId getContactId() {
|
||||
return contactId;
|
||||
}
|
||||
|
||||
public ConnectionStatus getConnectionStatus() {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.briarproject.bramble.api.plugin.event;
|
||||
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a contact disconnects and is no longer
|
||||
* connected via any transport.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class ContactDisconnectedEvent extends Event {
|
||||
|
||||
private final ContactId contactId;
|
||||
|
||||
public ContactDisconnectedEvent(ContactId contactId) {
|
||||
this.contactId = contactId;
|
||||
}
|
||||
|
||||
public ContactId getContactId() {
|
||||
return contactId;
|
||||
}
|
||||
}
|
||||
@@ -2,20 +2,22 @@ package org.briarproject.bramble.api.plugin.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a transport is enabled.
|
||||
* An event that is broadcast when a plugin enters the {@link State#ACTIVE}
|
||||
* state.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class TransportEnabledEvent extends Event {
|
||||
public class TransportActiveEvent extends Event {
|
||||
|
||||
private final TransportId transportId;
|
||||
|
||||
public TransportEnabledEvent(TransportId transportId) {
|
||||
public TransportActiveEvent(TransportId transportId) {
|
||||
this.transportId = transportId;
|
||||
}
|
||||
|
||||
@@ -2,20 +2,22 @@ package org.briarproject.bramble.api.plugin.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a transport is disabled.
|
||||
* An event that is broadcast when a plugin leaves the {@link State#ACTIVE}
|
||||
* state.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class TransportDisabledEvent extends Event {
|
||||
public class TransportInactiveEvent extends Event {
|
||||
|
||||
private final TransportId transportId;
|
||||
|
||||
public TransportDisabledEvent(TransportId transportId) {
|
||||
public TransportInactiveEvent(TransportId transportId) {
|
||||
this.transportId = transportId;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.briarproject.bramble.api.plugin.event;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when the {@link State state} of a plugin changes.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class TransportStateEvent extends Event {
|
||||
|
||||
private final TransportId transportId;
|
||||
private final State state;
|
||||
|
||||
public TransportStateEvent(TransportId transportId, State state) {
|
||||
this.transportId = transportId;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public TransportId getTransportId() {
|
||||
return transportId;
|
||||
}
|
||||
|
||||
public State getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
@@ -11,28 +11,4 @@ public interface TransportPropertyConstants {
|
||||
* The maximum length of a property's key or value in UTF-8 bytes.
|
||||
*/
|
||||
int MAX_PROPERTY_LENGTH = 100;
|
||||
|
||||
/**
|
||||
* Message metadata key for the transport ID of a local or remote update,
|
||||
* as a BDF string.
|
||||
*/
|
||||
String MSG_KEY_TRANSPORT_ID = "transportId";
|
||||
|
||||
/**
|
||||
* Message metadata key for the version number of a local or remote update,
|
||||
* as a BDF long.
|
||||
*/
|
||||
String MSG_KEY_VERSION = "version";
|
||||
|
||||
/**
|
||||
* Message metadata key for whether an update is local or remote, as a BDF
|
||||
* boolean.
|
||||
*/
|
||||
String MSG_KEY_LOCAL = "local";
|
||||
|
||||
/**
|
||||
* Group metadata key for any discovered transport properties of the
|
||||
* contact, as a BDF dictionary.
|
||||
*/
|
||||
String GROUP_KEY_DISCOVERED = "discovered";
|
||||
}
|
||||
|
||||
@@ -34,14 +34,6 @@ public interface TransportPropertyManager {
|
||||
void addRemoteProperties(Transaction txn, ContactId c,
|
||||
Map<TransportId, TransportProperties> props) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores the given properties discovered from an incoming transport
|
||||
* connection. They will be overridden by any properties received while
|
||||
* adding the contact or synced from the contact.
|
||||
*/
|
||||
void addRemotePropertiesFromConnection(ContactId c, TransportId t,
|
||||
TransportProperties props) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the local transport properties for all transports.
|
||||
*/
|
||||
|
||||
@@ -117,10 +117,4 @@ public class IoUtils {
|
||||
throw new IOException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isNonEmptyDirectory(File f) {
|
||||
if (!f.isDirectory()) return false;
|
||||
File[] children = f.listFiles();
|
||||
return children != null && children.length > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.bramble.account;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
||||
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
@@ -18,7 +17,6 @@ import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
@@ -26,7 +24,6 @@ import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_CIPHERTEXT;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
@@ -98,7 +95,7 @@ class AccountManagerImpl implements AccountManager {
|
||||
}
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||
new FileInputStream(f), Charset.forName("UTF-8")));
|
||||
new FileInputStream(f), "UTF-8"));
|
||||
String key = reader.readLine();
|
||||
reader.close();
|
||||
return key;
|
||||
@@ -150,7 +147,7 @@ class AccountManagerImpl implements AccountManager {
|
||||
@GuardedBy("stateChangeLock")
|
||||
private void writeDbKeyToFile(String key, File f) throws IOException {
|
||||
FileOutputStream out = new FileOutputStream(f);
|
||||
out.write(key.getBytes(Charset.forName("UTF-8")));
|
||||
out.write(key.getBytes("UTF-8"));
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
@@ -158,7 +155,8 @@ class AccountManagerImpl implements AccountManager {
|
||||
@Override
|
||||
public boolean accountExists() {
|
||||
synchronized (stateChangeLock) {
|
||||
return loadEncryptedDatabaseKey() != null;
|
||||
return loadEncryptedDatabaseKey() != null
|
||||
&& databaseConfig.getDatabaseDirectory().isDirectory();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -195,24 +193,31 @@ class AccountManagerImpl implements AccountManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void signIn(String password) throws DecryptionException {
|
||||
public boolean signIn(String password) {
|
||||
synchronized (stateChangeLock) {
|
||||
databaseKey = loadAndDecryptDatabaseKey(password);
|
||||
SecretKey key = loadAndDecryptDatabaseKey(password);
|
||||
if (key == null) return false;
|
||||
databaseKey = key;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@GuardedBy("stateChangeLock")
|
||||
private SecretKey loadAndDecryptDatabaseKey(String password)
|
||||
throws DecryptionException {
|
||||
@Nullable
|
||||
private SecretKey loadAndDecryptDatabaseKey(String password) {
|
||||
String hex = loadEncryptedDatabaseKey();
|
||||
if (hex == null) {
|
||||
LOG.warning("Failed to load encrypted database key");
|
||||
throw new DecryptionException(INVALID_CIPHERTEXT);
|
||||
return null;
|
||||
}
|
||||
byte[] ciphertext = fromHexString(hex);
|
||||
KeyStrengthener keyStrengthener = databaseConfig.getKeyStrengthener();
|
||||
byte[] plaintext = crypto.decryptWithPassword(ciphertext, password,
|
||||
keyStrengthener);
|
||||
if (plaintext == null) {
|
||||
LOG.info("Failed to decrypt database key");
|
||||
return null;
|
||||
}
|
||||
SecretKey key = new SecretKey(plaintext);
|
||||
// If the DB key was encrypted with a weak key and a key strengthener
|
||||
// is now available, re-encrypt the DB key with a strengthened key
|
||||
@@ -225,11 +230,10 @@ class AccountManagerImpl implements AccountManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changePassword(String oldPassword, String newPassword)
|
||||
throws DecryptionException {
|
||||
public boolean changePassword(String oldPassword, String newPassword) {
|
||||
synchronized (stateChangeLock) {
|
||||
SecretKey key = loadAndDecryptDatabaseKey(oldPassword);
|
||||
encryptAndStoreDatabaseKey(key, newPassword);
|
||||
return key != null && encryptAndStoreDatabaseKey(key, newPassword);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import net.i2p.crypto.eddsa.KeyPairGenerator;
|
||||
import org.briarproject.bramble.api.crypto.AgreementPrivateKey;
|
||||
import org.briarproject.bramble.api.crypto.AgreementPublicKey;
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
||||
import org.briarproject.bramble.api.crypto.KeyPair;
|
||||
import org.briarproject.bramble.api.crypto.KeyParser;
|
||||
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
||||
@@ -40,9 +39,6 @@ import static java.lang.System.arraycopy;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static org.briarproject.bramble.api.crypto.CryptoConstants.KEY_TYPE_AGREEMENT;
|
||||
import static org.briarproject.bramble.api.crypto.CryptoConstants.KEY_TYPE_SIGNATURE;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_CIPHERTEXT;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_PASSWORD;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.KEY_STRENGTHENER_ERROR;
|
||||
import static org.briarproject.bramble.util.ByteUtils.INT_32_BYTES;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
@@ -363,17 +359,16 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public byte[] decryptWithPassword(byte[] input, String password,
|
||||
@Nullable KeyStrengthener keyStrengthener)
|
||||
throws DecryptionException {
|
||||
@Nullable KeyStrengthener keyStrengthener) {
|
||||
AuthenticatedCipher cipher = new XSalsa20Poly1305AuthenticatedCipher();
|
||||
int macBytes = cipher.getMacBytes();
|
||||
// The input contains the format version, salt, cost parameter, IV,
|
||||
// ciphertext and MAC
|
||||
if (input.length < 1 + PBKDF_SALT_BYTES + INT_32_BYTES
|
||||
+ STORAGE_IV_BYTES + macBytes) {
|
||||
throw new DecryptionException(INVALID_CIPHERTEXT);
|
||||
}
|
||||
+ STORAGE_IV_BYTES + macBytes)
|
||||
return null; // Invalid input
|
||||
int inputOff = 0;
|
||||
// Format version
|
||||
byte formatVersion = input[inputOff];
|
||||
@@ -381,7 +376,7 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
// Check whether we support this format version
|
||||
if (formatVersion != PBKDF_FORMAT_SCRYPT &&
|
||||
formatVersion != PBKDF_FORMAT_SCRYPT_STRENGTHENED) {
|
||||
throw new DecryptionException(INVALID_CIPHERTEXT);
|
||||
return null;
|
||||
}
|
||||
// Salt
|
||||
byte[] salt = new byte[PBKDF_SALT_BYTES];
|
||||
@@ -390,9 +385,8 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
// Cost parameter
|
||||
long cost = ByteUtils.readUint32(input, inputOff);
|
||||
inputOff += INT_32_BYTES;
|
||||
if (cost < 2 || cost > Integer.MAX_VALUE) {
|
||||
throw new DecryptionException(INVALID_CIPHERTEXT);
|
||||
}
|
||||
if (cost < 2 || cost > Integer.MAX_VALUE)
|
||||
return null; // Invalid cost parameter
|
||||
// IV
|
||||
byte[] iv = new byte[STORAGE_IV_BYTES];
|
||||
arraycopy(input, inputOff, iv, 0, iv.length);
|
||||
@@ -400,10 +394,8 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
// Derive the decryption key from the password
|
||||
SecretKey key = passwordBasedKdf.deriveKey(password, salt, (int) cost);
|
||||
if (formatVersion == PBKDF_FORMAT_SCRYPT_STRENGTHENED) {
|
||||
if (keyStrengthener == null || !keyStrengthener.isInitialised()) {
|
||||
// Can't derive the same strengthened key
|
||||
throw new DecryptionException(KEY_STRENGTHENER_ERROR);
|
||||
}
|
||||
if (keyStrengthener == null || !keyStrengthener.isInitialised())
|
||||
return null; // Can't derive the same strengthened key
|
||||
key = keyStrengthener.strengthenKey(key);
|
||||
}
|
||||
// Initialise the cipher
|
||||
@@ -419,7 +411,7 @@ class CryptoComponentImpl implements CryptoComponent {
|
||||
cipher.process(input, inputOff, inputLen, output, 0);
|
||||
return output;
|
||||
} catch (GeneralSecurityException e) {
|
||||
throw new DecryptionException(INVALID_PASSWORD);
|
||||
return null; // Invalid ciphertext
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@ import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.IoUtils.isNonEmptyDirectory;
|
||||
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
||||
|
||||
/**
|
||||
@@ -70,9 +69,8 @@ class H2Database extends JdbcDatabase {
|
||||
LOG.info("Contents of account directory before opening DB:");
|
||||
logFileOrDir(LOG, INFO, dir.getParentFile());
|
||||
}
|
||||
boolean reopen = isNonEmptyDirectory(dir);
|
||||
boolean reopen = !dir.mkdirs();
|
||||
if (LOG.isLoggable(INFO)) LOG.info("Reopening DB: " + reopen);
|
||||
if (!reopen && dir.mkdirs()) LOG.info("Created database directory");
|
||||
super.open("org.h2.Driver", reopen, key, listener);
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Contents of account directory after opening DB:");
|
||||
|
||||
@@ -20,11 +20,9 @@ import java.util.logging.Logger;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.db.JdbcUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.IoUtils.isNonEmptyDirectory;
|
||||
|
||||
/**
|
||||
* Contains all the HSQLDB-specific code for the database.
|
||||
@@ -66,10 +64,7 @@ class HyperSqlDatabase extends JdbcDatabase {
|
||||
public boolean open(SecretKey key, @Nullable MigrationListener listener)
|
||||
throws DbException {
|
||||
this.key = key;
|
||||
File dir = config.getDatabaseDirectory();
|
||||
boolean reopen = isNonEmptyDirectory(dir);
|
||||
if (LOG.isLoggable(INFO)) LOG.info("Reopening DB: " + reopen);
|
||||
if (!reopen && dir.mkdirs()) LOG.info("Created database directory");
|
||||
boolean reopen = !config.getDatabaseDirectory().mkdirs();
|
||||
super.open("org.hsqldb.jdbc.JDBCDriver", reopen, key, listener);
|
||||
return reopen;
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@ import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||
import org.briarproject.bramble.api.sync.SyncSession;
|
||||
import org.briarproject.bramble.api.sync.SyncSessionFactory;
|
||||
import org.briarproject.bramble.api.transport.KeyManager;
|
||||
@@ -54,7 +52,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
private final HandshakeManager handshakeManager;
|
||||
private final ContactExchangeManager contactExchangeManager;
|
||||
private final ConnectionRegistry connectionRegistry;
|
||||
private final TransportPropertyManager transportPropertyManager;
|
||||
|
||||
@Inject
|
||||
ConnectionManagerImpl(@IoExecutor Executor ioExecutor,
|
||||
@@ -63,8 +60,7 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
SyncSessionFactory syncSessionFactory,
|
||||
HandshakeManager handshakeManager,
|
||||
ContactExchangeManager contactExchangeManager,
|
||||
ConnectionRegistry connectionRegistry,
|
||||
TransportPropertyManager transportPropertyManager) {
|
||||
ConnectionRegistry connectionRegistry) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.keyManager = keyManager;
|
||||
this.streamReaderFactory = streamReaderFactory;
|
||||
@@ -73,7 +69,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
this.handshakeManager = handshakeManager;
|
||||
this.contactExchangeManager = contactExchangeManager;
|
||||
this.connectionRegistry = connectionRegistry;
|
||||
this.transportPropertyManager = transportPropertyManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -274,7 +269,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
private final TransportId transportId;
|
||||
private final TransportConnectionReader reader;
|
||||
private final TransportConnectionWriter writer;
|
||||
private final TransportProperties remote;
|
||||
|
||||
@Nullable
|
||||
private volatile SyncSession outgoingSession = null;
|
||||
@@ -284,7 +278,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
this.transportId = transportId;
|
||||
reader = connection.getReader();
|
||||
writer = connection.getWriter();
|
||||
remote = connection.getRemoteProperties();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -320,16 +313,13 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
// Start the outgoing session on another thread
|
||||
ioExecutor.execute(() -> runOutgoingSession(contactId));
|
||||
try {
|
||||
// Store any transport properties discovered from the connection
|
||||
transportPropertyManager.addRemotePropertiesFromConnection(
|
||||
contactId, transportId, remote);
|
||||
// Create and run the incoming session
|
||||
createIncomingSession(ctx, reader).run();
|
||||
reader.dispose(false, true);
|
||||
// Interrupt the outgoing session so it finishes cleanly
|
||||
SyncSession out = outgoingSession;
|
||||
if (out != null) out.interrupt();
|
||||
} catch (DbException | IOException e) {
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
onReadError(true);
|
||||
} finally {
|
||||
@@ -385,7 +375,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
private final TransportId transportId;
|
||||
private final TransportConnectionReader reader;
|
||||
private final TransportConnectionWriter writer;
|
||||
private final TransportProperties remote;
|
||||
|
||||
@Nullable
|
||||
private volatile SyncSession outgoingSession = null;
|
||||
@@ -396,7 +385,6 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
this.transportId = transportId;
|
||||
reader = connection.getReader();
|
||||
writer = connection.getWriter();
|
||||
remote = connection.getRemoteProperties();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -473,16 +461,13 @@ class ConnectionManagerImpl implements ConnectionManager {
|
||||
connectionRegistry.registerConnection(contactId, transportId,
|
||||
false);
|
||||
try {
|
||||
// Store any transport properties discovered from the connection
|
||||
transportPropertyManager.addRemotePropertiesFromConnection(
|
||||
contactId, transportId, remote);
|
||||
// Create and run the incoming session
|
||||
createIncomingSession(ctx, reader).run();
|
||||
reader.dispose(false, true);
|
||||
// Interrupt the outgoing session so it finishes cleanly
|
||||
SyncSession out = outgoingSession;
|
||||
if (out != null) out.interrupt();
|
||||
} catch (DbException | IOException e) {
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
onReadError();
|
||||
} finally {
|
||||
|
||||
@@ -6,41 +6,30 @@ import org.briarproject.bramble.api.contact.PendingContactId;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.api.system.Scheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.CONNECTED;
|
||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.DISCONNECTED;
|
||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.RECENTLY_CONNECTED;
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
@@ -49,30 +38,22 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
private static final Logger LOG =
|
||||
getLogger(ConnectionRegistryImpl.class.getName());
|
||||
|
||||
private static final long RECENTLY_CONNECTED_MS = MINUTES.toMillis(1);
|
||||
private static final long EXPIRY_INTERVAL_MS = SECONDS.toMillis(10);
|
||||
|
||||
private final EventBus eventBus;
|
||||
private final Clock clock;
|
||||
|
||||
private final Object lock = new Object();
|
||||
@GuardedBy("lock")
|
||||
private final Map<TransportId, Multiset<ContactId>> contactConnections;
|
||||
@GuardedBy("lock")
|
||||
private final Map<ContactId, Counter> contactCounts;
|
||||
private final Multiset<ContactId> contactCounts;
|
||||
@GuardedBy("lock")
|
||||
private final Set<PendingContactId> connectedPendingContacts;
|
||||
|
||||
@Inject
|
||||
ConnectionRegistryImpl(EventBus eventBus, Clock clock,
|
||||
@Scheduler ScheduledExecutorService scheduler) {
|
||||
ConnectionRegistryImpl(EventBus eventBus) {
|
||||
this.eventBus = eventBus;
|
||||
this.clock = clock;
|
||||
contactConnections = new HashMap<>();
|
||||
contactCounts = new HashMap<>();
|
||||
contactCounts = new Multiset<>();
|
||||
connectedPendingContacts = new HashSet<>();
|
||||
scheduler.scheduleWithFixedDelay(this::expireRecentConnections,
|
||||
EXPIRY_INTERVAL_MS, EXPIRY_INTERVAL_MS, MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -90,22 +71,12 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
contactConnections.put(t, m);
|
||||
}
|
||||
m.add(c);
|
||||
|
||||
Counter counter = contactCounts.get(c);
|
||||
if (counter == null) {
|
||||
counter = new Counter();
|
||||
contactCounts.put(c, counter);
|
||||
}
|
||||
if (counter.connections == 0) {
|
||||
counter.disconnectedTime = 0;
|
||||
firstConnection = true;
|
||||
}
|
||||
counter.connections++;
|
||||
if (contactCounts.add(c) == 1) firstConnection = true;
|
||||
}
|
||||
eventBus.broadcast(new ConnectionOpenedEvent(c, t, incoming));
|
||||
if (firstConnection) {
|
||||
LOG.info("Contact connected");
|
||||
eventBus.broadcast(new ConnectionStatusChangedEvent(c, CONNECTED));
|
||||
eventBus.broadcast(new ContactConnectedEvent(c));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,22 +93,12 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
if (m == null || !m.contains(c))
|
||||
throw new IllegalArgumentException();
|
||||
m.remove(c);
|
||||
|
||||
Counter counter = contactCounts.get(c);
|
||||
if (counter == null || counter.connections == 0) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
counter.connections--;
|
||||
if (counter.connections == 0) {
|
||||
counter.disconnectedTime = clock.currentTimeMillis();
|
||||
lastConnection = true;
|
||||
}
|
||||
if (contactCounts.remove(c) == 0) lastConnection = true;
|
||||
}
|
||||
eventBus.broadcast(new ConnectionClosedEvent(c, t, incoming));
|
||||
if (lastConnection) {
|
||||
LOG.info("Contact disconnected");
|
||||
eventBus.broadcast(
|
||||
new ConnectionStatusChangedEvent(c, RECENTLY_CONNECTED));
|
||||
eventBus.broadcast(new ContactDisconnectedEvent(c));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +106,7 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
public Collection<ContactId> getConnectedContacts(TransportId t) {
|
||||
synchronized (lock) {
|
||||
Multiset<ContactId> m = contactConnections.get(t);
|
||||
if (m == null) return emptyList();
|
||||
if (m == null) return Collections.emptyList();
|
||||
List<ContactId> ids = new ArrayList<>(m.keySet());
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info(ids.size() + " contacts connected: " + t);
|
||||
@@ -162,11 +123,9 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ConnectionStatus getConnectionStatus(ContactId c) {
|
||||
public boolean isConnected(ContactId c) {
|
||||
synchronized (lock) {
|
||||
Counter counter = contactCounts.get(c);
|
||||
if (counter == null) return DISCONNECTED;
|
||||
return counter.connections > 0 ? CONNECTED : RECENTLY_CONNECTED;
|
||||
return contactCounts.contains(c);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -188,36 +147,4 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
||||
}
|
||||
eventBus.broadcast(new RendezvousConnectionClosedEvent(p, success));
|
||||
}
|
||||
|
||||
@Scheduler
|
||||
private void expireRecentConnections() {
|
||||
long now = clock.currentTimeMillis();
|
||||
List<ContactId> disconnected = new ArrayList<>();
|
||||
synchronized (lock) {
|
||||
Iterator<Entry<ContactId, Counter>> it =
|
||||
contactCounts.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Entry<ContactId, Counter> e = it.next();
|
||||
if (e.getValue().isExpired(now)) {
|
||||
disconnected.add(e.getKey());
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
for (ContactId c : disconnected) {
|
||||
eventBus.broadcast(
|
||||
new ConnectionStatusChangedEvent(c, DISCONNECTED));
|
||||
}
|
||||
}
|
||||
|
||||
private static class Counter {
|
||||
|
||||
private int connections = 0;
|
||||
private long disconnectedTime = 0;
|
||||
|
||||
private boolean isExpired(long now) {
|
||||
return connections == 0 &&
|
||||
now - disconnectedTime > RECENTLY_CONNECTED_MS;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.briarproject.bramble.api.lifecycle.ServiceException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionManager;
|
||||
import org.briarproject.bramble.api.plugin.Plugin;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.PluginConfig;
|
||||
import org.briarproject.bramble.api.plugin.PluginException;
|
||||
@@ -18,8 +19,9 @@ import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
@@ -36,6 +38,7 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
@@ -45,6 +48,9 @@ import static java.util.logging.Level.FINE;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.PREF_PLUGIN_ENABLE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
@@ -177,6 +183,26 @@ class PluginManagerImpl implements PluginManager, Service {
|
||||
return supported;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPluginEnabled(TransportId t, boolean enabled) {
|
||||
Plugin plugin = plugins.get(t);
|
||||
if (plugin == null) return;
|
||||
|
||||
Settings s = new Settings();
|
||||
s.putBoolean(PREF_PLUGIN_ENABLE, enabled);
|
||||
ioExecutor.execute(() -> mergeSettings(s, t.getString()));
|
||||
}
|
||||
|
||||
private void mergeSettings(Settings s, String namespace) {
|
||||
try {
|
||||
long start = now();
|
||||
settingsManager.mergeSettings(s, namespace);
|
||||
logDuration(LOG, "Merging settings", start);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
|
||||
private class PluginStarter implements Runnable {
|
||||
|
||||
private final Plugin plugin;
|
||||
@@ -250,7 +276,8 @@ class PluginManagerImpl implements PluginManager, Service {
|
||||
private class Callback implements PluginCallback {
|
||||
|
||||
private final TransportId id;
|
||||
private final AtomicBoolean enabled = new AtomicBoolean(false);
|
||||
private final AtomicReference<State> state =
|
||||
new AtomicReference<>(STARTING_STOPPING);
|
||||
|
||||
private Callback(TransportId id) {
|
||||
this.id = id;
|
||||
@@ -278,11 +305,7 @@ class PluginManagerImpl implements PluginManager, Service {
|
||||
|
||||
@Override
|
||||
public void mergeSettings(Settings s) {
|
||||
try {
|
||||
settingsManager.mergeSettings(s, id.getString());
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
PluginManagerImpl.this.mergeSettings(s, id.getString());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -295,15 +318,20 @@ class PluginManagerImpl implements PluginManager, Service {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transportEnabled() {
|
||||
if (!enabled.getAndSet(true))
|
||||
eventBus.broadcast(new TransportEnabledEvent(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transportDisabled() {
|
||||
if (enabled.getAndSet(false))
|
||||
eventBus.broadcast(new TransportDisabledEvent(id));
|
||||
public void pluginStateChanged(State newState) {
|
||||
State oldState = state.getAndSet(newState);
|
||||
if (newState != oldState) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info(id + " changed from state " + oldState
|
||||
+ " to " + newState);
|
||||
}
|
||||
eventBus.broadcast(new TransportStateEvent(id, newState));
|
||||
if (newState == ACTIVE) {
|
||||
eventBus.broadcast(new TransportActiveEvent(id));
|
||||
} else if (oldState == ACTIVE) {
|
||||
eventBus.broadcast(new TransportInactiveEvent(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -11,6 +11,7 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionManager;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.plugin.DiscoveryHandler;
|
||||
import org.briarproject.bramble.api.plugin.Plugin;
|
||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
@@ -20,8 +21,8 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||
@@ -32,6 +33,7 @@ import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.Executor;
|
||||
@@ -106,13 +108,13 @@ class PollerImpl implements Poller, EventListener {
|
||||
ConnectionOpenedEvent c = (ConnectionOpenedEvent) e;
|
||||
// Reschedule polling, the polling interval may have decreased
|
||||
reschedule(c.getTransportId());
|
||||
} else if (e instanceof TransportEnabledEvent) {
|
||||
TransportEnabledEvent t = (TransportEnabledEvent) e;
|
||||
// Poll the newly enabled transport
|
||||
} else if (e instanceof TransportActiveEvent) {
|
||||
TransportActiveEvent t = (TransportActiveEvent) e;
|
||||
// Poll the newly activated transport
|
||||
pollNow(t.getTransportId());
|
||||
} else if (e instanceof TransportDisabledEvent) {
|
||||
TransportDisabledEvent t = (TransportDisabledEvent) e;
|
||||
// Cancel polling for the disabled transport
|
||||
} else if (e instanceof TransportInactiveEvent) {
|
||||
TransportInactiveEvent t = (TransportInactiveEvent) e;
|
||||
// Cancel polling for the deactivated transport
|
||||
cancel(t.getTransportId());
|
||||
}
|
||||
}
|
||||
@@ -210,18 +212,20 @@ class PollerImpl implements Poller, EventListener {
|
||||
@IoExecutor
|
||||
private void poll(Plugin p) {
|
||||
TransportId t = p.getId();
|
||||
if (LOG.isLoggable(INFO)) LOG.info("Polling plugin " + t);
|
||||
if (LOG.isLoggable(INFO)) LOG.info("Polling " + t);
|
||||
try {
|
||||
Map<ContactId, TransportProperties> remote =
|
||||
transportPropertyManager.getRemoteProperties(t);
|
||||
Collection<ContactId> connected =
|
||||
connectionRegistry.getConnectedContacts(t);
|
||||
Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||
properties = new ArrayList<>();
|
||||
List<Pair<TransportProperties, ConnectionHandler>> properties =
|
||||
new ArrayList<>();
|
||||
for (Entry<ContactId, TransportProperties> e : remote.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
if (!connected.contains(c))
|
||||
properties.add(new Pair<>(e.getValue(), new Handler(c, t)));
|
||||
if (!connected.contains(c)) {
|
||||
ConnHandler handler = new ConnHandler(c, t);
|
||||
properties.add(new Pair<>(e.getValue(), handler));
|
||||
}
|
||||
}
|
||||
if (!properties.isEmpty()) p.poll(properties);
|
||||
} catch (DbException e) {
|
||||
@@ -229,6 +233,30 @@ class PollerImpl implements Poller, EventListener {
|
||||
}
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
private void discover(DuplexPlugin p) {
|
||||
TransportId t = p.getId();
|
||||
if (LOG.isLoggable(INFO)) LOG.info("Discovering peers for " + t);
|
||||
try {
|
||||
Map<ContactId, TransportProperties> remote =
|
||||
transportPropertyManager.getRemoteProperties(t);
|
||||
Collection<ContactId> connected =
|
||||
connectionRegistry.getConnectedContacts(t);
|
||||
List<Pair<TransportProperties, DiscoveryHandler>> properties =
|
||||
new ArrayList<>();
|
||||
for (Entry<ContactId, TransportProperties> e : remote.entrySet()) {
|
||||
ContactId c = e.getKey();
|
||||
if (!connected.contains(c)) {
|
||||
DiscoHandler handler = new DiscoHandler(c, p);
|
||||
properties.add(new Pair<>(e.getValue(), handler));
|
||||
}
|
||||
}
|
||||
if (!properties.isEmpty()) p.discoverPeers(properties);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
|
||||
private class ScheduledPollTask {
|
||||
|
||||
private final PollTask task;
|
||||
@@ -268,16 +296,23 @@ class PollerImpl implements Poller, EventListener {
|
||||
int delay = plugin.getPollingInterval();
|
||||
if (randomiseNext) delay = (int) (delay * random.nextDouble());
|
||||
schedule(plugin, delay, false);
|
||||
poll(plugin);
|
||||
// FIXME: Revert
|
||||
if (plugin instanceof DuplexPlugin) {
|
||||
DuplexPlugin d = (DuplexPlugin) plugin;
|
||||
if (d.supportsDiscovery()) discover(d);
|
||||
else poll(d);
|
||||
} else {
|
||||
poll(plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class Handler implements ConnectionHandler {
|
||||
private class ConnHandler implements ConnectionHandler {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final TransportId transportId;
|
||||
|
||||
private Handler(ContactId contactId, TransportId transportId) {
|
||||
private ConnHandler(ContactId contactId, TransportId transportId) {
|
||||
this.contactId = contactId;
|
||||
this.transportId = transportId;
|
||||
}
|
||||
@@ -300,4 +335,27 @@ class PollerImpl implements Poller, EventListener {
|
||||
transportId, w);
|
||||
}
|
||||
}
|
||||
|
||||
private class DiscoHandler implements DiscoveryHandler {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final DuplexPlugin plugin;
|
||||
|
||||
private DiscoHandler(ContactId contactId, DuplexPlugin plugin) {
|
||||
this.contactId = contactId;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleDevice(TransportProperties p) {
|
||||
LOG.info("Discovered contact via " + plugin.getId());
|
||||
ioExecutor.execute(() -> {
|
||||
DuplexTransportConnection c = plugin.createConnection(p);
|
||||
if (c != null) {
|
||||
connectionManager.manageOutgoingConnection(contactId,
|
||||
plugin.getId(), c);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,10 +9,13 @@ import org.briarproject.bramble.api.keyagreement.KeyAgreementConnection;
|
||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementListeningEvent;
|
||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementStoppedListeningEvent;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||
import org.briarproject.bramble.api.plugin.DiscoveryHandler;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.PluginException;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
@@ -29,23 +32,28 @@ import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.ID;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_BT_ENABLE;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_UUID;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.UUID_BYTES;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
@@ -68,9 +76,9 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
private final int maxLatency;
|
||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||
|
||||
private volatile boolean running = false, contactConnections = false;
|
||||
protected final PluginState state = new PluginState();
|
||||
|
||||
private volatile String contactConnectionsUuid = null;
|
||||
private volatile SS socket = null;
|
||||
|
||||
abstract void initialiseAdapter() throws IOException;
|
||||
|
||||
@@ -119,14 +127,18 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
LOG.info("Bluetooth enabled");
|
||||
// We may not have been able to get the local address before
|
||||
ioExecutor.execute(this::updateProperties);
|
||||
if (shouldAllowContactConnections()) bind();
|
||||
if (getState() == INACTIVE) bind();
|
||||
}
|
||||
|
||||
void onAdapterDisabled() {
|
||||
LOG.info("Bluetooth disabled");
|
||||
tryToClose(socket);
|
||||
connectionLimiter.allConnectionsClosed();
|
||||
callback.transportDisabled();
|
||||
// The server socket may not have been closed automatically
|
||||
SS ss = state.clearServerSocket();
|
||||
if (ss != null) {
|
||||
LOG.info("Closing server socket");
|
||||
tryToClose(ss);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -148,31 +160,24 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
@Override
|
||||
public void start() throws PluginException {
|
||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||
Settings settings = callback.getSettings();
|
||||
boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE, false);
|
||||
state.setStarted(enabledByUser);
|
||||
try {
|
||||
initialiseAdapter();
|
||||
} catch (IOException e) {
|
||||
throw new PluginException(e);
|
||||
}
|
||||
updateProperties();
|
||||
running = true;
|
||||
loadSettings(callback.getSettings());
|
||||
if (shouldAllowContactConnections()) {
|
||||
if (enabledByUser) {
|
||||
if (isAdapterEnabled()) bind();
|
||||
else enableAdapter();
|
||||
}
|
||||
}
|
||||
|
||||
private void loadSettings(Settings settings) {
|
||||
contactConnections = settings.getBoolean(PREF_BT_ENABLE, false);
|
||||
}
|
||||
|
||||
private boolean shouldAllowContactConnections() {
|
||||
return contactConnections;
|
||||
}
|
||||
|
||||
private void bind() {
|
||||
ioExecutor.execute(() -> {
|
||||
if (!isRunning() || !shouldAllowContactConnections()) return;
|
||||
if (getState() != INACTIVE) return;
|
||||
// Bind a server socket to accept connections from contacts
|
||||
SS ss;
|
||||
try {
|
||||
@@ -181,14 +186,13 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
logException(LOG, WARNING, e);
|
||||
return;
|
||||
}
|
||||
if (!isRunning() || !shouldAllowContactConnections()) {
|
||||
if (!state.setServerSocket(ss)) {
|
||||
LOG.info("Closing redundant server socket");
|
||||
tryToClose(ss);
|
||||
return;
|
||||
}
|
||||
socket = ss;
|
||||
backoff.reset();
|
||||
callback.transportEnabled();
|
||||
acceptContactConnections();
|
||||
acceptContactConnections(ss);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -217,34 +221,39 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
if (changed) callback.mergeLocalProperties(p);
|
||||
}
|
||||
|
||||
private void acceptContactConnections() {
|
||||
private void acceptContactConnections(SS ss) {
|
||||
while (true) {
|
||||
DuplexTransportConnection conn;
|
||||
try {
|
||||
conn = acceptConnection(socket);
|
||||
conn = acceptConnection(ss);
|
||||
} catch (IOException e) {
|
||||
// This is expected when the socket is closed
|
||||
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
|
||||
// This is expected when the server socket is closed
|
||||
LOG.info("Server socket closed");
|
||||
state.clearServerSocket();
|
||||
return;
|
||||
}
|
||||
LOG.info("Connection received");
|
||||
backoff.reset();
|
||||
if (connectionLimiter.contactConnectionOpened(conn))
|
||||
callback.handleConnection(conn);
|
||||
if (!running) return;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
running = false;
|
||||
tryToClose(socket);
|
||||
callback.transportDisabled();
|
||||
SS ss = state.setStopped();
|
||||
tryToClose(ss);
|
||||
disableAdapterIfEnabledByUs();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return running && isAdapterEnabled();
|
||||
public State getState() {
|
||||
return state.getState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReasonsDisabled() {
|
||||
return state.getReasonsDisabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -258,9 +267,9 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||
properties) {
|
||||
if (!isRunning() || !shouldAllowContactConnections()) return;
|
||||
public void poll(
|
||||
List<Pair<TransportProperties, ConnectionHandler>> properties) {
|
||||
if (getState() != ACTIVE) return;
|
||||
backoff.increment();
|
||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||
connect(p.getFirst(), p.getSecond());
|
||||
@@ -273,7 +282,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
String uuid = p.get(PROP_UUID);
|
||||
if (isNullOrEmpty(uuid)) return;
|
||||
ioExecutor.execute(() -> {
|
||||
if (!isRunning() || !shouldAllowContactConnections()) return;
|
||||
if (getState() != ACTIVE) return;
|
||||
if (!connectionLimiter.canOpenContactConnection()) return;
|
||||
DuplexTransportConnection d = createConnection(p);
|
||||
if (d != null) {
|
||||
@@ -317,7 +326,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
|
||||
@Override
|
||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||
if (!isRunning() || !shouldAllowContactConnections()) return null;
|
||||
if (getState() != ACTIVE) return null;
|
||||
if (!connectionLimiter.canOpenContactConnection()) return null;
|
||||
String address = p.get(PROP_ADDRESS);
|
||||
if (isNullOrEmpty(address)) return null;
|
||||
@@ -336,7 +345,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
|
||||
@Override
|
||||
public KeyAgreementListener createKeyAgreementListener(byte[] commitment) {
|
||||
if (!isRunning()) return null;
|
||||
if (getState() != ACTIVE) return null;
|
||||
// No truncation necessary because COMMIT_LENGTH = 16
|
||||
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
||||
if (LOG.isLoggable(INFO)) LOG.info("Key agreement UUID " + uuid);
|
||||
@@ -348,7 +357,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
logException(LOG, WARNING, e);
|
||||
return null;
|
||||
}
|
||||
if (!isRunning()) {
|
||||
if (getState() != ACTIVE) {
|
||||
tryToClose(ss);
|
||||
return null;
|
||||
}
|
||||
@@ -362,7 +371,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
@Override
|
||||
public DuplexTransportConnection createKeyAgreementConnection(
|
||||
byte[] commitment, BdfList descriptor) {
|
||||
if (!isRunning()) return null;
|
||||
if (getState() != ACTIVE) return null;
|
||||
// No truncation necessary because COMMIT_LENGTH = 16
|
||||
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
||||
DuplexTransportConnection conn;
|
||||
@@ -403,6 +412,17 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsDiscovery() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discoverPeers(
|
||||
List<Pair<TransportProperties, DiscoveryHandler>> properties) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof EnableBluetoothEvent) {
|
||||
@@ -422,17 +442,17 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
}
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
private void onSettingsUpdated(Settings settings) {
|
||||
boolean wasAllowed = shouldAllowContactConnections();
|
||||
loadSettings(settings);
|
||||
boolean isAllowed = shouldAllowContactConnections();
|
||||
if (wasAllowed && !isAllowed) {
|
||||
LOG.info("Contact connections disabled");
|
||||
tryToClose(socket);
|
||||
callback.transportDisabled();
|
||||
boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE, false);
|
||||
SS ss = state.setEnabledByUser(enabledByUser);
|
||||
State s = getState();
|
||||
if (ss != null) {
|
||||
LOG.info("Disabled by user, closing server socket");
|
||||
tryToClose(ss);
|
||||
disableAdapterIfEnabledByUs();
|
||||
} else if (!wasAllowed && isAllowed) {
|
||||
LOG.info("Contact connections enabled");
|
||||
} else if (s == INACTIVE) {
|
||||
LOG.info("Enabled by user, opening server socket");
|
||||
if (isAdapterEnabled()) bind();
|
||||
else enableAdapter();
|
||||
}
|
||||
@@ -460,4 +480,70 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
||||
tryToClose(ss);
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
protected class PluginState {
|
||||
|
||||
@GuardedBy("this")
|
||||
private boolean started = false,
|
||||
stopped = false,
|
||||
enabledByUser = false;
|
||||
|
||||
@GuardedBy("this")
|
||||
@Nullable
|
||||
private SS serverSocket = null;
|
||||
|
||||
synchronized void setStarted(boolean enabledByUser) {
|
||||
started = true;
|
||||
this.enabledByUser = enabledByUser;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
synchronized SS setStopped() {
|
||||
stopped = true;
|
||||
SS ss = serverSocket;
|
||||
serverSocket = null;
|
||||
callback.pluginStateChanged(getState());
|
||||
return ss;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
synchronized SS setEnabledByUser(boolean enabledByUser) {
|
||||
this.enabledByUser = enabledByUser;
|
||||
SS ss = null;
|
||||
if (!enabledByUser) {
|
||||
ss = serverSocket;
|
||||
serverSocket = null;
|
||||
}
|
||||
callback.pluginStateChanged(getState());
|
||||
return ss;
|
||||
}
|
||||
|
||||
synchronized boolean setServerSocket(SS ss) {
|
||||
if (stopped || serverSocket != null) return false;
|
||||
serverSocket = ss;
|
||||
callback.pluginStateChanged(getState());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
synchronized SS clearServerSocket() {
|
||||
SS ss = serverSocket;
|
||||
serverSocket = null;
|
||||
callback.pluginStateChanged(getState());
|
||||
return ss;
|
||||
}
|
||||
|
||||
synchronized State getState() {
|
||||
if (!started || stopped) return STARTING_STOPPING;
|
||||
if (!enabledByUser) return DISABLED;
|
||||
return serverSocket == null ? INACTIVE : ACTIVE;
|
||||
}
|
||||
|
||||
synchronized int getReasonsDisabled() {
|
||||
return getState() == DISABLED ? REASON_USER : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.util.logging.Logger;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.FileConstants.PROP_PATH;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
|
||||
@@ -45,7 +46,7 @@ abstract class FilePlugin implements SimplexPlugin {
|
||||
|
||||
@Override
|
||||
public TransportConnectionReader createReader(TransportProperties p) {
|
||||
if (!isRunning()) return null;
|
||||
if (getState() != ACTIVE) return null;
|
||||
String path = p.get(PROP_PATH);
|
||||
if (isNullOrEmpty(path)) return null;
|
||||
try {
|
||||
@@ -60,7 +61,7 @@ abstract class FilePlugin implements SimplexPlugin {
|
||||
|
||||
@Override
|
||||
public TransportConnectionWriter createWriter(TransportProperties p) {
|
||||
if (!isRunning()) return null;
|
||||
if (getState() != ACTIVE) return null;
|
||||
String path = p.get(PROP_PATH);
|
||||
if (isNullOrEmpty(path)) return null;
|
||||
try {
|
||||
|
||||
@@ -11,7 +11,6 @@ import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
@@ -41,6 +40,7 @@ import static org.briarproject.bramble.api.plugin.LanTcpConstants.PREF_LAN_IP_PO
|
||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_IP_PORTS;
|
||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_PORT;
|
||||
import static org.briarproject.bramble.util.ByteUtils.MAX_16_BIT_UNSIGNED;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
import static org.briarproject.bramble.util.StringUtils.join;
|
||||
@@ -91,7 +91,8 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
public void start() {
|
||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||
initialisePortProperty();
|
||||
running = true;
|
||||
Settings settings = callback.getSettings();
|
||||
state.setStarted(settings.getBoolean(PREF_PLUGIN_ENABLE, false));
|
||||
bind();
|
||||
}
|
||||
|
||||
@@ -271,10 +272,10 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Failed to bind " + scrubSocketAddress(addr));
|
||||
tryToClose(ss);
|
||||
tryToClose(ss, LOG, WARNING);
|
||||
}
|
||||
}
|
||||
if (ss == null || !ss.isBound()) {
|
||||
if (ss == null) {
|
||||
LOG.info("Could not bind server socket for key agreement");
|
||||
return null;
|
||||
}
|
||||
@@ -290,8 +291,8 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
@Override
|
||||
public DuplexTransportConnection createKeyAgreementConnection(
|
||||
byte[] commitment, BdfList descriptor) {
|
||||
if (!isRunning()) return null;
|
||||
ServerSocket ss = socket;
|
||||
ServerSocket ss = state.getServerSocket();
|
||||
if (ss == null) return null;
|
||||
InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress());
|
||||
if (local == null) {
|
||||
LOG.warning("No interface for key agreement server socket");
|
||||
@@ -363,7 +364,7 @@ class LanTcpPlugin extends TcpPlugin {
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
IoUtils.tryToClose(ss, LOG, WARNING);
|
||||
tryToClose(ss, LOG, WARNING);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.bramble.plugin.tcp;
|
||||
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
||||
@@ -26,11 +27,13 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
|
||||
private static final double BACKOFF_BASE = 1.2;
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final EventBus eventBus;
|
||||
private final BackoffFactory backoffFactory;
|
||||
|
||||
public LanTcpPluginFactory(Executor ioExecutor,
|
||||
public LanTcpPluginFactory(Executor ioExecutor, EventBus eventBus,
|
||||
BackoffFactory backoffFactory) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.eventBus = eventBus;
|
||||
this.backoffFactory = backoffFactory;
|
||||
}
|
||||
|
||||
@@ -48,7 +51,9 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
|
||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
return new LanTcpPlugin(ioExecutor, backoff, callback, MAX_LATENCY,
|
||||
LanTcpPlugin plugin = new LanTcpPlugin(ioExecutor, backoff, callback, MAX_LATENCY,
|
||||
MAX_IDLE_TIME, CONNECTION_TIMEOUT);
|
||||
eventBus.addListener(plugin);
|
||||
return plugin;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,19 +2,23 @@ package org.briarproject.bramble.plugin.tcp;
|
||||
|
||||
import org.briarproject.bramble.PoliteExecutor;
|
||||
import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||
import org.briarproject.bramble.api.plugin.DiscoveryHandler;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.api.settings.event.SettingsUpdatedEvent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
@@ -35,19 +39,26 @@ import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.list;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
abstract class TcpPlugin implements DuplexPlugin {
|
||||
abstract class TcpPlugin implements DuplexPlugin, EventListener {
|
||||
|
||||
private static final Logger LOG = getLogger(TcpPlugin.class.getName());
|
||||
|
||||
@@ -60,9 +71,7 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
protected final int maxLatency, maxIdleTime;
|
||||
protected final int connectionTimeout, socketTimeout;
|
||||
protected final AtomicBoolean used = new AtomicBoolean(false);
|
||||
|
||||
protected volatile boolean running = false;
|
||||
protected volatile ServerSocket socket = null;
|
||||
protected final PluginState state = new PluginState();
|
||||
|
||||
/**
|
||||
* Returns zero or more socket addresses on which the plugin should listen,
|
||||
@@ -118,14 +127,14 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
@Override
|
||||
public void start() {
|
||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||
running = true;
|
||||
Settings settings = callback.getSettings();
|
||||
state.setStarted(settings.getBoolean(PREF_PLUGIN_ENABLE, false));
|
||||
bind();
|
||||
}
|
||||
|
||||
protected void bind() {
|
||||
bindExecutor.execute(() -> {
|
||||
if (!running) return;
|
||||
if (socket != null && !socket.isClosed()) return;
|
||||
if (getState() != INACTIVE) return;
|
||||
ServerSocket ss = null;
|
||||
for (InetSocketAddress addr : getLocalSocketAddresses()) {
|
||||
try {
|
||||
@@ -135,34 +144,28 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
} catch (IOException e) {
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Failed to bind " + scrubSocketAddress(addr));
|
||||
tryToClose(ss);
|
||||
tryToClose(ss, LOG, WARNING);
|
||||
}
|
||||
}
|
||||
if (ss == null || !ss.isBound()) {
|
||||
if (ss == null) {
|
||||
LOG.info("Could not bind server socket");
|
||||
return;
|
||||
}
|
||||
if (!running) {
|
||||
tryToClose(ss);
|
||||
if (!state.setServerSocket(ss)) {
|
||||
LOG.info("Closing redundant server socket");
|
||||
tryToClose(ss, LOG, WARNING);
|
||||
return;
|
||||
}
|
||||
socket = ss;
|
||||
backoff.reset();
|
||||
InetSocketAddress local =
|
||||
(InetSocketAddress) ss.getLocalSocketAddress();
|
||||
setLocalSocketAddress(local);
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Listening on " + scrubSocketAddress(local));
|
||||
callback.transportEnabled();
|
||||
acceptContactConnections();
|
||||
acceptContactConnections(ss);
|
||||
});
|
||||
}
|
||||
|
||||
protected void tryToClose(@Nullable ServerSocket ss) {
|
||||
IoUtils.tryToClose(ss, LOG, WARNING);
|
||||
callback.transportDisabled();
|
||||
}
|
||||
|
||||
String getIpPortString(InetSocketAddress a) {
|
||||
String addr = a.getAddress().getHostAddress();
|
||||
int percent = addr.indexOf('%');
|
||||
@@ -170,15 +173,16 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
return addr + ":" + a.getPort();
|
||||
}
|
||||
|
||||
private void acceptContactConnections() {
|
||||
while (isRunning()) {
|
||||
private void acceptContactConnections(ServerSocket ss) {
|
||||
while (true) {
|
||||
Socket s;
|
||||
try {
|
||||
s = socket.accept();
|
||||
s = ss.accept();
|
||||
s.setSoTimeout(socketTimeout);
|
||||
} catch (IOException e) {
|
||||
// This is expected when the socket is closed
|
||||
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
|
||||
// This is expected when the server socket is closed
|
||||
LOG.info("Server socket closed");
|
||||
state.clearServerSocket(ss);
|
||||
return;
|
||||
}
|
||||
if (LOG.isLoggable(INFO))
|
||||
@@ -191,13 +195,18 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
running = false;
|
||||
tryToClose(socket);
|
||||
ServerSocket ss = state.setStopped();
|
||||
tryToClose(ss, LOG, WARNING);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return running && socket != null && !socket.isClosed();
|
||||
public State getState() {
|
||||
return state.getState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReasonsDisabled() {
|
||||
return state.getReasonsDisabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -211,9 +220,9 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||
properties) {
|
||||
if (!isRunning()) return;
|
||||
public void poll(
|
||||
List<Pair<TransportProperties, ConnectionHandler>> properties) {
|
||||
if (getState() != ACTIVE) return;
|
||||
backoff.increment();
|
||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||
connect(p.getFirst(), p.getSecond());
|
||||
@@ -232,8 +241,8 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
|
||||
@Override
|
||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||
if (!isRunning()) return null;
|
||||
ServerSocket ss = socket;
|
||||
ServerSocket ss = state.getServerSocket();
|
||||
if (ss == null) return null;
|
||||
InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress());
|
||||
if (local == null) {
|
||||
LOG.warning("No interface for server socket");
|
||||
@@ -313,22 +322,6 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsKeyAgreement() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyAgreementListener createKeyAgreementListener(byte[] commitment) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DuplexTransportConnection createKeyAgreementConnection(
|
||||
byte[] commitment, BdfList descriptor) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsRendezvous() {
|
||||
return false;
|
||||
@@ -340,6 +333,17 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsDiscovery() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discoverPeers(
|
||||
List<Pair<TransportProperties, DiscoveryHandler>> properties) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
List<InterfaceAddress> getLocalInterfaceAddresses() {
|
||||
List<InterfaceAddress> addrs = new ArrayList<>();
|
||||
for (NetworkInterface iface : getNetworkInterfaces()) {
|
||||
@@ -366,4 +370,93 @@ abstract class TcpPlugin implements DuplexPlugin {
|
||||
return emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof SettingsUpdatedEvent) {
|
||||
SettingsUpdatedEvent s = (SettingsUpdatedEvent) e;
|
||||
if (s.getNamespace().equals(getId().getString()))
|
||||
ioExecutor.execute(() -> onSettingsUpdated(s.getSettings()));
|
||||
}
|
||||
}
|
||||
|
||||
@IoExecutor
|
||||
private void onSettingsUpdated(Settings settings) {
|
||||
boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE, false);
|
||||
ServerSocket ss = state.setEnabledByUser(enabledByUser);
|
||||
State s = getState();
|
||||
if (ss != null) {
|
||||
LOG.info("Disabled by user, closing server socket");
|
||||
tryToClose(ss, LOG, WARNING);
|
||||
} else if (s == INACTIVE) {
|
||||
LOG.info("Enabled by user, opening server socket");
|
||||
bind();
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
protected class PluginState {
|
||||
|
||||
@GuardedBy("this")
|
||||
private boolean started = false, stopped = false, enabledByUser = false;
|
||||
|
||||
@GuardedBy("this")
|
||||
@Nullable
|
||||
private ServerSocket serverSocket = null;
|
||||
|
||||
synchronized void setStarted(boolean enabledByUser) {
|
||||
started = true;
|
||||
this.enabledByUser = enabledByUser;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
synchronized ServerSocket setStopped() {
|
||||
stopped = true;
|
||||
ServerSocket ss = serverSocket;
|
||||
serverSocket = null;
|
||||
callback.pluginStateChanged(getState());
|
||||
return ss;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
synchronized ServerSocket setEnabledByUser(boolean enabledByUser) {
|
||||
this.enabledByUser = enabledByUser;
|
||||
ServerSocket ss = null;
|
||||
if (!enabledByUser) {
|
||||
ss = serverSocket;
|
||||
serverSocket = null;
|
||||
}
|
||||
callback.pluginStateChanged(getState());
|
||||
return ss;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
synchronized ServerSocket getServerSocket() {
|
||||
return serverSocket;
|
||||
}
|
||||
|
||||
synchronized boolean setServerSocket(ServerSocket ss) {
|
||||
if (stopped || serverSocket != null) return false;
|
||||
serverSocket = ss;
|
||||
callback.pluginStateChanged(getState());
|
||||
return true;
|
||||
}
|
||||
|
||||
synchronized void clearServerSocket(ServerSocket ss) {
|
||||
if (serverSocket == ss) serverSocket = null;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
synchronized State getState() {
|
||||
if (!started || stopped) return STARTING_STOPPING;
|
||||
if (!enabledByUser) return DISABLED;
|
||||
return serverSocket == null ? INACTIVE : ACTIVE;
|
||||
}
|
||||
|
||||
synchronized int getReasonsDisabled() {
|
||||
return getState() == DISABLED ? REASON_USER : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package org.briarproject.bramble.plugin.tcp;
|
||||
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
|
||||
import java.net.Inet4Address;
|
||||
@@ -42,6 +45,22 @@ class WanTcpPlugin extends TcpPlugin {
|
||||
return ID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsKeyAgreement() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KeyAgreementListener createKeyAgreementListener(byte[] commitment) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DuplexTransportConnection createKeyAgreementConnection(
|
||||
byte[] commitment, BdfList descriptor) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<InetSocketAddress> getLocalSocketAddresses() {
|
||||
// Use the same address and port as last time if available
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.briarproject.bramble.plugin.tcp;
|
||||
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
@@ -27,12 +28,14 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
|
||||
private static final double BACKOFF_BASE = 1.2;
|
||||
|
||||
private final Executor ioExecutor;
|
||||
private final EventBus eventBus;
|
||||
private final BackoffFactory backoffFactory;
|
||||
private final ShutdownManager shutdownManager;
|
||||
|
||||
public WanTcpPluginFactory(Executor ioExecutor,
|
||||
public WanTcpPluginFactory(Executor ioExecutor, EventBus eventBus,
|
||||
BackoffFactory backoffFactory, ShutdownManager shutdownManager) {
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.eventBus = eventBus;
|
||||
this.backoffFactory = backoffFactory;
|
||||
this.shutdownManager = shutdownManager;
|
||||
}
|
||||
@@ -51,8 +54,10 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
|
||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||
return new WanTcpPlugin(ioExecutor, backoff,
|
||||
WanTcpPlugin plugin = new WanTcpPlugin(ioExecutor, backoff,
|
||||
new PortMapperImpl(shutdownManager), callback, MAX_LATENCY,
|
||||
MAX_IDLE_TIME, CONNECTION_TIMEOUT);
|
||||
eventBus.addListener(plugin);
|
||||
return plugin;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ public interface CircumventionProvider {
|
||||
String[] BLOCKED = {"CN", "IR", "EG", "BY", "TR", "SY", "VE"};
|
||||
|
||||
/**
|
||||
* Countries where obfs4 bridge connection are likely to work.
|
||||
* Countries where obfs4 or meek bridge connections are likely to work.
|
||||
* Should be a subset of {@link #BLOCKED}.
|
||||
*/
|
||||
String[] BRIDGES = { "CN", "IR", "EG", "BY", "TR", "SY", "VE" };
|
||||
|
||||
@@ -15,9 +15,11 @@ import org.briarproject.bramble.api.network.NetworkManager;
|
||||
import org.briarproject.bramble.api.network.NetworkStatus;
|
||||
import org.briarproject.bramble.api.network.event.NetworkStatusEvent;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||
import org.briarproject.bramble.api.plugin.DiscoveryHandler;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.PluginException;
|
||||
import org.briarproject.bramble.api.plugin.TorConstants;
|
||||
@@ -54,6 +56,9 @@ import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
@@ -65,6 +70,11 @@ import static java.util.logging.Logger.getLogger;
|
||||
import static net.freehaven.tor.control.TorControlCommands.HS_ADDRESS;
|
||||
import static net.freehaven.tor.control.TorControlCommands.HS_PRIVKEY;
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ENABLING;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.CONTROL_PORT;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.ID;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_MOBILE;
|
||||
@@ -76,6 +86,9 @@ import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_ONLY_WHE
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_PORT;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V2;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V3;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.REASON_BATTERY;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.REASON_COUNTRY_BLOCKED;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.REASON_MOBILE_DATA;
|
||||
import static org.briarproject.bramble.plugin.tor.TorRendezvousCrypto.SEED_BYTES;
|
||||
import static org.briarproject.bramble.util.IoUtils.copyAndClose;
|
||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||
@@ -113,16 +126,14 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
private final int maxLatency, maxIdleTime, socketTimeout;
|
||||
private final File torDirectory, torFile, geoIpFile, obfs4File, configFile;
|
||||
private final File doneFile, cookieFile;
|
||||
private final ConnectionStatus connectionStatus;
|
||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||
|
||||
private volatile ServerSocket socket = null;
|
||||
protected final PluginState state = new PluginState();
|
||||
|
||||
private volatile Socket controlSocket = null;
|
||||
private volatile TorControlConnection controlConnection = null;
|
||||
private volatile Settings settings = null;
|
||||
|
||||
protected volatile boolean running = false;
|
||||
|
||||
protected abstract int getProcessId();
|
||||
|
||||
protected abstract long getLastUpdateTime();
|
||||
@@ -159,7 +170,6 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
configFile = new File(torDirectory, "torrc");
|
||||
doneFile = new File(torDirectory, "done");
|
||||
cookieFile = new File(torDirectory, ".tor/control_auth_cookie");
|
||||
connectionStatus = new ConnectionStatus();
|
||||
// Don't execute more than one connection status check at a time
|
||||
connectionStatusExecutor =
|
||||
new PoliteExecutor("TorPlugin", ioExecutor, 1);
|
||||
@@ -190,7 +200,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
}
|
||||
}
|
||||
// Load the settings
|
||||
settings = callback.getSettings();
|
||||
settings = migrateSettings(callback.getSettings());
|
||||
// Install or update the assets if necessary
|
||||
if (!assetsAreUpToDate()) installAssets();
|
||||
if (cookieFile.exists() && !cookieFile.delete())
|
||||
@@ -258,7 +268,6 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
// Tell Tor to exit when the control connection is closed
|
||||
controlConnection.takeOwnership();
|
||||
controlConnection.resetConf(singletonList(OWNER));
|
||||
running = true;
|
||||
// Register to receive events from the Tor process
|
||||
controlConnection.setEventHandler(this);
|
||||
controlConnection.setEvents(asList(EVENTS));
|
||||
@@ -266,11 +275,12 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
String phase = controlConnection.getInfo("status/bootstrap-phase");
|
||||
if (phase != null && phase.contains("PROGRESS=100")) {
|
||||
LOG.info("Tor has already bootstrapped");
|
||||
connectionStatus.setBootstrapped();
|
||||
state.setBootstrapped();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new PluginException(e);
|
||||
}
|
||||
state.setStarted();
|
||||
// Check whether we're online
|
||||
updateConnectionStatus(networkManager.getNetworkStatus(),
|
||||
batteryManager.isCharging());
|
||||
@@ -278,6 +288,18 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
bind();
|
||||
}
|
||||
|
||||
// TODO: Remove after a reasonable migration period (added 2020-01-16)
|
||||
private Settings migrateSettings(Settings settings) {
|
||||
int network = settings.getInt(PREF_TOR_NETWORK,
|
||||
PREF_TOR_NETWORK_AUTOMATIC);
|
||||
if (network == PREF_TOR_NETWORK_NEVER) {
|
||||
settings.putInt(PREF_TOR_NETWORK, PREF_TOR_NETWORK_AUTOMATIC);
|
||||
settings.putBoolean(PREF_PLUGIN_ENABLE, false);
|
||||
callback.mergeSettings(settings);
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
private boolean assetsAreUpToDate() {
|
||||
return doneFile.lastModified() > getLastUpdateTime();
|
||||
}
|
||||
@@ -393,11 +415,11 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
tryToClose(ss, LOG, WARNING);
|
||||
return;
|
||||
}
|
||||
if (!running) {
|
||||
if (!state.setServerSocket(ss)) {
|
||||
LOG.info("Closing redundant server socket");
|
||||
tryToClose(ss, LOG, WARNING);
|
||||
return;
|
||||
}
|
||||
socket = ss;
|
||||
// Store the port number
|
||||
String localPort = String.valueOf(ss.getLocalPort());
|
||||
Settings s = new Settings();
|
||||
@@ -412,7 +434,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
}
|
||||
|
||||
private void publishHiddenService(String port) {
|
||||
if (!running) return;
|
||||
if (!state.isTorRunning()) return;
|
||||
LOG.info("Creating hidden service");
|
||||
String privKey = settings.get(HS_PRIVKEY);
|
||||
Map<Integer, String> portLines = singletonMap(80, "127.0.0.1:" + port);
|
||||
@@ -450,14 +472,15 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
}
|
||||
|
||||
private void acceptContactConnections(ServerSocket ss) {
|
||||
while (running) {
|
||||
while (true) {
|
||||
Socket s;
|
||||
try {
|
||||
s = ss.accept();
|
||||
s.setSoTimeout(socketTimeout);
|
||||
} catch (IOException e) {
|
||||
// This is expected when the socket is closed
|
||||
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
|
||||
// This is expected when the server socket is closed
|
||||
LOG.info("Server socket closed");
|
||||
state.clearServerSocket(ss);
|
||||
return;
|
||||
}
|
||||
LOG.info("Connection received");
|
||||
@@ -467,10 +490,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
}
|
||||
|
||||
protected void enableNetwork(boolean enable) throws IOException {
|
||||
if (!running) return;
|
||||
connectionStatus.enableNetwork(enable);
|
||||
state.enableNetwork(enable);
|
||||
controlConnection.setConf("DisableNetwork", enable ? "0" : "1");
|
||||
if (!enable) callback.transportDisabled();
|
||||
}
|
||||
|
||||
private void enableBridges(boolean enable, boolean needsMeek)
|
||||
@@ -494,9 +515,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
running = false;
|
||||
tryToClose(socket, LOG, WARNING);
|
||||
callback.transportDisabled();
|
||||
ServerSocket ss = state.setStopped();
|
||||
tryToClose(ss, LOG, WARNING);
|
||||
if (controlSocket != null && controlConnection != null) {
|
||||
try {
|
||||
LOG.info("Stopping Tor");
|
||||
@@ -510,8 +530,13 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return running && connectionStatus.isConnected();
|
||||
public State getState() {
|
||||
return state.getState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReasonsDisabled() {
|
||||
return state.getReasonsDisabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -525,9 +550,9 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||
properties) {
|
||||
if (!isRunning()) return;
|
||||
public void poll(
|
||||
List<Pair<TransportProperties, ConnectionHandler>> properties) {
|
||||
if (getState() != ACTIVE) return;
|
||||
backoff.increment();
|
||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||
connect(p.getFirst(), p.getSecond());
|
||||
@@ -546,7 +571,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
|
||||
@Override
|
||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||
if (!isRunning()) return null;
|
||||
if (getState() != ACTIVE) return null;
|
||||
String bestOnion = null;
|
||||
String onion2 = p.get(PROP_ONION_V2);
|
||||
String onion3 = p.get(PROP_ONION_V3);
|
||||
@@ -634,8 +659,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
new TorTransportConnection(this, s));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// This is expected when the socket is closed
|
||||
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
|
||||
// This is expected when the server socket is closed
|
||||
LOG.info("Rendezvous server socket closed");
|
||||
}
|
||||
});
|
||||
Map<Integer, String> portLines =
|
||||
@@ -660,13 +685,23 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsDiscovery() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discoverPeers(
|
||||
List<Pair<TransportProperties, DiscoveryHandler>> properties) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void circuitStatus(String status, String id, String path) {
|
||||
if (status.equals("BUILT") &&
|
||||
connectionStatus.getAndSetCircuitBuilt()) {
|
||||
state.getAndSetCircuitBuilt()) {
|
||||
LOG.info("First circuit built");
|
||||
backoff.reset();
|
||||
if (isRunning()) callback.transportEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -697,9 +732,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
public void message(String severity, String msg) {
|
||||
if (LOG.isLoggable(INFO)) LOG.info(severity + " " + msg);
|
||||
if (severity.equals("NOTICE") && msg.startsWith("Bootstrapped 100%")) {
|
||||
connectionStatus.setBootstrapped();
|
||||
state.setBootstrapped();
|
||||
backoff.reset();
|
||||
if (isRunning()) callback.transportEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -736,7 +770,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
private void disableNetwork() {
|
||||
connectionStatusExecutor.execute(() -> {
|
||||
try {
|
||||
enableNetwork(false);
|
||||
if (state.isTorRunning()) enableNetwork(false);
|
||||
} catch (IOException ex) {
|
||||
logException(LOG, WARNING, ex);
|
||||
}
|
||||
@@ -746,12 +780,14 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
private void updateConnectionStatus(NetworkStatus status,
|
||||
boolean charging) {
|
||||
connectionStatusExecutor.execute(() -> {
|
||||
if (!running) return;
|
||||
if (!state.isTorRunning()) return;
|
||||
boolean online = status.isConnected();
|
||||
boolean wifi = status.isWifi();
|
||||
String country = locationUtils.getCurrentCountry();
|
||||
boolean blocked =
|
||||
circumventionProvider.isTorProbablyBlocked(country);
|
||||
boolean enabledByUser =
|
||||
settings.getBoolean(PREF_PLUGIN_ENABLE, true);
|
||||
int network = settings.getInt(PREF_TOR_NETWORK,
|
||||
PREF_TOR_NETWORK_AUTOMATIC);
|
||||
boolean useMobile = settings.getBoolean(PREF_TOR_MOBILE, true);
|
||||
@@ -762,47 +798,70 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Online: " + online + ", wifi: " + wifi);
|
||||
if ("".equals(country)) LOG.info("Country code unknown");
|
||||
if (country.isEmpty()) LOG.info("Country code unknown");
|
||||
else LOG.info("Country code: " + country);
|
||||
LOG.info("Charging: " + charging);
|
||||
}
|
||||
|
||||
try {
|
||||
if (!online) {
|
||||
LOG.info("Disabling network, device is offline");
|
||||
enableNetwork(false);
|
||||
} else if (!charging && onlyWhenCharging) {
|
||||
LOG.info("Disabling network, device is on battery");
|
||||
enableNetwork(false);
|
||||
} else if (network == PREF_TOR_NETWORK_NEVER ||
|
||||
(!useMobile && !wifi)) {
|
||||
LOG.info("Disabling network, device is using mobile data");
|
||||
enableNetwork(false);
|
||||
} else if (automatic && blocked && !bridgesWork) {
|
||||
LOG.info("Disabling network, country is blocked");
|
||||
enableNetwork(false);
|
||||
} else if (network == PREF_TOR_NETWORK_WITH_BRIDGES ||
|
||||
(automatic && bridgesWork)) {
|
||||
if (circumventionProvider.needsMeek(country)) {
|
||||
LOG.info("Enabling network, using meek bridges");
|
||||
enableBridges(true, true);
|
||||
int reasonsDisabled = 0;
|
||||
boolean enableNetwork = false, enableBridges = false;
|
||||
boolean useMeek = false, enableConnectionPadding = false;
|
||||
|
||||
if (!online) {
|
||||
LOG.info("Disabling network, device is offline");
|
||||
} else {
|
||||
if (!enabledByUser) {
|
||||
LOG.info("User has disabled Tor");
|
||||
reasonsDisabled |= REASON_USER;
|
||||
}
|
||||
if (!charging && onlyWhenCharging) {
|
||||
LOG.info("Configured not to use battery");
|
||||
reasonsDisabled |= REASON_BATTERY;
|
||||
}
|
||||
if (!useMobile && !wifi) {
|
||||
LOG.info("Configured not to use mobile data");
|
||||
reasonsDisabled |= REASON_MOBILE_DATA;
|
||||
}
|
||||
if (automatic && blocked && !bridgesWork) {
|
||||
LOG.info("Country is blocked");
|
||||
reasonsDisabled |= REASON_COUNTRY_BLOCKED;
|
||||
}
|
||||
|
||||
if (reasonsDisabled != 0) {
|
||||
LOG.info("Disabling network due to settings");
|
||||
} else {
|
||||
LOG.info("Enabling network");
|
||||
enableNetwork = true;
|
||||
if (network == PREF_TOR_NETWORK_WITH_BRIDGES ||
|
||||
(automatic && bridgesWork)) {
|
||||
if (circumventionProvider.needsMeek(country)) {
|
||||
LOG.info("Using meek bridges");
|
||||
enableBridges = true;
|
||||
useMeek = true;
|
||||
} else {
|
||||
LOG.info("Using obfs4 bridges");
|
||||
enableBridges = true;
|
||||
}
|
||||
} else {
|
||||
LOG.info("Enabling network, using obfs4 bridges");
|
||||
enableBridges(true, false);
|
||||
LOG.info("Not using bridges");
|
||||
}
|
||||
if (wifi && charging) {
|
||||
LOG.info("Enabling connection padding");
|
||||
enableConnectionPadding = true;
|
||||
} else {
|
||||
LOG.info("Disabling connection padding");
|
||||
}
|
||||
enableNetwork(true);
|
||||
} else {
|
||||
LOG.info("Enabling network, not using bridges");
|
||||
enableBridges(false, false);
|
||||
enableNetwork(true);
|
||||
}
|
||||
if (online && wifi && charging) {
|
||||
LOG.info("Enabling connection padding");
|
||||
enableConnectionPadding(true);
|
||||
} else {
|
||||
LOG.info("Disabling connection padding");
|
||||
enableConnectionPadding(false);
|
||||
}
|
||||
|
||||
state.setReasonsDisabled(reasonsDisabled);
|
||||
|
||||
try {
|
||||
if (enableNetwork) {
|
||||
enableBridges(enableBridges, useMeek);
|
||||
enableConnectionPadding(enableConnectionPadding);
|
||||
}
|
||||
enableNetwork(enableNetwork);
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
@@ -810,33 +869,96 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
||||
}
|
||||
|
||||
private void enableConnectionPadding(boolean enable) throws IOException {
|
||||
if (!running) return;
|
||||
controlConnection.setConf("ConnectionPadding", enable ? "1" : "0");
|
||||
}
|
||||
|
||||
private static class ConnectionStatus {
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
protected class PluginState {
|
||||
|
||||
// All of the following are locking: this
|
||||
private boolean networkEnabled = false;
|
||||
private boolean bootstrapped = false, circuitBuilt = false;
|
||||
@GuardedBy("this")
|
||||
private boolean started = false,
|
||||
stopped = false,
|
||||
networkInitialised = false,
|
||||
networkEnabled = false,
|
||||
bootstrapped = false,
|
||||
circuitBuilt = false,
|
||||
settingsChecked = false;
|
||||
|
||||
private synchronized void setBootstrapped() {
|
||||
bootstrapped = true;
|
||||
@GuardedBy("this")
|
||||
private int reasonsDisabled = 0;
|
||||
|
||||
@GuardedBy("this")
|
||||
@Nullable
|
||||
private ServerSocket serverSocket = null;
|
||||
|
||||
synchronized void setStarted() {
|
||||
started = true;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
private synchronized boolean getAndSetCircuitBuilt() {
|
||||
synchronized boolean isTorRunning() {
|
||||
return started && !stopped;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
synchronized ServerSocket setStopped() {
|
||||
stopped = true;
|
||||
ServerSocket ss = serverSocket;
|
||||
serverSocket = null;
|
||||
callback.pluginStateChanged(getState());
|
||||
return ss;
|
||||
}
|
||||
|
||||
synchronized void setBootstrapped() {
|
||||
bootstrapped = true;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
synchronized boolean getAndSetCircuitBuilt() {
|
||||
boolean firstCircuit = !circuitBuilt;
|
||||
circuitBuilt = true;
|
||||
callback.pluginStateChanged(getState());
|
||||
return firstCircuit;
|
||||
}
|
||||
|
||||
private synchronized void enableNetwork(boolean enable) {
|
||||
synchronized void enableNetwork(boolean enable) {
|
||||
networkInitialised = true;
|
||||
networkEnabled = enable;
|
||||
if (!enable) circuitBuilt = false;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
private synchronized boolean isConnected() {
|
||||
return networkEnabled && bootstrapped && circuitBuilt;
|
||||
synchronized void setReasonsDisabled(int reasonsDisabled) {
|
||||
settingsChecked = true;
|
||||
this.reasonsDisabled = reasonsDisabled;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
// Doesn't affect getState()
|
||||
synchronized boolean setServerSocket(ServerSocket ss) {
|
||||
if (stopped || serverSocket != null) return false;
|
||||
serverSocket = ss;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Doesn't affect getState()
|
||||
synchronized void clearServerSocket(ServerSocket ss) {
|
||||
if (serverSocket == ss) serverSocket = null;
|
||||
}
|
||||
|
||||
synchronized State getState() {
|
||||
if (!started || stopped || !settingsChecked) {
|
||||
return STARTING_STOPPING;
|
||||
}
|
||||
if (reasonsDisabled != 0) return DISABLED;
|
||||
if (!networkInitialised) return ENABLING;
|
||||
if (!networkEnabled) return INACTIVE;
|
||||
return bootstrapped && circuitBuilt ? ACTIVE : ENABLING;
|
||||
}
|
||||
|
||||
synchronized int getReasonsDisabled() {
|
||||
return getState() == DISABLED ? reasonsDisabled : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,11 +37,6 @@ import javax.annotation.Nullable;
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.GROUP_KEY_DISCOVERED;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_LOCAL;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_TRANSPORT_ID;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_VERSION;
|
||||
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
class TransportPropertyManagerImpl implements TransportPropertyManager,
|
||||
@@ -116,10 +111,10 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
||||
try {
|
||||
// Find the latest update for this transport, if any
|
||||
BdfDictionary d = metadataParser.parse(meta);
|
||||
TransportId t = new TransportId(d.getString(MSG_KEY_TRANSPORT_ID));
|
||||
TransportId t = new TransportId(d.getString("transportId"));
|
||||
LatestUpdate latest = findLatest(txn, m.getGroupId(), t, false);
|
||||
if (latest != null) {
|
||||
if (d.getLong(MSG_KEY_VERSION) > latest.version) {
|
||||
if (d.getLong("version") > latest.version) {
|
||||
// This update is newer - delete the previous update
|
||||
db.deleteMessage(txn, latest.messageId);
|
||||
db.deleteMessageMetadata(txn, latest.messageId);
|
||||
@@ -145,27 +140,6 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRemotePropertiesFromConnection(ContactId c, TransportId t,
|
||||
TransportProperties props) throws DbException {
|
||||
if (props.isEmpty()) return;
|
||||
try {
|
||||
db.transaction(false, txn -> {
|
||||
Group g = getContactGroup(db.getContact(txn, c));
|
||||
BdfDictionary meta = clientHelper.getGroupMetadataAsDictionary(
|
||||
txn, g.getId());
|
||||
BdfDictionary discovered =
|
||||
meta.getOptionalDictionary(GROUP_KEY_DISCOVERED);
|
||||
if (discovered == null) discovered = new BdfDictionary();
|
||||
discovered.putAll(props);
|
||||
meta.put(GROUP_KEY_DISCOVERED, discovered);
|
||||
clientHelper.mergeGroupMetadata(txn, g.getId(), meta);
|
||||
});
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<TransportId, TransportProperties> getLocalProperties()
|
||||
throws DbException {
|
||||
@@ -229,26 +203,12 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
||||
Group g = getContactGroup(c);
|
||||
try {
|
||||
// Find the latest remote update
|
||||
TransportProperties remote;
|
||||
LatestUpdate latest = findLatest(txn, g.getId(), t, false);
|
||||
if (latest == null) {
|
||||
remote = new TransportProperties();
|
||||
} else {
|
||||
// Retrieve and parse the latest remote properties
|
||||
BdfList message =
|
||||
clientHelper.getMessageAsList(txn, latest.messageId);
|
||||
remote = parseProperties(message);
|
||||
}
|
||||
// Merge in any discovered properties
|
||||
BdfDictionary meta =
|
||||
clientHelper.getGroupMetadataAsDictionary(txn, g.getId());
|
||||
BdfDictionary d = meta.getOptionalDictionary(GROUP_KEY_DISCOVERED);
|
||||
if (d == null) return remote;
|
||||
TransportProperties merged =
|
||||
clientHelper.parseAndValidateTransportProperties(d);
|
||||
// Received properties override discovered properties
|
||||
merged.putAll(remote);
|
||||
return merged;
|
||||
if (latest == null) return new TransportProperties();
|
||||
// Retrieve and parse the latest remote properties
|
||||
BdfList message =
|
||||
clientHelper.getMessageAsList(txn, latest.messageId);
|
||||
return parseProperties(message);
|
||||
} catch (FormatException e) {
|
||||
throw new DbException(e);
|
||||
}
|
||||
@@ -321,9 +281,9 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
||||
long now = clock.currentTimeMillis();
|
||||
Message m = clientHelper.createMessage(g, now, body);
|
||||
BdfDictionary meta = new BdfDictionary();
|
||||
meta.put(MSG_KEY_TRANSPORT_ID, t.getString());
|
||||
meta.put(MSG_KEY_VERSION, version);
|
||||
meta.put(MSG_KEY_LOCAL, local);
|
||||
meta.put("transportId", t.getString());
|
||||
meta.put("version", version);
|
||||
meta.put("local", local);
|
||||
clientHelper.addLocalMessage(txn, m, meta, shared, false);
|
||||
} catch (FormatException e) {
|
||||
throw new RuntimeException(e);
|
||||
@@ -342,9 +302,8 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
||||
.getMessageMetadataAsDictionary(txn, localGroup.getId());
|
||||
for (Entry<MessageId, BdfDictionary> e : metadata.entrySet()) {
|
||||
BdfDictionary meta = e.getValue();
|
||||
TransportId t =
|
||||
new TransportId(meta.getString(MSG_KEY_TRANSPORT_ID));
|
||||
long version = meta.getLong(MSG_KEY_VERSION);
|
||||
TransportId t = new TransportId(meta.getString("transportId"));
|
||||
long version = meta.getLong("version");
|
||||
latestUpdates.put(t, new LatestUpdate(e.getKey(), version));
|
||||
}
|
||||
return latestUpdates;
|
||||
@@ -357,10 +316,9 @@ class TransportPropertyManagerImpl implements TransportPropertyManager,
|
||||
clientHelper.getMessageMetadataAsDictionary(txn, g);
|
||||
for (Entry<MessageId, BdfDictionary> e : metadata.entrySet()) {
|
||||
BdfDictionary meta = e.getValue();
|
||||
if (meta.getString(MSG_KEY_TRANSPORT_ID).equals(t.getString())
|
||||
&& meta.getBoolean(MSG_KEY_LOCAL) == local) {
|
||||
return new LatestUpdate(e.getKey(),
|
||||
meta.getLong(MSG_KEY_VERSION));
|
||||
if (meta.getString("transportId").equals(t.getString())
|
||||
&& meta.getBoolean("local") == local) {
|
||||
return new LatestUpdate(e.getKey(), meta.getLong("version"));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -31,8 +31,8 @@ import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||
@@ -269,11 +269,11 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
||||
} else if (e instanceof PendingContactRemovedEvent) {
|
||||
PendingContactRemovedEvent p = (PendingContactRemovedEvent) e;
|
||||
removePendingContactAsync(p.getId());
|
||||
} else if (e instanceof TransportEnabledEvent) {
|
||||
TransportEnabledEvent t = (TransportEnabledEvent) e;
|
||||
} else if (e instanceof TransportActiveEvent) {
|
||||
TransportActiveEvent t = (TransportActiveEvent) e;
|
||||
addTransportAsync(t.getTransportId());
|
||||
} else if (e instanceof TransportDisabledEvent) {
|
||||
TransportDisabledEvent t = (TransportDisabledEvent) e;
|
||||
} else if (e instanceof TransportInactiveEvent) {
|
||||
TransportInactiveEvent t = (TransportInactiveEvent) e;
|
||||
removeTransportAsync(t.getTransportId());
|
||||
} else if (e instanceof RendezvousConnectionOpenedEvent) {
|
||||
RendezvousConnectionOpenedEvent r =
|
||||
|
||||
@@ -6,7 +6,7 @@ import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TorConstants;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||
import org.briarproject.bramble.api.reporting.DevConfig;
|
||||
import org.briarproject.bramble.api.reporting.DevReporter;
|
||||
import org.briarproject.bramble.util.IoUtils;
|
||||
@@ -92,8 +92,8 @@ class DevReporterImpl implements DevReporter, EventListener {
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof TransportEnabledEvent) {
|
||||
TransportEnabledEvent t = (TransportEnabledEvent) e;
|
||||
if (e instanceof TransportActiveEvent) {
|
||||
TransportActiveEvent t = (TransportActiveEvent) e;
|
||||
if (t.getTransportId().equals(TorConstants.ID))
|
||||
ioExecutor.execute(this::sendReports);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.briarproject.bramble.account;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
||||
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||
@@ -20,15 +19,12 @@ import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import static junit.framework.Assert.assertFalse;
|
||||
import static junit.framework.Assert.assertNull;
|
||||
import static junit.framework.Assert.assertTrue;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_CIPHERTEXT;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_PASSWORD;
|
||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||
import static org.briarproject.bramble.test.TestUtils.getIdentity;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
@@ -39,7 +35,6 @@ import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
|
||||
@@ -88,13 +83,8 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSignInThrowsExceptionIfDbKeyCannotBeLoaded() {
|
||||
try {
|
||||
accountManager.signIn(password);
|
||||
fail();
|
||||
} catch (DecryptionException expected) {
|
||||
assertEquals(INVALID_CIPHERTEXT, expected.getDecryptionResult());
|
||||
}
|
||||
public void testSignInReturnsFalseIfDbKeyCannotBeLoaded() {
|
||||
assertFalse(accountManager.signIn(password));
|
||||
assertFalse(accountManager.hasDatabaseKey());
|
||||
|
||||
assertFalse(keyFile.exists());
|
||||
@@ -102,11 +92,11 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSignInThrowsExceptionIfPasswordIsWrong() throws Exception {
|
||||
public void testSignInReturnsFalseIfPasswordIsWrong() throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(crypto).decryptWithPassword(encryptedKey, password,
|
||||
keyStrengthener);
|
||||
will(throwException(new DecryptionException(INVALID_PASSWORD)));
|
||||
will(returnValue(null));
|
||||
}});
|
||||
|
||||
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||
@@ -115,12 +105,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||
|
||||
try {
|
||||
accountManager.signIn(password);
|
||||
fail();
|
||||
} catch (DecryptionException expected) {
|
||||
assertEquals(INVALID_PASSWORD, expected.getDecryptionResult());
|
||||
}
|
||||
assertFalse(accountManager.signIn(password));
|
||||
assertFalse(accountManager.hasDatabaseKey());
|
||||
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||
@@ -143,7 +128,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||
|
||||
accountManager.signIn(password);
|
||||
assertTrue(accountManager.signIn(password));
|
||||
assertTrue(accountManager.hasDatabaseKey());
|
||||
SecretKey decrypted = accountManager.getDatabaseKey();
|
||||
assertNotNull(decrypted);
|
||||
@@ -172,7 +157,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||
|
||||
accountManager.signIn(password);
|
||||
assertTrue(accountManager.signIn(password));
|
||||
assertTrue(accountManager.hasDatabaseKey());
|
||||
SecretKey decrypted = accountManager.getDatabaseKey();
|
||||
assertNotNull(decrypted);
|
||||
@@ -254,6 +239,55 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
assertFalse(keyBackupFile.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccountExistsReturnsFalseIfDbDirectoryDoesNotExist()
|
||||
throws Exception {
|
||||
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||
|
||||
assertFalse(dbDir.exists());
|
||||
|
||||
assertFalse(accountManager.accountExists());
|
||||
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||
assertFalse(dbDir.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccountExistsReturnsFalseIfDbDirectoryIsNotDirectory()
|
||||
throws Exception {
|
||||
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||
|
||||
assertTrue(dbDir.createNewFile());
|
||||
assertFalse(dbDir.isDirectory());
|
||||
|
||||
assertFalse(accountManager.accountExists());
|
||||
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||
assertTrue(dbDir.exists());
|
||||
assertFalse(dbDir.isDirectory());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAccountExistsReturnsTrueIfDbDirectoryIsDirectory()
|
||||
throws Exception {
|
||||
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||
|
||||
assertTrue(dbDir.mkdirs());
|
||||
assertTrue(dbDir.isDirectory());
|
||||
|
||||
assertTrue(accountManager.accountExists());
|
||||
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||
assertTrue(dbDir.exists());
|
||||
assertTrue(dbDir.isDirectory());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateAccountStoresDbKey() throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
@@ -281,36 +315,26 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangePasswordThrowsExceptionIfDbKeyCannotBeLoaded() {
|
||||
try {
|
||||
accountManager.changePassword(password, newPassword);
|
||||
fail();
|
||||
} catch (DecryptionException expected) {
|
||||
assertEquals(INVALID_CIPHERTEXT, expected.getDecryptionResult());
|
||||
}
|
||||
public void testChangePasswordReturnsFalseIfDbKeyCannotBeLoaded() {
|
||||
assertFalse(accountManager.changePassword(password, newPassword));
|
||||
|
||||
assertFalse(keyFile.exists());
|
||||
assertFalse(keyBackupFile.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangePasswordThrowsExceptionIfPasswordIsWrong()
|
||||
public void testChangePasswordReturnsFalseIfPasswordIsWrong()
|
||||
throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(crypto).decryptWithPassword(encryptedKey, password,
|
||||
keyStrengthener);
|
||||
will(throwException(new DecryptionException(INVALID_PASSWORD)));
|
||||
will(returnValue(null));
|
||||
}});
|
||||
|
||||
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||
|
||||
try {
|
||||
accountManager.changePassword(password, newPassword);
|
||||
fail();
|
||||
} catch (DecryptionException expected) {
|
||||
assertEquals(INVALID_PASSWORD, expected.getDecryptionResult());
|
||||
}
|
||||
assertFalse(accountManager.changePassword(password, newPassword));
|
||||
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyFile));
|
||||
assertEquals(encryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||
@@ -333,7 +357,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
storeDatabaseKey(keyFile, encryptedKeyHex);
|
||||
storeDatabaseKey(keyBackupFile, encryptedKeyHex);
|
||||
|
||||
accountManager.changePassword(password, newPassword);
|
||||
assertTrue(accountManager.changePassword(password, newPassword));
|
||||
|
||||
assertEquals(newEncryptedKeyHex, loadDatabaseKey(keyFile));
|
||||
assertEquals(newEncryptedKeyHex, loadDatabaseKey(keyBackupFile));
|
||||
@@ -342,7 +366,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
private void storeDatabaseKey(File f, String hex) throws IOException {
|
||||
f.getParentFile().mkdirs();
|
||||
FileOutputStream out = new FileOutputStream(f);
|
||||
out.write(hex.getBytes(Charset.forName("UTF-8")));
|
||||
out.write(hex.getBytes("UTF-8"));
|
||||
out.flush();
|
||||
out.close();
|
||||
}
|
||||
@@ -350,7 +374,7 @@ public class AccountManagerImplTest extends BrambleMockTestCase {
|
||||
@Nullable
|
||||
private String loadDatabaseKey(File f) throws IOException {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||
new FileInputStream(f), Charset.forName("UTF-8")));
|
||||
new FileInputStream(f), "UTF-8"));
|
||||
String hex = reader.readLine();
|
||||
reader.close();
|
||||
return hex;
|
||||
|
||||
@@ -1,35 +1,25 @@
|
||||
package org.briarproject.bramble.crypto;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
||||
import org.briarproject.bramble.api.crypto.KeyStrengthener;
|
||||
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||
import org.briarproject.bramble.system.SystemClock;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.briarproject.bramble.test.BrambleTestCase;
|
||||
import org.briarproject.bramble.test.TestSecureRandomProvider;
|
||||
import org.jmock.Expectations;
|
||||
import org.briarproject.bramble.test.TestUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_CIPHERTEXT;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.INVALID_PASSWORD;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.KEY_STRENGTHENER_ERROR;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
|
||||
import static org.briarproject.bramble.test.TestUtils.getSecretKey;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
public class PasswordBasedEncryptionTest extends BrambleMockTestCase {
|
||||
|
||||
private final KeyStrengthener keyStrengthener =
|
||||
context.mock(KeyStrengthener.class);
|
||||
public class PasswordBasedEncryptionTest extends BrambleTestCase {
|
||||
|
||||
private final CryptoComponentImpl crypto =
|
||||
new CryptoComponentImpl(new TestSecureRandomProvider(),
|
||||
new ScryptKdf(new SystemClock()));
|
||||
|
||||
@Test
|
||||
public void testEncryptionAndDecryption() throws Exception {
|
||||
byte[] input = getRandomBytes(1234);
|
||||
public void testEncryptionAndDecryption() {
|
||||
byte[] input = TestUtils.getRandomBytes(1234);
|
||||
String password = "password";
|
||||
byte[] ciphertext = crypto.encryptWithPassword(input, password, null);
|
||||
byte[] output = crypto.decryptWithPassword(ciphertext, password, null);
|
||||
@@ -37,80 +27,14 @@ public class PasswordBasedEncryptionTest extends BrambleMockTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidFormatVersionThrowsException() {
|
||||
byte[] input = getRandomBytes(1234);
|
||||
public void testInvalidCiphertextReturnsNull() {
|
||||
byte[] input = TestUtils.getRandomBytes(1234);
|
||||
String password = "password";
|
||||
byte[] ciphertext = crypto.encryptWithPassword(input, password, null);
|
||||
|
||||
// Modify the format version
|
||||
ciphertext[0] ^= (byte) 0xFF;
|
||||
try {
|
||||
crypto.decryptWithPassword(ciphertext, password, null);
|
||||
fail();
|
||||
} catch (DecryptionException expected) {
|
||||
assertEquals(INVALID_CIPHERTEXT, expected.getDecryptionResult());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidPasswordThrowsException() {
|
||||
byte[] input = getRandomBytes(1234);
|
||||
byte[] ciphertext = crypto.encryptWithPassword(input, "password", null);
|
||||
|
||||
// Try to decrypt with the wrong password
|
||||
try {
|
||||
crypto.decryptWithPassword(ciphertext, "wrong", null);
|
||||
fail();
|
||||
} catch (DecryptionException expected) {
|
||||
assertEquals(INVALID_PASSWORD, expected.getDecryptionResult());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMissingKeyStrengthenerThrowsException() {
|
||||
SecretKey strengthened = getSecretKey();
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(keyStrengthener).strengthenKey(with(any(SecretKey.class)));
|
||||
will(returnValue(strengthened));
|
||||
}});
|
||||
|
||||
// Use the key strengthener during encryption
|
||||
byte[] input = getRandomBytes(1234);
|
||||
String password = "password";
|
||||
byte[] ciphertext =
|
||||
crypto.encryptWithPassword(input, password, keyStrengthener);
|
||||
|
||||
// The key strengthener is missing during decryption
|
||||
try {
|
||||
crypto.decryptWithPassword(ciphertext, password, null);
|
||||
fail();
|
||||
} catch (DecryptionException expected) {
|
||||
assertEquals(KEY_STRENGTHENER_ERROR, expected.getDecryptionResult());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKeyStrengthenerFailureThrowsException() {
|
||||
SecretKey strengthened = getSecretKey();
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(keyStrengthener).strengthenKey(with(any(SecretKey.class)));
|
||||
will(returnValue(strengthened));
|
||||
oneOf(keyStrengthener).isInitialised();
|
||||
will(returnValue(false));
|
||||
}});
|
||||
|
||||
// Use the key strengthener during encryption
|
||||
byte[] input = getRandomBytes(1234);
|
||||
String password = "password";
|
||||
byte[] ciphertext =
|
||||
crypto.encryptWithPassword(input, password, keyStrengthener);
|
||||
|
||||
// The key strengthener fails during decryption
|
||||
try {
|
||||
crypto.decryptWithPassword(ciphertext, password, keyStrengthener);
|
||||
fail();
|
||||
} catch (DecryptionException expected) {
|
||||
assertEquals(KEY_STRENGTHENER_ERROR, expected.getDecryptionResult());
|
||||
}
|
||||
// Modify the ciphertext
|
||||
int position = new Random().nextInt(ciphertext.length);
|
||||
ciphertext[position] = (byte) (ciphertext[position] ^ 0xFF);
|
||||
byte[] output = crypto.decryptWithPassword(ciphertext, password, null);
|
||||
assertNull(output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,20 +7,18 @@ import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.rendezvous.event.RendezvousConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.system.Clock;
|
||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.test.TestUtils.getContactId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getRandomId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
@@ -32,9 +30,6 @@ import static org.junit.Assert.fail;
|
||||
public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
|
||||
private final EventBus eventBus = context.mock(EventBus.class);
|
||||
private final Clock clock = context.mock(Clock.class);
|
||||
private final ScheduledExecutorService scheduler =
|
||||
context.mock(ScheduledExecutorService.class);
|
||||
|
||||
private final ContactId contactId = getContactId();
|
||||
private final ContactId contactId1 = getContactId();
|
||||
@@ -45,25 +40,17 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
|
||||
@Test
|
||||
public void testRegisterAndUnregister() {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
|
||||
with(10_000L), with(10_000L), with(MILLISECONDS));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c = new ConnectionRegistryImpl(eventBus, clock,
|
||||
scheduler);
|
||||
context.assertIsSatisfied();
|
||||
ConnectionRegistry c = new ConnectionRegistryImpl(eventBus);
|
||||
|
||||
// The registry should be empty
|
||||
assertEquals(emptyList(), c.getConnectedContacts(transportId));
|
||||
assertEquals(emptyList(), c.getConnectedContacts(transportId1));
|
||||
|
||||
// Check that a registered connection shows up - this should
|
||||
// broadcast a ConnectionOpenedEvent and a ConnectionStatusChangedEvent
|
||||
// broadcast a ConnectionOpenedEvent and a ContactConnectedEvent
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(eventBus).broadcast(with(any(ConnectionOpenedEvent.class)));
|
||||
oneOf(eventBus).broadcast(with(any(
|
||||
ConnectionStatusChangedEvent.class)));
|
||||
oneOf(eventBus).broadcast(with(any(ContactConnectedEvent.class)));
|
||||
}});
|
||||
c.registerConnection(contactId, transportId, true);
|
||||
assertEquals(singletonList(contactId),
|
||||
@@ -94,13 +81,11 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Unregister the other connection - this should broadcast a
|
||||
// ConnectionClosedEvent and a ConnectionStatusChangedEvent
|
||||
// ConnectionClosedEvent and a ContactDisconnectedEvent
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(System.currentTimeMillis()));
|
||||
oneOf(eventBus).broadcast(with(any(ConnectionClosedEvent.class)));
|
||||
oneOf(eventBus).broadcast(with(any(
|
||||
ConnectionStatusChangedEvent.class)));
|
||||
ContactDisconnectedEvent.class)));
|
||||
}});
|
||||
c.unregisterConnection(contactId, transportId, true);
|
||||
assertEquals(emptyList(), c.getConnectedContacts(transportId));
|
||||
@@ -117,12 +102,12 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
|
||||
// Register both contacts with one transport, one contact with both -
|
||||
// this should broadcast three ConnectionOpenedEvents and two
|
||||
// ConnectionStatusChangedEvents
|
||||
// ContactConnectedEvents
|
||||
context.checking(new Expectations() {{
|
||||
exactly(3).of(eventBus).broadcast(with(any(
|
||||
ConnectionOpenedEvent.class)));
|
||||
exactly(2).of(eventBus).broadcast(with(any(
|
||||
ConnectionStatusChangedEvent.class)));
|
||||
ContactConnectedEvent.class)));
|
||||
}});
|
||||
c.registerConnection(contactId, transportId, true);
|
||||
c.registerConnection(contactId1, transportId, true);
|
||||
@@ -137,14 +122,7 @@ public class ConnectionRegistryImplTest extends BrambleMockTestCase {
|
||||
|
||||
@Test
|
||||
public void testRegisterAndUnregisterPendingContacts() {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(scheduler).scheduleWithFixedDelay(with(any(Runnable.class)),
|
||||
with(10_000L), with(10_000L), with(MILLISECONDS));
|
||||
}});
|
||||
|
||||
ConnectionRegistry c = new ConnectionRegistryImpl(eventBus, clock,
|
||||
scheduler);
|
||||
context.assertIsSatisfied();
|
||||
ConnectionRegistry c = new ConnectionRegistryImpl(eventBus);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(eventBus).broadcast(with(any(
|
||||
|
||||
@@ -13,8 +13,8 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||
@@ -38,7 +38,7 @@ import static java.util.Collections.emptyList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||
import static org.briarproject.bramble.test.CollectionMatcher.collectionOf;
|
||||
import static org.briarproject.bramble.test.ListMatcher.listOf;
|
||||
import static org.briarproject.bramble.test.PairMatcher.pairOf;
|
||||
import static org.briarproject.bramble.test.TestUtils.getContactId;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTransportId;
|
||||
@@ -322,7 +322,7 @@ public class PollerImplTest extends BrambleMockTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPollsOnTransportEnabled() throws Exception {
|
||||
public void testPollsOnTransportActivated() throws Exception {
|
||||
DuplexPlugin plugin = context.mock(DuplexPlugin.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
@@ -351,17 +351,20 @@ public class PollerImplTest extends BrambleMockTestCase {
|
||||
oneOf(scheduler).schedule(with(any(Runnable.class)),
|
||||
with((long) (pollingInterval * 0.5)), with(MILLISECONDS));
|
||||
will(returnValue(future));
|
||||
// FIXME: Revert
|
||||
oneOf(plugin).supportsDiscovery();
|
||||
will(returnValue(false));
|
||||
// Get the transport properties and connected contacts
|
||||
oneOf(transportPropertyManager).getRemoteProperties(transportId);
|
||||
will(returnValue(singletonMap(contactId, properties)));
|
||||
oneOf(connectionRegistry).getConnectedContacts(transportId);
|
||||
will(returnValue(emptyList()));
|
||||
// Poll the plugin
|
||||
oneOf(plugin).poll(with(collectionOf(
|
||||
pairOf(equal(properties), any(ConnectionHandler.class)))));
|
||||
oneOf(plugin).poll(with(listOf(pairOf(
|
||||
equal(properties), any(ConnectionHandler.class)))));
|
||||
}});
|
||||
|
||||
poller.eventOccurred(new TransportEnabledEvent(transportId));
|
||||
poller.eventOccurred(new TransportActiveEvent(transportId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -394,6 +397,9 @@ public class PollerImplTest extends BrambleMockTestCase {
|
||||
oneOf(scheduler).schedule(with(any(Runnable.class)),
|
||||
with((long) (pollingInterval * 0.5)), with(MILLISECONDS));
|
||||
will(returnValue(future));
|
||||
// FIXME: Revert
|
||||
oneOf(plugin).supportsDiscovery();
|
||||
will(returnValue(false));
|
||||
// Get the transport properties and connected contacts
|
||||
oneOf(transportPropertyManager).getRemoteProperties(transportId);
|
||||
will(returnValue(singletonMap(contactId, properties)));
|
||||
@@ -402,11 +408,11 @@ public class PollerImplTest extends BrambleMockTestCase {
|
||||
// All contacts are connected, so don't poll the plugin
|
||||
}});
|
||||
|
||||
poller.eventOccurred(new TransportEnabledEvent(transportId));
|
||||
poller.eventOccurred(new TransportActiveEvent(transportId));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCancelsPollingOnTransportDisabled() {
|
||||
public void testCancelsPollingOnTransportDeactivated() {
|
||||
Plugin plugin = context.mock(Plugin.class);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
@@ -424,11 +430,11 @@ public class PollerImplTest extends BrambleMockTestCase {
|
||||
oneOf(scheduler).schedule(with(any(Runnable.class)), with(0L),
|
||||
with(MILLISECONDS));
|
||||
will(returnValue(future));
|
||||
// The plugin is disabled before the task runs - cancel the task
|
||||
// The plugin is deactivated before the task runs - cancel the task
|
||||
oneOf(future).cancel(false);
|
||||
}});
|
||||
|
||||
poller.eventOccurred(new TransportEnabledEvent(transportId));
|
||||
poller.eventOccurred(new TransportDisabledEvent(transportId));
|
||||
poller.eventOccurred(new TransportActiveEvent(transportId));
|
||||
poller.eventOccurred(new TransportInactiveEvent(transportId));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Backoff;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
@@ -31,6 +32,7 @@ import static java.util.concurrent.Executors.newCachedThreadPool;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.COMMIT_LENGTH;
|
||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.PREF_PLUGIN_ENABLE;
|
||||
import static org.briarproject.bramble.plugin.tcp.LanTcpPlugin.areAddressesInSameNetwork;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -302,10 +304,15 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
||||
private final CountDownLatch propertiesLatch = new CountDownLatch(2);
|
||||
private final CountDownLatch connectionsLatch = new CountDownLatch(1);
|
||||
private final TransportProperties local = new TransportProperties();
|
||||
private final Settings settings = new Settings();
|
||||
|
||||
private Callback() {
|
||||
settings.putBoolean(PREF_PLUGIN_ENABLE, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Settings getSettings() {
|
||||
return new Settings();
|
||||
return settings;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -324,11 +331,7 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transportEnabled() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transportDisabled() {
|
||||
public void pluginStateChanged(State newState) {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,18 +24,14 @@ import org.briarproject.bramble.test.DbExpectations;
|
||||
import org.jmock.Expectations;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyMap;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.GROUP_KEY_DISCOVERED;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_LOCAL;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_TRANSPORT_ID;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MSG_KEY_VERSION;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyManager.CLIENT_ID;
|
||||
import static org.briarproject.bramble.api.properties.TransportPropertyManager.MAJOR_VERSION;
|
||||
import static org.briarproject.bramble.api.sync.Group.Visibility.SHARED;
|
||||
@@ -190,25 +186,25 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
Message message = getMessage(contactGroupId);
|
||||
Metadata meta = new Metadata();
|
||||
BdfDictionary metaDictionary = BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 2),
|
||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 2),
|
||||
new BdfEntry("local", false)
|
||||
);
|
||||
Map<MessageId, BdfDictionary> messageMetadata =
|
||||
new LinkedHashMap<>();
|
||||
// A remote update for another transport should be ignored
|
||||
MessageId barUpdateId = new MessageId(getRandomId());
|
||||
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
||||
new BdfEntry("transportId", "bar"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", false)
|
||||
));
|
||||
// A local update for the same transport should be ignored
|
||||
MessageId localUpdateId = new MessageId(getRandomId());
|
||||
messageMetadata.put(localUpdateId, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", true)
|
||||
));
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
@@ -232,18 +228,18 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
Metadata meta = new Metadata();
|
||||
// Version 4 is being delivered
|
||||
BdfDictionary metaDictionary = BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 4),
|
||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 4),
|
||||
new BdfEntry("local", false)
|
||||
);
|
||||
Map<MessageId, BdfDictionary> messageMetadata =
|
||||
new LinkedHashMap<>();
|
||||
// An older remote update for the same transport should be deleted
|
||||
MessageId fooVersion3 = new MessageId(getRandomId());
|
||||
messageMetadata.put(fooVersion3, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 3),
|
||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 3),
|
||||
new BdfEntry("local", false)
|
||||
));
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
@@ -269,18 +265,18 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
Metadata meta = new Metadata();
|
||||
// Version 3 is being delivered
|
||||
BdfDictionary metaDictionary = BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 3),
|
||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 3),
|
||||
new BdfEntry("local", false)
|
||||
);
|
||||
Map<MessageId, BdfDictionary> messageMetadata =
|
||||
new LinkedHashMap<>();
|
||||
// A newer remote update for the same transport should not be deleted
|
||||
MessageId fooVersion4 = new MessageId(getRandomId());
|
||||
messageMetadata.put(fooVersion4, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 4),
|
||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 4),
|
||||
new BdfEntry("local", false)
|
||||
));
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
@@ -346,9 +342,9 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
// A local update for another transport should be ignored
|
||||
MessageId barUpdateId = new MessageId(getRandomId());
|
||||
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
||||
new BdfEntry("transportId", "bar"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", true)
|
||||
));
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
@@ -370,16 +366,16 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
// A local update for another transport should be ignored
|
||||
MessageId barUpdateId = new MessageId(getRandomId());
|
||||
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
||||
new BdfEntry("transportId", "bar"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", true)
|
||||
));
|
||||
// A local update for the right transport should be returned
|
||||
MessageId fooUpdateId = new MessageId(getRandomId());
|
||||
messageMetadata.put(fooUpdateId, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", true)
|
||||
));
|
||||
BdfList fooUpdate = BdfList.of("foo", 1, fooPropertiesDict);
|
||||
|
||||
@@ -409,28 +405,28 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
List<Contact> contacts = asList(contact1, contact2);
|
||||
Group contactGroup1 = getGroup(CLIENT_ID, MAJOR_VERSION);
|
||||
Group contactGroup2 = getGroup(CLIENT_ID, MAJOR_VERSION);
|
||||
Map<MessageId, BdfDictionary> messageMetadata =
|
||||
Map<MessageId, BdfDictionary> messageMetadata2 =
|
||||
new LinkedHashMap<>();
|
||||
// A remote update for another transport should be ignored
|
||||
MessageId barUpdateId = new MessageId(getRandomId());
|
||||
messageMetadata.put(barUpdateId, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
||||
messageMetadata2.put(barUpdateId, BdfDictionary.of(
|
||||
new BdfEntry("transportId", "bar"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", false)
|
||||
));
|
||||
// A local update for the right transport should be ignored
|
||||
MessageId localUpdateId = new MessageId(getRandomId());
|
||||
messageMetadata.put(localUpdateId, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
||||
messageMetadata2.put(localUpdateId, BdfDictionary.of(
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", true)
|
||||
));
|
||||
// A remote update for the right transport should be returned
|
||||
MessageId fooUpdateId = new MessageId(getRandomId());
|
||||
messageMetadata.put(fooUpdateId, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
||||
messageMetadata2.put(fooUpdateId, BdfDictionary.of(
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", false)
|
||||
));
|
||||
BdfList fooUpdate = BdfList.of("foo", 1, fooPropertiesDict);
|
||||
|
||||
@@ -444,25 +440,19 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(contactGroup1));
|
||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
||||
contactGroup1.getId());
|
||||
will(returnValue(emptyMap()));
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
contactGroup1.getId());
|
||||
will(returnValue(new BdfDictionary()));
|
||||
will(returnValue(Collections.emptyMap()));
|
||||
// Second contact: returns an update
|
||||
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
|
||||
MAJOR_VERSION, contact2);
|
||||
will(returnValue(contactGroup2));
|
||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
||||
contactGroup2.getId());
|
||||
will(returnValue(messageMetadata));
|
||||
will(returnValue(messageMetadata2));
|
||||
oneOf(clientHelper).getMessageAsList(txn, fooUpdateId);
|
||||
will(returnValue(fooUpdate));
|
||||
oneOf(clientHelper).parseAndValidateTransportProperties(
|
||||
fooPropertiesDict);
|
||||
will(returnValue(fooProperties));
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
contactGroup2.getId());
|
||||
will(returnValue(new BdfDictionary()));
|
||||
}});
|
||||
|
||||
TransportPropertyManagerImpl t = createInstance();
|
||||
@@ -473,62 +463,6 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
assertEquals(fooProperties, properties.get(contact2.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReceivePropertiesOverrideDiscoveredProperties()
|
||||
throws Exception {
|
||||
Transaction txn = new Transaction(null, true);
|
||||
Contact contact = getContact();
|
||||
List<Contact> contacts = singletonList(contact);
|
||||
Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
|
||||
MessageId updateId = new MessageId(getRandomId());
|
||||
Map<MessageId, BdfDictionary> messageMetadata = singletonMap(updateId,
|
||||
BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, false)
|
||||
));
|
||||
BdfList update = BdfList.of("foo", 1, fooPropertiesDict);
|
||||
TransportProperties discovered = new TransportProperties();
|
||||
discovered.put("fooKey1", "overridden");
|
||||
discovered.put("fooKey3", "fooValue3");
|
||||
BdfDictionary discoveredDict = new BdfDictionary(discovered);
|
||||
BdfDictionary groupMeta = BdfDictionary.of(
|
||||
new BdfEntry(GROUP_KEY_DISCOVERED, discoveredDict)
|
||||
);
|
||||
TransportProperties merged = new TransportProperties();
|
||||
merged.putAll(fooProperties);
|
||||
merged.put("fooKey3", "fooValue3");
|
||||
|
||||
context.checking(new DbExpectations() {{
|
||||
oneOf(db).transactionWithResult(with(true), withDbCallable(txn));
|
||||
oneOf(db).getContacts(txn);
|
||||
will(returnValue(contacts));
|
||||
// One update
|
||||
oneOf(contactGroupFactory).createContactGroup(CLIENT_ID,
|
||||
MAJOR_VERSION, contact);
|
||||
will(returnValue(contactGroup));
|
||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
||||
contactGroup.getId());
|
||||
will(returnValue(messageMetadata));
|
||||
oneOf(clientHelper).getMessageAsList(txn, updateId);
|
||||
will(returnValue(update));
|
||||
oneOf(clientHelper).parseAndValidateTransportProperties(
|
||||
fooPropertiesDict);
|
||||
will(returnValue(fooProperties));
|
||||
oneOf(clientHelper).getGroupMetadataAsDictionary(txn,
|
||||
contactGroup.getId());
|
||||
will(returnValue(groupMeta));
|
||||
oneOf(clientHelper).parseAndValidateTransportProperties(
|
||||
discoveredDict);
|
||||
will(returnValue(discovered));
|
||||
}});
|
||||
|
||||
TransportPropertyManagerImpl t = createInstance();
|
||||
Map<ContactId, TransportProperties> properties =
|
||||
t.getRemoteProperties(new TransportId("foo"));
|
||||
assertEquals(merged, properties.get(contact.getId()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergingUnchangedPropertiesDoesNotCreateUpdate()
|
||||
throws Exception {
|
||||
@@ -536,9 +470,9 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
MessageId updateId = new MessageId(getRandomId());
|
||||
Map<MessageId, BdfDictionary> messageMetadata = singletonMap(updateId,
|
||||
BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", true)
|
||||
));
|
||||
BdfList update = BdfList.of("foo", 1, fooPropertiesDict);
|
||||
|
||||
@@ -571,7 +505,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
// There are no existing properties to merge with
|
||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
||||
localGroup.getId());
|
||||
will(returnValue(emptyMap()));
|
||||
will(returnValue(Collections.emptyMap()));
|
||||
// Store the new properties in the local group, version 1
|
||||
expectStoreMessage(txn, localGroup.getId(), "foo",
|
||||
fooPropertiesDict, 1, true, false);
|
||||
@@ -583,7 +517,7 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
will(returnValue(contactGroup));
|
||||
oneOf(clientHelper).getMessageMetadataAsDictionary(txn,
|
||||
contactGroup.getId());
|
||||
will(returnValue(emptyMap()));
|
||||
will(returnValue(Collections.emptyMap()));
|
||||
expectStoreMessage(txn, contactGroup.getId(), "foo",
|
||||
fooPropertiesDict, 1, true, true);
|
||||
}});
|
||||
@@ -598,9 +532,9 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
Contact contact = getContact();
|
||||
Group contactGroup = getGroup(CLIENT_ID, MAJOR_VERSION);
|
||||
BdfDictionary oldMetadata = BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
new BdfEntry(MSG_KEY_LOCAL, true)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 1),
|
||||
new BdfEntry("local", true)
|
||||
);
|
||||
MessageId localGroupUpdateId = new MessageId(getRandomId());
|
||||
Map<MessageId, BdfDictionary> localGroupMessageMetadata =
|
||||
@@ -655,14 +589,14 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
// The latest update for transport "foo" should be returned
|
||||
MessageId fooVersion999 = new MessageId(getRandomId());
|
||||
messageMetadata.put(fooVersion999, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "foo"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 999)
|
||||
new BdfEntry("transportId", "foo"),
|
||||
new BdfEntry("version", 999)
|
||||
));
|
||||
// The latest update for transport "bar" should be returned
|
||||
MessageId barVersion3 = new MessageId(getRandomId());
|
||||
messageMetadata.put(barVersion3, BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, "bar"),
|
||||
new BdfEntry(MSG_KEY_VERSION, 3)
|
||||
new BdfEntry("transportId", "bar"),
|
||||
new BdfEntry("version", 3)
|
||||
));
|
||||
BdfList fooUpdate = BdfList.of("foo", 999, fooPropertiesDict);
|
||||
BdfList barUpdate = BdfList.of("bar", 3, barPropertiesDict);
|
||||
@@ -693,9 +627,9 @@ public class TransportPropertyManagerImplTest extends BrambleMockTestCase {
|
||||
Message message = getMessage(g);
|
||||
long timestamp = message.getTimestamp();
|
||||
BdfDictionary meta = BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_TRANSPORT_ID, transportId),
|
||||
new BdfEntry(MSG_KEY_VERSION, version),
|
||||
new BdfEntry(MSG_KEY_LOCAL, local)
|
||||
new BdfEntry("transportId", transportId),
|
||||
new BdfEntry("version", version),
|
||||
new BdfEntry("local", local)
|
||||
);
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
|
||||
@@ -17,8 +17,8 @@ import org.briarproject.bramble.api.plugin.ConnectionManager;
|
||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||
@@ -49,7 +49,7 @@ import static org.briarproject.bramble.api.contact.PendingContactState.OFFLINE;
|
||||
import static org.briarproject.bramble.api.contact.PendingContactState.WAITING_FOR_CONNECTION;
|
||||
import static org.briarproject.bramble.rendezvous.RendezvousConstants.POLLING_INTERVAL_MS;
|
||||
import static org.briarproject.bramble.rendezvous.RendezvousConstants.RENDEZVOUS_TIMEOUT_MS;
|
||||
import static org.briarproject.bramble.test.CollectionMatcher.collectionOf;
|
||||
import static org.briarproject.bramble.test.ListMatcher.listOf;
|
||||
import static org.briarproject.bramble.test.PairMatcher.pairOf;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAgreementPrivateKey;
|
||||
import static org.briarproject.bramble.test.TestUtils.getAgreementPublicKey;
|
||||
@@ -178,10 +178,10 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
rendezvousPoller.startService();
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Enable the transport - no endpoints should be created yet
|
||||
// Activate the transport - no endpoints should be created yet
|
||||
expectGetPlugin();
|
||||
|
||||
rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId));
|
||||
rendezvousPoller.eventOccurred(new TransportActiveEvent(transportId));
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Add the pending contact - endpoint should be created and polled
|
||||
@@ -196,7 +196,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(beforeExpiry));
|
||||
oneOf(eventBus).broadcast(with(any(RendezvousPollEvent.class)));
|
||||
oneOf(plugin).poll(with(collectionOf(pairOf(
|
||||
oneOf(plugin).poll(with(listOf(pairOf(
|
||||
equal(transportProperties),
|
||||
any(ConnectionHandler.class)))));
|
||||
}});
|
||||
@@ -212,8 +212,8 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
new PendingContactRemovedEvent(pendingContact.getId()));
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Disable the transport - endpoint is already closed
|
||||
rendezvousPoller.eventOccurred(new TransportDisabledEvent(transportId));
|
||||
// Deactivate the transport - endpoint is already closed
|
||||
rendezvousPoller.eventOccurred(new TransportInactiveEvent(transportId));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -230,10 +230,10 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
rendezvousPoller.startService();
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Enable the transport - no endpoints should be created yet
|
||||
// Activate the transport - no endpoints should be created yet
|
||||
expectGetPlugin();
|
||||
|
||||
rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId));
|
||||
rendezvousPoller.eventOccurred(new TransportActiveEvent(transportId));
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Add the pending contact - endpoint should be created and polled
|
||||
@@ -248,7 +248,7 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
oneOf(clock).currentTimeMillis();
|
||||
will(returnValue(beforeExpiry));
|
||||
oneOf(eventBus).broadcast(with(any(RendezvousPollEvent.class)));
|
||||
oneOf(plugin).poll(with(collectionOf(pairOf(
|
||||
oneOf(plugin).poll(with(listOf(pairOf(
|
||||
equal(transportProperties),
|
||||
any(ConnectionHandler.class)))));
|
||||
}});
|
||||
@@ -269,12 +269,12 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
new PendingContactRemovedEvent(pendingContact.getId()));
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Disable the transport - endpoint is already closed
|
||||
rendezvousPoller.eventOccurred(new TransportDisabledEvent(transportId));
|
||||
// Deactivate the transport - endpoint is already closed
|
||||
rendezvousPoller.eventOccurred(new TransportInactiveEvent(transportId));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreatesAndClosesEndpointsWhenTransportIsEnabledAndDisabled()
|
||||
public void testCreatesAndClosesEndpointsWhenTransportIsActivatedAndDeactivated()
|
||||
throws Exception {
|
||||
long beforeExpiry = pendingContact.getTimestamp();
|
||||
|
||||
@@ -292,19 +292,19 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
||||
new PendingContactAddedEvent(pendingContact));
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Enable the transport - endpoint should be created
|
||||
// Activate the transport - endpoint should be created
|
||||
expectGetPlugin();
|
||||
expectCreateEndpoint();
|
||||
expectStateChangedEvent(WAITING_FOR_CONNECTION);
|
||||
|
||||
rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId));
|
||||
rendezvousPoller.eventOccurred(new TransportActiveEvent(transportId));
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Disable the transport - endpoint should be closed
|
||||
// Deactivate the transport - endpoint should be closed
|
||||
expectCloseEndpoint();
|
||||
expectStateChangedEvent(OFFLINE);
|
||||
|
||||
rendezvousPoller.eventOccurred(new TransportDisabledEvent(transportId));
|
||||
rendezvousPoller.eventOccurred(new TransportInactiveEvent(transportId));
|
||||
context.assertIsSatisfied();
|
||||
|
||||
// Remove the pending contact - endpoint is already closed
|
||||
|
||||
@@ -5,24 +5,24 @@ import org.hamcrest.BaseMatcher;
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@NotNullByDefault
|
||||
public class CollectionMatcher<T> extends BaseMatcher<Collection<T>> {
|
||||
public class ListMatcher<T> extends BaseMatcher<List<T>> {
|
||||
|
||||
private final Matcher<T> elementMatcher;
|
||||
|
||||
public CollectionMatcher(Matcher<T> elementMatcher) {
|
||||
public ListMatcher(Matcher<T> elementMatcher) {
|
||||
this.elementMatcher = elementMatcher;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(@Nullable Object item) {
|
||||
if (!(item instanceof Collection)) return false;
|
||||
Collection collection = (Collection) item;
|
||||
for (Object element : collection) {
|
||||
if (!(item instanceof List)) return false;
|
||||
List list = (List) item;
|
||||
for (Object element : list) {
|
||||
if (!elementMatcher.matches(element)) return false;
|
||||
}
|
||||
return true;
|
||||
@@ -33,7 +33,7 @@ public class CollectionMatcher<T> extends BaseMatcher<Collection<T>> {
|
||||
description.appendText("matches a collection");
|
||||
}
|
||||
|
||||
public static <T> CollectionMatcher<T> collectionOf(Matcher<T> t) {
|
||||
return new CollectionMatcher<>(t);
|
||||
public static <T> ListMatcher<T> listOf(Matcher<T> t) {
|
||||
return new ListMatcher<>(t);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -38,11 +37,6 @@ public class TestDuplexTransportConnection
|
||||
return writer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TransportProperties getRemoteProperties() {
|
||||
return new TransportProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates and returns a pair of TestDuplexTransportConnections that are
|
||||
* connected to each other.
|
||||
|
||||
@@ -16,7 +16,7 @@ dependencies {
|
||||
implementation fileTree(dir: 'libs', include: '*.jar')
|
||||
implementation 'net.java.dev.jna:jna:4.5.2'
|
||||
implementation 'net.java.dev.jna:jna-platform:4.5.2'
|
||||
tor 'org.briarproject:tor:0.3.5.10@zip'
|
||||
tor 'org.briarproject:tor:0.3.5.9@zip'
|
||||
tor 'org.briarproject:obfs4proxy:0.0.7@zip'
|
||||
|
||||
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
|
||||
|
||||
@@ -37,9 +37,9 @@ public class DesktopPluginModule extends PluginModule {
|
||||
backoffFactory);
|
||||
DuplexPluginFactory modem = new ModemPluginFactory(ioExecutor,
|
||||
reliabilityFactory);
|
||||
DuplexPluginFactory lan = new LanTcpPluginFactory(ioExecutor,
|
||||
DuplexPluginFactory lan = new LanTcpPluginFactory(ioExecutor, eventBus,
|
||||
backoffFactory);
|
||||
DuplexPluginFactory wan = new WanTcpPluginFactory(ioExecutor,
|
||||
DuplexPluginFactory wan = new WanTcpPluginFactory(ioExecutor, eventBus,
|
||||
backoffFactory, shutdownManager);
|
||||
Collection<DuplexPluginFactory> duplex =
|
||||
asList(bluetooth, modem, lan, wan);
|
||||
|
||||
@@ -4,8 +4,10 @@ import org.briarproject.bramble.api.Pair;
|
||||
import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||
import org.briarproject.bramble.api.plugin.DiscoveryHandler;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.PluginException;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
@@ -19,13 +21,20 @@ import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.concurrent.GuardedBy;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ENABLING;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||
|
||||
@@ -44,8 +53,8 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
private final PluginCallback callback;
|
||||
private final int maxLatency;
|
||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||
private final PluginState state = new PluginState();
|
||||
|
||||
private volatile boolean running = false;
|
||||
private volatile Modem modem = null;
|
||||
|
||||
ModemPlugin(ModemFactory modemFactory, SerialPortList serialPortList,
|
||||
@@ -75,6 +84,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
@Override
|
||||
public void start() throws PluginException {
|
||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||
state.setStarted();
|
||||
for (String portName : serialPortList.getPortNames()) {
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Trying to initialise modem on " + portName);
|
||||
@@ -83,18 +93,20 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
if (!modem.start()) continue;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Initialised modem on " + portName);
|
||||
running = true;
|
||||
state.setInitialised();
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
LOG.warning("Failed to initialised modem");
|
||||
state.setFailed();
|
||||
throw new PluginException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
running = false;
|
||||
state.setStopped();
|
||||
if (modem != null) {
|
||||
try {
|
||||
modem.stop();
|
||||
@@ -105,8 +117,13 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
public State getState() {
|
||||
return state.getState();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getReasonsDisabled() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -120,13 +137,13 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||
properties) {
|
||||
public void poll(
|
||||
List<Pair<TransportProperties, ConnectionHandler>> properties) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private boolean resetModem() {
|
||||
if (!running) return false;
|
||||
private void resetModem() {
|
||||
if (getState() != ACTIVE) return;
|
||||
for (String portName : serialPortList.getPortNames()) {
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Trying to initialise modem on " + portName);
|
||||
@@ -135,18 +152,18 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
if (!modem.start()) continue;
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Initialised modem on " + portName);
|
||||
return true;
|
||||
return;
|
||||
} catch (IOException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
}
|
||||
running = false;
|
||||
return false;
|
||||
LOG.warning("Failed to initialise modem");
|
||||
state.setFailed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||
if (!running) return null;
|
||||
if (getState() != ACTIVE) return null;
|
||||
// Get the ISO 3166 code for the caller's country
|
||||
String fromIso = callback.getLocalProperties().get("iso3166");
|
||||
if (isNullOrEmpty(fromIso)) return null;
|
||||
@@ -197,6 +214,17 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsDiscovery() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void discoverPeers(
|
||||
List<Pair<TransportProperties, DiscoveryHandler>> properties) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void incomingCallConnected() {
|
||||
LOG.info("Incoming call connected");
|
||||
@@ -232,4 +260,41 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
||||
if (exception) resetModem();
|
||||
}
|
||||
}
|
||||
|
||||
@ThreadSafe
|
||||
@NotNullByDefault
|
||||
private class PluginState {
|
||||
|
||||
@GuardedBy("this")
|
||||
private boolean started = false,
|
||||
stopped = false,
|
||||
initialised = false,
|
||||
failed = false;
|
||||
|
||||
private synchronized void setStarted() {
|
||||
started = true;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
private synchronized void setStopped() {
|
||||
stopped = true;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
private synchronized void setInitialised() {
|
||||
initialised = true;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
private synchronized void setFailed() {
|
||||
failed = true;
|
||||
callback.pluginStateChanged(getState());
|
||||
}
|
||||
|
||||
private State getState() {
|
||||
if (!started || stopped) return STARTING_STOPPING;
|
||||
if (failed) return INACTIVE;
|
||||
return initialised ? ACTIVE : ENABLING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,8 @@ import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ENABLING;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
@@ -33,6 +35,7 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
||||
@Test
|
||||
public void testModemCreation() throws Exception {
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(callback).pluginStateChanged(ENABLING);
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] {"foo", "bar", "baz"}));
|
||||
// First call to createModem() returns false
|
||||
@@ -50,6 +53,7 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
oneOf(callback).pluginStateChanged(ACTIVE);
|
||||
}});
|
||||
|
||||
plugin.start();
|
||||
@@ -65,12 +69,14 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(callback).pluginStateChanged(ENABLING);
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] {"foo"}));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
oneOf(callback).pluginStateChanged(ACTIVE);
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
@@ -93,12 +99,14 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(callback).pluginStateChanged(ENABLING);
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] {"foo"}));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
oneOf(callback).pluginStateChanged(ACTIVE);
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
@@ -121,12 +129,14 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
// start()
|
||||
oneOf(callback).pluginStateChanged(ENABLING);
|
||||
oneOf(serialPortList).getPortNames();
|
||||
will(returnValue(new String[] {"foo"}));
|
||||
oneOf(modemFactory).createModem(plugin, "foo");
|
||||
will(returnValue(modem));
|
||||
oneOf(modem).start();
|
||||
will(returnValue(true));
|
||||
oneOf(callback).pluginStateChanged(ACTIVE);
|
||||
// createConnection()
|
||||
oneOf(callback).getLocalProperties();
|
||||
will(returnValue(local));
|
||||
|
||||
@@ -32,6 +32,7 @@ import javax.net.SocketFactory;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
||||
import static org.briarproject.bramble.test.TestUtils.isOptionalTestEnabled;
|
||||
@@ -141,10 +142,10 @@ public class BridgeTest extends BrambleTestCase {
|
||||
plugin.start();
|
||||
long start = clock.currentTimeMillis();
|
||||
while (clock.currentTimeMillis() - start < TIMEOUT) {
|
||||
if (plugin.isRunning()) return;
|
||||
if (plugin.getState() == ACTIVE) return;
|
||||
clock.sleep(500);
|
||||
}
|
||||
if (!plugin.isRunning()) {
|
||||
if (plugin.getState() != ACTIVE) {
|
||||
fail("Could not connect to Tor within timeout.");
|
||||
}
|
||||
} finally {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package org.briarproject.bramble.plugin.tor;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||
@@ -30,11 +31,7 @@ public class TestPluginCallback implements PluginCallback {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transportEnabled() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transportDisabled() {
|
||||
public void pluginStateChanged(State state) {
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -24,7 +24,7 @@ dependencyVerification {
|
||||
'org.apache.ant:ant:1.9.4:ant-1.9.4.jar:649ae0730251de07b8913f49286d46bba7b92d47c5f332610aa426c4f02161d8',
|
||||
'org.beanshell:bsh:1.3.0:bsh-1.3.0.jar:9b04edc75d19db54f1b4e8b5355e9364384c6cf71eb0a1b9724c159d779879f8',
|
||||
'org.briarproject:obfs4proxy:0.0.7:obfs4proxy-0.0.7.zip:5b2f693262ce43a7e130f7cc7d5d1617925330640a2eb6d71085e95df8ee0642',
|
||||
'org.briarproject:tor:0.3.5.10:tor-0.3.5.10.zip:7b387d3523ae8af289c23be59dc4c64ec5d3721385d7825a09705095e3318d5c',
|
||||
'org.briarproject:tor:0.3.5.9:tor-0.3.5.9.zip:6c3994b129db019cc23caaf50d6b4383903c40d05fbc47fc94211170a3e5d38c',
|
||||
'org.checkerframework:checker-compat-qual:2.5.3:checker-compat-qual-2.5.3.jar:d76b9afea61c7c082908023f0cbc1427fab9abd2df915c8b8a3e7a509bccbc6d',
|
||||
'org.checkerframework:checker-qual:2.5.2:checker-qual-2.5.2.jar:64b02691c8b9d4e7700f8ee2e742dce7ea2c6e81e662b7522c9ee3bf568c040a',
|
||||
'org.codehaus.mojo:animal-sniffer-annotations:1.17:animal-sniffer-annotations-1.17.jar:92654f493ecfec52082e76354f0ebf87648dc3d5cec2e3c3cdb947c016747a53',
|
||||
|
||||
@@ -28,9 +28,7 @@ import static android.security.keystore.KeyProperties.PURPOSE_SIGN;
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.singletonList;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
|
||||
@RequiresApi(23)
|
||||
@NotNullByDefault
|
||||
@@ -81,10 +79,7 @@ class AndroidKeyStrengthener implements KeyStrengthener {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (GeneralSecurityException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
return false;
|
||||
} catch (IOException e) {
|
||||
} catch (GeneralSecurityException | IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ import org.briarproject.bramble.util.AndroidUtils;
|
||||
import org.briarproject.bramble.util.StringUtils;
|
||||
import org.briarproject.briar.android.account.LockManagerImpl;
|
||||
import org.briarproject.briar.android.keyagreement.ContactExchangeModule;
|
||||
import org.briarproject.briar.android.login.LoginModule;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerModule;
|
||||
import org.briarproject.briar.android.viewmodel.ViewModelModule;
|
||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||
import org.briarproject.briar.api.android.DozeWatchdog;
|
||||
@@ -66,8 +66,8 @@ import static org.briarproject.bramble.api.reporting.ReportingConstants.DEV_PUBL
|
||||
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||
|
||||
@Module(includes = {
|
||||
NavDrawerModule.class,
|
||||
ContactExchangeModule.class,
|
||||
LoginModule.class,
|
||||
ViewModelModule.class
|
||||
})
|
||||
public class AppModule {
|
||||
|
||||
@@ -8,8 +8,8 @@ import org.briarproject.briar.android.controller.BriarController;
|
||||
import org.briarproject.briar.android.controller.BriarControllerImpl;
|
||||
import org.briarproject.briar.android.controller.DbController;
|
||||
import org.briarproject.briar.android.controller.DbControllerImpl;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerController;
|
||||
import org.briarproject.briar.android.navdrawer.NavDrawerControllerImpl;
|
||||
import org.briarproject.briar.android.login.ChangePasswordController;
|
||||
import org.briarproject.briar.android.login.ChangePasswordControllerImpl;
|
||||
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
@@ -44,6 +44,13 @@ public class ActivityModule {
|
||||
return setupController;
|
||||
}
|
||||
|
||||
@ActivityScope
|
||||
@Provides
|
||||
ChangePasswordController providePasswordController(
|
||||
ChangePasswordControllerImpl passwordController) {
|
||||
return passwordController;
|
||||
}
|
||||
|
||||
@ActivityScope
|
||||
@Provides
|
||||
protected BriarController provideBriarController(
|
||||
@@ -58,17 +65,10 @@ public class ActivityModule {
|
||||
return dbController;
|
||||
}
|
||||
|
||||
@ActivityScope
|
||||
@Provides
|
||||
NavDrawerController provideNavDrawerController(
|
||||
NavDrawerControllerImpl navDrawerController) {
|
||||
activity.addLifecycleController(navDrawerController);
|
||||
return navDrawerController;
|
||||
}
|
||||
|
||||
@ActivityScope
|
||||
@Provides
|
||||
BriarServiceConnection provideBriarServiceConnection() {
|
||||
return new BriarServiceConnection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -92,9 +92,6 @@ public abstract class BaseActivity extends AppCompatActivity
|
||||
.build();
|
||||
injectActivity(activityComponent);
|
||||
super.onCreate(state);
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Creating " + getClass().getSimpleName());
|
||||
}
|
||||
|
||||
// WARNING: When removing this or making it possible to turn it off,
|
||||
// we need a solution for the app lock feature.
|
||||
@@ -130,9 +127,8 @@ public abstract class BaseActivity extends AppCompatActivity
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Starting " + getClass().getSimpleName());
|
||||
}
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Starting " + this.getClass().getSimpleName());
|
||||
for (ActivityLifecycleController alc : lifecycleControllers) {
|
||||
alc.onActivityStart();
|
||||
}
|
||||
@@ -148,28 +144,11 @@ public abstract class BaseActivity extends AppCompatActivity
|
||||
return (ScreenFilterDialogFragment) f;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Resuming " + getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Pausing " + getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Stopping " + getClass().getSimpleName());
|
||||
}
|
||||
if (LOG.isLoggable(INFO))
|
||||
LOG.info("Stopping " + this.getClass().getSimpleName());
|
||||
for (ActivityLifecycleController alc : lifecycleControllers) {
|
||||
alc.onActivityStop();
|
||||
}
|
||||
@@ -224,9 +203,6 @@ public abstract class BaseActivity extends AppCompatActivity
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Destroying " + getClass().getSimpleName());
|
||||
}
|
||||
destroyed = true;
|
||||
for (ActivityLifecycleController alc : lifecycleControllers) {
|
||||
alc.onActivityDestroy();
|
||||
|
||||
@@ -95,14 +95,12 @@ public abstract class BriarActivity extends BaseActivity {
|
||||
// Also check that the activity isn't finishing already.
|
||||
// This is possible if we finished in onActivityResult().
|
||||
// Launching another StartupActivity would cause a loop.
|
||||
LOG.info("Not signed in, launching StartupActivity");
|
||||
Intent i = new Intent(this, StartupActivity.class);
|
||||
startActivityForResult(i, REQUEST_PASSWORD);
|
||||
} else if (lockManager.isLocked() && !isFinishing()) {
|
||||
// Also check that the activity isn't finishing already.
|
||||
// This is possible if we finished in onActivityResult().
|
||||
// Launching another UnlockActivity would cause a loop.
|
||||
LOG.info("Locked, launching UnlockActivity");
|
||||
Intent i = new Intent(this, UnlockActivity.class);
|
||||
startActivityForResult(i, REQUEST_UNLOCK);
|
||||
} else if (SDK_INT >= 23) {
|
||||
|
||||
@@ -2,38 +2,35 @@ package org.briarproject.briar.android.contact;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
||||
|
||||
import javax.annotation.concurrent.NotThreadSafe;
|
||||
|
||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.DISCONNECTED;
|
||||
|
||||
@NotThreadSafe
|
||||
@NotNullByDefault
|
||||
public class ContactItem {
|
||||
|
||||
private final Contact contact;
|
||||
private ConnectionStatus status;
|
||||
private boolean connected;
|
||||
|
||||
public ContactItem(Contact contact) {
|
||||
this(contact, DISCONNECTED);
|
||||
this(contact, false);
|
||||
}
|
||||
|
||||
public ContactItem(Contact contact, ConnectionStatus status) {
|
||||
public ContactItem(Contact contact, boolean connected) {
|
||||
this.contact = contact;
|
||||
this.status = status;
|
||||
this.connected = connected;
|
||||
}
|
||||
|
||||
public Contact getContact() {
|
||||
return contact;
|
||||
}
|
||||
|
||||
ConnectionStatus getConnectionStatus() {
|
||||
return status;
|
||||
boolean isConnected() {
|
||||
return connected;
|
||||
}
|
||||
|
||||
void setConnectionStatus(ConnectionStatus status) {
|
||||
this.status = status;
|
||||
void setConnected(boolean connected) {
|
||||
this.connected = connected;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import android.widget.TextView;
|
||||
|
||||
import org.briarproject.bramble.api.identity.Author;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
|
||||
@@ -17,8 +16,6 @@ import androidx.annotation.UiThread;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
import im.delight.android.identicons.IdenticonDrawable;
|
||||
|
||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.CONNECTED;
|
||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.RECENTLY_CONNECTED;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
|
||||
|
||||
@UiThread
|
||||
@@ -30,7 +27,7 @@ public class ContactItemViewHolder<I extends ContactItem>
|
||||
protected final ImageView avatar;
|
||||
protected final TextView name;
|
||||
@Nullable
|
||||
private final ImageView bulb;
|
||||
protected final ImageView bulb;
|
||||
|
||||
public ContactItemViewHolder(View v) {
|
||||
super(v);
|
||||
@@ -50,13 +47,10 @@ public class ContactItemViewHolder<I extends ContactItem>
|
||||
|
||||
if (bulb != null) {
|
||||
// online/offline
|
||||
ConnectionStatus status = item.getConnectionStatus();
|
||||
if (status == CONNECTED) {
|
||||
bulb.setImageResource(R.drawable.ic_connected);
|
||||
} else if (status == RECENTLY_CONNECTED) {
|
||||
bulb.setImageResource(R.drawable.ic_recently_connected);
|
||||
if (item.isConnected()) {
|
||||
bulb.setImageResource(R.drawable.contact_connected);
|
||||
} else {
|
||||
bulb.setImageResource(R.drawable.ic_disconnected);
|
||||
bulb.setImageResource(R.drawable.contact_disconnected);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ public class ContactListAdapter extends
|
||||
if (c1.getTimestamp() != c2.getTimestamp()) {
|
||||
return false;
|
||||
}
|
||||
return c1.getConnectionStatus() == c2.getConnectionStatus();
|
||||
return c1.isConnected() == c2.isConnected();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -25,8 +25,8 @@ import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
@@ -53,6 +53,7 @@ import javax.inject.Inject;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.app.ActivityOptionsCompat;
|
||||
import androidx.core.util.Pair;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
import io.github.kobakei.materialfabspeeddial.FabSpeedDial;
|
||||
import io.github.kobakei.materialfabspeeddial.FabSpeedDial.OnMenuItemClickListener;
|
||||
@@ -136,15 +137,26 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
||||
ContactId contactId = item.getContact().getId();
|
||||
i.putExtra(CONTACT_ID, contactId.getInt());
|
||||
|
||||
Bundle options = null;
|
||||
// work-around for android bug #224270
|
||||
if (SDK_INT >= 23 && !isSamsung7()) {
|
||||
options = makeTransitionOptions(view);
|
||||
}
|
||||
if (options == null) {
|
||||
startActivity(i);
|
||||
ContactListItemViewHolder holder =
|
||||
(ContactListItemViewHolder) list
|
||||
.getRecyclerView()
|
||||
.findViewHolderForAdapterPosition(
|
||||
adapter.findItemPosition(item));
|
||||
Pair<View, String> avatar =
|
||||
Pair.create(holder.avatar,
|
||||
getTransitionName(holder.avatar));
|
||||
Pair<View, String> bulb =
|
||||
Pair.create(holder.bulb,
|
||||
getTransitionName(holder.bulb));
|
||||
ActivityOptionsCompat options =
|
||||
makeSceneTransitionAnimation(getActivity(),
|
||||
avatar, bulb);
|
||||
ActivityCompat.startActivity(getActivity(), i,
|
||||
options.toBundle());
|
||||
} else {
|
||||
ActivityCompat.startActivity(getActivity(), i, options);
|
||||
// work-around for android bug #224270
|
||||
startActivity(i);
|
||||
}
|
||||
};
|
||||
adapter = new ContactListAdapter(requireContext(),
|
||||
@@ -159,15 +171,6 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
||||
return contentView;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Bundle makeTransitionOptions(View view) {
|
||||
View avatar = view.findViewById(R.id.avatarView);
|
||||
String name = requireNonNull(getTransitionName(avatar));
|
||||
ActivityOptionsCompat options = makeSceneTransitionAnimation(
|
||||
requireActivity(), view, name);
|
||||
return options.toBundle();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMenuItemClick(FloatingActionButton fab, @Nullable TextView v,
|
||||
int itemId) {
|
||||
@@ -229,9 +232,9 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
||||
ContactId id = c.getId();
|
||||
GroupCount count =
|
||||
conversationManager.getGroupCount(id);
|
||||
ConnectionStatus status = connectionRegistry
|
||||
.getConnectionStatus(c.getId());
|
||||
contacts.add(new ContactListItem(c, status, count));
|
||||
boolean connected =
|
||||
connectionRegistry.isConnected(c.getId());
|
||||
contacts.add(new ContactListItem(c, connected, count));
|
||||
} catch (NoSuchContactException e) {
|
||||
// Continue
|
||||
}
|
||||
@@ -262,9 +265,10 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
||||
if (e instanceof ContactAddedEvent) {
|
||||
LOG.info("Contact added, reloading");
|
||||
loadContacts();
|
||||
} else if (e instanceof ConnectionStatusChangedEvent) {
|
||||
ConnectionStatusChangedEvent c = (ConnectionStatusChangedEvent) e;
|
||||
setConnectionStatus(c.getContactId(), c.getConnectionStatus());
|
||||
} else if (e instanceof ContactConnectedEvent) {
|
||||
setConnected(((ContactConnectedEvent) e).getContactId(), true);
|
||||
} else if (e instanceof ContactDisconnectedEvent) {
|
||||
setConnected(((ContactDisconnectedEvent) e).getContactId(), false);
|
||||
} else if (e instanceof ContactRemovedEvent) {
|
||||
LOG.info("Contact removed, removing item");
|
||||
removeItem(((ContactRemovedEvent) e).getContactId());
|
||||
@@ -300,12 +304,12 @@ public class ContactListFragment extends BaseFragment implements EventListener,
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void setConnectionStatus(ContactId c, ConnectionStatus status) {
|
||||
private void setConnected(ContactId c, boolean connected) {
|
||||
adapter.incrementRevision();
|
||||
int position = adapter.findItemPosition(c);
|
||||
ContactListItem item = adapter.getItemAt(position);
|
||||
if (item != null) {
|
||||
item.setConnectionStatus(status);
|
||||
item.setConnected(connected);
|
||||
adapter.updateItemAt(position, item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.briar.android.contact;
|
||||
|
||||
import org.briarproject.bramble.api.contact.Contact;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
||||
import org.briarproject.briar.api.client.MessageTracker.GroupCount;
|
||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
|
||||
@@ -16,9 +15,9 @@ public class ContactListItem extends ContactItem {
|
||||
private long timestamp;
|
||||
private int unread;
|
||||
|
||||
public ContactListItem(Contact contact, ConnectionStatus status,
|
||||
public ContactListItem(Contact contact, boolean connected,
|
||||
GroupCount count) {
|
||||
super(contact, status);
|
||||
super(contact, connected);
|
||||
this.empty = count.getMsgCount() == 0;
|
||||
this.unread = count.getUnreadCount();
|
||||
this.timestamp = count.getLatestMsgTime();
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
import org.briarproject.briar.android.util.UiUtils;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@@ -14,11 +15,8 @@ import javax.annotation.Nullable;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static androidx.core.view.ViewCompat.setTransitionName;
|
||||
import static org.briarproject.briar.android.util.UiUtils.formatDate;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getAvatarTransitionName;
|
||||
|
||||
@UiThread
|
||||
@NotNullByDefault
|
||||
@@ -41,11 +39,10 @@ class ContactListItemViewHolder extends ContactItemViewHolder<ContactListItem> {
|
||||
// unread count
|
||||
int unreadCount = item.getUnreadCount();
|
||||
if (unreadCount > 0) {
|
||||
unread.setText(String.format(Locale.getDefault(), "%d",
|
||||
unreadCount));
|
||||
unread.setVisibility(VISIBLE);
|
||||
unread.setText(String.format(Locale.getDefault(), "%d", unreadCount));
|
||||
unread.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
unread.setVisibility(INVISIBLE);
|
||||
unread.setVisibility(View.INVISIBLE);
|
||||
}
|
||||
|
||||
// date of last message
|
||||
@@ -57,7 +54,8 @@ class ContactListItemViewHolder extends ContactItemViewHolder<ContactListItem> {
|
||||
}
|
||||
|
||||
ContactId c = item.getContact().getId();
|
||||
setTransitionName(avatar, getAvatarTransitionName(c));
|
||||
setTransitionName(avatar, UiUtils.getAvatarTransitionName(c));
|
||||
setTransitionName(bulb, UiUtils.getBulbTransitionName(c));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.briarproject.briar.android.contact.add.remote;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.text.Editable;
|
||||
import android.view.LayoutInflater;
|
||||
@@ -38,12 +37,10 @@ import androidx.lifecycle.ViewModelProviders;
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static androidx.core.content.ContextCompat.getColor;
|
||||
import static androidx.core.content.ContextCompat.getDrawable;
|
||||
import static androidx.core.graphics.drawable.DrawableCompat.setTint;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.util.StringUtils.utf8IsTooLong;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getDialogIcon;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
@@ -199,9 +196,7 @@ public class NicknameFragment extends BaseFragment {
|
||||
private void showWarningDialog(String name1, String name2) {
|
||||
Context ctx = requireContext();
|
||||
Builder b = new Builder(ctx, R.style.BriarDialogTheme);
|
||||
Drawable icon = getDrawable(ctx, R.drawable.alerts_and_states_error);
|
||||
setTint(requireNonNull(icon), getColor(ctx, R.color.color_primary));
|
||||
b.setIcon(icon);
|
||||
b.setIcon(getDialogIcon(ctx, R.drawable.alerts_and_states_error));
|
||||
b.setTitle(getString(R.string.duplicate_link_dialog_title));
|
||||
b.setMessage(
|
||||
getString(R.string.duplicate_link_dialog_text_3, name1, name2));
|
||||
|
||||
@@ -6,7 +6,8 @@ import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
@@ -17,8 +18,6 @@ import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.CONNECTED;
|
||||
|
||||
@NotNullByDefault
|
||||
public class SharingControllerImpl implements SharingController, EventListener {
|
||||
|
||||
@@ -61,14 +60,15 @@ public class SharingControllerImpl implements SharingController, EventListener {
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof ConnectionStatusChangedEvent) {
|
||||
ConnectionStatusChangedEvent c = (ConnectionStatusChangedEvent) e;
|
||||
setConnectionStatus(c.getContactId());
|
||||
if (e instanceof ContactConnectedEvent) {
|
||||
setConnected(((ContactConnectedEvent) e).getContactId());
|
||||
} else if (e instanceof ContactDisconnectedEvent) {
|
||||
setConnected(((ContactDisconnectedEvent) e).getContactId());
|
||||
}
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void setConnectionStatus(ContactId c) {
|
||||
private void setConnected(ContactId c) {
|
||||
if (listener == null) throw new IllegalStateException();
|
||||
if (contacts.contains(c)) {
|
||||
int online = getOnlineCount();
|
||||
@@ -95,9 +95,7 @@ public class SharingControllerImpl implements SharingController, EventListener {
|
||||
public int getOnlineCount() {
|
||||
int online = 0;
|
||||
for (ContactId c : contacts) {
|
||||
if (connectionRegistry.getConnectionStatus(c) == CONNECTED) {
|
||||
online++;
|
||||
}
|
||||
if (connectionRegistry.isConnected(c)) online++;
|
||||
}
|
||||
return online;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
@@ -33,8 +34,8 @@ import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
||||
import org.briarproject.bramble.api.plugin.event.ConnectionStatusChangedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactConnectedEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.ContactDisconnectedEvent;
|
||||
import org.briarproject.bramble.api.sync.ClientId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.bramble.api.sync.event.MessagesAckedEvent;
|
||||
@@ -124,8 +125,6 @@ import static java.util.Objects.requireNonNull;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.CONNECTED;
|
||||
import static org.briarproject.bramble.api.plugin.ConnectionStatus.RECENTLY_CONNECTED;
|
||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.bramble.util.LogUtils.now;
|
||||
@@ -139,6 +138,7 @@ import static org.briarproject.briar.android.conversation.ImageActivity.ATTACHME
|
||||
import static org.briarproject.briar.android.conversation.ImageActivity.DATE;
|
||||
import static org.briarproject.briar.android.conversation.ImageActivity.NAME;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getAvatarTransitionName;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getBulbTransitionName;
|
||||
import static org.briarproject.briar.android.util.UiUtils.observeOnce;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_ATTACHMENTS_PER_MESSAGE;
|
||||
import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_PRIVATE_MESSAGE_TEXT_LENGTH;
|
||||
@@ -198,8 +198,8 @@ public class ConversationActivity extends BriarActivity
|
||||
private ConversationAdapter adapter;
|
||||
private Toolbar toolbar;
|
||||
private CircleImageView toolbarAvatar;
|
||||
private ImageView toolbarStatus;
|
||||
private TextView toolbarTitle;
|
||||
private TextView toolbarStatus;
|
||||
private BriarRecyclerView list;
|
||||
private LinearLayoutManager layoutManager;
|
||||
private TextInputView textInputView;
|
||||
@@ -237,8 +237,8 @@ public class ConversationActivity extends BriarActivity
|
||||
// Custom Toolbar
|
||||
toolbar = requireNonNull(setUpCustomToolbar(true));
|
||||
toolbarAvatar = toolbar.findViewById(R.id.contactAvatar);
|
||||
toolbarTitle = toolbar.findViewById(R.id.contactName);
|
||||
toolbarStatus = toolbar.findViewById(R.id.contactStatus);
|
||||
toolbarTitle = toolbar.findViewById(R.id.contactName);
|
||||
|
||||
observeOnce(viewModel.getContactAuthorId(), this, authorId -> {
|
||||
requireNonNull(authorId);
|
||||
@@ -257,6 +257,7 @@ public class ConversationActivity extends BriarActivity
|
||||
this::onAddedPrivateMessage);
|
||||
|
||||
setTransitionName(toolbarAvatar, getAvatarTransitionName(contactId));
|
||||
setTransitionName(toolbarStatus, getBulbTransitionName(contactId));
|
||||
|
||||
visitor = new ConversationVisitor(this, this, this,
|
||||
viewModel.getContactDisplayName());
|
||||
@@ -498,14 +499,14 @@ public class ConversationActivity extends BriarActivity
|
||||
|
||||
@UiThread
|
||||
private void displayContactOnlineStatus() {
|
||||
ConnectionStatus status =
|
||||
connectionRegistry.getConnectionStatus(contactId);
|
||||
if (status == CONNECTED) {
|
||||
toolbarStatus.setText(R.string.online);
|
||||
} else if (status == RECENTLY_CONNECTED) {
|
||||
toolbarStatus.setText(R.string.recently_online);
|
||||
if (connectionRegistry.isConnected(contactId)) {
|
||||
toolbarStatus.setImageDrawable(ContextCompat.getDrawable(
|
||||
ConversationActivity.this, R.drawable.contact_online));
|
||||
toolbarStatus.setContentDescription(getString(R.string.online));
|
||||
} else {
|
||||
toolbarStatus.setText(R.string.offline);
|
||||
toolbarStatus.setImageDrawable(ContextCompat.getDrawable(
|
||||
ConversationActivity.this, R.drawable.contact_offline));
|
||||
toolbarStatus.setContentDescription(getString(R.string.offline));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -728,10 +729,16 @@ public class ConversationActivity extends BriarActivity
|
||||
LOG.info("Messages acked");
|
||||
markMessages(m.getMessageIds(), true, true);
|
||||
}
|
||||
} else if (e instanceof ConnectionStatusChangedEvent) {
|
||||
ConnectionStatusChangedEvent c = (ConnectionStatusChangedEvent) e;
|
||||
} else if (e instanceof ContactConnectedEvent) {
|
||||
ContactConnectedEvent c = (ContactConnectedEvent) e;
|
||||
if (c.getContactId().equals(contactId)) {
|
||||
LOG.info("Connection status changed");
|
||||
LOG.info("Contact connected");
|
||||
displayContactOnlineStatus();
|
||||
}
|
||||
} else if (e instanceof ContactDisconnectedEvent) {
|
||||
ContactDisconnectedEvent c = (ContactDisconnectedEvent) e;
|
||||
if (c.getContactId().equals(contactId)) {
|
||||
LOG.info("Contact disconnected");
|
||||
displayContactOnlineStatus();
|
||||
}
|
||||
} else if (e instanceof ClientVersionUpdatedEvent) {
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.briarproject.briar.android.conversation;
|
||||
|
||||
import android.content.DialogInterface.OnClickListener;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.transition.Fade;
|
||||
import android.transition.Transition;
|
||||
@@ -35,8 +34,6 @@ import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.appcompat.app.AlertDialog.Builder;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.core.graphics.drawable.DrawableCompat;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentStatePagerAdapter;
|
||||
@@ -59,6 +56,7 @@ import static com.google.android.material.snackbar.Snackbar.LENGTH_LONG;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_SAVE_ATTACHMENT;
|
||||
import static org.briarproject.briar.android.util.UiUtils.formatDateAbsolute;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getDialogIcon;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
@@ -278,10 +276,7 @@ public class ImageActivity extends BriarActivity
|
||||
Builder builder = new Builder(this, R.style.BriarDialogTheme);
|
||||
builder.setTitle(getString(R.string.dialog_title_save_image));
|
||||
builder.setMessage(getString(R.string.dialog_message_save_image));
|
||||
Drawable icon = ContextCompat.getDrawable(this, R.drawable.ic_security);
|
||||
DrawableCompat.setTint(requireNonNull(icon),
|
||||
ContextCompat.getColor(this, R.color.color_primary));
|
||||
builder.setIcon(icon);
|
||||
builder.setIcon(getDialogIcon(this, R.drawable.ic_security));
|
||||
builder.setPositiveButton(R.string.save_image, okListener);
|
||||
builder.setNegativeButton(R.string.cancel, null);
|
||||
builder.show();
|
||||
|
||||
@@ -12,7 +12,6 @@ import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionRegistry;
|
||||
import org.briarproject.bramble.api.plugin.ConnectionStatus;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.contact.BaseContactListAdapter.OnContactClickListener;
|
||||
@@ -74,8 +73,7 @@ public class ContactChooserFragment extends BaseFragment {
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
|
||||
View contentView = inflater.inflate(R.layout.list, container, false);
|
||||
@@ -129,9 +127,9 @@ public class ContactChooserFragment extends BaseFragment {
|
||||
ContactId id = c.getId();
|
||||
GroupCount count =
|
||||
conversationManager.getGroupCount(id);
|
||||
ConnectionStatus status = connectionRegistry
|
||||
.getConnectionStatus(c.getId());
|
||||
contacts.add(new ContactListItem(c, status, count));
|
||||
boolean connected =
|
||||
connectionRegistry.isConnected(c.getId());
|
||||
contacts.add(new ContactListItem(c, connected, count));
|
||||
}
|
||||
}
|
||||
displayContacts(contacts);
|
||||
|
||||
@@ -9,6 +9,7 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
@@ -79,13 +80,13 @@ public class ContactExchangeActivity extends KeyAgreementActivity {
|
||||
|
||||
@UiThread
|
||||
private void contactExchangeFailed() {
|
||||
showErrorFragment(R.string.connection_error_explanation);
|
||||
showErrorFragment();
|
||||
}
|
||||
|
||||
@UiThread
|
||||
@Override
|
||||
public void keyAgreementFailed() {
|
||||
showErrorFragment(R.string.connection_error_explanation);
|
||||
showErrorFragment();
|
||||
}
|
||||
|
||||
@UiThread
|
||||
@@ -103,7 +104,7 @@ public class ContactExchangeActivity extends KeyAgreementActivity {
|
||||
@UiThread
|
||||
@Override
|
||||
public void keyAgreementAborted(boolean remoteAborted) {
|
||||
showErrorFragment(R.string.connection_error_explanation);
|
||||
showErrorFragment();
|
||||
}
|
||||
|
||||
@UiThread
|
||||
@@ -112,4 +113,10 @@ public class ContactExchangeActivity extends KeyAgreementActivity {
|
||||
startContactExchange(result);
|
||||
return getString(R.string.exchanging_contact_details);
|
||||
}
|
||||
|
||||
protected void showErrorFragment() {
|
||||
String errorMsg = getString(R.string.connection_error_explanation);
|
||||
BaseFragment f = ContactExchangeErrorFragment.newInstance(errorMsg);
|
||||
showNextFragment(f);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,10 +8,18 @@ import android.content.IntentFilter;
|
||||
import android.os.Bundle;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
||||
import org.briarproject.bramble.api.plugin.LanTcpConstants;
|
||||
import org.briarproject.bramble.api.plugin.Plugin;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||
import org.briarproject.bramble.api.plugin.event.BluetoothEnabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.activity.BriarActivity;
|
||||
@@ -37,13 +45,15 @@ import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
|
||||
import static android.Manifest.permission.CAMERA;
|
||||
import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
|
||||
import static android.bluetooth.BluetoothAdapter.ACTION_SCAN_MODE_CHANGED;
|
||||
import static android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED;
|
||||
import static android.bluetooth.BluetoothAdapter.EXTRA_SCAN_MODE;
|
||||
import static android.bluetooth.BluetoothAdapter.EXTRA_STATE;
|
||||
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE;
|
||||
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
|
||||
import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
||||
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.INACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_BLUETOOTH_DISCOVERABLE;
|
||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PERMISSION_CAMERA_LOCATION;
|
||||
|
||||
@@ -51,10 +61,33 @@ import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PERMI
|
||||
@ParametersNotNullByDefault
|
||||
public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
BaseFragmentListener, IntroScreenSeenListener,
|
||||
KeyAgreementEventListener {
|
||||
KeyAgreementEventListener, EventListener {
|
||||
|
||||
private enum BluetoothState {
|
||||
UNKNOWN, NO_ADAPTER, WAITING, REFUSED, ENABLED, DISCOVERABLE
|
||||
private enum BluetoothDecision {
|
||||
/**
|
||||
* We haven't asked the user about Bluetooth discoverability.
|
||||
*/
|
||||
UNKNOWN,
|
||||
|
||||
/**
|
||||
* The device doesn't have a Bluetooth adapter.
|
||||
*/
|
||||
NO_ADAPTER,
|
||||
|
||||
/**
|
||||
* We're waiting for the user to accept or refuse discoverability.
|
||||
*/
|
||||
WAITING,
|
||||
|
||||
/**
|
||||
* The user has accepted discoverability.
|
||||
*/
|
||||
ACCEPTED,
|
||||
|
||||
/**
|
||||
* The user has refused discoverability.
|
||||
*/
|
||||
REFUSED
|
||||
}
|
||||
|
||||
private enum Permission {
|
||||
@@ -62,11 +95,14 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
}
|
||||
|
||||
private static final Logger LOG =
|
||||
Logger.getLogger(KeyAgreementActivity.class.getName());
|
||||
getLogger(KeyAgreementActivity.class.getName());
|
||||
|
||||
@Inject
|
||||
EventBus eventBus;
|
||||
|
||||
@Inject
|
||||
PluginManager pluginManager;
|
||||
|
||||
/**
|
||||
* Set to true in onPostResume() and false in onPause(). This prevents the
|
||||
* QR code fragment from being shown if onRequestPermissionsResult() is
|
||||
@@ -74,21 +110,36 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
* https://issuetracker.google.com/issues/37067655.
|
||||
*/
|
||||
private boolean isResumed = false;
|
||||
|
||||
/**
|
||||
* Set to true when the continue button is clicked, and false when the QR
|
||||
* code fragment is shown. This prevents the QR code fragment from being
|
||||
* shown automatically before the continue button has been clicked.
|
||||
*/
|
||||
private boolean continueClicked = false;
|
||||
|
||||
/**
|
||||
* Records whether the Bluetooth adapter was already enabled before we
|
||||
* asked for Bluetooth discoverability, so we know whether to broadcast a
|
||||
* {@link BluetoothEnabledEvent}.
|
||||
*/
|
||||
private boolean wasAdapterEnabled = false;
|
||||
|
||||
/**
|
||||
* Records whether we've enabled the wifi plugin so we don't enable it more
|
||||
* than once.
|
||||
*/
|
||||
private boolean hasEnabledWifi = false;
|
||||
|
||||
/**
|
||||
* Records whether we've enabled the Bluetooth plugin so we don't enable it
|
||||
* more than once.
|
||||
*/
|
||||
private boolean hasEnabledBluetooth = false;
|
||||
|
||||
private Permission cameraPermission = Permission.UNKNOWN;
|
||||
private Permission locationPermission = Permission.UNKNOWN;
|
||||
private BluetoothState bluetoothState = BluetoothState.UNKNOWN;
|
||||
private BluetoothDecision bluetoothDecision = BluetoothDecision.UNKNOWN;
|
||||
private BroadcastReceiver bluetoothReceiver = null;
|
||||
|
||||
@Override
|
||||
@@ -96,20 +147,17 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
component.inject(this);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle state) {
|
||||
super.onCreate(state);
|
||||
setContentView(R.layout.activity_fragment_container_toolbar);
|
||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||
requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
|
||||
if (state == null) {
|
||||
showInitialFragment(IntroFragment.newInstance());
|
||||
}
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(ACTION_STATE_CHANGED);
|
||||
filter.addAction(ACTION_SCAN_MODE_CHANGED);
|
||||
IntentFilter filter = new IntentFilter(ACTION_SCAN_MODE_CHANGED);
|
||||
bluetoothReceiver = new BluetoothStateReceiver();
|
||||
registerReceiver(bluetoothReceiver, filter);
|
||||
}
|
||||
@@ -122,18 +170,17 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
onBackPressed();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
if (item.getItemId() == android.R.id.home) {
|
||||
onBackPressed();
|
||||
return true;
|
||||
}
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
eventBus.addListener(this);
|
||||
// Permissions may have been granted manually while we were stopped
|
||||
cameraPermission = Permission.UNKNOWN;
|
||||
locationPermission = Permission.UNKNOWN;
|
||||
@@ -150,11 +197,22 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
|
||||
private void showQrCodeFragmentIfAllowed() {
|
||||
if (isResumed && continueClicked && areEssentialPermissionsGranted()) {
|
||||
if (bluetoothState == BluetoothState.UNKNOWN ||
|
||||
bluetoothState == BluetoothState.ENABLED) {
|
||||
requestBluetoothDiscoverable();
|
||||
} else if (bluetoothState != BluetoothState.WAITING) {
|
||||
if (isWifiReady() && isBluetoothReady()) {
|
||||
LOG.info("Wifi and Bluetooth are ready");
|
||||
showQrCodeFragment();
|
||||
} else {
|
||||
if (shouldEnableWifi()) {
|
||||
LOG.info("Enabling wifi plugin");
|
||||
hasEnabledWifi = true;
|
||||
pluginManager.setPluginEnabled(LanTcpConstants.ID, true);
|
||||
}
|
||||
if (bluetoothDecision == BluetoothDecision.UNKNOWN) {
|
||||
requestBluetoothDiscoverable();
|
||||
} else if (shouldEnableBluetooth()) {
|
||||
LOG.info("Enabling Bluetooth plugin");
|
||||
hasEnabledBluetooth = true;
|
||||
pluginManager.setPluginEnabled(BluetoothConstants.ID, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,57 +225,108 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
locationPermission == Permission.PERMANENTLY_DENIED);
|
||||
}
|
||||
|
||||
private boolean isWifiReady() {
|
||||
Plugin p = pluginManager.getPlugin(LanTcpConstants.ID);
|
||||
if (p == null) return true; // Continue without wifi
|
||||
State state = p.getState();
|
||||
// Wait for plugin to become enabled
|
||||
return state == ACTIVE || state == INACTIVE;
|
||||
}
|
||||
|
||||
private boolean isBluetoothReady() {
|
||||
if (bluetoothDecision == BluetoothDecision.UNKNOWN ||
|
||||
bluetoothDecision == BluetoothDecision.WAITING) {
|
||||
// Wait for decision
|
||||
return false;
|
||||
}
|
||||
if (bluetoothDecision == BluetoothDecision.NO_ADAPTER
|
||||
|| bluetoothDecision == BluetoothDecision.REFUSED) {
|
||||
// Continue without Bluetooth
|
||||
return true;
|
||||
}
|
||||
BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
|
||||
if (bt == null) return true; // Continue without Bluetooth
|
||||
if (bt.getScanMode() != SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
|
||||
// Wait for adapter to become discoverable
|
||||
return false;
|
||||
}
|
||||
Plugin p = pluginManager.getPlugin(BluetoothConstants.ID);
|
||||
if (p == null) return true; // Continue without Bluetooth
|
||||
// Wait for plugin to become active
|
||||
return p.getState() == ACTIVE;
|
||||
}
|
||||
|
||||
private boolean shouldEnableWifi() {
|
||||
if (hasEnabledWifi) return false;
|
||||
Plugin p = pluginManager.getPlugin(LanTcpConstants.ID);
|
||||
if (p == null) return false;
|
||||
State state = p.getState();
|
||||
return state == STARTING_STOPPING || state == DISABLED;
|
||||
}
|
||||
|
||||
private void requestBluetoothDiscoverable() {
|
||||
BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
|
||||
if (bt == null) {
|
||||
bluetoothDecision = BluetoothDecision.NO_ADAPTER;
|
||||
showQrCodeFragmentIfAllowed();
|
||||
} else {
|
||||
Intent i = new Intent(ACTION_REQUEST_DISCOVERABLE);
|
||||
if (i.resolveActivity(getPackageManager()) != null) {
|
||||
LOG.info("Asking for Bluetooth discoverability");
|
||||
bluetoothDecision = BluetoothDecision.WAITING;
|
||||
wasAdapterEnabled = bt.isEnabled();
|
||||
startActivityForResult(i, REQUEST_BLUETOOTH_DISCOVERABLE);
|
||||
} else {
|
||||
bluetoothDecision = BluetoothDecision.NO_ADAPTER;
|
||||
showQrCodeFragmentIfAllowed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean shouldEnableBluetooth() {
|
||||
if (bluetoothDecision != BluetoothDecision.ACCEPTED) return false;
|
||||
if (hasEnabledBluetooth) return false;
|
||||
Plugin p = pluginManager.getPlugin(BluetoothConstants.ID);
|
||||
if (p == null) return false;
|
||||
State state = p.getState();
|
||||
return state == STARTING_STOPPING || state == DISABLED;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
isResumed = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showNextScreen() {
|
||||
continueClicked = true;
|
||||
if (checkPermissions()) showQrCodeFragmentIfAllowed();
|
||||
}
|
||||
|
||||
private void requestBluetoothDiscoverable() {
|
||||
BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
|
||||
if (bt == null) {
|
||||
setBluetoothState(BluetoothState.NO_ADAPTER);
|
||||
} else {
|
||||
Intent i = new Intent(ACTION_REQUEST_DISCOVERABLE);
|
||||
if (i.resolveActivity(getPackageManager()) != null) {
|
||||
setBluetoothState(BluetoothState.WAITING);
|
||||
wasAdapterEnabled = bt.isEnabled();
|
||||
startActivityForResult(i, REQUEST_BLUETOOTH_DISCOVERABLE);
|
||||
} else {
|
||||
setBluetoothState(BluetoothState.NO_ADAPTER);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setBluetoothState(BluetoothState bluetoothState) {
|
||||
LOG.info("Setting Bluetooth state to " + bluetoothState);
|
||||
this.bluetoothState = bluetoothState;
|
||||
if (!wasAdapterEnabled && bluetoothState == BluetoothState.ENABLED) {
|
||||
eventBus.broadcast(new BluetoothEnabledEvent());
|
||||
wasAdapterEnabled = true;
|
||||
}
|
||||
showQrCodeFragmentIfAllowed();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int request, int result, Intent data) {
|
||||
public void onActivityResult(int request, int result,
|
||||
@Nullable Intent data) {
|
||||
if (request == REQUEST_BLUETOOTH_DISCOVERABLE) {
|
||||
if (result == RESULT_CANCELED) {
|
||||
setBluetoothState(BluetoothState.REFUSED);
|
||||
LOG.info("Bluetooth discoverability was refused");
|
||||
bluetoothDecision = BluetoothDecision.REFUSED;
|
||||
} else {
|
||||
// If Bluetooth is already discoverable, show the QR code -
|
||||
// otherwise wait for the state or scan mode to change
|
||||
BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
|
||||
if (bt == null) throw new AssertionError();
|
||||
if (bt.getScanMode() == SCAN_MODE_CONNECTABLE_DISCOVERABLE)
|
||||
setBluetoothState(BluetoothState.DISCOVERABLE);
|
||||
LOG.info("Bluetooth discoverability was accepted");
|
||||
bluetoothDecision = BluetoothDecision.ACCEPTED;
|
||||
if (!wasAdapterEnabled) {
|
||||
LOG.info("Bluetooth adapter was enabled by us");
|
||||
eventBus.broadcast(new BluetoothEnabledEvent());
|
||||
wasAdapterEnabled = true;
|
||||
}
|
||||
}
|
||||
showQrCodeFragmentIfAllowed();
|
||||
} else super.onActivityResult(request, result, data);
|
||||
}
|
||||
|
||||
@@ -227,7 +336,12 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
continueClicked = false;
|
||||
// If we return to the intro fragment, ask for Bluetooth
|
||||
// discoverability again before showing the QR code fragment
|
||||
bluetoothState = BluetoothState.UNKNOWN;
|
||||
bluetoothDecision = BluetoothDecision.UNKNOWN;
|
||||
// If we return to the intro fragment, we may need to enable wifi and
|
||||
// Bluetooth again
|
||||
hasEnabledWifi = false;
|
||||
hasEnabledBluetooth = false;
|
||||
|
||||
// FIXME #824
|
||||
FragmentManager fm = getSupportFragmentManager();
|
||||
if (fm.findFragmentByTag(KeyAgreementFragment.TAG) == null) {
|
||||
@@ -239,12 +353,6 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
}
|
||||
}
|
||||
|
||||
protected void showErrorFragment(@StringRes int errorResId) {
|
||||
String errorMsg = getString(errorResId);
|
||||
BaseFragment f = ContactExchangeErrorFragment.newInstance(errorMsg);
|
||||
showNextFragment(f);
|
||||
}
|
||||
|
||||
private boolean checkPermissions() {
|
||||
if (areEssentialPermissionsGranted()) return true;
|
||||
// If the camera permission has been permanently denied, ask the
|
||||
@@ -335,24 +443,30 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||
permission);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof TransportStateEvent) {
|
||||
TransportStateEvent t = (TransportStateEvent) e;
|
||||
if (t.getTransportId().equals(BluetoothConstants.ID)) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Bluetooth state changed to " + t.getState());
|
||||
}
|
||||
showQrCodeFragmentIfAllowed();
|
||||
} else if (t.getTransportId().equals(LanTcpConstants.ID)) {
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("Wifi state changed to " + t.getState());
|
||||
}
|
||||
showQrCodeFragmentIfAllowed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class BluetoothStateReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (ACTION_STATE_CHANGED.equals(action)) {
|
||||
int state = intent.getIntExtra(EXTRA_STATE, 0);
|
||||
if (state == STATE_ON)
|
||||
setBluetoothState(BluetoothState.ENABLED);
|
||||
else setBluetoothState(BluetoothState.UNKNOWN);
|
||||
} else if (ACTION_SCAN_MODE_CHANGED.equals(action)) {
|
||||
int scanMode = intent.getIntExtra(EXTRA_SCAN_MODE, 0);
|
||||
if (scanMode == SCAN_MODE_CONNECTABLE_DISCOVERABLE)
|
||||
setBluetoothState(BluetoothState.DISCOVERABLE);
|
||||
else if (scanMode == SCAN_MODE_CONNECTABLE)
|
||||
setBluetoothState(BluetoothState.ENABLED);
|
||||
else setBluetoothState(BluetoothState.UNKNOWN);
|
||||
}
|
||||
LOG.info("Bluetooth scan mode changed");
|
||||
showQrCodeFragmentIfAllowed();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,33 +15,27 @@ import android.widget.Toast;
|
||||
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.DecryptionResult;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.activity.BriarActivity;
|
||||
import org.briarproject.briar.android.controller.handler.UiResultHandler;
|
||||
import org.briarproject.briar.android.util.UiUtils;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.lifecycle.ViewModelProviders;
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static android.widget.Toast.LENGTH_LONG;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.KEY_STRENGTHENER_ERROR;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.SUCCESS;
|
||||
import static org.briarproject.bramble.api.crypto.PasswordStrengthEstimator.QUITE_WEAK;
|
||||
import static org.briarproject.briar.android.login.LoginUtils.createKeyStrengthenerErrorDialog;
|
||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||
import static org.briarproject.briar.android.util.UiUtils.setError;
|
||||
import static org.briarproject.briar.android.util.UiUtils.showSoftKeyboard;
|
||||
|
||||
public class ChangePasswordActivity extends BriarActivity
|
||||
implements OnClickListener, OnEditorActionListener {
|
||||
|
||||
@Inject
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
protected ChangePasswordController passwordController;
|
||||
|
||||
private TextInputLayout currentPasswordEntryWrapper;
|
||||
private TextInputLayout newPasswordEntryWrapper;
|
||||
@@ -53,17 +47,11 @@ public class ChangePasswordActivity extends BriarActivity
|
||||
private Button changePasswordButton;
|
||||
private ProgressBar progress;
|
||||
|
||||
@VisibleForTesting
|
||||
ChangePasswordViewModel viewModel;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle state) {
|
||||
super.onCreate(state);
|
||||
setContentView(R.layout.activity_change_password);
|
||||
|
||||
viewModel = ViewModelProviders.of(this, viewModelFactory)
|
||||
.get(ChangePasswordViewModel.class);
|
||||
|
||||
currentPasswordEntryWrapper =
|
||||
findViewById(R.id.current_password_entry_wrapper);
|
||||
newPasswordEntryWrapper = findViewById(R.id.new_password_entry_wrapper);
|
||||
@@ -114,12 +102,13 @@ public class ChangePasswordActivity extends BriarActivity
|
||||
String firstPassword = newPassword.getText().toString();
|
||||
String secondPassword = newPasswordConfirmation.getText().toString();
|
||||
boolean passwordsMatch = firstPassword.equals(secondPassword);
|
||||
float strength = viewModel.estimatePasswordStrength(firstPassword);
|
||||
float strength =
|
||||
passwordController.estimatePasswordStrength(firstPassword);
|
||||
strengthMeter.setStrength(strength);
|
||||
setError(newPasswordEntryWrapper,
|
||||
UiUtils.setError(newPasswordEntryWrapper,
|
||||
getString(R.string.password_too_weak),
|
||||
firstPassword.length() > 0 && strength < QUITE_WEAK);
|
||||
setError(newPasswordConfirmationWrapper,
|
||||
UiUtils.setError(newPasswordConfirmationWrapper,
|
||||
getString(R.string.passwords_do_not_match),
|
||||
secondPassword.length() > 0 && !passwordsMatch);
|
||||
changePasswordButton.setEnabled(
|
||||
@@ -138,34 +127,32 @@ public class ChangePasswordActivity extends BriarActivity
|
||||
// Replace the button with a progress bar
|
||||
changePasswordButton.setVisibility(INVISIBLE);
|
||||
progress.setVisibility(VISIBLE);
|
||||
|
||||
String curPwd = currentPassword.getText().toString();
|
||||
String newPwd = newPassword.getText().toString();
|
||||
viewModel.changePassword(curPwd, newPwd).observeEvent(this, result -> {
|
||||
if (result == SUCCESS) {
|
||||
Toast.makeText(ChangePasswordActivity.this,
|
||||
R.string.password_changed,
|
||||
LENGTH_LONG).show();
|
||||
setResult(RESULT_OK);
|
||||
supportFinishAfterTransition();
|
||||
} else {
|
||||
tryAgain(result);
|
||||
passwordController.changePassword(currentPassword.getText().toString(),
|
||||
newPassword.getText().toString(),
|
||||
new UiResultHandler<Boolean>(this) {
|
||||
@Override
|
||||
public void onResultUi(@NonNull Boolean result) {
|
||||
if (result) {
|
||||
Toast.makeText(ChangePasswordActivity.this,
|
||||
R.string.password_changed,
|
||||
Toast.LENGTH_LONG).show();
|
||||
setResult(RESULT_OK);
|
||||
supportFinishAfterTransition();
|
||||
} else {
|
||||
tryAgain();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private void tryAgain(DecryptionResult result) {
|
||||
private void tryAgain() {
|
||||
UiUtils.setError(currentPasswordEntryWrapper,
|
||||
getString(R.string.try_again), true);
|
||||
changePasswordButton.setVisibility(VISIBLE);
|
||||
progress.setVisibility(INVISIBLE);
|
||||
if (result == KEY_STRENGTHENER_ERROR) {
|
||||
createKeyStrengthenerErrorDialog(this).show();
|
||||
} else {
|
||||
setError(currentPasswordEntryWrapper,
|
||||
getString(R.string.try_again), true);
|
||||
currentPassword.setText("");
|
||||
// show the keyboard again
|
||||
showSoftKeyboard(currentPassword);
|
||||
}
|
||||
currentPassword.setText("");
|
||||
|
||||
// show the keyboard again
|
||||
showSoftKeyboard(currentPassword);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.controller.handler.ResultHandler;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@NotNullByDefault
|
||||
public class ChangePasswordControllerImpl implements ChangePasswordController {
|
||||
|
||||
protected final AccountManager accountManager;
|
||||
protected final Executor ioExecutor;
|
||||
private final PasswordStrengthEstimator strengthEstimator;
|
||||
|
||||
@Inject
|
||||
ChangePasswordControllerImpl(AccountManager accountManager,
|
||||
@IoExecutor Executor ioExecutor,
|
||||
PasswordStrengthEstimator strengthEstimator) {
|
||||
this.accountManager = accountManager;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.strengthEstimator = strengthEstimator;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float estimatePasswordStrength(String password) {
|
||||
return strengthEstimator.estimateStrength(password);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void changePassword(String oldPassword, String newPassword,
|
||||
ResultHandler<Boolean> resultHandler) {
|
||||
ioExecutor.execute(() -> {
|
||||
boolean changed =
|
||||
accountManager.changePassword(oldPassword, newPassword);
|
||||
resultHandler.onResult(changed);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
||||
import org.briarproject.bramble.api.crypto.DecryptionResult;
|
||||
import org.briarproject.bramble.api.crypto.PasswordStrengthEstimator;
|
||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.android.viewmodel.LiveEvent;
|
||||
import org.briarproject.briar.android.viewmodel.MutableLiveEvent;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.SUCCESS;
|
||||
|
||||
@NotNullByDefault
|
||||
public class ChangePasswordViewModel extends ViewModel {
|
||||
|
||||
private final AccountManager accountManager;
|
||||
private final Executor ioExecutor;
|
||||
private final PasswordStrengthEstimator strengthEstimator;
|
||||
|
||||
@Inject
|
||||
ChangePasswordViewModel(AccountManager accountManager,
|
||||
@IoExecutor Executor ioExecutor,
|
||||
PasswordStrengthEstimator strengthEstimator) {
|
||||
this.accountManager = accountManager;
|
||||
this.ioExecutor = ioExecutor;
|
||||
this.strengthEstimator = strengthEstimator;
|
||||
}
|
||||
|
||||
float estimatePasswordStrength(String password) {
|
||||
return strengthEstimator.estimateStrength(password);
|
||||
}
|
||||
|
||||
LiveEvent<DecryptionResult> changePassword(String oldPassword,
|
||||
String newPassword) {
|
||||
MutableLiveEvent<DecryptionResult> result = new MutableLiveEvent<>();
|
||||
ioExecutor.execute(() -> {
|
||||
try {
|
||||
accountManager.changePassword(oldPassword, newPassword);
|
||||
result.postEvent(SUCCESS);
|
||||
} catch (DecryptionException e) {
|
||||
result.postEvent(e.getDecryptionResult());
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import org.briarproject.briar.android.viewmodel.ViewModelKey;
|
||||
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import dagger.Binds;
|
||||
import dagger.Module;
|
||||
import dagger.multibindings.IntoMap;
|
||||
|
||||
@Module
|
||||
public abstract class LoginModule {
|
||||
|
||||
@Binds
|
||||
@IntoMap
|
||||
@ViewModelKey(StartupViewModel.class)
|
||||
abstract ViewModel bindStartupViewModel(StartupViewModel viewModel);
|
||||
|
||||
@Binds
|
||||
@IntoMap
|
||||
@ViewModelKey(ChangePasswordViewModel.class)
|
||||
abstract ViewModel bindChangePasswordViewModel(
|
||||
ChangePasswordViewModel viewModel);
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package org.briarproject.briar.android.login;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import static androidx.core.content.ContextCompat.getColor;
|
||||
import static androidx.core.content.ContextCompat.getDrawable;
|
||||
import static androidx.core.graphics.drawable.DrawableCompat.setTint;
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
@NotNullByDefault
|
||||
class LoginUtils {
|
||||
|
||||
static AlertDialog createKeyStrengthenerErrorDialog(Context ctx) {
|
||||
AlertDialog.Builder builder =
|
||||
new AlertDialog.Builder(ctx, R.style.BriarDialogTheme);
|
||||
Drawable icon = getDrawable(ctx, R.drawable.alerts_and_states_error);
|
||||
setTint(requireNonNull(icon), getColor(ctx, R.color.color_primary));
|
||||
builder.setIcon(icon);
|
||||
builder.setTitle(R.string.dialog_title_cannot_check_password);
|
||||
builder.setMessage(R.string.dialog_message_cannot_check_password);
|
||||
builder.setPositiveButton(R.string.ok, null);
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import android.widget.ProgressBar;
|
||||
import com.google.android.material.textfield.TextInputEditText;
|
||||
import com.google.android.material.textfield.TextInputLayout;
|
||||
|
||||
import org.briarproject.bramble.api.crypto.DecryptionResult;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.briar.R;
|
||||
@@ -29,9 +28,6 @@ import androidx.lifecycle.ViewModelProviders;
|
||||
import static android.view.View.INVISIBLE;
|
||||
import static android.view.View.VISIBLE;
|
||||
import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.KEY_STRENGTHENER_ERROR;
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.SUCCESS;
|
||||
import static org.briarproject.briar.android.login.LoginUtils.createKeyStrengthenerErrorDialog;
|
||||
import static org.briarproject.briar.android.util.UiUtils.enterPressed;
|
||||
import static org.briarproject.briar.android.util.UiUtils.hideSoftKeyboard;
|
||||
import static org.briarproject.briar.android.util.UiUtils.setError;
|
||||
@@ -62,13 +58,12 @@ public class PasswordFragment extends BaseFragment implements TextWatcher {
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState) {
|
||||
View v = inflater.inflate(R.layout.fragment_password, container,
|
||||
false);
|
||||
false);
|
||||
|
||||
viewModel = ViewModelProviders.of(requireActivity(), viewModelFactory)
|
||||
.get(StartupViewModel.class);
|
||||
|
||||
viewModel.getPasswordValidated().observeEvent(this, result -> {
|
||||
if (result != SUCCESS) onPasswordInvalid(result);
|
||||
viewModel.getPasswordValidated().observeEvent(this, valid -> {
|
||||
if (!valid) onPasswordInvalid();
|
||||
});
|
||||
|
||||
signInButton = v.findViewById(R.id.btn_sign_in);
|
||||
@@ -112,20 +107,18 @@ public class PasswordFragment extends BaseFragment implements TextWatcher {
|
||||
viewModel.validatePassword(password.getText().toString());
|
||||
}
|
||||
|
||||
private void onPasswordInvalid(DecryptionResult result) {
|
||||
private void onPasswordInvalid() {
|
||||
setError(input, getString(R.string.try_again), true);
|
||||
signInButton.setVisibility(VISIBLE);
|
||||
progress.setVisibility(INVISIBLE);
|
||||
if (result == KEY_STRENGTHENER_ERROR) {
|
||||
createKeyStrengthenerErrorDialog(requireContext()).show();
|
||||
} else {
|
||||
setError(input, getString(R.string.try_again), true);
|
||||
password.setText(null);
|
||||
// show the keyboard again
|
||||
showSoftKeyboard(password);
|
||||
}
|
||||
password.setText(null);
|
||||
|
||||
// show the keyboard again
|
||||
showSoftKeyboard(password);
|
||||
}
|
||||
|
||||
private void onForgottenPasswordClick() {
|
||||
public void onForgottenPasswordClick() {
|
||||
// TODO Encapsulate the dialog in a re-usable fragment
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext(),
|
||||
R.style.BriarDialogTheme);
|
||||
builder.setTitle(R.string.dialog_title_lost_password);
|
||||
|
||||
@@ -3,8 +3,6 @@ package org.briarproject.briar.android.login;
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.api.account.AccountManager;
|
||||
import org.briarproject.bramble.api.crypto.DecryptionException;
|
||||
import org.briarproject.bramble.api.crypto.DecryptionResult;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
@@ -26,7 +24,6 @@ import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static org.briarproject.bramble.api.crypto.DecryptionResult.SUCCESS;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.COMPACTING_DATABASE;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.MIGRATING_DATABASE;
|
||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.STARTING_SERVICES;
|
||||
@@ -49,7 +46,7 @@ public class StartupViewModel extends AndroidViewModel
|
||||
@IoExecutor
|
||||
private final Executor ioExecutor;
|
||||
|
||||
private final MutableLiveEvent<DecryptionResult> passwordValidated =
|
||||
private final MutableLiveEvent<Boolean> passwordValidated =
|
||||
new MutableLiveEvent<>();
|
||||
private final MutableLiveEvent<Boolean> accountDeleted =
|
||||
new MutableLiveEvent<>();
|
||||
@@ -108,17 +105,13 @@ public class StartupViewModel extends AndroidViewModel
|
||||
|
||||
void validatePassword(String password) {
|
||||
ioExecutor.execute(() -> {
|
||||
try {
|
||||
accountManager.signIn(password);
|
||||
passwordValidated.postEvent(SUCCESS);
|
||||
state.postValue(SIGNED_IN);
|
||||
} catch (DecryptionException e) {
|
||||
passwordValidated.postEvent(e.getDecryptionResult());
|
||||
}
|
||||
boolean signedIn = accountManager.signIn(password);
|
||||
passwordValidated.postEvent(signedIn);
|
||||
if (signedIn) state.postValue(SIGNED_IN);
|
||||
});
|
||||
}
|
||||
|
||||
LiveEvent<DecryptionResult> getPasswordValidated() {
|
||||
LiveEvent<Boolean> getPasswordValidated() {
|
||||
return passwordValidated;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
package org.briarproject.briar.android.navdrawer;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Configuration;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.GridView;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
@@ -21,16 +17,11 @@ import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
||||
import org.briarproject.bramble.api.plugin.LanTcpConstants;
|
||||
import org.briarproject.bramble.api.plugin.TorConstants;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.briar.R;
|
||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||
import org.briarproject.briar.android.activity.BriarActivity;
|
||||
import org.briarproject.briar.android.blog.FeedFragment;
|
||||
import org.briarproject.briar.android.contact.ContactListFragment;
|
||||
import org.briarproject.briar.android.controller.handler.UiResultHandler;
|
||||
import org.briarproject.briar.android.forum.ForumListFragment;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
||||
@@ -38,23 +29,21 @@ import org.briarproject.briar.android.logout.SignOutFragment;
|
||||
import org.briarproject.briar.android.privategroup.list.GroupListFragment;
|
||||
import org.briarproject.briar.android.settings.SettingsActivity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.appcompat.app.ActionBar;
|
||||
import androidx.appcompat.app.ActionBarDrawerToggle;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import androidx.drawerlayout.widget.DrawerLayout;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.lifecycle.ViewModelProviders;
|
||||
|
||||
import static android.view.View.GONE;
|
||||
import static android.view.View.VISIBLE;
|
||||
@@ -72,8 +61,7 @@ import static org.briarproject.briar.android.util.UiUtils.getDaysUntilExpiry;
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class NavDrawerActivity extends BriarActivity implements
|
||||
BaseFragmentListener, TransportStateListener,
|
||||
OnNavigationItemSelectedListener {
|
||||
BaseFragmentListener, OnNavigationItemSelectedListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(NavDrawerActivity.class.getName());
|
||||
@@ -91,19 +79,18 @@ public class NavDrawerActivity extends BriarActivity implements
|
||||
public static Uri SIGN_OUT_URI =
|
||||
Uri.parse("briar-content://org.briarproject.briar/sign-out");
|
||||
|
||||
private NavDrawerViewModel viewModel;
|
||||
private ActionBarDrawerToggle drawerToggle;
|
||||
|
||||
@Inject
|
||||
NavDrawerController controller;
|
||||
ViewModelProvider.Factory viewModelFactory;
|
||||
|
||||
@Inject
|
||||
LifecycleManager lifecycleManager;
|
||||
|
||||
private DrawerLayout drawerLayout;
|
||||
private NavigationView navigation;
|
||||
|
||||
private List<Transport> transports;
|
||||
private BaseAdapter transportsAdapter;
|
||||
|
||||
@Override
|
||||
public void injectActivity(ActivityComponent component) {
|
||||
component.inject(this);
|
||||
@@ -115,10 +102,20 @@ public class NavDrawerActivity extends BriarActivity implements
|
||||
exitIfStartupFailed(getIntent());
|
||||
setContentView(R.layout.activity_nav_drawer);
|
||||
|
||||
viewModel = ViewModelProviders.of(this, viewModelFactory)
|
||||
.get(NavDrawerViewModel.class);
|
||||
|
||||
viewModel.showExpiryWarning().observe(this, this::showExpiryWarning);
|
||||
viewModel.shouldAskForDozeWhitelisting().observe(this, ask -> {
|
||||
if (ask) showDozeDialog(getString(R.string.setup_doze_intro));
|
||||
});
|
||||
|
||||
View drawerScrollView = findViewById(R.id.drawerScrollView);
|
||||
new PluginViewController(drawerScrollView, this, viewModel);
|
||||
|
||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||
drawerLayout = findViewById(R.id.drawer_layout);
|
||||
navigation = findViewById(R.id.navigation);
|
||||
GridView transportsView = findViewById(R.id.transportsView);
|
||||
|
||||
setSupportActionBar(toolbar);
|
||||
ActionBar actionBar = requireNonNull(getSupportActionBar());
|
||||
@@ -131,9 +128,6 @@ public class NavDrawerActivity extends BriarActivity implements
|
||||
drawerLayout.addDrawerListener(drawerToggle);
|
||||
navigation.setNavigationItemSelectedListener(this);
|
||||
|
||||
initializeTransports(getLayoutInflater());
|
||||
transportsView.setAdapter(transportsAdapter);
|
||||
|
||||
lockManager.isLockable().observe(this, this::setLockVisible);
|
||||
|
||||
if (lifecycleManager.getLifecycleState().isAfter(RUNNING)) {
|
||||
@@ -149,17 +143,10 @@ public class NavDrawerActivity extends BriarActivity implements
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressLint("NewApi")
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
updateTransports();
|
||||
lockManager.checkIfLockable();
|
||||
controller.showExpiryWarning(new UiResultHandler<Boolean>(this) {
|
||||
@Override
|
||||
public void onResultUi(Boolean expiry) {
|
||||
if (expiry) showExpiryWarning();
|
||||
}
|
||||
});
|
||||
viewModel.checkExpiryWarning();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -167,16 +154,7 @@ public class NavDrawerActivity extends BriarActivity implements
|
||||
@Nullable Intent data) {
|
||||
super.onActivityResult(request, result, data);
|
||||
if (request == REQUEST_PASSWORD && result == RESULT_OK) {
|
||||
controller.shouldAskForDozeWhitelisting(this,
|
||||
new UiResultHandler<Boolean>(this) {
|
||||
@Override
|
||||
public void onResultUi(Boolean ask) {
|
||||
if (ask) {
|
||||
showDozeDialog(
|
||||
getString(R.string.setup_doze_intro));
|
||||
}
|
||||
}
|
||||
});
|
||||
viewModel.checkDozeWhitelisting();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,134 +324,30 @@ public class NavDrawerActivity extends BriarActivity implements
|
||||
if (item != null) item.setVisible(visible);
|
||||
}
|
||||
|
||||
private void showExpiryWarning() {
|
||||
private void showExpiryWarning(boolean show) {
|
||||
int daysUntilExpiry = getDaysUntilExpiry();
|
||||
if (daysUntilExpiry < 0) signOut();
|
||||
if (daysUntilExpiry < 0) {
|
||||
signOut();
|
||||
return;
|
||||
}
|
||||
|
||||
// show expiry warning text
|
||||
ViewGroup expiryWarning = findViewById(R.id.expiryWarning);
|
||||
TextView expiryWarningText =
|
||||
expiryWarning.findViewById(R.id.expiryWarningText);
|
||||
// make close button functional
|
||||
ImageView expiryWarningClose =
|
||||
expiryWarning.findViewById(R.id.expiryWarningClose);
|
||||
|
||||
expiryWarningText.setText(getResources()
|
||||
.getQuantityString(R.plurals.expiry_warning,
|
||||
daysUntilExpiry, daysUntilExpiry));
|
||||
|
||||
expiryWarningClose.setOnClickListener(v -> {
|
||||
controller.expiryWarningDismissed();
|
||||
if (show) {
|
||||
// show expiry warning text
|
||||
TextView expiryWarningText =
|
||||
expiryWarning.findViewById(R.id.expiryWarningText);
|
||||
String text = getResources().getQuantityString(
|
||||
R.plurals.expiry_warning, daysUntilExpiry, daysUntilExpiry);
|
||||
expiryWarningText.setText(text);
|
||||
// make close button functional
|
||||
ImageView expiryWarningClose =
|
||||
expiryWarning.findViewById(R.id.expiryWarningClose);
|
||||
expiryWarningClose.setOnClickListener(v ->
|
||||
viewModel.expiryWarningDismissed()
|
||||
);
|
||||
expiryWarning.setVisibility(VISIBLE);
|
||||
} else {
|
||||
expiryWarning.setVisibility(GONE);
|
||||
});
|
||||
|
||||
expiryWarning.setVisibility(VISIBLE);
|
||||
}
|
||||
|
||||
private void initializeTransports(LayoutInflater inflater) {
|
||||
transports = new ArrayList<>(3);
|
||||
|
||||
Transport tor = new Transport();
|
||||
tor.id = TorConstants.ID;
|
||||
tor.enabled = controller.isTransportRunning(tor.id);
|
||||
tor.iconId = R.drawable.transport_tor;
|
||||
tor.textId = R.string.transport_tor;
|
||||
transports.add(tor);
|
||||
|
||||
Transport bt = new Transport();
|
||||
bt.id = BluetoothConstants.ID;
|
||||
bt.enabled = controller.isTransportRunning(bt.id);
|
||||
bt.iconId = R.drawable.transport_bt;
|
||||
bt.textId = R.string.transport_bt;
|
||||
transports.add(bt);
|
||||
|
||||
Transport lan = new Transport();
|
||||
lan.id = LanTcpConstants.ID;
|
||||
lan.enabled = controller.isTransportRunning(lan.id);
|
||||
lan.iconId = R.drawable.transport_lan;
|
||||
lan.textId = R.string.transport_lan;
|
||||
transports.add(lan);
|
||||
|
||||
transportsAdapter = new BaseAdapter() {
|
||||
@Override
|
||||
public int getCount() {
|
||||
return transports.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Transport getItem(int position) {
|
||||
return transports.get(position);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView,
|
||||
ViewGroup parent) {
|
||||
View view;
|
||||
if (convertView != null) {
|
||||
view = convertView;
|
||||
} else {
|
||||
view = inflater.inflate(R.layout.list_item_transport,
|
||||
parent, false);
|
||||
}
|
||||
|
||||
Transport t = getItem(position);
|
||||
int c;
|
||||
if (t.enabled) {
|
||||
c = ContextCompat.getColor(NavDrawerActivity.this,
|
||||
R.color.briar_green_light);
|
||||
} else {
|
||||
c = ContextCompat.getColor(NavDrawerActivity.this,
|
||||
android.R.color.tertiary_text_light);
|
||||
}
|
||||
|
||||
ImageView icon = view.findViewById(R.id.imageView);
|
||||
icon.setImageDrawable(ContextCompat
|
||||
.getDrawable(NavDrawerActivity.this, t.iconId));
|
||||
icon.setColorFilter(c);
|
||||
|
||||
TextView text = view.findViewById(R.id.textView);
|
||||
text.setText(getString(t.textId));
|
||||
|
||||
return view;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@UiThread
|
||||
private void setTransport(TransportId id, boolean enabled) {
|
||||
if (transports == null || transportsAdapter == null) return;
|
||||
for (Transport t : transports) {
|
||||
if (t.id.equals(id)) {
|
||||
t.enabled = enabled;
|
||||
transportsAdapter.notifyDataSetChanged();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateTransports() {
|
||||
if (transports == null || transportsAdapter == null) return;
|
||||
for (Transport t : transports) {
|
||||
t.enabled = controller.isTransportRunning(t.id);
|
||||
}
|
||||
transportsAdapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stateUpdate(TransportId id, boolean enabled) {
|
||||
setTransport(id, enabled);
|
||||
}
|
||||
|
||||
private static class Transport {
|
||||
|
||||
private TransportId id;
|
||||
private boolean enabled;
|
||||
private int iconId;
|
||||
private int textId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
package org.briarproject.briar.android.navdrawer;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.briar.android.controller.ActivityLifecycleController;
|
||||
import org.briarproject.briar.android.controller.handler.ResultHandler;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface NavDrawerController extends ActivityLifecycleController {
|
||||
|
||||
boolean isTransportRunning(TransportId transportId);
|
||||
|
||||
void showExpiryWarning(ResultHandler<Boolean> handler);
|
||||
|
||||
void expiryWarningDismissed();
|
||||
|
||||
void shouldAskForDozeWhitelisting(Context ctx,
|
||||
ResultHandler<Boolean> handler);
|
||||
|
||||
}
|
||||
@@ -1,182 +0,0 @@
|
||||
package org.briarproject.briar.android.navdrawer;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.Plugin;
|
||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||
import org.briarproject.briar.android.controller.DbControllerImpl;
|
||||
import org.briarproject.briar.android.controller.handler.ResultHandler;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.DAYS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.briar.android.TestingConstants.EXPIRY_DATE;
|
||||
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||
import static org.briarproject.briar.android.controller.BriarControllerImpl.DOZE_ASK_AGAIN;
|
||||
import static org.briarproject.briar.android.settings.SettingsFragment.SETTINGS_NAMESPACE;
|
||||
import static org.briarproject.briar.android.util.UiUtils.needsDozeWhitelisting;
|
||||
|
||||
@MethodsNotNullByDefault
|
||||
@ParametersNotNullByDefault
|
||||
public class NavDrawerControllerImpl extends DbControllerImpl
|
||||
implements NavDrawerController, EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(NavDrawerControllerImpl.class.getName());
|
||||
|
||||
private static final String EXPIRY_DATE_WARNING = "expiryDateWarning";
|
||||
|
||||
private final PluginManager pluginManager;
|
||||
private final SettingsManager settingsManager;
|
||||
private final EventBus eventBus;
|
||||
|
||||
// UI thread
|
||||
private TransportStateListener listener;
|
||||
|
||||
@Inject
|
||||
NavDrawerControllerImpl(@DatabaseExecutor Executor dbExecutor,
|
||||
LifecycleManager lifecycleManager, PluginManager pluginManager,
|
||||
SettingsManager settingsManager, EventBus eventBus) {
|
||||
super(dbExecutor, lifecycleManager);
|
||||
this.pluginManager = pluginManager;
|
||||
this.settingsManager = settingsManager;
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreate(Activity activity) {
|
||||
listener = (TransportStateListener) activity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStart() {
|
||||
eventBus.addListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityStop() {
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityDestroy() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof TransportEnabledEvent) {
|
||||
TransportId id = ((TransportEnabledEvent) e).getTransportId();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("TransportEnabledEvent: " + id.getString());
|
||||
}
|
||||
listener.stateUpdate(id, true);
|
||||
} else if (e instanceof TransportDisabledEvent) {
|
||||
TransportId id = ((TransportDisabledEvent) e).getTransportId();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("TransportDisabledEvent: " + id.getString());
|
||||
}
|
||||
listener.stateUpdate(id, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void showExpiryWarning(ResultHandler<Boolean> handler) {
|
||||
if (!IS_DEBUG_BUILD) {
|
||||
handler.onResult(false);
|
||||
return;
|
||||
}
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
Settings settings =
|
||||
settingsManager.getSettings(SETTINGS_NAMESPACE);
|
||||
int warningInt = settings.getInt(EXPIRY_DATE_WARNING, 0);
|
||||
|
||||
if (warningInt == 0) {
|
||||
// we have not warned before
|
||||
handler.onResult(true);
|
||||
} else {
|
||||
long warningLong = warningInt * 1000L;
|
||||
long now = System.currentTimeMillis();
|
||||
long daysSinceLastWarning =
|
||||
(now - warningLong) / DAYS.toMillis(1);
|
||||
long daysBeforeExpiry =
|
||||
(EXPIRY_DATE - now) / DAYS.toMillis(1);
|
||||
|
||||
if (daysSinceLastWarning >= 30) {
|
||||
handler.onResult(true);
|
||||
} else if (daysBeforeExpiry <= 3 &&
|
||||
daysSinceLastWarning > 0) {
|
||||
handler.onResult(true);
|
||||
} else {
|
||||
handler.onResult(false);
|
||||
}
|
||||
}
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void expiryWarningDismissed() {
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
Settings settings = new Settings();
|
||||
int date = (int) (System.currentTimeMillis() / 1000L);
|
||||
settings.putInt(EXPIRY_DATE_WARNING, date);
|
||||
settingsManager.mergeSettings(settings, SETTINGS_NAMESPACE);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shouldAskForDozeWhitelisting(Context ctx,
|
||||
ResultHandler<Boolean> handler) {
|
||||
// check this first, to hit the DbThread only when really necessary
|
||||
if (!needsDozeWhitelisting(ctx)) {
|
||||
handler.onResult(false);
|
||||
return;
|
||||
}
|
||||
runOnDbThread(() -> {
|
||||
try {
|
||||
Settings settings =
|
||||
settingsManager.getSettings(SETTINGS_NAMESPACE);
|
||||
boolean ask = settings.getBoolean(DOZE_ASK_AGAIN, true);
|
||||
handler.onResult(ask);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
handler.onResult(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTransportRunning(TransportId transportId) {
|
||||
Plugin plugin = pluginManager.getPlugin(transportId);
|
||||
return plugin != null && plugin.isRunning();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.briarproject.briar.android.navdrawer;
|
||||
|
||||
import org.briarproject.briar.android.viewmodel.ViewModelKey;
|
||||
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import dagger.Binds;
|
||||
import dagger.Module;
|
||||
import dagger.multibindings.IntoMap;
|
||||
|
||||
@Module
|
||||
public abstract class NavDrawerModule {
|
||||
|
||||
@Binds
|
||||
@IntoMap
|
||||
@ViewModelKey(NavDrawerViewModel.class)
|
||||
abstract ViewModel bindNavDrawerViewModel(
|
||||
NavDrawerViewModel navDrawerViewModel);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,255 @@
|
||||
package org.briarproject.briar.android.navdrawer;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import org.briarproject.bramble.api.db.DatabaseExecutor;
|
||||
import org.briarproject.bramble.api.db.DbException;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.event.EventBus;
|
||||
import org.briarproject.bramble.api.event.EventListener;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
||||
import org.briarproject.bramble.api.plugin.LanTcpConstants;
|
||||
import org.briarproject.bramble.api.plugin.Plugin;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||
import org.briarproject.bramble.api.plugin.TorConstants;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
||||
import org.briarproject.bramble.api.settings.Settings;
|
||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||
import org.briarproject.bramble.api.system.LocationUtils;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.UiThread;
|
||||
import androidx.lifecycle.AndroidViewModel;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
|
||||
import static java.util.concurrent.TimeUnit.DAYS;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static java.util.logging.Logger.getLogger;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.PREF_PLUGIN_ENABLE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_MOBILE;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_WITH_BRIDGES;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_ONLY_WHEN_CHARGING;
|
||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||
import static org.briarproject.briar.android.TestingConstants.EXPIRY_DATE;
|
||||
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||
import static org.briarproject.briar.android.controller.BriarControllerImpl.DOZE_ASK_AGAIN;
|
||||
import static org.briarproject.briar.android.settings.SettingsFragment.SETTINGS_NAMESPACE;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getCountryDisplayName;
|
||||
import static org.briarproject.briar.android.util.UiUtils.needsDozeWhitelisting;
|
||||
|
||||
@NotNullByDefault
|
||||
public class NavDrawerViewModel extends AndroidViewModel
|
||||
implements EventListener {
|
||||
|
||||
private static final Logger LOG =
|
||||
getLogger(NavDrawerViewModel.class.getName());
|
||||
|
||||
private static final String EXPIRY_DATE_WARNING = "expiryDateWarning";
|
||||
static final TransportId[] TRANSPORT_IDS =
|
||||
{TorConstants.ID, LanTcpConstants.ID, BluetoothConstants.ID};
|
||||
|
||||
@DatabaseExecutor
|
||||
private final Executor dbExecutor;
|
||||
private final SettingsManager settingsManager;
|
||||
private final PluginManager pluginManager;
|
||||
private final LocationUtils locationUtils;
|
||||
private final EventBus eventBus;
|
||||
|
||||
private final MutableLiveData<Boolean> showExpiryWarning =
|
||||
new MutableLiveData<>();
|
||||
private final MutableLiveData<Boolean> shouldAskForDozeWhitelisting =
|
||||
new MutableLiveData<>();
|
||||
|
||||
private final MutableLiveData<Plugin.State> torPluginState =
|
||||
new MutableLiveData<>();
|
||||
private final MutableLiveData<Plugin.State> wifiPluginState =
|
||||
new MutableLiveData<>();
|
||||
private final MutableLiveData<Plugin.State> btPluginState =
|
||||
new MutableLiveData<>();
|
||||
|
||||
@Inject
|
||||
NavDrawerViewModel(Application app, @DatabaseExecutor Executor dbExecutor,
|
||||
SettingsManager settingsManager, PluginManager pluginManager,
|
||||
LocationUtils locationUtils, EventBus eventBus) {
|
||||
super(app);
|
||||
this.dbExecutor = dbExecutor;
|
||||
this.settingsManager = settingsManager;
|
||||
this.pluginManager = pluginManager;
|
||||
this.locationUtils = locationUtils;
|
||||
this.eventBus = eventBus;
|
||||
eventBus.addListener(this);
|
||||
updatePluginStates();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
eventBus.removeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void eventOccurred(Event e) {
|
||||
if (e instanceof TransportStateEvent) {
|
||||
TransportStateEvent t = (TransportStateEvent) e;
|
||||
TransportId id = t.getTransportId();
|
||||
State state = t.getState();
|
||||
if (LOG.isLoggable(INFO)) {
|
||||
LOG.info("TransportStateEvent: " + id + " is " + state);
|
||||
}
|
||||
MutableLiveData<Plugin.State> liveData = getPluginLiveData(id);
|
||||
if (liveData != null) liveData.postValue(state);
|
||||
}
|
||||
}
|
||||
|
||||
LiveData<Boolean> showExpiryWarning() {
|
||||
return showExpiryWarning;
|
||||
}
|
||||
|
||||
@UiThread
|
||||
void checkExpiryWarning() {
|
||||
if (!IS_DEBUG_BUILD) {
|
||||
showExpiryWarning.setValue(false);
|
||||
return;
|
||||
}
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
Settings settings =
|
||||
settingsManager.getSettings(SETTINGS_NAMESPACE);
|
||||
int warningInt = settings.getInt(EXPIRY_DATE_WARNING, 0);
|
||||
|
||||
if (warningInt == 0) {
|
||||
// we have not warned before
|
||||
showExpiryWarning.postValue(true);
|
||||
} else {
|
||||
long warningLong = warningInt * 1000L;
|
||||
long now = System.currentTimeMillis();
|
||||
long daysSinceLastWarning =
|
||||
(now - warningLong) / DAYS.toMillis(1);
|
||||
long daysBeforeExpiry =
|
||||
(EXPIRY_DATE - now) / DAYS.toMillis(1);
|
||||
|
||||
if (daysSinceLastWarning >= 30) {
|
||||
showExpiryWarning.postValue(true);
|
||||
} else if (daysBeforeExpiry <= 3 &&
|
||||
daysSinceLastWarning > 0) {
|
||||
showExpiryWarning.postValue(true);
|
||||
} else {
|
||||
showExpiryWarning.postValue(false);
|
||||
}
|
||||
}
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@UiThread
|
||||
void expiryWarningDismissed() {
|
||||
showExpiryWarning.setValue(false);
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
Settings settings = new Settings();
|
||||
int date = (int) (System.currentTimeMillis() / 1000L);
|
||||
settings.putInt(EXPIRY_DATE_WARNING, date);
|
||||
settingsManager.mergeSettings(settings, SETTINGS_NAMESPACE);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
LiveData<Boolean> shouldAskForDozeWhitelisting() {
|
||||
return shouldAskForDozeWhitelisting;
|
||||
}
|
||||
|
||||
@UiThread
|
||||
void checkDozeWhitelisting() {
|
||||
// check this first, to hit the DbThread only when really necessary
|
||||
if (!needsDozeWhitelisting(getApplication())) {
|
||||
shouldAskForDozeWhitelisting.setValue(false);
|
||||
return;
|
||||
}
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
Settings settings =
|
||||
settingsManager.getSettings(SETTINGS_NAMESPACE);
|
||||
boolean ask = settings.getBoolean(DOZE_ASK_AGAIN, true);
|
||||
shouldAskForDozeWhitelisting.postValue(ask);
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
shouldAskForDozeWhitelisting.postValue(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void updatePluginStates() {
|
||||
for (TransportId t : TRANSPORT_IDS) {
|
||||
MutableLiveData<Plugin.State> liveData = getPluginLiveData(t);
|
||||
if (liveData == null) throw new AssertionError();
|
||||
liveData.setValue(getTransportState(t));
|
||||
}
|
||||
}
|
||||
|
||||
private State getTransportState(TransportId id) {
|
||||
Plugin plugin = pluginManager.getPlugin(id);
|
||||
return plugin == null ? STARTING_STOPPING : plugin.getState();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private MutableLiveData<State> getPluginLiveData(TransportId t) {
|
||||
if (t.equals(TorConstants.ID)) {
|
||||
return torPluginState;
|
||||
} else if (t.equals(LanTcpConstants.ID)) {
|
||||
return wifiPluginState;
|
||||
} else if (t.equals(BluetoothConstants.ID)) {
|
||||
return btPluginState;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
LiveData<State> getPluginState(TransportId t) {
|
||||
LiveData<Plugin.State> liveData = getPluginLiveData(t);
|
||||
if (liveData == null) throw new AssertionError();
|
||||
return liveData;
|
||||
}
|
||||
|
||||
int getReasonsDisabled(TransportId id) {
|
||||
Plugin plugin = pluginManager.getPlugin(id);
|
||||
return plugin == null ? 0 : plugin.getReasonsDisabled();
|
||||
}
|
||||
|
||||
void setPluginEnabled(TransportId t, boolean enabled) {
|
||||
pluginManager.setPluginEnabled(t, enabled);
|
||||
}
|
||||
|
||||
void setTorEnabled(boolean battery, boolean mobileData, boolean location) {
|
||||
Settings s = new Settings();
|
||||
s.putBoolean(PREF_PLUGIN_ENABLE, true);
|
||||
if (battery) s.putBoolean(PREF_TOR_ONLY_WHEN_CHARGING, false);
|
||||
if (mobileData) s.putBoolean(PREF_TOR_MOBILE, true);
|
||||
if (location) s.putInt(PREF_TOR_NETWORK, PREF_TOR_NETWORK_WITH_BRIDGES);
|
||||
dbExecutor.execute(() -> {
|
||||
try {
|
||||
settingsManager.mergeSettings(s, TorConstants.ID.getString());
|
||||
} catch (DbException e) {
|
||||
logException(LOG, WARNING, e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
String getCurrentCountryName() {
|
||||
return getCountryDisplayName(locationUtils.getCurrentCountry());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
package org.briarproject.briar.android.navdrawer;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ScrollView;
|
||||
|
||||
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
||||
import org.briarproject.bramble.api.plugin.LanTcpConstants;
|
||||
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||
import org.briarproject.bramble.api.plugin.TorConstants;
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
import org.briarproject.briar.R;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.appcompat.widget.AppCompatImageButton;
|
||||
import androidx.appcompat.widget.SwitchCompat;
|
||||
import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
import androidx.constraintlayout.widget.ConstraintSet;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
|
||||
import static android.bluetooth.BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION;
|
||||
import static android.os.Build.VERSION.SDK_INT;
|
||||
import static android.transition.TransitionManager.beginDelayedTransition;
|
||||
import static android.view.View.FOCUS_DOWN;
|
||||
import static androidx.core.content.ContextCompat.getColor;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.REASON_USER;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ACTIVE;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.DISABLED;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.ENABLING;
|
||||
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.REASON_BATTERY;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.REASON_COUNTRY_BLOCKED;
|
||||
import static org.briarproject.bramble.api.plugin.TorConstants.REASON_MOBILE_DATA;
|
||||
import static org.briarproject.briar.android.navdrawer.NavDrawerViewModel.TRANSPORT_IDS;
|
||||
import static org.briarproject.briar.android.util.UiUtils.getDialogIcon;
|
||||
|
||||
class PluginViewController {
|
||||
|
||||
private final AppCompatActivity activity;
|
||||
private final NavDrawerViewModel viewModel;
|
||||
private final ConstraintLayout drawerContent;
|
||||
private final ConstraintSet collapsedConstraints, expandedConstraints;
|
||||
private final AppCompatImageButton chevronView;
|
||||
private final ImageView torIcon, wifiIcon, btIcon;
|
||||
private final SwitchCompat torSwitch, wifiSwitch, btSwitch;
|
||||
|
||||
private boolean expanded = false;
|
||||
|
||||
PluginViewController(View v, AppCompatActivity activity,
|
||||
NavDrawerViewModel viewModel) {
|
||||
this.activity = activity;
|
||||
this.viewModel = viewModel;
|
||||
drawerContent = v.findViewById(R.id.drawerContent);
|
||||
|
||||
collapsedConstraints = new ConstraintSet();
|
||||
collapsedConstraints.clone(v.getContext(),
|
||||
R.layout.navigation_menu_collapsed);
|
||||
|
||||
expandedConstraints = new ConstraintSet();
|
||||
expandedConstraints.clone(v.getContext(),
|
||||
R.layout.navigation_menu_expanded);
|
||||
|
||||
// Scroll the drawer to the bottom when the view is expanded/collapsed
|
||||
ScrollView scrollView = v.findViewById(R.id.drawerScrollView);
|
||||
drawerContent.addOnLayoutChangeListener((view, left, top, right,
|
||||
bottom, oldLeft, oldTop, oldRight, oldBottom) ->
|
||||
scrollView.fullScroll(FOCUS_DOWN));
|
||||
|
||||
// Clicking the chevron expands or collapses the view
|
||||
chevronView = v.findViewById(R.id.chevronView);
|
||||
chevronView.setOnClickListener(view -> expandOrCollapseView());
|
||||
|
||||
// The whole view is clickable when collapsed
|
||||
v.findViewById(R.id.connectionsBackground).setOnClickListener(view ->
|
||||
expandOrCollapseView());
|
||||
|
||||
torIcon = v.findViewById(R.id.torIcon);
|
||||
wifiIcon = v.findViewById(R.id.wifiIcon);
|
||||
btIcon = v.findViewById(R.id.btIcon);
|
||||
|
||||
torSwitch = v.findViewById(R.id.torSwitch);
|
||||
wifiSwitch = v.findViewById(R.id.wifiSwitch);
|
||||
btSwitch = v.findViewById(R.id.btSwitch);
|
||||
|
||||
for (TransportId t : TRANSPORT_IDS) {
|
||||
// a OnCheckedChangeListener would get triggered on programmatic updates
|
||||
SwitchCompat switchCompat = getSwitch(t);
|
||||
switchCompat.setOnClickListener(buttonView -> {
|
||||
if (switchCompat.isChecked()) tryToEnablePlugin(t);
|
||||
else viewModel.setPluginEnabled(t, false);
|
||||
// Revert the switch to its previous state until the plugin
|
||||
// changes its state
|
||||
switchCompat.toggle();
|
||||
});
|
||||
viewModel.getPluginState(t).observe(activity, state ->
|
||||
stateUpdate(t, state));
|
||||
}
|
||||
}
|
||||
|
||||
private void expandOrCollapseView() {
|
||||
if (SDK_INT >= 19) beginDelayedTransition(drawerContent);
|
||||
if (expanded) {
|
||||
collapsedConstraints.applyTo(drawerContent);
|
||||
chevronView.setImageResource(R.drawable.chevron_up_white);
|
||||
} else {
|
||||
expandedConstraints.applyTo(drawerContent);
|
||||
chevronView.setImageResource(R.drawable.chevron_down_white);
|
||||
}
|
||||
expanded = !expanded;
|
||||
}
|
||||
|
||||
private void tryToEnablePlugin(TransportId id) {
|
||||
if (id.equals(TorConstants.ID)) {
|
||||
int reasons = viewModel.getReasonsDisabled(id);
|
||||
if (reasons == 0 || reasons == REASON_USER) {
|
||||
viewModel.setPluginEnabled(id, true);
|
||||
} else {
|
||||
showTorSettingsDialog(reasons);
|
||||
}
|
||||
} else if (id.equals(BluetoothConstants.ID)) {
|
||||
Intent i = new Intent(ACTION_REQUEST_DISCOVERABLE);
|
||||
i.putExtra(EXTRA_DISCOVERABLE_DURATION, 0);
|
||||
PackageManager pm = activity.getPackageManager();
|
||||
if (i.resolveActivity(pm) != null) {
|
||||
ActivityCompat.startActivity(activity, i, null);
|
||||
}
|
||||
viewModel.setPluginEnabled(id, true);
|
||||
} else {
|
||||
viewModel.setPluginEnabled(id, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void stateUpdate(TransportId id, State state) {
|
||||
updateIcon(getIcon(id), state);
|
||||
updateSwitch(getSwitch(id), state);
|
||||
}
|
||||
|
||||
private SwitchCompat getSwitch(TransportId id) {
|
||||
if (id == TorConstants.ID) return torSwitch;
|
||||
if (id == BluetoothConstants.ID) return btSwitch;
|
||||
if (id == LanTcpConstants.ID) return wifiSwitch;
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
private void updateSwitch(SwitchCompat switchCompat, State state) {
|
||||
boolean checked = state != STARTING_STOPPING && state != DISABLED;
|
||||
switchCompat.setChecked(checked);
|
||||
switchCompat.setEnabled(state != STARTING_STOPPING);
|
||||
}
|
||||
|
||||
private ImageView getIcon(TransportId id) {
|
||||
if (id == TorConstants.ID) return torIcon;
|
||||
if (id == BluetoothConstants.ID) return btIcon;
|
||||
if (id == LanTcpConstants.ID) return wifiIcon;
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
private void updateIcon(ImageView icon, State state) {
|
||||
int colorRes;
|
||||
if (state == ACTIVE) {
|
||||
colorRes = R.color.briar_green_light;
|
||||
} else if (state == ENABLING) {
|
||||
colorRes = R.color.briar_yellow;
|
||||
} else {
|
||||
colorRes = android.R.color.tertiary_text_light;
|
||||
}
|
||||
int color = getColor(icon.getContext(), colorRes);
|
||||
icon.setColorFilter(color);
|
||||
}
|
||||
|
||||
private void showTorSettingsDialog(int reasonsDisabled) {
|
||||
boolean battery = (reasonsDisabled & REASON_BATTERY) != 0;
|
||||
boolean mobileData = (reasonsDisabled & REASON_MOBILE_DATA) != 0;
|
||||
boolean location = (reasonsDisabled & REASON_COUNTRY_BLOCKED) != 0;
|
||||
|
||||
StringBuilder s = new StringBuilder();
|
||||
if (location) {
|
||||
s.append("\t\u2022 ");
|
||||
s.append(activity.getString(R.string.tor_override_network_setting,
|
||||
viewModel.getCurrentCountryName()));
|
||||
s.append('\n');
|
||||
}
|
||||
if (mobileData) {
|
||||
s.append("\t\u2022 ");
|
||||
s.append(activity.getString(
|
||||
R.string.tor_override_mobile_data_setting));
|
||||
s.append('\n');
|
||||
}
|
||||
if (battery) {
|
||||
s.append("\t\u2022 ");
|
||||
s.append(activity.getString(R.string.tor_only_when_charging_title));
|
||||
s.append('\n');
|
||||
}
|
||||
String message = activity.getString(
|
||||
R.string.tor_override_settings_body, s.toString());
|
||||
|
||||
AlertDialog.Builder b =
|
||||
new AlertDialog.Builder(activity, R.style.BriarDialogTheme);
|
||||
b.setTitle(R.string.tor_override_settings_title);
|
||||
b.setIcon(getDialogIcon(activity, R.drawable.ic_settings_black_24dp));
|
||||
b.setMessage(message);
|
||||
b.setPositiveButton(R.string.tor_override_settings_confirm,
|
||||
(dialog, which) ->
|
||||
viewModel.setTorEnabled(battery, mobileData, location));
|
||||
b.setNegativeButton(R.string.cancel, (dialog, which) ->
|
||||
dialog.dismiss());
|
||||
b.show();
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
package org.briarproject.briar.android.navdrawer;
|
||||
|
||||
import org.briarproject.bramble.api.plugin.TransportId;
|
||||
|
||||
import androidx.annotation.UiThread;
|
||||
|
||||
interface TransportStateListener {
|
||||
|
||||
@UiThread
|
||||
void stateUpdate(TransportId id, boolean enabled);
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user