Ignore reflected address until we've made a Bluetooth connection.

This reduces the opportunities for contacts to reflect a false address.
This commit is contained in:
akwizgran
2020-07-20 16:18:25 +01:00
parent 49f0640278
commit a4091be6f7
2 changed files with 31 additions and 4 deletions

View File

@@ -12,7 +12,10 @@ public interface BluetoothConstants {
// Local settings (not shared with contacts) // Local settings (not shared with contacts)
String PREF_ADDRESS_IS_REFLECTED = "addressIsReflected"; String PREF_ADDRESS_IS_REFLECTED = "addressIsReflected";
String PREF_EVER_CONNECTED = "everConnected";
// Default value for PREF_PLUGIN_ENABLE // Default values for local settings
boolean DEFAULT_PREF_PLUGIN_ENABLE = false; boolean DEFAULT_PREF_PLUGIN_ENABLE = false;
boolean DEFAULT_PREF_ADDRESS_IS_REFLECTED = false;
boolean DEFAULT_PREF_EVER_CONNECTED = false;
} }

View File

@@ -48,9 +48,12 @@ import static java.util.logging.Level.INFO;
import static java.util.logging.Level.WARNING; import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger; import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH; import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_BLUETOOTH;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.DEFAULT_PREF_ADDRESS_IS_REFLECTED;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.DEFAULT_PREF_EVER_CONNECTED;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.DEFAULT_PREF_PLUGIN_ENABLE; import static org.briarproject.bramble.api.plugin.BluetoothConstants.DEFAULT_PREF_PLUGIN_ENABLE;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.ID; import static org.briarproject.bramble.api.plugin.BluetoothConstants.ID;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_ADDRESS_IS_REFLECTED; import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_ADDRESS_IS_REFLECTED;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_EVER_CONNECTED;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PROP_ADDRESS; 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.PROP_UUID;
import static org.briarproject.bramble.api.plugin.BluetoothConstants.UUID_BYTES; import static org.briarproject.bramble.api.plugin.BluetoothConstants.UUID_BYTES;
@@ -81,6 +84,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
private final PluginCallback callback; private final PluginCallback callback;
private final int maxLatency, maxIdleTime; private final int maxLatency, maxIdleTime;
private final AtomicBoolean used = new AtomicBoolean(false); private final AtomicBoolean used = new AtomicBoolean(false);
private final AtomicBoolean everConnected = new AtomicBoolean(false);
protected final PluginState state = new PluginState(); protected final PluginState state = new PluginState();
@@ -171,6 +175,8 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
Settings settings = callback.getSettings(); Settings settings = callback.getSettings();
boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE, boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE,
DEFAULT_PREF_PLUGIN_ENABLE); DEFAULT_PREF_PLUGIN_ENABLE);
everConnected.set(settings.getBoolean(PREF_EVER_CONNECTED,
DEFAULT_PREF_EVER_CONNECTED));
state.setStarted(enabledByUser); state.setStarted(enabledByUser);
try { try {
initialiseAdapter(); initialiseAdapter();
@@ -210,14 +216,15 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
String address = p.get(PROP_ADDRESS); String address = p.get(PROP_ADDRESS);
String uuid = p.get(PROP_UUID); String uuid = p.get(PROP_UUID);
Settings s = callback.getSettings(); Settings s = callback.getSettings();
boolean isReflected = s.getBoolean(PREF_ADDRESS_IS_REFLECTED, false); boolean isReflected = s.getBoolean(PREF_ADDRESS_IS_REFLECTED,
DEFAULT_PREF_ADDRESS_IS_REFLECTED);
boolean changed = false; boolean changed = false;
if (address == null || isReflected) { if (address == null || isReflected) {
address = getBluetoothAddress(); address = getBluetoothAddress();
if (LOG.isLoggable(INFO)) { if (LOG.isLoggable(INFO)) {
LOG.info("Local address " + scrubMacAddress(address)); LOG.info("Local address " + scrubMacAddress(address));
} }
if (address == null) { if (address == null && everConnected.get()) {
address = getReflectedAddress(); address = getReflectedAddress();
if (LOG.isLoggable(INFO)) { if (LOG.isLoggable(INFO)) {
LOG.info("Reflected address " + scrubMacAddress(address)); LOG.info("Reflected address " + scrubMacAddress(address));
@@ -283,10 +290,23 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
LOG.info("Connection received"); LOG.info("Connection received");
connectionLimiter.connectionOpened(conn); connectionLimiter.connectionOpened(conn);
backoff.reset(); backoff.reset();
setEverConnected();
callback.handleConnection(conn); callback.handleConnection(conn);
} }
} }
private void setEverConnected() {
if (!everConnected.getAndSet(true)) {
ioExecutor.execute(() -> {
Settings s = new Settings();
s.putBoolean(PREF_EVER_CONNECTED, true);
callback.mergeSettings(s);
// Contacts may already have sent a reflected address
updateProperties();
});
}
}
@Override @Override
public void stop() { public void stop() {
SS ss = state.setStopped(); SS ss = state.setStopped();
@@ -333,6 +353,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
DuplexTransportConnection d = createConnection(p); DuplexTransportConnection d = createConnection(p);
if (d != null) { if (d != null) {
backoff.reset(); backoff.reset();
setEverConnected();
h.handleConnection(d); h.handleConnection(d);
} }
}); });
@@ -435,7 +456,10 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
LOG.info("Connecting to key agreement UUID " + uuid); LOG.info("Connecting to key agreement UUID " + uuid);
conn = connect(address, uuid); conn = connect(address, uuid);
} }
if (conn != null) connectionLimiter.connectionOpened(conn); if (conn != null) {
connectionLimiter.connectionOpened(conn);
setEverConnected();
}
return conn; return conn;
} }