mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-14 03:39:05 +01:00
Compare commits
109 Commits
1712-passi
...
1387-persi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ea4463cbc | ||
|
|
a83682d4b6 | ||
|
|
b1ec344bdb | ||
|
|
821327d62e | ||
|
|
cda722a8b2 | ||
|
|
ca5c18ece3 | ||
|
|
81ed5978d6 | ||
|
|
a63619ab47 | ||
|
|
2d88819e80 | ||
|
|
fef19c1329 | ||
|
|
7ce91066f5 | ||
|
|
dfb581ef12 | ||
|
|
d9b4c013bb | ||
|
|
61407c3e06 | ||
|
|
ab682c82a3 | ||
|
|
375a7276ad | ||
|
|
b7084b2486 | ||
|
|
aa152a80d1 | ||
|
|
3f0d9233d9 | ||
|
|
9d96ce6db0 | ||
|
|
45fb5bb445 | ||
|
|
0756d92ca1 | ||
|
|
37f80c7682 | ||
|
|
b409215c57 | ||
|
|
4f0aaf03fd | ||
|
|
597fef6d50 | ||
|
|
1dd15567de | ||
|
|
428f06abdd | ||
|
|
e1d1c62708 | ||
|
|
ae75090d23 | ||
|
|
443043ae09 | ||
|
|
fb85730b8e | ||
|
|
48b1e77065 | ||
|
|
a03953563f | ||
|
|
033fd2d3b4 | ||
|
|
011d8e1df7 | ||
|
|
ef5e2dad72 | ||
|
|
f35e87c8ad | ||
|
|
e4940a046a | ||
|
|
0a666df164 | ||
|
|
6fb4b95b18 | ||
|
|
5567982fb4 | ||
|
|
25e50ceb10 | ||
|
|
1495daf977 | ||
|
|
badc6da649 | ||
|
|
e065d45d16 | ||
|
|
d0c53f1310 | ||
|
|
e1084ffadd | ||
|
|
2bd2f67693 | ||
|
|
c2b0a4b8d1 | ||
|
|
ee19d2f574 | ||
|
|
e9ec5734e2 | ||
|
|
7b1c6f3fdd | ||
|
|
d689cf776c | ||
|
|
f0fd1844dd | ||
|
|
d16a301fc4 | ||
|
|
3ab88181eb | ||
|
|
802e599f09 | ||
|
|
a6bd59d3c9 | ||
|
|
b04b724028 | ||
|
|
71b0408fe6 | ||
|
|
2d38bd5734 | ||
|
|
ff5da8404a | ||
|
|
75615a4e7f | ||
|
|
96e32ad64e | ||
|
|
0fec5d7783 | ||
|
|
ee74b3774b | ||
|
|
c783a2f352 | ||
|
|
77aa5401f3 | ||
|
|
99686f5316 | ||
|
|
f5b4f6e071 | ||
|
|
a2de841e6a | ||
|
|
1f94c2d4e8 | ||
|
|
413ce29c0c | ||
|
|
c67f758c90 | ||
|
|
339524500b | ||
|
|
03811f78fa | ||
|
|
fc86c46456 | ||
|
|
7ae86d70af | ||
|
|
63e3c661a3 | ||
|
|
4f54bd90fb | ||
|
|
706c03aa8b | ||
|
|
c42a987927 | ||
|
|
297dbe0b16 | ||
|
|
4130662e1f | ||
|
|
c08bdf96cd | ||
|
|
8bb534564f | ||
|
|
5e60a717fc | ||
|
|
dd1509350c | ||
|
|
465ba3d337 | ||
|
|
7561c5039e | ||
|
|
242d6f8a0e | ||
|
|
c554847b54 | ||
|
|
d30b250389 | ||
|
|
ecea2c587d | ||
|
|
43a91e2e57 | ||
|
|
ea288b998b | ||
|
|
48dc598ca3 | ||
|
|
e2d63ac6a4 | ||
|
|
afc85cdf52 | ||
|
|
b2a1ea84f8 | ||
|
|
fcc26c093b | ||
|
|
5a741bf13b | ||
|
|
5dc460851b | ||
|
|
b805514f70 | ||
|
|
69d94c9f29 | ||
|
|
53d4b7a0df | ||
|
|
dcb5f95934 | ||
|
|
ff8a422638 |
9
TRANSLATION.md
Normal file
9
TRANSLATION.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
Translations for this project are managed through Transifex:
|
||||||
|
|
||||||
|
https://transifex.com/otf/briar
|
||||||
|
|
||||||
|
If you'd like to volunteer as a translator, please create a Transifex account and request to be
|
||||||
|
added to the project's translation team. The Localization Lab has some instructions and advice for
|
||||||
|
translators here:
|
||||||
|
|
||||||
|
https://wiki.localizationlab.org/index.php/Briar
|
||||||
@@ -11,8 +11,8 @@ android {
|
|||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 16
|
minSdkVersion 16
|
||||||
targetSdkVersion 28
|
targetSdkVersion 28
|
||||||
versionCode 10207
|
versionCode 10209
|
||||||
versionName "1.2.7"
|
versionName "1.2.9"
|
||||||
consumerProguardFiles 'proguard-rules.txt'
|
consumerProguardFiles 'proguard-rules.txt'
|
||||||
|
|
||||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||||
|
|||||||
@@ -9,8 +9,10 @@ import org.briarproject.bramble.api.account.AccountManager;
|
|||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -20,17 +22,21 @@ import javax.annotation.Nullable;
|
|||||||
import javax.annotation.concurrent.GuardedBy;
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static android.content.Context.MODE_PRIVATE;
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static java.util.logging.Level.INFO;
|
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.IoUtils.deleteFileOrDir;
|
import static org.briarproject.bramble.util.IoUtils.deleteFileOrDir;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
import static org.briarproject.bramble.util.LogUtils.logFileOrDir;
|
||||||
|
|
||||||
class AndroidAccountManager extends AccountManagerImpl
|
class AndroidAccountManager extends AccountManagerImpl
|
||||||
implements AccountManager {
|
implements AccountManager {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(AndroidAccountManager.class.getName());
|
getLogger(AndroidAccountManager.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Directories that shouldn't be deleted when deleting the user's account.
|
* Directories that shouldn't be deleted when deleting the user's account.
|
||||||
@@ -40,13 +46,16 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
|
|
||||||
protected final Context appContext;
|
protected final Context appContext;
|
||||||
private final SharedPreferences prefs;
|
private final SharedPreferences prefs;
|
||||||
|
private final PersistentLogManager logManager;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
AndroidAccountManager(DatabaseConfig databaseConfig,
|
AndroidAccountManager(DatabaseConfig databaseConfig,
|
||||||
CryptoComponent crypto, IdentityManager identityManager,
|
CryptoComponent crypto, IdentityManager identityManager,
|
||||||
SharedPreferences prefs, Application app) {
|
SharedPreferences prefs, PersistentLogManager logManager,
|
||||||
|
Application app) {
|
||||||
super(databaseConfig, crypto, identityManager);
|
super(databaseConfig, crypto, identityManager);
|
||||||
this.prefs = prefs;
|
this.prefs = prefs;
|
||||||
|
this.logManager = logManager;
|
||||||
appContext = app.getApplicationContext();
|
appContext = app.getApplicationContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -74,6 +83,7 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
LOG.info("Contents of account directory after deleting:");
|
LOG.info("Contents of account directory after deleting:");
|
||||||
logFileOrDir(LOG, INFO, getDataDir());
|
logFileOrDir(LOG, INFO, getDataDir());
|
||||||
}
|
}
|
||||||
|
replacePersistentLogger();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,4 +144,13 @@ class AndroidAccountManager extends AccountManagerImpl
|
|||||||
private void addIfNotNull(Set<File> files, @Nullable File file) {
|
private void addIfNotNull(Set<File> files, @Nullable File file) {
|
||||||
if (file != null) files.add(file);
|
if (file != null) files.add(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void replacePersistentLogger() {
|
||||||
|
File logDir = appContext.getDir("log", MODE_PRIVATE);
|
||||||
|
try {
|
||||||
|
logManager.addLogHandler(logDir, getLogger(""));
|
||||||
|
} catch (IOException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import java.util.concurrent.BlockingQueue;
|
|||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
@@ -66,6 +67,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
|||||||
|
|
||||||
private static final int MAX_DISCOVERY_MS = 10_000;
|
private static final int MAX_DISCOVERY_MS = 10_000;
|
||||||
|
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
private final AndroidExecutor androidExecutor;
|
private final AndroidExecutor androidExecutor;
|
||||||
private final Context appContext;
|
private final Context appContext;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
@@ -78,11 +80,13 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
|||||||
|
|
||||||
AndroidBluetoothPlugin(BluetoothConnectionLimiter connectionLimiter,
|
AndroidBluetoothPlugin(BluetoothConnectionLimiter connectionLimiter,
|
||||||
TimeoutMonitor timeoutMonitor, Executor ioExecutor,
|
TimeoutMonitor timeoutMonitor, Executor ioExecutor,
|
||||||
SecureRandom secureRandom, AndroidExecutor androidExecutor,
|
SecureRandom secureRandom, ScheduledExecutorService scheduler,
|
||||||
Context appContext, Clock clock, Backoff backoff,
|
AndroidExecutor androidExecutor, Context appContext, Clock clock,
|
||||||
PluginCallback callback, int maxLatency, int maxIdleTime) {
|
Backoff backoff, PluginCallback callback, int maxLatency,
|
||||||
|
int maxIdleTime) {
|
||||||
super(connectionLimiter, timeoutMonitor, ioExecutor, secureRandom,
|
super(connectionLimiter, timeoutMonitor, ioExecutor, secureRandom,
|
||||||
backoff, callback, maxLatency, maxIdleTime);
|
backoff, callback, maxLatency, maxIdleTime);
|
||||||
|
this.scheduler = scheduler;
|
||||||
this.androidExecutor = androidExecutor;
|
this.androidExecutor = androidExecutor;
|
||||||
this.appContext = appContext;
|
this.appContext = appContext;
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
@@ -150,6 +154,12 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
|||||||
wasEnabledByUs = true;
|
wasEnabledByUs = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
void onAdapterDisabled() {
|
||||||
|
super.onAdapterDisabled();
|
||||||
|
wasEnabledByUs = false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Nullable
|
@Nullable
|
||||||
String getBluetoothAddress() {
|
String getBluetoothAddress() {
|
||||||
@@ -177,7 +187,7 @@ class AndroidBluetoothPlugin extends BluetoothPlugin<BluetoothServerSocket> {
|
|||||||
private DuplexTransportConnection wrapSocket(BluetoothSocket s)
|
private DuplexTransportConnection wrapSocket(BluetoothSocket s)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
return new AndroidBluetoothTransportConnection(this, connectionLimiter,
|
return new AndroidBluetoothTransportConnection(this, connectionLimiter,
|
||||||
timeoutMonitor, s);
|
timeoutMonitor, appContext, scheduler, s);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import org.briarproject.bramble.api.system.Clock;
|
|||||||
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
@@ -32,6 +33,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
|||||||
private static final double BACKOFF_BASE = 1.2;
|
private static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
private final AndroidExecutor androidExecutor;
|
private final AndroidExecutor androidExecutor;
|
||||||
private final Context appContext;
|
private final Context appContext;
|
||||||
private final SecureRandom secureRandom;
|
private final SecureRandom secureRandom;
|
||||||
@@ -41,10 +43,12 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
|||||||
private final BackoffFactory backoffFactory;
|
private final BackoffFactory backoffFactory;
|
||||||
|
|
||||||
public AndroidBluetoothPluginFactory(Executor ioExecutor,
|
public AndroidBluetoothPluginFactory(Executor ioExecutor,
|
||||||
|
ScheduledExecutorService scheduler,
|
||||||
AndroidExecutor androidExecutor, Context appContext,
|
AndroidExecutor androidExecutor, Context appContext,
|
||||||
SecureRandom secureRandom, EventBus eventBus, Clock clock,
|
SecureRandom secureRandom, EventBus eventBus, Clock clock,
|
||||||
TimeoutMonitor timeoutMonitor, BackoffFactory backoffFactory) {
|
TimeoutMonitor timeoutMonitor, BackoffFactory backoffFactory) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
|
this.scheduler = scheduler;
|
||||||
this.androidExecutor = androidExecutor;
|
this.androidExecutor = androidExecutor;
|
||||||
this.appContext = appContext;
|
this.appContext = appContext;
|
||||||
this.secureRandom = secureRandom;
|
this.secureRandom = secureRandom;
|
||||||
@@ -72,7 +76,7 @@ public class AndroidBluetoothPluginFactory implements DuplexPluginFactory {
|
|||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin(
|
AndroidBluetoothPlugin plugin = new AndroidBluetoothPlugin(
|
||||||
connectionLimiter, timeoutMonitor, ioExecutor, secureRandom,
|
connectionLimiter, timeoutMonitor, ioExecutor, secureRandom,
|
||||||
androidExecutor, appContext, clock, backoff,
|
scheduler, androidExecutor, appContext, clock, backoff,
|
||||||
callback, MAX_LATENCY, MAX_IDLE_TIME);
|
callback, MAX_LATENCY, MAX_IDLE_TIME);
|
||||||
eventBus.addListener(plugin);
|
eventBus.addListener(plugin);
|
||||||
return plugin;
|
return plugin;
|
||||||
|
|||||||
@@ -1,17 +1,26 @@
|
|||||||
package org.briarproject.bramble.plugin.bluetooth;
|
package org.briarproject.bramble.plugin.bluetooth;
|
||||||
|
|
||||||
import android.bluetooth.BluetoothSocket;
|
import android.bluetooth.BluetoothSocket;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.PowerManager;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
import org.briarproject.bramble.api.io.TimeoutMonitor;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Plugin;
|
import org.briarproject.bramble.api.plugin.Plugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.AbstractDuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.AbstractDuplexTransportConnection;
|
||||||
|
import org.briarproject.bramble.util.RenewableWakeLock;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
|
||||||
|
import static android.content.Context.POWER_SERVICE;
|
||||||
|
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
|
||||||
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||||
|
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_ADDRESS;
|
||||||
|
import static org.briarproject.bramble.util.AndroidUtils.getWakeLockTag;
|
||||||
import static org.briarproject.bramble.util.AndroidUtils.isValidBluetoothAddress;
|
import static org.briarproject.bramble.util.AndroidUtils.isValidBluetoothAddress;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
@@ -19,18 +28,26 @@ class AndroidBluetoothTransportConnection
|
|||||||
extends AbstractDuplexTransportConnection {
|
extends AbstractDuplexTransportConnection {
|
||||||
|
|
||||||
private final BluetoothConnectionLimiter connectionLimiter;
|
private final BluetoothConnectionLimiter connectionLimiter;
|
||||||
|
private final RenewableWakeLock wakeLock;
|
||||||
private final BluetoothSocket socket;
|
private final BluetoothSocket socket;
|
||||||
private final InputStream in;
|
private final InputStream in;
|
||||||
|
|
||||||
AndroidBluetoothTransportConnection(Plugin plugin,
|
AndroidBluetoothTransportConnection(Plugin plugin,
|
||||||
BluetoothConnectionLimiter connectionLimiter,
|
BluetoothConnectionLimiter connectionLimiter,
|
||||||
TimeoutMonitor timeoutMonitor, BluetoothSocket socket)
|
TimeoutMonitor timeoutMonitor, Context appContext,
|
||||||
|
ScheduledExecutorService scheduler, BluetoothSocket socket)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
super(plugin);
|
super(plugin);
|
||||||
this.connectionLimiter = connectionLimiter;
|
this.connectionLimiter = connectionLimiter;
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
in = timeoutMonitor.createTimeoutInputStream(
|
in = timeoutMonitor.createTimeoutInputStream(
|
||||||
socket.getInputStream(), plugin.getMaxIdleTime() * 2);
|
socket.getInputStream(), plugin.getMaxIdleTime() * 2);
|
||||||
|
PowerManager powerManager = (PowerManager)
|
||||||
|
requireNonNull(appContext.getSystemService(POWER_SERVICE));
|
||||||
|
String tag = getWakeLockTag(appContext);
|
||||||
|
wakeLock = new RenewableWakeLock(powerManager, scheduler,
|
||||||
|
PARTIAL_WAKE_LOCK, tag, 1, MINUTES);
|
||||||
|
wakeLock.acquire();
|
||||||
String address = socket.getRemoteDevice().getAddress();
|
String address = socket.getRemoteDevice().getAddress();
|
||||||
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);
|
if (isValidBluetoothAddress(address)) remote.put(PROP_ADDRESS, address);
|
||||||
}
|
}
|
||||||
@@ -50,6 +67,7 @@ class AndroidBluetoothTransportConnection
|
|||||||
try {
|
try {
|
||||||
socket.close();
|
socket.close();
|
||||||
} finally {
|
} finally {
|
||||||
|
wakeLock.release();
|
||||||
connectionLimiter.connectionClosed(this);
|
connectionLimiter.connectionClosed(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,30 @@
|
|||||||
package org.briarproject.bramble.plugin.tcp;
|
package org.briarproject.bramble.plugin.tcp;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.LinkAddress;
|
||||||
|
import android.net.LinkProperties;
|
||||||
import android.net.Network;
|
import android.net.Network;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkCapabilities;
|
||||||
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiInfo;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
|
|
||||||
import org.briarproject.bramble.PoliteExecutor;
|
import org.briarproject.bramble.PoliteExecutor;
|
||||||
|
import org.briarproject.bramble.api.Pair;
|
||||||
import org.briarproject.bramble.api.event.Event;
|
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.network.event.NetworkStatusEvent;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||||
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
import java.net.InterfaceAddress;
|
||||||
|
import java.net.NetworkInterface;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.net.SocketException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
@@ -28,14 +35,21 @@ import javax.net.SocketFactory;
|
|||||||
|
|
||||||
import static android.content.Context.CONNECTIVITY_SERVICE;
|
import static android.content.Context.CONNECTIVITY_SERVICE;
|
||||||
import static android.content.Context.WIFI_SERVICE;
|
import static android.content.Context.WIFI_SERVICE;
|
||||||
import static android.net.ConnectivityManager.TYPE_WIFI;
|
import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
||||||
import static android.os.Build.VERSION.SDK_INT;
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
import static java.util.Collections.emptyList;
|
import static java.util.Collections.emptyList;
|
||||||
|
import static java.util.Collections.list;
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
|
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.plugin.LanTcpConstants.DEFAULT_PREF_PLUGIN_ENABLE;
|
||||||
|
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;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
class AndroidLanTcpPlugin extends LanTcpPlugin {
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(AndroidLanTcpPlugin.class.getName());
|
getLogger(AndroidLanTcpPlugin.class.getName());
|
||||||
@@ -68,37 +82,137 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
|||||||
public void start() {
|
public void start() {
|
||||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||||
initialisePortProperty();
|
initialisePortProperty();
|
||||||
running = true;
|
Settings settings = callback.getSettings();
|
||||||
|
state.setStarted(settings.getBoolean(PREF_PLUGIN_ENABLE,
|
||||||
|
DEFAULT_PREF_PLUGIN_ENABLE));
|
||||||
updateConnectionStatus();
|
updateConnectionStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void stop() {
|
|
||||||
running = false;
|
|
||||||
tryToClose(socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Socket createSocket() throws IOException {
|
protected Socket createSocket() throws IOException {
|
||||||
return socketFactory.createSocket();
|
return socketFactory.createSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<InetAddress> getUsableLocalInetAddresses() {
|
protected List<InetAddress> getUsableLocalInetAddresses(boolean ipv4) {
|
||||||
// If the device doesn't have wifi, don't open any sockets
|
InetAddress addr = getWifiAddress(ipv4);
|
||||||
if (wifiManager == null) return emptyList();
|
return addr == null ? emptyList() : singletonList(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private InetAddress getWifiAddress(boolean ipv4) {
|
||||||
|
Pair<InetAddress, Boolean> wifi = getWifiIpv4Address();
|
||||||
|
if (ipv4) return wifi == null ? null : wifi.getFirst();
|
||||||
|
// If there's no wifi IPv4 address, we might be a client on an
|
||||||
|
// IPv6-only wifi network. We can only detect this on API 21+
|
||||||
|
if (wifi == null) {
|
||||||
|
return SDK_INT >= 21 ? getWifiClientIpv6Address() : null;
|
||||||
|
}
|
||||||
|
// Use the wifi IPv4 address to determine which interface's IPv6
|
||||||
|
// address we should return (if the interface has a suitable address)
|
||||||
|
return getIpv6AddressForInterface(wifi.getFirst());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a {@link Pair} where the first element is the IPv4 address of
|
||||||
|
* the wifi interface and the second element is true if this device is
|
||||||
|
* providing an access point, or false if this device is a client. Returns
|
||||||
|
* null if this device isn't connected to wifi as an access point or client.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private Pair<InetAddress, Boolean> getWifiIpv4Address() {
|
||||||
|
if (wifiManager == null) return null;
|
||||||
// If we're connected to a wifi network, return its address
|
// If we're connected to a wifi network, return its address
|
||||||
WifiInfo info = wifiManager.getConnectionInfo();
|
WifiInfo info = wifiManager.getConnectionInfo();
|
||||||
if (info != null && info.getIpAddress() != 0) {
|
if (info != null && info.getIpAddress() != 0) {
|
||||||
return singletonList(intToInetAddress(info.getIpAddress()));
|
return new Pair<>(intToInetAddress(info.getIpAddress()), false);
|
||||||
}
|
}
|
||||||
// If we're running an access point, return its address
|
List<InterfaceAddress> ifAddrs = getLocalInterfaceAddresses();
|
||||||
for (InetAddress addr : getLocalInetAddresses()) {
|
// If we're providing a normal access point, return its address
|
||||||
if (addr.equals(WIFI_AP_ADDRESS)) return singletonList(addr);
|
for (InterfaceAddress ifAddr : ifAddrs) {
|
||||||
if (addr.equals(WIFI_DIRECT_AP_ADDRESS)) return singletonList(addr);
|
if (isAndroidWifiApAddress(ifAddr)) {
|
||||||
|
return new Pair<>(ifAddr.getAddress(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If we're providing a wifi direct access point, return its address
|
||||||
|
for (InterfaceAddress ifAddr : ifAddrs) {
|
||||||
|
if (isAndroidWifiDirectApAddress(ifAddr)) {
|
||||||
|
return new Pair<>(ifAddr.getAddress(), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Not connected to wifi
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the given address belongs to a network provided by an
|
||||||
|
* Android access point (including the access point's own address).
|
||||||
|
* <p>
|
||||||
|
* The access point's address is usually 192.168.43.1, but at least one
|
||||||
|
* device (Honor 8A) may use other addresses in the range 192.168.43.0/24.
|
||||||
|
*/
|
||||||
|
private boolean isAndroidWifiApAddress(InterfaceAddress ifAddr) {
|
||||||
|
if (ifAddr.getNetworkPrefixLength() != 24) return false;
|
||||||
|
byte[] ip = ifAddr.getAddress().getAddress();
|
||||||
|
return ip.length == 4
|
||||||
|
&& ip[0] == (byte) 192
|
||||||
|
&& ip[1] == (byte) 168
|
||||||
|
&& ip[2] == (byte) 43;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the given address belongs to a network provided by an
|
||||||
|
* Android wifi direct legacy mode access point (including the access
|
||||||
|
* point's own address).
|
||||||
|
*/
|
||||||
|
private boolean isAndroidWifiDirectApAddress(InterfaceAddress ifAddr) {
|
||||||
|
if (ifAddr.getNetworkPrefixLength() != 24) return false;
|
||||||
|
byte[] ip = ifAddr.getAddress().getAddress();
|
||||||
|
return ip.length == 4
|
||||||
|
&& ip[0] == (byte) 192
|
||||||
|
&& ip[1] == (byte) 168
|
||||||
|
&& ip[2] == (byte) 49;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a link-local IPv6 address for the wifi client interface, or null
|
||||||
|
* if there's no such interface or it doesn't have a suitable address.
|
||||||
|
*/
|
||||||
|
@TargetApi(21)
|
||||||
|
@Nullable
|
||||||
|
private InetAddress getWifiClientIpv6Address() {
|
||||||
|
for (Network net : connectivityManager.getAllNetworks()) {
|
||||||
|
NetworkCapabilities caps =
|
||||||
|
connectivityManager.getNetworkCapabilities(net);
|
||||||
|
if (caps == null || !caps.hasTransport(TRANSPORT_WIFI)) continue;
|
||||||
|
LinkProperties props = connectivityManager.getLinkProperties(net);
|
||||||
|
if (props == null) continue;
|
||||||
|
for (LinkAddress linkAddress : props.getLinkAddresses()) {
|
||||||
|
InetAddress addr = linkAddress.getAddress();
|
||||||
|
if (isIpv6LinkLocalAddress(addr)) return addr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a link-local IPv6 address for the interface with the given IPv4
|
||||||
|
* address, or null if the interface doesn't have a suitable address.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private InetAddress getIpv6AddressForInterface(InetAddress ipv4) {
|
||||||
|
try {
|
||||||
|
NetworkInterface iface = NetworkInterface.getByInetAddress(ipv4);
|
||||||
|
if (iface == null) return null;
|
||||||
|
for (InetAddress addr : list(iface.getInetAddresses())) {
|
||||||
|
if (isIpv6LinkLocalAddress(addr)) return addr;
|
||||||
|
}
|
||||||
|
// No suitable address
|
||||||
|
return null;
|
||||||
|
} catch (SocketException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
// No suitable addresses
|
|
||||||
return emptyList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private InetAddress intToInetAddress(int ip) {
|
private InetAddress intToInetAddress(int ip) {
|
||||||
@@ -120,9 +234,11 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
|||||||
private SocketFactory getSocketFactory() {
|
private SocketFactory getSocketFactory() {
|
||||||
if (SDK_INT < 21) return SocketFactory.getDefault();
|
if (SDK_INT < 21) return SocketFactory.getDefault();
|
||||||
for (Network net : connectivityManager.getAllNetworks()) {
|
for (Network net : connectivityManager.getAllNetworks()) {
|
||||||
NetworkInfo info = connectivityManager.getNetworkInfo(net);
|
NetworkCapabilities caps =
|
||||||
if (info != null && info.getType() == TYPE_WIFI)
|
connectivityManager.getNetworkCapabilities(net);
|
||||||
|
if (caps != null && caps.hasTransport(TRANSPORT_WIFI)) {
|
||||||
return net.getSocketFactory();
|
return net.getSocketFactory();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
LOG.warning("Could not find suitable socket factory");
|
LOG.warning("Could not find suitable socket factory");
|
||||||
return SocketFactory.getDefault();
|
return SocketFactory.getDefault();
|
||||||
@@ -130,31 +246,59 @@ class AndroidLanTcpPlugin extends LanTcpPlugin implements EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
|
super.eventOccurred(e);
|
||||||
if (e instanceof NetworkStatusEvent) updateConnectionStatus();
|
if (e instanceof NetworkStatusEvent) updateConnectionStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateConnectionStatus() {
|
private void updateConnectionStatus() {
|
||||||
connectionStatusExecutor.execute(() -> {
|
connectionStatusExecutor.execute(() -> {
|
||||||
if (!running) return;
|
State s = getState();
|
||||||
List<InetAddress> addrs = getUsableLocalInetAddresses();
|
if (s != ACTIVE && s != INACTIVE) return;
|
||||||
if (addrs.contains(WIFI_AP_ADDRESS)
|
Pair<InetAddress, Boolean> wifi = getPreferredWifiAddress();
|
||||||
|| addrs.contains(WIFI_DIRECT_AP_ADDRESS)) {
|
if (wifi == null) {
|
||||||
|
LOG.info("Not connected to wifi");
|
||||||
|
socketFactory = SocketFactory.getDefault();
|
||||||
|
// Server sockets may not have been closed automatically when
|
||||||
|
// interface was taken down. If any sockets are open, closing
|
||||||
|
// them here will cause the sockets to be cleared and the state
|
||||||
|
// to be updated in acceptContactConnections()
|
||||||
|
if (s == ACTIVE) {
|
||||||
|
LOG.info("Closing server sockets");
|
||||||
|
tryToClose(state.getServerSocket(true), LOG, WARNING);
|
||||||
|
tryToClose(state.getServerSocket(false), LOG, WARNING);
|
||||||
|
}
|
||||||
|
} else if (wifi.getSecond()) {
|
||||||
LOG.info("Providing wifi hotspot");
|
LOG.info("Providing wifi hotspot");
|
||||||
// There's no corresponding Network object and thus no way
|
// There's no corresponding Network object and thus no way
|
||||||
// to get a suitable socket factory, so we won't be able to
|
// to get a suitable socket factory, so we won't be able to
|
||||||
// make outgoing connections on API 21+ if another network
|
// make outgoing connections on API 21+ if another network
|
||||||
// has internet access
|
// has internet access
|
||||||
socketFactory = SocketFactory.getDefault();
|
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);
|
|
||||||
} else {
|
} else {
|
||||||
LOG.info("Connected to wifi");
|
LOG.info("Connected to wifi");
|
||||||
socketFactory = getSocketFactory();
|
socketFactory = getSocketFactory();
|
||||||
if (socket == null || socket.isClosed()) bind();
|
if (s == INACTIVE) bind();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a {@link Pair} where the first element is an IP address (IPv4 if
|
||||||
|
* available, otherwise IPv6) of the wifi interface and the second element
|
||||||
|
* is true if this device is providing an access point, or false if this
|
||||||
|
* device is a client. Returns null if this device isn't connected to wifi
|
||||||
|
* as an access point or client.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private Pair<InetAddress, Boolean> getPreferredWifiAddress() {
|
||||||
|
Pair<InetAddress, Boolean> wifi = getWifiIpv4Address();
|
||||||
|
// If there's no wifi IPv4 address, we might be a client on an
|
||||||
|
// IPv6-only wifi network. We can only detect this on API 21+
|
||||||
|
if (wifi == null && SDK_INT >= 21) {
|
||||||
|
InetAddress ipv6 = getWifiClientIpv6Address();
|
||||||
|
if (ipv6 != null) return new Pair<>(ipv6, false);
|
||||||
|
}
|
||||||
|
return wifi;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -27,6 +27,7 @@ import static android.content.Context.MODE_PRIVATE;
|
|||||||
import static android.content.Context.POWER_SERVICE;
|
import static android.content.Context.POWER_SERVICE;
|
||||||
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
|
import static android.os.PowerManager.PARTIAL_WAKE_LOCK;
|
||||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||||
|
import static org.briarproject.bramble.util.AndroidUtils.getWakeLockTag;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
@@ -53,7 +54,7 @@ class AndroidTorPlugin extends TorPlugin {
|
|||||||
appContext.getSystemService(POWER_SERVICE);
|
appContext.getSystemService(POWER_SERVICE);
|
||||||
if (pm == null) throw new AssertionError();
|
if (pm == null) throw new AssertionError();
|
||||||
wakeLock = new RenewableWakeLock(pm, scheduler, PARTIAL_WAKE_LOCK,
|
wakeLock = new RenewableWakeLock(pm, scheduler, PARTIAL_WAKE_LOCK,
|
||||||
getWakeLockTag(), 1, MINUTES);
|
getWakeLockTag(appContext), 1, MINUTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -74,7 +75,6 @@ class AndroidTorPlugin extends TorPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void enableNetwork(boolean enable) throws IOException {
|
protected void enableNetwork(boolean enable) throws IOException {
|
||||||
if (!running) return;
|
|
||||||
if (enable) wakeLock.acquire();
|
if (enable) wakeLock.acquire();
|
||||||
super.enableNetwork(enable);
|
super.enableNetwork(enable);
|
||||||
if (!enable) wakeLock.release();
|
if (!enable) wakeLock.release();
|
||||||
@@ -85,17 +85,4 @@ class AndroidTorPlugin extends TorPlugin {
|
|||||||
super.stop();
|
super.stop();
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getWakeLockTag() {
|
|
||||||
PackageManager pm = appContext.getPackageManager();
|
|
||||||
for (PackageInfo info : pm.getInstalledPackages(0)) {
|
|
||||||
String name = info.packageName.toLowerCase();
|
|
||||||
if (name.startsWith("com.huawei.powergenie")) {
|
|
||||||
return "LocationManagerService";
|
|
||||||
} else if (name.startsWith("com.evenwell.powermonitor")) {
|
|
||||||
return "AudioIn";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return getClass().getSimpleName();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,12 +61,12 @@ class AndroidLocationUtils implements LocationUtils {
|
|||||||
private String getCountryFromPhoneNetwork() {
|
private String getCountryFromPhoneNetwork() {
|
||||||
Object o = appContext.getSystemService(TELEPHONY_SERVICE);
|
Object o = appContext.getSystemService(TELEPHONY_SERVICE);
|
||||||
TelephonyManager tm = (TelephonyManager) o;
|
TelephonyManager tm = (TelephonyManager) o;
|
||||||
return tm.getNetworkCountryIso();
|
return tm == null ? "" : tm.getNetworkCountryIso();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getCountryFromSimCard() {
|
private String getCountryFromSimCard() {
|
||||||
Object o = appContext.getSystemService(TELEPHONY_SERVICE);
|
Object o = appContext.getSystemService(TELEPHONY_SERVICE);
|
||||||
TelephonyManager tm = (TelephonyManager) o;
|
TelephonyManager tm = (TelephonyManager) o;
|
||||||
return tm.getSimCountryIso();
|
return tm == null ? "" : tm.getSimCountryIso();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package org.briarproject.bramble.util;
|
|||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
@@ -117,4 +119,17 @@ public class AndroidUtils {
|
|||||||
if (SDK_INT < 24) return new String[] {"image/jpeg", "image/png"};
|
if (SDK_INT < 24) return new String[] {"image/jpeg", "image/png"};
|
||||||
else return new String[] {"image/jpeg", "image/png", "image/gif"};
|
else return new String[] {"image/jpeg", "image/png", "image/gif"};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getWakeLockTag(Context ctx) {
|
||||||
|
PackageManager pm = ctx.getPackageManager();
|
||||||
|
for (PackageInfo info : pm.getInstalledPackages(0)) {
|
||||||
|
String name = info.packageName.toLowerCase();
|
||||||
|
if (name.startsWith("com.huawei.powergenie")) {
|
||||||
|
return "LocationManagerService";
|
||||||
|
} else if (name.startsWith("com.evenwell.powermonitor")) {
|
||||||
|
return "AudioIn";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ctx.getPackageName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import android.content.pm.ApplicationInfo;
|
|||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
import org.briarproject.bramble.test.BrambleMockTestCase;
|
import org.briarproject.bramble.test.BrambleMockTestCase;
|
||||||
import org.jmock.Expectations;
|
import org.jmock.Expectations;
|
||||||
import org.jmock.lib.legacy.ClassImposteriser;
|
import org.jmock.lib.legacy.ClassImposteriser;
|
||||||
@@ -15,7 +16,9 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import static android.content.Context.MODE_PRIVATE;
|
||||||
import static junit.framework.Assert.assertFalse;
|
import static junit.framework.Assert.assertFalse;
|
||||||
import static junit.framework.Assert.assertTrue;
|
import static junit.framework.Assert.assertTrue;
|
||||||
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
import static org.briarproject.bramble.test.TestUtils.deleteTestDirectory;
|
||||||
@@ -27,6 +30,8 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
context.mock(SharedPreferences.class, "prefs");
|
context.mock(SharedPreferences.class, "prefs");
|
||||||
private final SharedPreferences defaultPrefs =
|
private final SharedPreferences defaultPrefs =
|
||||||
context.mock(SharedPreferences.class, "defaultPrefs");
|
context.mock(SharedPreferences.class, "defaultPrefs");
|
||||||
|
private final PersistentLogManager logManager =
|
||||||
|
context.mock(PersistentLogManager.class);
|
||||||
private final DatabaseConfig databaseConfig =
|
private final DatabaseConfig databaseConfig =
|
||||||
context.mock(DatabaseConfig.class);
|
context.mock(DatabaseConfig.class);
|
||||||
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
private final CryptoComponent crypto = context.mock(CryptoComponent.class);
|
||||||
@@ -40,6 +45,7 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
private final File testDir = getTestDirectory();
|
private final File testDir = getTestDirectory();
|
||||||
private final File keyDir = new File(testDir, "key");
|
private final File keyDir = new File(testDir, "key");
|
||||||
private final File dbDir = new File(testDir, "db");
|
private final File dbDir = new File(testDir, "db");
|
||||||
|
private final File logDir = new File(testDir, "log");
|
||||||
|
|
||||||
private AndroidAccountManager accountManager;
|
private AndroidAccountManager accountManager;
|
||||||
|
|
||||||
@@ -61,7 +67,7 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
will(returnValue(app));
|
will(returnValue(app));
|
||||||
}});
|
}});
|
||||||
accountManager = new AndroidAccountManager(databaseConfig, crypto,
|
accountManager = new AndroidAccountManager(databaseConfig, crypto,
|
||||||
identityManager, prefs, app) {
|
identityManager, prefs, logManager, app) {
|
||||||
@Override
|
@Override
|
||||||
SharedPreferences getDefaultSharedPreferences() {
|
SharedPreferences getDefaultSharedPreferences() {
|
||||||
return defaultPrefs;
|
return defaultPrefs;
|
||||||
@@ -109,10 +115,15 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
will(returnValue(cacheDir));
|
will(returnValue(cacheDir));
|
||||||
oneOf(app).getExternalCacheDir();
|
oneOf(app).getExternalCacheDir();
|
||||||
will(returnValue(externalCacheDir));
|
will(returnValue(externalCacheDir));
|
||||||
|
oneOf(app).getDir("log", MODE_PRIVATE);
|
||||||
|
will(returnValue(logDir));
|
||||||
|
oneOf(logManager).addLogHandler(with(logDir),
|
||||||
|
with(any(Logger.class)));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
assertTrue(dbDir.mkdirs());
|
assertTrue(dbDir.mkdirs());
|
||||||
assertTrue(keyDir.mkdirs());
|
assertTrue(keyDir.mkdirs());
|
||||||
|
assertTrue(logDir.mkdirs());
|
||||||
assertTrue(codeCacheDir.mkdirs());
|
assertTrue(codeCacheDir.mkdirs());
|
||||||
assertTrue(codeCacheFile.createNewFile());
|
assertTrue(codeCacheFile.createNewFile());
|
||||||
assertTrue(libDir.mkdirs());
|
assertTrue(libDir.mkdirs());
|
||||||
@@ -130,6 +141,7 @@ public class AndroidAccountManagerTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
assertFalse(dbDir.exists());
|
assertFalse(dbDir.exists());
|
||||||
assertFalse(keyDir.exists());
|
assertFalse(keyDir.exists());
|
||||||
|
assertFalse(logDir.exists());
|
||||||
assertTrue(codeCacheDir.exists());
|
assertTrue(codeCacheDir.exists());
|
||||||
assertTrue(codeCacheFile.exists());
|
assertTrue(codeCacheFile.exists());
|
||||||
assertTrue(libDir.exists());
|
assertTrue(libDir.exists());
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
package org.briarproject.bramble.api;
|
package org.briarproject.bramble.api;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
import java.util.Hashtable;
|
import java.util.Hashtable;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
public abstract class StringMap extends Hashtable<String, String> {
|
public abstract class StringMap extends Hashtable<String, String> {
|
||||||
|
|
||||||
protected StringMap(Map<String, String> m) {
|
protected StringMap(Map<String, String> m) {
|
||||||
@@ -52,4 +60,19 @@ public abstract class StringMap extends Hashtable<String, String> {
|
|||||||
public void putLong(String key, long value) {
|
public void putLong(String key, long value) {
|
||||||
put(key, String.valueOf(value));
|
put(key, String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public byte[] getBytes(String key) {
|
||||||
|
String s = get(key);
|
||||||
|
if (s == null) return null;
|
||||||
|
try {
|
||||||
|
return fromHexString(s);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putBytes(String key, byte[] value) {
|
||||||
|
put(key, toHexString(value));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,4 +19,10 @@ public interface StreamDecrypterFactory {
|
|||||||
*/
|
*/
|
||||||
StreamDecrypter createContactExchangeStreamDecrypter(InputStream in,
|
StreamDecrypter createContactExchangeStreamDecrypter(InputStream in,
|
||||||
SecretKey headerKey);
|
SecretKey headerKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@link StreamDecrypter} for decrypting a log stream.
|
||||||
|
*/
|
||||||
|
StreamDecrypter createLogStreamDecrypter(InputStream in,
|
||||||
|
SecretKey headerKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ public interface StreamEncrypterFactory {
|
|||||||
* Creates a {@link StreamEncrypter} for encrypting a contact exchange
|
* Creates a {@link StreamEncrypter} for encrypting a contact exchange
|
||||||
* stream.
|
* stream.
|
||||||
*/
|
*/
|
||||||
StreamEncrypter createContactExchangeStreamDecrypter(OutputStream out,
|
StreamEncrypter createContactExchangeStreamEncrypter(OutputStream out,
|
||||||
|
SecretKey headerKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@link StreamEncrypter} for encrypting a log stream.
|
||||||
|
*/
|
||||||
|
StreamEncrypter createLogStreamEncrypter(OutputStream out,
|
||||||
SecretKey headerKey);
|
SecretKey headerKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ public interface EventBus {
|
|||||||
/**
|
/**
|
||||||
* Asynchronously notifies all listeners of an event. Listeners are
|
* Asynchronously notifies all listeners of an event. Listeners are
|
||||||
* notified on the {@link EventExecutor}.
|
* notified on the {@link EventExecutor}.
|
||||||
|
* <p>
|
||||||
|
* This method can safely be called while holding a lock.
|
||||||
*/
|
*/
|
||||||
void broadcast(Event e);
|
void broadcast(Event e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
package org.briarproject.bramble.api.logging;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.logging.Handler;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public interface PersistentLogManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The namespace of the (@link Settings) where the log key is stored.
|
||||||
|
*/
|
||||||
|
String LOG_SETTINGS_NAMESPACE = "log";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link Settings} key under which the log key is stored.
|
||||||
|
*/
|
||||||
|
String LOG_KEY_KEY = "logKey";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and returns a persistent log handler that stores its logs in
|
||||||
|
* the given directory.
|
||||||
|
*/
|
||||||
|
Handler createLogHandler(File dir) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a persistent log handler that stores its logs in the given
|
||||||
|
* directory and adds the handler to the given logger, replacing any
|
||||||
|
* existing persistent log handler.
|
||||||
|
*/
|
||||||
|
void addLogHandler(File dir, Logger logger) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a {@link Scanner} for reading the persistent log entries stored
|
||||||
|
* in the given directory.
|
||||||
|
*
|
||||||
|
* @param old True if the previous session's log should be loaded, or false
|
||||||
|
* if the current session's log should be loaded
|
||||||
|
*/
|
||||||
|
Scanner getPersistedLog(File dir, boolean old) throws IOException;
|
||||||
|
}
|
||||||
@@ -6,8 +6,10 @@ public interface BluetoothConstants {
|
|||||||
|
|
||||||
int UUID_BYTES = 16;
|
int UUID_BYTES = 16;
|
||||||
|
|
||||||
|
// Transport properties
|
||||||
String PROP_ADDRESS = "address";
|
String PROP_ADDRESS = "address";
|
||||||
String PROP_UUID = "uuid";
|
String PROP_UUID = "uuid";
|
||||||
|
|
||||||
String PREF_BT_ENABLE = "enable";
|
// Default value for PREF_PLUGIN_ENABLE
|
||||||
|
boolean DEFAULT_PREF_PLUGIN_ENABLE = false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,12 @@ public interface LanTcpConstants {
|
|||||||
// Transport properties (shared with contacts)
|
// Transport properties (shared with contacts)
|
||||||
String PROP_IP_PORTS = "ipPorts";
|
String PROP_IP_PORTS = "ipPorts";
|
||||||
String PROP_PORT = "port";
|
String PROP_PORT = "port";
|
||||||
|
String PROP_IPV6 = "ipv6";
|
||||||
|
|
||||||
// A local setting
|
// Local settings (not shared with contacts)
|
||||||
String PREF_LAN_IP_PORTS = "ipPorts";
|
String PREF_LAN_IP_PORTS = "ipPorts";
|
||||||
|
String PREF_IPV6 = "ipv6";
|
||||||
|
|
||||||
|
// Default value for PREF_PLUGIN_ENABLE
|
||||||
|
boolean DEFAULT_PREF_PLUGIN_ENABLE = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,55 @@ package org.briarproject.bramble.api.plugin;
|
|||||||
import org.briarproject.bramble.api.Pair;
|
import org.briarproject.bramble.api.Pair;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
|
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public interface Plugin {
|
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.
|
* Returns the plugin's transport identifier.
|
||||||
*/
|
*/
|
||||||
@@ -35,9 +78,18 @@ public interface Plugin {
|
|||||||
void stop() throws PluginException;
|
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
|
* Returns true if the plugin should be polled periodically to attempt to
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package org.briarproject.bramble.api.plugin;
|
package org.briarproject.bramble.api.plugin;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
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.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.settings.Settings;
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
|
|
||||||
@@ -32,12 +36,17 @@ public interface PluginCallback extends ConnectionHandler {
|
|||||||
void mergeLocalProperties(TransportProperties p);
|
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();
|
void pluginStateChanged(State state);
|
||||||
|
|
||||||
/**
|
|
||||||
* Signals that the transport is disabled.
|
|
||||||
*/
|
|
||||||
void transportDisabled();
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,4 +41,17 @@ public interface PluginManager {
|
|||||||
* Returns any duplex plugins that support rendezvous.
|
* Returns any duplex plugins that support rendezvous.
|
||||||
*/
|
*/
|
||||||
Collection<DuplexPlugin> getRendezvousPlugins();
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ public interface TorConstants {
|
|||||||
|
|
||||||
TransportId ID = new TransportId("org.briarproject.bramble.tor");
|
TransportId ID = new TransportId("org.briarproject.bramble.tor");
|
||||||
|
|
||||||
|
// Transport properties
|
||||||
String PROP_ONION_V2 = "onion";
|
String PROP_ONION_V2 = "onion";
|
||||||
String PROP_ONION_V3 = "onion3";
|
String PROP_ONION_V3 = "onion3";
|
||||||
|
|
||||||
@@ -13,14 +14,37 @@ public interface TorConstants {
|
|||||||
int CONNECT_TO_PROXY_TIMEOUT = 5000; // Milliseconds
|
int CONNECT_TO_PROXY_TIMEOUT = 5000; // Milliseconds
|
||||||
int EXTRA_SOCKET_TIMEOUT = 30000; // Milliseconds
|
int EXTRA_SOCKET_TIMEOUT = 30000; // Milliseconds
|
||||||
|
|
||||||
|
// Local settings (not shared with contacts)
|
||||||
String PREF_TOR_NETWORK = "network2";
|
String PREF_TOR_NETWORK = "network2";
|
||||||
String PREF_TOR_PORT = "port";
|
String PREF_TOR_PORT = "port";
|
||||||
String PREF_TOR_MOBILE = "useMobileData";
|
String PREF_TOR_MOBILE = "useMobileData";
|
||||||
String PREF_TOR_ONLY_WHEN_CHARGING = "onlyWhenCharging";
|
String PREF_TOR_ONLY_WHEN_CHARGING = "onlyWhenCharging";
|
||||||
|
|
||||||
|
// Values for PREF_TOR_NETWORK
|
||||||
int PREF_TOR_NETWORK_AUTOMATIC = 0;
|
int PREF_TOR_NETWORK_AUTOMATIC = 0;
|
||||||
int PREF_TOR_NETWORK_WITHOUT_BRIDGES = 1;
|
int PREF_TOR_NETWORK_WITHOUT_BRIDGES = 1;
|
||||||
int PREF_TOR_NETWORK_WITH_BRIDGES = 2;
|
int PREF_TOR_NETWORK_WITH_BRIDGES = 2;
|
||||||
|
// TODO: Remove when settings migration code is removed
|
||||||
int PREF_TOR_NETWORK_NEVER = 3;
|
int PREF_TOR_NETWORK_NEVER = 3;
|
||||||
|
|
||||||
|
// Default values for local settings
|
||||||
|
boolean DEFAULT_PREF_PLUGIN_ENABLE = true;
|
||||||
|
int DEFAULT_PREF_TOR_NETWORK = PREF_TOR_NETWORK_AUTOMATIC;
|
||||||
|
boolean DEFAULT_PREF_TOR_MOBILE = true;
|
||||||
|
boolean DEFAULT_PREF_TOR_ONLY_WHEN_CHARGING = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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,4 +4,7 @@ public interface WanTcpConstants {
|
|||||||
|
|
||||||
TransportId ID = new TransportId("org.briarproject.bramble.wan");
|
TransportId ID = new TransportId("org.briarproject.bramble.wan");
|
||||||
|
|
||||||
|
// Default value for PREF_PLUGIN_ENABLE
|
||||||
|
boolean DEFAULT_PREF_PLUGIN_ENABLE = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,20 +2,22 @@ package org.briarproject.bramble.api.plugin.event;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.event.Event;
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An event that is broadcast when a transport is disabled.
|
* An event that is broadcast when a plugin enters the {@link State#ACTIVE}
|
||||||
|
* state.
|
||||||
*/
|
*/
|
||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class TransportDisabledEvent extends Event {
|
public class TransportActiveEvent extends Event {
|
||||||
|
|
||||||
private final TransportId transportId;
|
private final TransportId transportId;
|
||||||
|
|
||||||
public TransportDisabledEvent(TransportId transportId) {
|
public TransportActiveEvent(TransportId transportId) {
|
||||||
this.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.event.Event;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.plugin.Plugin.State;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An event that is broadcast when a transport is enabled.
|
* An event that is broadcast when a plugin leaves the {@link State#ACTIVE}
|
||||||
|
* state.
|
||||||
*/
|
*/
|
||||||
@Immutable
|
@Immutable
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class TransportEnabledEvent extends Event {
|
public class TransportInactiveEvent extends Event {
|
||||||
|
|
||||||
private final TransportId transportId;
|
private final TransportId transportId;
|
||||||
|
|
||||||
public TransportEnabledEvent(TransportId transportId) {
|
public TransportInactiveEvent(TransportId transportId) {
|
||||||
this.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -16,8 +16,13 @@ public interface StreamReaderFactory {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an {@link InputStream InputStream} for reading from a contact
|
* Creates an {@link InputStream InputStream} for reading from a contact
|
||||||
* exchangestream.
|
* exchange stream.
|
||||||
*/
|
*/
|
||||||
InputStream createContactExchangeStreamReader(InputStream in,
|
InputStream createContactExchangeStreamReader(InputStream in,
|
||||||
SecretKey headerKey);
|
SecretKey headerKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an {@link InputStream} for reading from a log stream.
|
||||||
|
*/
|
||||||
|
InputStream createLogStreamReader(InputStream in, SecretKey headerKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,15 +9,18 @@ import java.io.OutputStream;
|
|||||||
public interface StreamWriterFactory {
|
public interface StreamWriterFactory {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an {@link OutputStream OutputStream} for writing to a
|
* Creates a {@link StreamWriter} for writing to a transport stream.
|
||||||
* transport stream
|
|
||||||
*/
|
*/
|
||||||
StreamWriter createStreamWriter(OutputStream out, StreamContext ctx);
|
StreamWriter createStreamWriter(OutputStream out, StreamContext ctx);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an {@link OutputStream OutputStream} for writing to a contact
|
* Creates a {@link StreamWriter} for writing to a contact exchange stream.
|
||||||
* exchange stream.
|
|
||||||
*/
|
*/
|
||||||
StreamWriter createContactExchangeStreamWriter(OutputStream out,
|
StreamWriter createContactExchangeStreamWriter(OutputStream out,
|
||||||
SecretKey headerKey);
|
SecretKey headerKey);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@link StreamWriter} for writing to a log stream.
|
||||||
|
*/
|
||||||
|
StreamWriter createLogStreamWriter(OutputStream out, SecretKey headerKey);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,17 @@ package org.briarproject.bramble.util;
|
|||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
import java.net.Inet6Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.isValidMac;
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public class PrivacyUtils {
|
public class PrivacyUtils {
|
||||||
|
|
||||||
@@ -19,7 +23,7 @@ public class PrivacyUtils {
|
|||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static String scrubMacAddress(@Nullable String address) {
|
public static String scrubMacAddress(@Nullable String address) {
|
||||||
if (address == null || address.length() == 0) return null;
|
if (isNullOrEmpty(address) || !isValidMac(address)) return address;
|
||||||
// this is a fake address we need to know about
|
// this is a fake address we need to know about
|
||||||
if (address.equals("02:00:00:00:00:00")) return address;
|
if (address.equals("02:00:00:00:00:00")) return address;
|
||||||
// keep first and last octet of MAC address
|
// keep first and last octet of MAC address
|
||||||
@@ -27,39 +31,37 @@ public class PrivacyUtils {
|
|||||||
+ address.substring(14, 17);
|
+ address.substring(14, 17);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static String scrubInetAddress(InetAddress address) {
|
public static String scrubInetAddress(InetAddress address) {
|
||||||
// don't scrub link and site local addresses
|
if (address instanceof Inet4Address) {
|
||||||
if (address.isLinkLocalAddress() || address.isSiteLocalAddress())
|
// Don't scrub local IPv4 addresses
|
||||||
return address.toString();
|
if (address.isLoopbackAddress() || address.isLinkLocalAddress() ||
|
||||||
// completely scrub IPv6 addresses
|
address.isSiteLocalAddress()) {
|
||||||
if (address instanceof Inet6Address) return "[scrubbed]";
|
return address.getHostAddress();
|
||||||
// keep first and last octet of IPv4 addresses
|
}
|
||||||
return scrubInetAddress(address.toString());
|
// Keep first and last octet of non-local IPv4 addresses
|
||||||
|
return scrubIpv4Address(address.getAddress());
|
||||||
|
} else {
|
||||||
|
// Keep first and last octet of IPv6 addresses
|
||||||
|
return scrubIpv6Address(address.getAddress());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
private static String scrubIpv4Address(byte[] ipv4) {
|
||||||
public static String scrubInetAddress(@Nullable String address) {
|
return (ipv4[0] & 0xFF) + ".[scrubbed]." + (ipv4[3] & 0xFF);
|
||||||
if (address == null) return null;
|
}
|
||||||
|
|
||||||
int firstDot = address.indexOf(".");
|
private static String scrubIpv6Address(byte[] ipv6) {
|
||||||
if (firstDot == -1) return "[scrubbed]";
|
String hex = toHexString(ipv6).toLowerCase();
|
||||||
String prefix = address.substring(0, firstDot + 1);
|
return hex.substring(0, 2) + "[scrubbed]" + hex.substring(30);
|
||||||
int lastDot = address.lastIndexOf(".");
|
|
||||||
String suffix = address.substring(lastDot, address.length());
|
|
||||||
return prefix + "[scrubbed]" + suffix;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static String scrubSocketAddress(InetSocketAddress address) {
|
public static String scrubSocketAddress(InetSocketAddress address) {
|
||||||
InetAddress inetAddress = address.getAddress();
|
return scrubInetAddress(address.getAddress());
|
||||||
return scrubInetAddress(inetAddress);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static String scrubSocketAddress(SocketAddress address) {
|
public static String scrubSocketAddress(SocketAddress address) {
|
||||||
if (address instanceof InetSocketAddress)
|
if (address instanceof InetSocketAddress)
|
||||||
return scrubSocketAddress((InetSocketAddress) address);
|
return scrubSocketAddress((InetSocketAddress) address);
|
||||||
return scrubInetAddress(address.toString());
|
return "[scrubbed]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import org.briarproject.bramble.identity.IdentityModule;
|
|||||||
import org.briarproject.bramble.io.IoModule;
|
import org.briarproject.bramble.io.IoModule;
|
||||||
import org.briarproject.bramble.keyagreement.KeyAgreementModule;
|
import org.briarproject.bramble.keyagreement.KeyAgreementModule;
|
||||||
import org.briarproject.bramble.lifecycle.LifecycleModule;
|
import org.briarproject.bramble.lifecycle.LifecycleModule;
|
||||||
|
import org.briarproject.bramble.logging.LoggingModule;
|
||||||
import org.briarproject.bramble.plugin.PluginModule;
|
import org.briarproject.bramble.plugin.PluginModule;
|
||||||
import org.briarproject.bramble.properties.PropertiesModule;
|
import org.briarproject.bramble.properties.PropertiesModule;
|
||||||
import org.briarproject.bramble.record.RecordModule;
|
import org.briarproject.bramble.record.RecordModule;
|
||||||
@@ -41,6 +42,7 @@ import dagger.Module;
|
|||||||
IoModule.class,
|
IoModule.class,
|
||||||
KeyAgreementModule.class,
|
KeyAgreementModule.class,
|
||||||
LifecycleModule.class,
|
LifecycleModule.class,
|
||||||
|
LoggingModule.class,
|
||||||
PluginModule.class,
|
PluginModule.class,
|
||||||
PropertiesModule.class,
|
PropertiesModule.class,
|
||||||
RecordModule.class,
|
RecordModule.class,
|
||||||
|
|||||||
@@ -63,18 +63,12 @@ class ConnectionRegistryImpl implements ConnectionRegistry {
|
|||||||
@Override
|
@Override
|
||||||
public void registerIncomingConnection(ContactId c, TransportId t,
|
public void registerIncomingConnection(ContactId c, TransportId t,
|
||||||
InterruptibleConnection conn) {
|
InterruptibleConnection conn) {
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Incoming connection registered: " + t);
|
|
||||||
}
|
|
||||||
registerConnection(c, t, conn, true);
|
registerConnection(c, t, conn, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerOutgoingConnection(ContactId c, TransportId t,
|
public void registerOutgoingConnection(ContactId c, TransportId t,
|
||||||
InterruptibleConnection conn, Priority priority) {
|
InterruptibleConnection conn, Priority priority) {
|
||||||
if (LOG.isLoggable(INFO)) {
|
|
||||||
LOG.info("Outgoing connection registered: " + t);
|
|
||||||
}
|
|
||||||
registerConnection(c, t, conn, false);
|
registerConnection(c, t, conn, false);
|
||||||
setPriority(c, t, conn, priority);
|
setPriority(c, t, conn, priority);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,4 +36,10 @@ class StreamDecrypterFactoryImpl implements StreamDecrypterFactory {
|
|||||||
SecretKey headerKey) {
|
SecretKey headerKey) {
|
||||||
return new StreamDecrypterImpl(in, cipherProvider.get(), 0, headerKey);
|
return new StreamDecrypterImpl(in, cipherProvider.get(), 0, headerKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StreamDecrypter createLogStreamDecrypter(InputStream in,
|
||||||
|
SecretKey headerKey) {
|
||||||
|
return createContactExchangeStreamDecrypter(in, headerKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StreamEncrypter createContactExchangeStreamDecrypter(
|
public StreamEncrypter createContactExchangeStreamEncrypter(
|
||||||
OutputStream out, SecretKey headerKey) {
|
OutputStream out, SecretKey headerKey) {
|
||||||
AuthenticatedCipher cipher = cipherProvider.get();
|
AuthenticatedCipher cipher = cipherProvider.get();
|
||||||
byte[] streamHeaderNonce = new byte[STREAM_HEADER_NONCE_LENGTH];
|
byte[] streamHeaderNonce = new byte[STREAM_HEADER_NONCE_LENGTH];
|
||||||
@@ -60,4 +60,10 @@ class StreamEncrypterFactoryImpl implements StreamEncrypterFactory {
|
|||||||
return new StreamEncrypterImpl(out, cipher, 0, null, streamHeaderNonce,
|
return new StreamEncrypterImpl(out, cipher, 0, null, streamHeaderNonce,
|
||||||
headerKey, frameKey);
|
headerKey, frameKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StreamEncrypter createLogStreamEncrypter(OutputStream out,
|
||||||
|
SecretKey headerKey) {
|
||||||
|
return createContactExchangeStreamEncrypter(out, headerKey);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package org.briarproject.briar.android.logging;
|
package org.briarproject.bramble.logging;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
|
||||||
@@ -47,6 +47,7 @@ public class BriefLogFormatter extends Formatter {
|
|||||||
sb.append('\n');
|
sb.append('\n');
|
||||||
appendThrowable(sb, t);
|
appendThrowable(sb, t);
|
||||||
}
|
}
|
||||||
|
sb.append('\n');
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
package org.briarproject.bramble.logging;
|
||||||
|
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.logging.Formatter;
|
||||||
|
import java.util.logging.LogRecord;
|
||||||
|
import java.util.logging.StreamHandler;
|
||||||
|
|
||||||
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
|
|
||||||
|
class FlushingStreamHandler extends StreamHandler {
|
||||||
|
|
||||||
|
private static final int FLUSH_DELAY_MS = 5_000;
|
||||||
|
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
|
private final Executor ioExecutor;
|
||||||
|
private final AtomicBoolean flushScheduled = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
FlushingStreamHandler(ScheduledExecutorService scheduler,
|
||||||
|
Executor ioExecutor, OutputStream out, Formatter formatter) {
|
||||||
|
super(out, formatter);
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
this.ioExecutor = ioExecutor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void publish(LogRecord record) {
|
||||||
|
super.publish(record);
|
||||||
|
if (!flushScheduled.getAndSet(true)) {
|
||||||
|
scheduler.schedule(this::scheduledFlush,
|
||||||
|
FLUSH_DELAY_MS, MILLISECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scheduledFlush() {
|
||||||
|
ioExecutor.execute(() -> {
|
||||||
|
flushScheduled.set(false);
|
||||||
|
flush();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package org.briarproject.bramble.logging;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
|
|
||||||
|
import java.util.logging.Formatter;
|
||||||
|
|
||||||
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
|
import dagger.Module;
|
||||||
|
import dagger.Provides;
|
||||||
|
|
||||||
|
@Module
|
||||||
|
public class LoggingModule {
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
Formatter provideFormatter() {
|
||||||
|
return new BriefLogFormatter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
@Singleton
|
||||||
|
PersistentLogManager providePersistentLogManager(
|
||||||
|
LifecycleManager lifecycleManager,
|
||||||
|
PersistentLogManagerImpl persistentLogManager) {
|
||||||
|
lifecycleManager.registerOpenDatabaseHook(persistentLogManager);
|
||||||
|
return persistentLogManager;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
package org.briarproject.bramble.logging;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
|
import org.briarproject.bramble.api.crypto.SecretKey;
|
||||||
|
import org.briarproject.bramble.api.db.DatabaseComponent;
|
||||||
|
import org.briarproject.bramble.api.db.DbException;
|
||||||
|
import org.briarproject.bramble.api.db.Transaction;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager.OpenDatabaseHook;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.ShutdownManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
|
import org.briarproject.bramble.api.system.Scheduler;
|
||||||
|
import org.briarproject.bramble.api.transport.StreamReaderFactory;
|
||||||
|
import org.briarproject.bramble.api.transport.StreamWriter;
|
||||||
|
import org.briarproject.bramble.api.transport.StreamWriterFactory;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.logging.Formatter;
|
||||||
|
import java.util.logging.Handler;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
import java.util.logging.StreamHandler;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
|
|
||||||
|
@ThreadSafe
|
||||||
|
@NotNullByDefault
|
||||||
|
class PersistentLogManagerImpl implements PersistentLogManager,
|
||||||
|
OpenDatabaseHook {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(PersistentLogManagerImpl.class.getName());
|
||||||
|
|
||||||
|
private static final String LOG_FILE = "briar.log";
|
||||||
|
private static final String OLD_LOG_FILE = "briar.log.old";
|
||||||
|
|
||||||
|
private final ScheduledExecutorService scheduler;
|
||||||
|
private final Executor ioExecutor;
|
||||||
|
private final ShutdownManager shutdownManager;
|
||||||
|
private final DatabaseComponent db;
|
||||||
|
private final StreamReaderFactory streamReaderFactory;
|
||||||
|
private final StreamWriterFactory streamWriterFactory;
|
||||||
|
private final Formatter formatter;
|
||||||
|
private final SecretKey logKey;
|
||||||
|
private final AtomicReference<Integer> shutdownHookHandle =
|
||||||
|
new AtomicReference<>();
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private volatile SecretKey oldLogKey = null;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PersistentLogManagerImpl(
|
||||||
|
@Scheduler ScheduledExecutorService scheduler,
|
||||||
|
@IoExecutor Executor ioExecutor,
|
||||||
|
ShutdownManager shutdownManager,
|
||||||
|
DatabaseComponent db,
|
||||||
|
StreamReaderFactory streamReaderFactory,
|
||||||
|
StreamWriterFactory streamWriterFactory,
|
||||||
|
Formatter formatter,
|
||||||
|
CryptoComponent crypto) {
|
||||||
|
this.scheduler = scheduler;
|
||||||
|
this.ioExecutor = ioExecutor;
|
||||||
|
this.shutdownManager = shutdownManager;
|
||||||
|
this.db = db;
|
||||||
|
this.streamReaderFactory = streamReaderFactory;
|
||||||
|
this.streamWriterFactory = streamWriterFactory;
|
||||||
|
this.formatter = formatter;
|
||||||
|
logKey = crypto.generateSecretKey();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDatabaseOpened(Transaction txn) throws DbException {
|
||||||
|
Settings s = db.getSettings(txn, LOG_SETTINGS_NAMESPACE);
|
||||||
|
// Load the old log key, if any
|
||||||
|
byte[] oldKeyBytes = s.getBytes(LOG_KEY_KEY);
|
||||||
|
if (oldKeyBytes != null && oldKeyBytes.length == SecretKey.LENGTH) {
|
||||||
|
LOG.info("Loaded old log key");
|
||||||
|
oldLogKey = new SecretKey(oldKeyBytes);
|
||||||
|
}
|
||||||
|
// Store the current log key
|
||||||
|
s.putBytes(LOG_KEY_KEY, logKey.getBytes());
|
||||||
|
db.mergeSettings(txn, s, LOG_SETTINGS_NAMESPACE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Handler createLogHandler(File dir) throws IOException {
|
||||||
|
File logFile = new File(dir, LOG_FILE);
|
||||||
|
File oldLogFile = new File(dir, OLD_LOG_FILE);
|
||||||
|
if (oldLogFile.exists() && !oldLogFile.delete())
|
||||||
|
LOG.warning("Failed to delete old log file");
|
||||||
|
if (logFile.exists() && !logFile.renameTo(oldLogFile))
|
||||||
|
LOG.warning("Failed to rename log file");
|
||||||
|
try {
|
||||||
|
OutputStream out = new FileOutputStream(logFile);
|
||||||
|
StreamWriter writer =
|
||||||
|
streamWriterFactory.createLogStreamWriter(out, logKey);
|
||||||
|
StreamHandler handler = new FlushingStreamHandler(scheduler,
|
||||||
|
ioExecutor, writer.getOutputStream(), formatter);
|
||||||
|
// Flush the log and terminate the stream at shutdown
|
||||||
|
Runnable shutdownHook = () -> {
|
||||||
|
LOG.info("Shutting down");
|
||||||
|
handler.flush();
|
||||||
|
try {
|
||||||
|
writer.sendEndOfStream();
|
||||||
|
} catch (IOException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
int handle = shutdownManager.addShutdownHook(shutdownHook);
|
||||||
|
// If a previous handler registered a shutdown hook, remove it
|
||||||
|
Integer oldHandle = shutdownHookHandle.getAndSet(handle);
|
||||||
|
if (oldHandle != null) {
|
||||||
|
shutdownManager.removeShutdownHook(oldHandle);
|
||||||
|
}
|
||||||
|
return handler;
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
throw new IOException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addLogHandler(File dir, Logger logger) throws IOException {
|
||||||
|
for (Handler h : logger.getHandlers()) {
|
||||||
|
if (h instanceof FlushingStreamHandler) logger.removeHandler(h);
|
||||||
|
}
|
||||||
|
logger.addHandler(createLogHandler(dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Scanner getPersistedLog(File dir, boolean old)
|
||||||
|
throws IOException {
|
||||||
|
if (old) {
|
||||||
|
SecretKey oldLogKey = this.oldLogKey;
|
||||||
|
if (oldLogKey == null) {
|
||||||
|
LOG.info("Old log key has not been loaded");
|
||||||
|
return emptyScanner();
|
||||||
|
}
|
||||||
|
return getPersistedLog(new File(dir, OLD_LOG_FILE), oldLogKey);
|
||||||
|
} else {
|
||||||
|
return getPersistedLog(new File(dir, LOG_FILE), logKey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Scanner getPersistedLog(File logFile, SecretKey key)
|
||||||
|
throws IOException {
|
||||||
|
if (logFile.exists()) {
|
||||||
|
LOG.info("Reading log file");
|
||||||
|
InputStream in = new FileInputStream(logFile);
|
||||||
|
return new Scanner(streamReaderFactory.createLogStreamReader(in,
|
||||||
|
key));
|
||||||
|
} else {
|
||||||
|
LOG.info("Log file does not exist");
|
||||||
|
return emptyScanner();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Scanner emptyScanner() {
|
||||||
|
return new Scanner(new ByteArrayInputStream(new byte[0]));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,6 +8,7 @@ import org.briarproject.bramble.api.lifecycle.Service;
|
|||||||
import org.briarproject.bramble.api.lifecycle.ServiceException;
|
import org.briarproject.bramble.api.lifecycle.ServiceException;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Plugin;
|
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.PluginCallback;
|
||||||
import org.briarproject.bramble.api.plugin.PluginConfig;
|
import org.briarproject.bramble.api.plugin.PluginConfig;
|
||||||
import org.briarproject.bramble.api.plugin.PluginException;
|
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.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPluginFactory;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
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.SimplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
import org.briarproject.bramble.api.plugin.simplex.SimplexPluginFactory;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
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.CountDownLatch;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
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.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.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.logDuration;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.LogUtils.now;
|
import static org.briarproject.bramble.util.LogUtils.now;
|
||||||
@@ -177,6 +183,26 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
return supported;
|
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 class PluginStarter implements Runnable {
|
||||||
|
|
||||||
private final Plugin plugin;
|
private final Plugin plugin;
|
||||||
@@ -250,7 +276,8 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
private class Callback implements PluginCallback {
|
private class Callback implements PluginCallback {
|
||||||
|
|
||||||
private final TransportId id;
|
private final TransportId id;
|
||||||
private final AtomicBoolean enabled = new AtomicBoolean(false);
|
private final AtomicReference<State> state =
|
||||||
|
new AtomicReference<>(STARTING_STOPPING);
|
||||||
|
|
||||||
private Callback(TransportId id) {
|
private Callback(TransportId id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
@@ -278,11 +305,7 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mergeSettings(Settings s) {
|
public void mergeSettings(Settings s) {
|
||||||
try {
|
PluginManagerImpl.this.mergeSettings(s, id.getString());
|
||||||
settingsManager.mergeSettings(s, id.getString());
|
|
||||||
} catch (DbException e) {
|
|
||||||
logException(LOG, WARNING, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -295,15 +318,20 @@ class PluginManagerImpl implements PluginManager, Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void transportEnabled() {
|
public void pluginStateChanged(State newState) {
|
||||||
if (!enabled.getAndSet(true))
|
State oldState = state.getAndSet(newState);
|
||||||
eventBus.broadcast(new TransportEnabledEvent(id));
|
if (newState != oldState) {
|
||||||
}
|
if (LOG.isLoggable(INFO)) {
|
||||||
|
LOG.info(id + " changed from state " + oldState
|
||||||
@Override
|
+ " to " + newState);
|
||||||
public void transportDisabled() {
|
}
|
||||||
if (enabled.getAndSet(false))
|
eventBus.broadcast(new TransportStateEvent(id, newState));
|
||||||
eventBus.broadcast(new TransportDisabledEvent(id));
|
if (newState == ACTIVE) {
|
||||||
|
eventBus.broadcast(new TransportActiveEvent(id));
|
||||||
|
} else if (oldState == ACTIVE) {
|
||||||
|
eventBus.broadcast(new TransportInactiveEvent(id));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -20,8 +20,8 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
|||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||||
@@ -106,13 +106,13 @@ class PollerImpl implements Poller, EventListener {
|
|||||||
ConnectionOpenedEvent c = (ConnectionOpenedEvent) e;
|
ConnectionOpenedEvent c = (ConnectionOpenedEvent) e;
|
||||||
// Reschedule polling, the polling interval may have decreased
|
// Reschedule polling, the polling interval may have decreased
|
||||||
reschedule(c.getTransportId());
|
reschedule(c.getTransportId());
|
||||||
} else if (e instanceof TransportEnabledEvent) {
|
} else if (e instanceof TransportActiveEvent) {
|
||||||
TransportEnabledEvent t = (TransportEnabledEvent) e;
|
TransportActiveEvent t = (TransportActiveEvent) e;
|
||||||
// Poll the newly enabled transport
|
// Poll the newly activated transport
|
||||||
pollNow(t.getTransportId());
|
pollNow(t.getTransportId());
|
||||||
} else if (e instanceof TransportDisabledEvent) {
|
} else if (e instanceof TransportInactiveEvent) {
|
||||||
TransportDisabledEvent t = (TransportDisabledEvent) e;
|
TransportInactiveEvent t = (TransportInactiveEvent) e;
|
||||||
// Cancel polling for the disabled transport
|
// Cancel polling for the deactivated transport
|
||||||
cancel(t.getTransportId());
|
cancel(t.getTransportId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,9 @@ import org.briarproject.bramble.api.keyagreement.KeyAgreementConnection;
|
|||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementListeningEvent;
|
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementListeningEvent;
|
||||||
import org.briarproject.bramble.api.keyagreement.event.KeyAgreementStoppedListeningEvent;
|
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.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||||
@@ -37,16 +39,22 @@ import java.util.concurrent.atomic.AtomicBoolean;
|
|||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
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.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_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_BT_ENABLE;
|
|
||||||
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;
|
||||||
|
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.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
|
import static org.briarproject.bramble.util.PrivacyUtils.scrubMacAddress;
|
||||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
@@ -70,9 +78,9 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
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 volatile boolean running = false, contactConnections = false;
|
protected final PluginState state = new PluginState();
|
||||||
|
|
||||||
private volatile String contactConnectionsUuid = null;
|
private volatile String contactConnectionsUuid = null;
|
||||||
private volatile SS socket = null;
|
|
||||||
|
|
||||||
abstract void initialiseAdapter() throws IOException;
|
abstract void initialiseAdapter() throws IOException;
|
||||||
|
|
||||||
@@ -124,14 +132,18 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
LOG.info("Bluetooth enabled");
|
LOG.info("Bluetooth enabled");
|
||||||
// We may not have been able to get the local address before
|
// We may not have been able to get the local address before
|
||||||
ioExecutor.execute(this::updateProperties);
|
ioExecutor.execute(this::updateProperties);
|
||||||
if (shouldAllowContactConnections()) bind();
|
if (getState() == INACTIVE) bind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onAdapterDisabled() {
|
void onAdapterDisabled() {
|
||||||
LOG.info("Bluetooth disabled");
|
LOG.info("Bluetooth disabled");
|
||||||
tryToClose(socket);
|
|
||||||
connectionLimiter.allConnectionsClosed();
|
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
|
@Override
|
||||||
@@ -152,31 +164,25 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void start() throws PluginException {
|
public void start() throws PluginException {
|
||||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||||
|
Settings settings = callback.getSettings();
|
||||||
|
boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE,
|
||||||
|
DEFAULT_PREF_PLUGIN_ENABLE);
|
||||||
|
state.setStarted(enabledByUser);
|
||||||
try {
|
try {
|
||||||
initialiseAdapter();
|
initialiseAdapter();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new PluginException(e);
|
throw new PluginException(e);
|
||||||
}
|
}
|
||||||
updateProperties();
|
updateProperties();
|
||||||
running = true;
|
if (enabledByUser) {
|
||||||
loadSettings(callback.getSettings());
|
|
||||||
if (shouldAllowContactConnections()) {
|
|
||||||
if (isAdapterEnabled()) bind();
|
if (isAdapterEnabled()) bind();
|
||||||
else enableAdapter();
|
else enableAdapter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadSettings(Settings settings) {
|
|
||||||
contactConnections = settings.getBoolean(PREF_BT_ENABLE, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean shouldAllowContactConnections() {
|
|
||||||
return contactConnections;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void bind() {
|
private void bind() {
|
||||||
ioExecutor.execute(() -> {
|
ioExecutor.execute(() -> {
|
||||||
if (!isRunning() || !shouldAllowContactConnections()) return;
|
if (getState() != INACTIVE) return;
|
||||||
// Bind a server socket to accept connections from contacts
|
// Bind a server socket to accept connections from contacts
|
||||||
SS ss;
|
SS ss;
|
||||||
try {
|
try {
|
||||||
@@ -185,14 +191,13 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!isRunning() || !shouldAllowContactConnections()) {
|
if (!state.setServerSocket(ss)) {
|
||||||
|
LOG.info("Closing redundant server socket");
|
||||||
tryToClose(ss);
|
tryToClose(ss);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
socket = ss;
|
|
||||||
backoff.reset();
|
backoff.reset();
|
||||||
callback.transportEnabled();
|
acceptContactConnections(ss);
|
||||||
acceptContactConnections();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,35 +226,39 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
if (changed) callback.mergeLocalProperties(p);
|
if (changed) callback.mergeLocalProperties(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void acceptContactConnections() {
|
private void acceptContactConnections(SS ss) {
|
||||||
while (true) {
|
while (true) {
|
||||||
DuplexTransportConnection conn;
|
DuplexTransportConnection conn;
|
||||||
try {
|
try {
|
||||||
conn = acceptConnection(socket);
|
conn = acceptConnection(ss);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// This is expected when the socket is closed
|
// This is expected when the server socket is closed
|
||||||
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
|
LOG.info("Server socket closed");
|
||||||
|
state.clearServerSocket();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOG.info("Connection received");
|
LOG.info("Connection received");
|
||||||
connectionLimiter.connectionOpened(conn);
|
connectionLimiter.connectionOpened(conn);
|
||||||
backoff.reset();
|
backoff.reset();
|
||||||
callback.handleConnection(conn);
|
callback.handleConnection(conn);
|
||||||
if (!running) return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
running = false;
|
SS ss = state.setStopped();
|
||||||
tryToClose(socket);
|
tryToClose(ss);
|
||||||
callback.transportDisabled();
|
|
||||||
disableAdapterIfEnabledByUs();
|
disableAdapterIfEnabledByUs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isRunning() {
|
public State getState() {
|
||||||
return running && isAdapterEnabled();
|
return state.getState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getReasonsDisabled() {
|
||||||
|
return state.getReasonsDisabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -265,7 +274,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||||
properties) {
|
properties) {
|
||||||
if (!isRunning() || !shouldAllowContactConnections()) return;
|
if (getState() != ACTIVE) return;
|
||||||
backoff.increment();
|
backoff.increment();
|
||||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||||
connect(p.getFirst(), p.getSecond());
|
connect(p.getFirst(), p.getSecond());
|
||||||
@@ -319,7 +328,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||||
if (!isRunning() || !shouldAllowContactConnections()) return null;
|
if (getState() != ACTIVE) return null;
|
||||||
if (!connectionLimiter.canOpenContactConnection()) return null;
|
if (!connectionLimiter.canOpenContactConnection()) return null;
|
||||||
String address = p.get(PROP_ADDRESS);
|
String address = p.get(PROP_ADDRESS);
|
||||||
if (isNullOrEmpty(address)) return null;
|
if (isNullOrEmpty(address)) return null;
|
||||||
@@ -337,7 +346,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public KeyAgreementListener createKeyAgreementListener(byte[] commitment) {
|
public KeyAgreementListener createKeyAgreementListener(byte[] commitment) {
|
||||||
if (!isRunning()) return null;
|
if (getState() != ACTIVE) return null;
|
||||||
// No truncation necessary because COMMIT_LENGTH = 16
|
// No truncation necessary because COMMIT_LENGTH = 16
|
||||||
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
||||||
if (LOG.isLoggable(INFO)) LOG.info("Key agreement UUID " + uuid);
|
if (LOG.isLoggable(INFO)) LOG.info("Key agreement UUID " + uuid);
|
||||||
@@ -349,7 +358,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
if (!isRunning()) {
|
if (getState() != ACTIVE) {
|
||||||
tryToClose(ss);
|
tryToClose(ss);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -363,7 +372,7 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public DuplexTransportConnection createKeyAgreementConnection(
|
public DuplexTransportConnection createKeyAgreementConnection(
|
||||||
byte[] commitment, BdfList descriptor) {
|
byte[] commitment, BdfList descriptor) {
|
||||||
if (!isRunning()) return null;
|
if (getState() != ACTIVE) return null;
|
||||||
// No truncation necessary because COMMIT_LENGTH = 16
|
// No truncation necessary because COMMIT_LENGTH = 16
|
||||||
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
String uuid = UUID.nameUUIDFromBytes(commitment).toString();
|
||||||
DuplexTransportConnection conn;
|
DuplexTransportConnection conn;
|
||||||
@@ -423,17 +432,18 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@IoExecutor
|
||||||
private void onSettingsUpdated(Settings settings) {
|
private void onSettingsUpdated(Settings settings) {
|
||||||
boolean wasAllowed = shouldAllowContactConnections();
|
boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE,
|
||||||
loadSettings(settings);
|
DEFAULT_PREF_PLUGIN_ENABLE);
|
||||||
boolean isAllowed = shouldAllowContactConnections();
|
SS ss = state.setEnabledByUser(enabledByUser);
|
||||||
if (wasAllowed && !isAllowed) {
|
State s = getState();
|
||||||
LOG.info("Contact connections disabled");
|
if (ss != null) {
|
||||||
tryToClose(socket);
|
LOG.info("Disabled by user, closing server socket");
|
||||||
callback.transportDisabled();
|
tryToClose(ss);
|
||||||
disableAdapterIfEnabledByUs();
|
disableAdapterIfEnabledByUs();
|
||||||
} else if (!wasAllowed && isAllowed) {
|
} else if (s == INACTIVE) {
|
||||||
LOG.info("Contact connections enabled");
|
LOG.info("Enabled by user, opening server socket");
|
||||||
if (isAdapterEnabled()) bind();
|
if (isAdapterEnabled()) bind();
|
||||||
else enableAdapter();
|
else enableAdapter();
|
||||||
}
|
}
|
||||||
@@ -461,4 +471,70 @@ abstract class BluetoothPlugin<SS> implements DuplexPlugin, EventListener {
|
|||||||
tryToClose(ss);
|
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.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.plugin.FileConstants.PROP_PATH;
|
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.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
|
|
||||||
@@ -45,7 +46,7 @@ abstract class FilePlugin implements SimplexPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TransportConnectionReader createReader(TransportProperties p) {
|
public TransportConnectionReader createReader(TransportProperties p) {
|
||||||
if (!isRunning()) return null;
|
if (getState() != ACTIVE) return null;
|
||||||
String path = p.get(PROP_PATH);
|
String path = p.get(PROP_PATH);
|
||||||
if (isNullOrEmpty(path)) return null;
|
if (isNullOrEmpty(path)) return null;
|
||||||
try {
|
try {
|
||||||
@@ -60,7 +61,7 @@ abstract class FilePlugin implements SimplexPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TransportConnectionWriter createWriter(TransportProperties p) {
|
public TransportConnectionWriter createWriter(TransportProperties p) {
|
||||||
if (!isRunning()) return null;
|
if (getState() != ACTIVE) return null;
|
||||||
String path = p.get(PROP_PATH);
|
String path = p.get(PROP_PATH);
|
||||||
if (isNullOrEmpty(path)) return null;
|
if (isNullOrEmpty(path)) return null;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -11,10 +11,10 @@ import org.briarproject.bramble.api.plugin.TransportId;
|
|||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.settings.Settings;
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
import org.briarproject.bramble.util.IoUtils;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
|
import java.net.Inet6Address;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.InterfaceAddress;
|
import java.net.InterfaceAddress;
|
||||||
@@ -22,8 +22,9 @@ import java.net.ServerSocket;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Deque;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@@ -31,38 +32,49 @@ import javax.annotation.Nullable;
|
|||||||
|
|
||||||
import static java.lang.Integer.parseInt;
|
import static java.lang.Integer.parseInt;
|
||||||
import static java.util.Collections.addAll;
|
import static java.util.Collections.addAll;
|
||||||
|
import static java.util.Collections.emptyList;
|
||||||
import static java.util.Collections.sort;
|
import static java.util.Collections.sort;
|
||||||
import static java.util.logging.Level.INFO;
|
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_LAN;
|
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.TRANSPORT_ID_LAN;
|
||||||
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.DEFAULT_PREF_PLUGIN_ENABLE;
|
||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.ID;
|
||||||
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PREF_IPV6;
|
||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PREF_LAN_IP_PORTS;
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PREF_LAN_IP_PORTS;
|
||||||
|
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_IPV6;
|
||||||
import static org.briarproject.bramble.api.plugin.LanTcpConstants.PROP_IP_PORTS;
|
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.api.plugin.LanTcpConstants.PROP_PORT;
|
||||||
|
import static org.briarproject.bramble.api.properties.TransportPropertyConstants.MAX_PROPERTY_LENGTH;
|
||||||
import static org.briarproject.bramble.util.ByteUtils.MAX_16_BIT_UNSIGNED;
|
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.PrivacyUtils.scrubSocketAddress;
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.fromHexString;
|
||||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
import static org.briarproject.bramble.util.StringUtils.join;
|
import static org.briarproject.bramble.util.StringUtils.join;
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.toHexString;
|
||||||
|
import static org.briarproject.bramble.util.StringUtils.utf8IsTooLong;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
class LanTcpPlugin extends TcpPlugin {
|
class LanTcpPlugin extends TcpPlugin {
|
||||||
|
|
||||||
private static final Logger LOG = getLogger(LanTcpPlugin.class.getName());
|
private static final Logger LOG = getLogger(LanTcpPlugin.class.getName());
|
||||||
|
|
||||||
private static final int MAX_ADDRESSES = 4;
|
|
||||||
private static final String SEPARATOR = ",";
|
private static final String SEPARATOR = ",";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The IP address of an Android device providing a wifi access point.
|
* The IP address of an Android device providing a wifi access point.
|
||||||
|
* <p>
|
||||||
|
* Most devices use this address, but at least one device (Honor 8A) may
|
||||||
|
* use other addresses in the range 192.168.43.0/24.
|
||||||
*/
|
*/
|
||||||
protected static final InetAddress WIFI_AP_ADDRESS;
|
private static final InetAddress WIFI_AP_ADDRESS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The IP address of an Android device providing a wifi direct
|
* The IP address of an Android device providing a wifi direct
|
||||||
* legacy mode access point.
|
* legacy mode access point.
|
||||||
*/
|
*/
|
||||||
protected static final InetAddress WIFI_DIRECT_AP_ADDRESS;
|
private static final InetAddress WIFI_DIRECT_AP_ADDRESS;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
@@ -91,29 +103,36 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
public void start() {
|
public void start() {
|
||||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||||
initialisePortProperty();
|
initialisePortProperty();
|
||||||
running = true;
|
Settings settings = callback.getSettings();
|
||||||
|
state.setStarted(settings.getBoolean(PREF_PLUGIN_ENABLE,
|
||||||
|
DEFAULT_PREF_PLUGIN_ENABLE));
|
||||||
bind();
|
bind();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void initialisePortProperty() {
|
protected void initialisePortProperty() {
|
||||||
TransportProperties p = callback.getLocalProperties();
|
TransportProperties p = callback.getLocalProperties();
|
||||||
if (isNullOrEmpty(p.get(PROP_PORT))) {
|
if (isNullOrEmpty(p.get(PROP_PORT))) {
|
||||||
int port = new Random().nextInt(32768) + 32768;
|
int port = chooseEphemeralPort();
|
||||||
p.put(PROP_PORT, String.valueOf(port));
|
p.put(PROP_PORT, String.valueOf(port));
|
||||||
callback.mergeLocalProperties(p);
|
callback.mergeLocalProperties(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<InetSocketAddress> getLocalSocketAddresses() {
|
protected boolean isEnabledByDefault() {
|
||||||
|
return DEFAULT_PREF_PLUGIN_ENABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<InetSocketAddress> getLocalSocketAddresses(boolean ipv4) {
|
||||||
TransportProperties p = callback.getLocalProperties();
|
TransportProperties p = callback.getLocalProperties();
|
||||||
int preferredPort = parsePortProperty(p.get(PROP_PORT));
|
int preferredPort = parsePortProperty(p.get(PROP_PORT));
|
||||||
String oldIpPorts = p.get(PROP_IP_PORTS);
|
String oldIpPorts = p.get(PROP_IP_PORTS);
|
||||||
List<InetSocketAddress> olds = parseSocketAddresses(oldIpPorts);
|
List<InetSocketAddress> olds = parseIpv4SocketAddresses(oldIpPorts);
|
||||||
|
|
||||||
List<InetSocketAddress> locals = new ArrayList<>();
|
List<InetSocketAddress> locals = new ArrayList<>();
|
||||||
List<InetSocketAddress> fallbacks = new ArrayList<>();
|
List<InetSocketAddress> fallbacks = new ArrayList<>();
|
||||||
for (InetAddress local : getUsableLocalInetAddresses()) {
|
for (InetAddress local : getUsableLocalInetAddresses(ipv4)) {
|
||||||
// If we've used this address before, try to use the same port
|
// If we've used this address before, try to use the same port
|
||||||
int port = preferredPort;
|
int port = preferredPort;
|
||||||
for (InetSocketAddress old : olds) {
|
for (InetSocketAddress old : olds) {
|
||||||
@@ -139,17 +158,17 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<InetSocketAddress> parseSocketAddresses(String ipPorts) {
|
private List<InetSocketAddress> parseIpv4SocketAddresses(String ipPorts) {
|
||||||
List<InetSocketAddress> addresses = new ArrayList<>();
|
List<InetSocketAddress> addresses = new ArrayList<>();
|
||||||
if (isNullOrEmpty(ipPorts)) return addresses;
|
if (isNullOrEmpty(ipPorts)) return addresses;
|
||||||
for (String ipPort : ipPorts.split(SEPARATOR)) {
|
for (String ipPort : ipPorts.split(SEPARATOR)) {
|
||||||
InetSocketAddress a = parseSocketAddress(ipPort);
|
InetSocketAddress a = parseIpv4SocketAddress(ipPort);
|
||||||
if (a != null) addresses.add(a);
|
if (a != null) addresses.add(a);
|
||||||
}
|
}
|
||||||
return addresses;
|
return addresses;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<InetAddress> getUsableLocalInetAddresses() {
|
protected List<InetAddress> getUsableLocalInetAddresses(boolean ipv4) {
|
||||||
List<InterfaceAddress> ifAddrs =
|
List<InterfaceAddress> ifAddrs =
|
||||||
new ArrayList<>(getLocalInterfaceAddresses());
|
new ArrayList<>(getLocalInterfaceAddresses());
|
||||||
// Prefer longer network prefixes
|
// Prefer longer network prefixes
|
||||||
@@ -158,50 +177,74 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
List<InetAddress> addrs = new ArrayList<>();
|
List<InetAddress> addrs = new ArrayList<>();
|
||||||
for (InterfaceAddress ifAddr : ifAddrs) {
|
for (InterfaceAddress ifAddr : ifAddrs) {
|
||||||
InetAddress addr = ifAddr.getAddress();
|
InetAddress addr = ifAddr.getAddress();
|
||||||
if (isAcceptableAddress(addr)) addrs.add(addr);
|
if (isAcceptableAddress(addr, ipv4)) addrs.add(addr);
|
||||||
}
|
}
|
||||||
return addrs;
|
return addrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setLocalSocketAddress(InetSocketAddress a) {
|
protected void setLocalSocketAddress(InetSocketAddress a, boolean ipv4) {
|
||||||
|
if (ipv4) setLocalIpv4SocketAddress(a);
|
||||||
|
else setLocalIpv6SocketAddress(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setLocalIpv4SocketAddress(InetSocketAddress a) {
|
||||||
String ipPort = getIpPortString(a);
|
String ipPort = getIpPortString(a);
|
||||||
|
updateRecentAddresses(PREF_LAN_IP_PORTS, PROP_IP_PORTS, ipPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setLocalIpv6SocketAddress(InetSocketAddress a) {
|
||||||
|
String hex = toHexString(a.getAddress().getAddress());
|
||||||
|
updateRecentAddresses(PREF_IPV6, PROP_IPV6, hex);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateRecentAddresses(String settingKey, String propertyKey,
|
||||||
|
String item) {
|
||||||
// Get the list of recently used addresses
|
// Get the list of recently used addresses
|
||||||
String setting = callback.getSettings().get(PREF_LAN_IP_PORTS);
|
String setting = callback.getSettings().get(settingKey);
|
||||||
List<String> recent = new ArrayList<>();
|
Deque<String> recent = new LinkedList<>();
|
||||||
if (!isNullOrEmpty(setting))
|
if (!isNullOrEmpty(setting)) {
|
||||||
addAll(recent, setting.split(SEPARATOR));
|
addAll(recent, setting.split(SEPARATOR));
|
||||||
// Is the address already in the list?
|
}
|
||||||
if (recent.remove(ipPort)) {
|
if (recent.remove(item)) {
|
||||||
// Move the address to the start of the list
|
// Move the item to the start of the list
|
||||||
recent.add(0, ipPort);
|
recent.addFirst(item);
|
||||||
setting = join(recent, SEPARATOR);
|
setting = join(recent, SEPARATOR);
|
||||||
} else {
|
} else {
|
||||||
// Add the address to the start of the list
|
// Add the item to the start of the list
|
||||||
recent.add(0, ipPort);
|
recent.addFirst(item);
|
||||||
// Drop the least recently used address if the list is full
|
// Drop items from the end of the list if it's too long to encode
|
||||||
if (recent.size() > MAX_ADDRESSES)
|
|
||||||
recent = recent.subList(0, MAX_ADDRESSES);
|
|
||||||
setting = join(recent, SEPARATOR);
|
setting = join(recent, SEPARATOR);
|
||||||
|
while (utf8IsTooLong(setting, MAX_PROPERTY_LENGTH)) {
|
||||||
|
recent.removeLast();
|
||||||
|
setting = join(recent, SEPARATOR);
|
||||||
|
}
|
||||||
// Update the list of addresses shared with contacts
|
// Update the list of addresses shared with contacts
|
||||||
List<String> shared = new ArrayList<>(recent);
|
|
||||||
sort(shared);
|
|
||||||
String property = join(shared, SEPARATOR);
|
|
||||||
TransportProperties properties = new TransportProperties();
|
TransportProperties properties = new TransportProperties();
|
||||||
properties.put(PROP_IP_PORTS, property);
|
properties.put(propertyKey, setting);
|
||||||
callback.mergeLocalProperties(properties);
|
callback.mergeLocalProperties(properties);
|
||||||
}
|
}
|
||||||
// Save the setting
|
// Save the setting
|
||||||
Settings settings = new Settings();
|
Settings settings = new Settings();
|
||||||
settings.put(PREF_LAN_IP_PORTS, setting);
|
settings.put(settingKey, setting);
|
||||||
callback.mergeSettings(settings);
|
callback.mergeSettings(settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected boolean isIpv6LinkLocalAddress(InetAddress a) {
|
||||||
|
return a instanceof Inet6Address && a.isLinkLocalAddress();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<InetSocketAddress> getRemoteSocketAddresses(
|
protected List<InetSocketAddress> getRemoteSocketAddresses(
|
||||||
|
TransportProperties p, boolean ipv4) {
|
||||||
|
if (ipv4) return getRemoteIpv4SocketAddresses(p);
|
||||||
|
else return getRemoteIpv6SocketAddresses(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<InetSocketAddress> getRemoteIpv4SocketAddresses(
|
||||||
TransportProperties p) {
|
TransportProperties p) {
|
||||||
String ipPorts = p.get(PROP_IP_PORTS);
|
String ipPorts = p.get(PROP_IP_PORTS);
|
||||||
List<InetSocketAddress> remotes = parseSocketAddresses(ipPorts);
|
List<InetSocketAddress> remotes = parseIpv4SocketAddresses(ipPorts);
|
||||||
int port = parsePortProperty(p.get(PROP_PORT));
|
int port = parsePortProperty(p.get(PROP_PORT));
|
||||||
// If the contact has a preferred port, we can guess their IP:port when
|
// If the contact has a preferred port, we can guess their IP:port when
|
||||||
// they're providing a wifi access point
|
// they're providing a wifi access point
|
||||||
@@ -216,20 +259,52 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
return remotes;
|
return remotes;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isAcceptableAddress(InetAddress a) {
|
private List<InetSocketAddress> getRemoteIpv6SocketAddresses(
|
||||||
// Accept link-local and site-local IPv4 addresses
|
TransportProperties p) {
|
||||||
boolean ipv4 = a instanceof Inet4Address;
|
List<InetAddress> addrs = parseIpv6Addresses(p.get(PROP_IPV6));
|
||||||
boolean loop = a.isLoopbackAddress();
|
int port = parsePortProperty(p.get(PROP_PORT));
|
||||||
boolean link = a.isLinkLocalAddress();
|
if (addrs.isEmpty() || port == 0) return emptyList();
|
||||||
boolean site = a.isSiteLocalAddress();
|
List<InetSocketAddress> remotes = new ArrayList<>();
|
||||||
return ipv4 && !loop && (link || site);
|
for (InetAddress addr : addrs) {
|
||||||
|
remotes.add(new InetSocketAddress(addr, port));
|
||||||
|
}
|
||||||
|
return remotes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<InetAddress> parseIpv6Addresses(String property) {
|
||||||
|
if (isNullOrEmpty(property)) return emptyList();
|
||||||
|
try {
|
||||||
|
List<InetAddress> addrs = new ArrayList<>();
|
||||||
|
for (String hex : property.split(SEPARATOR)) {
|
||||||
|
byte[] ip = fromHexString(hex);
|
||||||
|
if (ip.length == 16) addrs.add(InetAddress.getByAddress(ip));
|
||||||
|
}
|
||||||
|
return addrs;
|
||||||
|
} catch (IllegalArgumentException | UnknownHostException e) {
|
||||||
|
return emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isAcceptableAddress(InetAddress a, boolean ipv4) {
|
||||||
|
if (ipv4) {
|
||||||
|
// Accept link-local and site-local IPv4 addresses
|
||||||
|
boolean isIpv4 = a instanceof Inet4Address;
|
||||||
|
boolean link = a.isLinkLocalAddress();
|
||||||
|
boolean site = a.isSiteLocalAddress();
|
||||||
|
return isIpv4 && (link || site);
|
||||||
|
} else {
|
||||||
|
// Accept link-local IPv6 addresses
|
||||||
|
return isIpv6LinkLocalAddress(a);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean isConnectable(InterfaceAddress local,
|
protected boolean isConnectable(InterfaceAddress local,
|
||||||
InetSocketAddress remote) {
|
InetSocketAddress remote) {
|
||||||
if (remote.getPort() == 0) return false;
|
if (remote.getPort() == 0) return false;
|
||||||
if (!isAcceptableAddress(remote.getAddress())) return false;
|
InetAddress remoteAddress = remote.getAddress();
|
||||||
|
boolean ipv4 = local.getAddress() instanceof Inet4Address;
|
||||||
|
if (!isAcceptableAddress(remoteAddress, ipv4)) return false;
|
||||||
// Try to determine whether the address is on the same LAN as us
|
// Try to determine whether the address is on the same LAN as us
|
||||||
byte[] localIp = local.getAddress().getAddress();
|
byte[] localIp = local.getAddress().getAddress();
|
||||||
byte[] remoteIp = remote.getAddress().getAddress();
|
byte[] remoteIp = remote.getAddress().getAddress();
|
||||||
@@ -271,7 +346,7 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Failed to bind " + scrubSocketAddress(addr));
|
LOG.info("Failed to bind " + scrubSocketAddress(addr));
|
||||||
tryToClose(ss);
|
tryToClose(ss, LOG, WARNING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ss == null || !ss.isBound()) {
|
if (ss == null || !ss.isBound()) {
|
||||||
@@ -287,11 +362,18 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
return new LanKeyAgreementListener(descriptor, ss);
|
return new LanKeyAgreementListener(descriptor, ss);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private List<InetSocketAddress> getLocalSocketAddresses() {
|
||||||
|
List<InetSocketAddress> addrs = new ArrayList<>();
|
||||||
|
addrs.addAll(getLocalSocketAddresses(true));
|
||||||
|
addrs.addAll(getLocalSocketAddresses(false));
|
||||||
|
return addrs;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DuplexTransportConnection createKeyAgreementConnection(
|
public DuplexTransportConnection createKeyAgreementConnection(
|
||||||
byte[] commitment, BdfList descriptor) {
|
byte[] commitment, BdfList descriptor) {
|
||||||
if (!isRunning()) return null;
|
ServerSocket ss = state.getServerSocket(true);
|
||||||
ServerSocket ss = socket;
|
if (ss == null) return null;
|
||||||
InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress());
|
InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress());
|
||||||
if (local == null) {
|
if (local == null) {
|
||||||
LOG.warning("No interface for key agreement server socket");
|
LOG.warning("No interface for key agreement server socket");
|
||||||
@@ -363,7 +445,7 @@ class LanTcpPlugin extends TcpPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
IoUtils.tryToClose(ss, LOG, WARNING);
|
tryToClose(ss, LOG, WARNING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.bramble.plugin.tcp;
|
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.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.BackoffFactory;
|
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 static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
|
private final EventBus eventBus;
|
||||||
private final BackoffFactory backoffFactory;
|
private final BackoffFactory backoffFactory;
|
||||||
|
|
||||||
public LanTcpPluginFactory(Executor ioExecutor,
|
public LanTcpPluginFactory(Executor ioExecutor, EventBus eventBus,
|
||||||
BackoffFactory backoffFactory) {
|
BackoffFactory backoffFactory) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
|
this.eventBus = eventBus;
|
||||||
this.backoffFactory = backoffFactory;
|
this.backoffFactory = backoffFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +51,9 @@ public class LanTcpPluginFactory implements DuplexPluginFactory {
|
|||||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
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);
|
MAX_IDLE_TIME, CONNECTION_TIMEOUT);
|
||||||
|
eventBus.addListener(plugin);
|
||||||
|
return plugin;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,11 +54,13 @@ class PortMapperImpl implements PortMapper {
|
|||||||
shutdownManager.addShutdownHook(() -> deleteMapping(port));
|
shutdownManager.addShutdownHook(() -> deleteMapping(port));
|
||||||
}
|
}
|
||||||
String externalString = gateway.getExternalIPAddress();
|
String externalString = gateway.getExternalIPAddress();
|
||||||
if (LOG.isLoggable(INFO))
|
if (externalString == null) {
|
||||||
LOG.info(
|
LOG.info("External address not available");
|
||||||
"External address " + scrubInetAddress(externalString));
|
} else {
|
||||||
if (externalString != null)
|
|
||||||
external = InetAddress.getByName(externalString);
|
external = InetAddress.getByName(externalString);
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info("External address " + scrubInetAddress(external));
|
||||||
|
}
|
||||||
} catch (IOException | SAXException e) {
|
} catch (IOException | SAXException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,12 @@ package org.briarproject.bramble.plugin.tcp;
|
|||||||
import org.briarproject.bramble.PoliteExecutor;
|
import org.briarproject.bramble.PoliteExecutor;
|
||||||
import org.briarproject.bramble.api.Pair;
|
import org.briarproject.bramble.api.Pair;
|
||||||
import org.briarproject.bramble.api.data.BdfList;
|
import org.briarproject.bramble.api.data.BdfList;
|
||||||
|
import org.briarproject.bramble.api.event.Event;
|
||||||
|
import org.briarproject.bramble.api.event.EventListener;
|
||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||||
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
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.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||||
@@ -14,7 +18,8 @@ import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
|||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
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.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
@@ -35,19 +40,26 @@ import java.util.logging.Logger;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
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.emptyList;
|
||||||
import static java.util.Collections.list;
|
import static java.util.Collections.list;
|
||||||
import static java.util.logging.Level.INFO;
|
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.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.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
import static org.briarproject.bramble.util.PrivacyUtils.scrubSocketAddress;
|
||||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
abstract class TcpPlugin implements DuplexPlugin {
|
abstract class TcpPlugin implements DuplexPlugin, EventListener {
|
||||||
|
|
||||||
private static final Logger LOG = getLogger(TcpPlugin.class.getName());
|
private static final Logger LOG = getLogger(TcpPlugin.class.getName());
|
||||||
|
|
||||||
@@ -60,28 +72,28 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
protected final int maxLatency, maxIdleTime;
|
protected final int maxLatency, maxIdleTime;
|
||||||
protected final int connectionTimeout, socketTimeout;
|
protected final int connectionTimeout, socketTimeout;
|
||||||
protected final AtomicBoolean used = new AtomicBoolean(false);
|
protected final AtomicBoolean used = new AtomicBoolean(false);
|
||||||
|
protected final PluginState state = new PluginState();
|
||||||
protected volatile boolean running = false;
|
|
||||||
protected volatile ServerSocket socket = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns zero or more socket addresses on which the plugin should listen,
|
* Returns zero or more socket addresses on which the plugin should listen,
|
||||||
* in order of preference. At most one of the addresses will be bound.
|
* in order of preference. At most one of the addresses will be bound.
|
||||||
*/
|
*/
|
||||||
protected abstract List<InetSocketAddress> getLocalSocketAddresses();
|
protected abstract List<InetSocketAddress> getLocalSocketAddresses(
|
||||||
|
boolean ipv4);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the address on which the plugin is listening to the transport
|
* Adds the address on which the plugin is listening to the transport
|
||||||
* properties.
|
* properties.
|
||||||
*/
|
*/
|
||||||
protected abstract void setLocalSocketAddress(InetSocketAddress a);
|
protected abstract void setLocalSocketAddress(InetSocketAddress a,
|
||||||
|
boolean ipv4);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns zero or more socket addresses for connecting to a contact with
|
* Returns zero or more socket addresses for connecting to a contact with
|
||||||
* the given transport properties.
|
* the given transport properties.
|
||||||
*/
|
*/
|
||||||
protected abstract List<InetSocketAddress> getRemoteSocketAddresses(
|
protected abstract List<InetSocketAddress> getRemoteSocketAddresses(
|
||||||
TransportProperties p);
|
TransportProperties p, boolean ipv4);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if connections to the given address can be attempted.
|
* Returns true if connections to the given address can be attempted.
|
||||||
@@ -90,6 +102,11 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
protected abstract boolean isConnectable(InterfaceAddress local,
|
protected abstract boolean isConnectable(InterfaceAddress local,
|
||||||
InetSocketAddress remote);
|
InetSocketAddress remote);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the plugin is enabled by default.
|
||||||
|
*/
|
||||||
|
protected abstract boolean isEnabledByDefault();
|
||||||
|
|
||||||
TcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback,
|
TcpPlugin(Executor ioExecutor, Backoff backoff, PluginCallback callback,
|
||||||
int maxLatency, int maxIdleTime, int connectionTimeout) {
|
int maxLatency, int maxIdleTime, int connectionTimeout) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
@@ -118,49 +135,50 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||||
running = true;
|
Settings settings = callback.getSettings();
|
||||||
|
state.setStarted(
|
||||||
|
settings.getBoolean(PREF_PLUGIN_ENABLE, isEnabledByDefault()));
|
||||||
bind();
|
bind();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void bind() {
|
protected void bind() {
|
||||||
bindExecutor.execute(() -> {
|
bindExecutor.execute(() -> {
|
||||||
if (!running) return;
|
if (getState() != INACTIVE) return;
|
||||||
if (socket != null && !socket.isClosed()) return;
|
bind(true);
|
||||||
ServerSocket ss = null;
|
bind(false);
|
||||||
for (InetSocketAddress addr : getLocalSocketAddresses()) {
|
|
||||||
try {
|
|
||||||
ss = new ServerSocket();
|
|
||||||
ss.bind(addr);
|
|
||||||
break;
|
|
||||||
} catch (IOException e) {
|
|
||||||
if (LOG.isLoggable(INFO))
|
|
||||||
LOG.info("Failed to bind " + scrubSocketAddress(addr));
|
|
||||||
tryToClose(ss);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (ss == null || !ss.isBound()) {
|
|
||||||
LOG.info("Could not bind server socket");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!running) {
|
|
||||||
tryToClose(ss);
|
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void tryToClose(@Nullable ServerSocket ss) {
|
private void bind(boolean ipv4) {
|
||||||
IoUtils.tryToClose(ss, LOG, WARNING);
|
ServerSocket ss = null;
|
||||||
callback.transportDisabled();
|
for (InetSocketAddress addr : getLocalSocketAddresses(ipv4)) {
|
||||||
|
try {
|
||||||
|
ss = new ServerSocket();
|
||||||
|
ss.bind(addr);
|
||||||
|
break;
|
||||||
|
} catch (IOException e) {
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info("Failed to bind " + scrubSocketAddress(addr));
|
||||||
|
tryToClose(ss, LOG, WARNING);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (ss == null || !ss.isBound()) {
|
||||||
|
LOG.info("Could not bind server socket");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!state.setServerSocket(ss, ipv4)) {
|
||||||
|
LOG.info("Closing redundant server socket");
|
||||||
|
tryToClose(ss, LOG, WARNING);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
backoff.reset();
|
||||||
|
InetSocketAddress local =
|
||||||
|
(InetSocketAddress) ss.getLocalSocketAddress();
|
||||||
|
setLocalSocketAddress(local, ipv4);
|
||||||
|
if (LOG.isLoggable(INFO))
|
||||||
|
LOG.info("Listening on " + scrubSocketAddress(local));
|
||||||
|
ServerSocket finalSocket = ss;
|
||||||
|
ioExecutor.execute(() -> acceptContactConnections(finalSocket, ipv4));
|
||||||
}
|
}
|
||||||
|
|
||||||
String getIpPortString(InetSocketAddress a) {
|
String getIpPortString(InetSocketAddress a) {
|
||||||
@@ -170,20 +188,22 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
return addr + ":" + a.getPort();
|
return addr + ":" + a.getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void acceptContactConnections() {
|
private void acceptContactConnections(ServerSocket ss, boolean ipv4) {
|
||||||
while (isRunning()) {
|
while (true) {
|
||||||
Socket s;
|
Socket s;
|
||||||
try {
|
try {
|
||||||
s = socket.accept();
|
s = ss.accept();
|
||||||
s.setSoTimeout(socketTimeout);
|
s.setSoTimeout(socketTimeout);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// This is expected when the socket is closed
|
// This is expected when the server socket is closed
|
||||||
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
|
LOG.info("Server socket closed");
|
||||||
|
state.clearServerSocket(ss, ipv4);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Connection from " +
|
LOG.info("Connection from " +
|
||||||
scrubSocketAddress(s.getRemoteSocketAddress()));
|
scrubSocketAddress(s.getRemoteSocketAddress()));
|
||||||
|
}
|
||||||
backoff.reset();
|
backoff.reset();
|
||||||
callback.handleConnection(new TcpTransportConnection(this, s));
|
callback.handleConnection(new TcpTransportConnection(this, s));
|
||||||
}
|
}
|
||||||
@@ -191,13 +211,17 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
running = false;
|
for (ServerSocket ss : state.setStopped()) tryToClose(ss, LOG, WARNING);
|
||||||
tryToClose(socket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isRunning() {
|
public State getState() {
|
||||||
return running && socket != null && !socket.isClosed();
|
return state.getState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getReasonsDisabled() {
|
||||||
|
return state.getReasonsDisabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -213,7 +237,7 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||||
properties) {
|
properties) {
|
||||||
if (!isRunning()) return;
|
if (getState() != ACTIVE) return;
|
||||||
backoff.increment();
|
backoff.increment();
|
||||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||||
connect(p.getFirst(), p.getSecond());
|
connect(p.getFirst(), p.getSecond());
|
||||||
@@ -232,14 +256,22 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||||
if (!isRunning()) return null;
|
DuplexTransportConnection c = createConnection(p, true);
|
||||||
ServerSocket ss = socket;
|
if (c != null) return c;
|
||||||
|
return createConnection(p, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private DuplexTransportConnection createConnection(TransportProperties p,
|
||||||
|
boolean ipv4) {
|
||||||
|
ServerSocket ss = state.getServerSocket(ipv4);
|
||||||
|
if (ss == null) return null;
|
||||||
InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress());
|
InterfaceAddress local = getLocalInterfaceAddress(ss.getInetAddress());
|
||||||
if (local == null) {
|
if (local == null) {
|
||||||
LOG.warning("No interface for server socket");
|
LOG.warning("No interface for server socket");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
for (InetSocketAddress remote : getRemoteSocketAddresses(p)) {
|
for (InetSocketAddress remote : getRemoteSocketAddresses(p, ipv4)) {
|
||||||
// Don't try to connect to our own address
|
// Don't try to connect to our own address
|
||||||
if (!canConnectToOwnAddress() &&
|
if (!canConnectToOwnAddress() &&
|
||||||
remote.getAddress().equals(ss.getInetAddress())) {
|
remote.getAddress().equals(ss.getInetAddress())) {
|
||||||
@@ -264,9 +296,10 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
LOG.info("Connected to " + scrubSocketAddress(remote));
|
LOG.info("Connected to " + scrubSocketAddress(remote));
|
||||||
return new TcpTransportConnection(this, s);
|
return new TcpTransportConnection(this, s);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Could not connect to " +
|
LOG.info("Could not connect to " +
|
||||||
scrubSocketAddress(remote));
|
scrubSocketAddress(remote));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
@@ -289,8 +322,12 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
return new Socket();
|
return new Socket();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int chooseEphemeralPort() {
|
||||||
|
return 32768 + (int) (Math.random() * 32768);
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
InetSocketAddress parseSocketAddress(String ipPort) {
|
InetSocketAddress parseIpv4SocketAddress(String ipPort) {
|
||||||
if (isNullOrEmpty(ipPort)) return null;
|
if (isNullOrEmpty(ipPort)) return null;
|
||||||
String[] split = ipPort.split(":");
|
String[] split = ipPort.split(":");
|
||||||
if (split.length != 2) return null;
|
if (split.length != 2) return null;
|
||||||
@@ -301,14 +338,7 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
InetAddress a = InetAddress.getByName(addr);
|
InetAddress a = InetAddress.getByName(addr);
|
||||||
int p = Integer.parseInt(port);
|
int p = Integer.parseInt(port);
|
||||||
return new InetSocketAddress(a, p);
|
return new InetSocketAddress(a, p);
|
||||||
} catch (UnknownHostException e) {
|
} catch (UnknownHostException | NumberFormatException e) {
|
||||||
if (LOG.isLoggable(WARNING))
|
|
||||||
// not scrubbing to enable us to find the problem
|
|
||||||
LOG.warning("Invalid address: " + addr);
|
|
||||||
return null;
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
if (LOG.isLoggable(WARNING))
|
|
||||||
LOG.warning("Invalid port: " + port);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -366,4 +396,114 @@ abstract class TcpPlugin implements DuplexPlugin {
|
|||||||
return emptyList();
|
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, isEnabledByDefault());
|
||||||
|
List<ServerSocket> toClose = state.setEnabledByUser(enabledByUser);
|
||||||
|
State s = getState();
|
||||||
|
if (!toClose.isEmpty()) {
|
||||||
|
LOG.info("Disabled by user, closing server sockets");
|
||||||
|
for (ServerSocket ss : toClose) tryToClose(ss, LOG, WARNING);
|
||||||
|
} else if (s == INACTIVE) {
|
||||||
|
LOG.info("Enabled by user, opening server sockets");
|
||||||
|
bind();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@ThreadSafe
|
||||||
|
@NotNullByDefault
|
||||||
|
protected class PluginState {
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
|
private boolean started = false, stopped = false, enabledByUser = false;
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
|
@Nullable
|
||||||
|
private ServerSocket serverSocketV4 = null, serverSocketV6 = null;
|
||||||
|
|
||||||
|
synchronized void setStarted(boolean enabledByUser) {
|
||||||
|
started = true;
|
||||||
|
this.enabledByUser = enabledByUser;
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized List<ServerSocket> setStopped() {
|
||||||
|
stopped = true;
|
||||||
|
List<ServerSocket> toClose = clearServerSockets();
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
|
return toClose;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GuardedBy("this")
|
||||||
|
private List<ServerSocket> clearServerSockets() {
|
||||||
|
List<ServerSocket> toClose = new ArrayList<>(2);
|
||||||
|
if (serverSocketV4 != null) {
|
||||||
|
toClose.add(serverSocketV4);
|
||||||
|
serverSocketV4 = null;
|
||||||
|
}
|
||||||
|
if (serverSocketV6 != null) {
|
||||||
|
toClose.add(serverSocketV6);
|
||||||
|
serverSocketV6 = null;
|
||||||
|
}
|
||||||
|
return toClose;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized List<ServerSocket> setEnabledByUser(
|
||||||
|
boolean enabledByUser) {
|
||||||
|
this.enabledByUser = enabledByUser;
|
||||||
|
List<ServerSocket> toClose = enabledByUser
|
||||||
|
? emptyList() : clearServerSockets();
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
|
return toClose;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
synchronized ServerSocket getServerSocket(boolean ipv4) {
|
||||||
|
return ipv4 ? serverSocketV4 : serverSocketV6;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized boolean setServerSocket(ServerSocket ss, boolean ipv4) {
|
||||||
|
if (stopped) return false;
|
||||||
|
if (ipv4) {
|
||||||
|
if (serverSocketV4 != null) return false;
|
||||||
|
serverSocketV4 = ss;
|
||||||
|
} else {
|
||||||
|
if (serverSocketV6 != null) return false;
|
||||||
|
serverSocketV6 = ss;
|
||||||
|
}
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized void clearServerSocket(ServerSocket ss, boolean ipv4) {
|
||||||
|
if (ipv4) {
|
||||||
|
if (serverSocketV4 == ss) serverSocketV4 = null;
|
||||||
|
} else {
|
||||||
|
if (serverSocketV6 == ss) serverSocketV6 = null;
|
||||||
|
}
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized State getState() {
|
||||||
|
if (!started || stopped) return STARTING_STOPPING;
|
||||||
|
if (!enabledByUser) return DISABLED;
|
||||||
|
if (serverSocketV4 != null || serverSocketV6 != null) return ACTIVE;
|
||||||
|
return INACTIVE;
|
||||||
|
}
|
||||||
|
|
||||||
|
synchronized int getReasonsDisabled() {
|
||||||
|
return getState() == DISABLED ? REASON_USER : 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import java.util.concurrent.Executor;
|
|||||||
|
|
||||||
import static java.util.Collections.emptyList;
|
import static java.util.Collections.emptyList;
|
||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
|
import static org.briarproject.bramble.api.plugin.WanTcpConstants.DEFAULT_PREF_PLUGIN_ENABLE;
|
||||||
import static org.briarproject.bramble.api.plugin.WanTcpConstants.ID;
|
import static org.briarproject.bramble.api.plugin.WanTcpConstants.ID;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@@ -43,10 +44,16 @@ class WanTcpPlugin extends TcpPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<InetSocketAddress> getLocalSocketAddresses() {
|
protected boolean isEnabledByDefault() {
|
||||||
|
return DEFAULT_PREF_PLUGIN_ENABLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<InetSocketAddress> getLocalSocketAddresses(boolean ipv4) {
|
||||||
|
if (!ipv4) return emptyList();
|
||||||
// Use the same address and port as last time if available
|
// Use the same address and port as last time if available
|
||||||
TransportProperties p = callback.getLocalProperties();
|
TransportProperties p = callback.getLocalProperties();
|
||||||
InetSocketAddress old = parseSocketAddress(p.get(PROP_IP_PORT));
|
InetSocketAddress old = parseIpv4SocketAddress(p.get(PROP_IP_PORT));
|
||||||
List<InetSocketAddress> addrs = new LinkedList<>();
|
List<InetSocketAddress> addrs = new LinkedList<>();
|
||||||
for (InetAddress a : getLocalInetAddresses()) {
|
for (InetAddress a : getLocalInetAddresses()) {
|
||||||
if (isAcceptableAddress(a)) {
|
if (isAcceptableAddress(a)) {
|
||||||
@@ -76,14 +83,11 @@ class WanTcpPlugin extends TcpPlugin {
|
|||||||
return ipv4 && !loop && !link && !site;
|
return ipv4 && !loop && !link && !site;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int chooseEphemeralPort() {
|
|
||||||
return 32768 + (int) (Math.random() * 32768);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<InetSocketAddress> getRemoteSocketAddresses(
|
protected List<InetSocketAddress> getRemoteSocketAddresses(
|
||||||
TransportProperties p) {
|
TransportProperties p, boolean ipv4) {
|
||||||
InetSocketAddress parsed = parseSocketAddress(p.get(PROP_IP_PORT));
|
if (!ipv4) return emptyList();
|
||||||
|
InetSocketAddress parsed = parseIpv4SocketAddress(p.get(PROP_IP_PORT));
|
||||||
if (parsed == null) return emptyList();
|
if (parsed == null) return emptyList();
|
||||||
return singletonList(parsed);
|
return singletonList(parsed);
|
||||||
}
|
}
|
||||||
@@ -96,7 +100,8 @@ class WanTcpPlugin extends TcpPlugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void setLocalSocketAddress(InetSocketAddress a) {
|
protected void setLocalSocketAddress(InetSocketAddress a, boolean ipv4) {
|
||||||
|
if (!ipv4) throw new AssertionError();
|
||||||
if (mappingResult != null && mappingResult.isUsable()) {
|
if (mappingResult != null && mappingResult.isUsable()) {
|
||||||
// Advertise the external address to contacts
|
// Advertise the external address to contacts
|
||||||
if (a.equals(mappingResult.getInternal())) {
|
if (a.equals(mappingResult.getInternal())) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.bramble.plugin.tcp;
|
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.lifecycle.ShutdownManager;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
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 static final double BACKOFF_BASE = 1.2;
|
||||||
|
|
||||||
private final Executor ioExecutor;
|
private final Executor ioExecutor;
|
||||||
|
private final EventBus eventBus;
|
||||||
private final BackoffFactory backoffFactory;
|
private final BackoffFactory backoffFactory;
|
||||||
private final ShutdownManager shutdownManager;
|
private final ShutdownManager shutdownManager;
|
||||||
|
|
||||||
public WanTcpPluginFactory(Executor ioExecutor,
|
public WanTcpPluginFactory(Executor ioExecutor, EventBus eventBus,
|
||||||
BackoffFactory backoffFactory, ShutdownManager shutdownManager) {
|
BackoffFactory backoffFactory, ShutdownManager shutdownManager) {
|
||||||
this.ioExecutor = ioExecutor;
|
this.ioExecutor = ioExecutor;
|
||||||
|
this.eventBus = eventBus;
|
||||||
this.backoffFactory = backoffFactory;
|
this.backoffFactory = backoffFactory;
|
||||||
this.shutdownManager = shutdownManager;
|
this.shutdownManager = shutdownManager;
|
||||||
}
|
}
|
||||||
@@ -51,8 +54,10 @@ public class WanTcpPluginFactory implements DuplexPluginFactory {
|
|||||||
public DuplexPlugin createPlugin(PluginCallback callback) {
|
public DuplexPlugin createPlugin(PluginCallback callback) {
|
||||||
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
Backoff backoff = backoffFactory.createBackoff(MIN_POLLING_INTERVAL,
|
||||||
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
MAX_POLLING_INTERVAL, BACKOFF_BASE);
|
||||||
return new WanTcpPlugin(ioExecutor, backoff,
|
WanTcpPlugin plugin = new WanTcpPlugin(ioExecutor, backoff,
|
||||||
new PortMapperImpl(shutdownManager), callback, MAX_LATENCY,
|
new PortMapperImpl(shutdownManager), callback, MAX_LATENCY,
|
||||||
MAX_IDLE_TIME, CONNECTION_TIMEOUT);
|
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"};
|
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}.
|
* Should be a subset of {@link #BLOCKED}.
|
||||||
*/
|
*/
|
||||||
String[] BRIDGES = { "CN", "IR", "EG", "BY", "TR", "SY", "VE" };
|
String[] BRIDGES = { "CN", "IR", "EG", "BY", "TR", "SY", "VE" };
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import org.briarproject.bramble.api.network.NetworkManager;
|
|||||||
import org.briarproject.bramble.api.network.NetworkStatus;
|
import org.briarproject.bramble.api.network.NetworkStatus;
|
||||||
import org.briarproject.bramble.api.network.event.NetworkStatusEvent;
|
import org.briarproject.bramble.api.network.event.NetworkStatusEvent;
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
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.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
import org.briarproject.bramble.api.plugin.Backoff;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||||
@@ -54,6 +55,9 @@ import java.util.logging.Logger;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.zip.ZipInputStream;
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.annotation.concurrent.GuardedBy;
|
||||||
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
import javax.net.SocketFactory;
|
import javax.net.SocketFactory;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
@@ -65,7 +69,16 @@ import static java.util.logging.Logger.getLogger;
|
|||||||
import static net.freehaven.tor.control.TorControlCommands.HS_ADDRESS;
|
import static net.freehaven.tor.control.TorControlCommands.HS_ADDRESS;
|
||||||
import static net.freehaven.tor.control.TorControlCommands.HS_PRIVKEY;
|
import static net.freehaven.tor.control.TorControlCommands.HS_PRIVKEY;
|
||||||
import static org.briarproject.bramble.api.nullsafety.NullSafety.requireNonNull;
|
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.CONTROL_PORT;
|
||||||
|
import static org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_PREF_PLUGIN_ENABLE;
|
||||||
|
import static org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_PREF_TOR_MOBILE;
|
||||||
|
import static org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_PREF_TOR_NETWORK;
|
||||||
|
import static org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_PREF_TOR_ONLY_WHEN_CHARGING;
|
||||||
import static org.briarproject.bramble.api.plugin.TorConstants.ID;
|
import static org.briarproject.bramble.api.plugin.TorConstants.ID;
|
||||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_MOBILE;
|
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;
|
||||||
@@ -76,6 +89,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.PREF_TOR_PORT;
|
||||||
import static org.briarproject.bramble.api.plugin.TorConstants.PROP_ONION_V2;
|
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.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.plugin.tor.TorRendezvousCrypto.SEED_BYTES;
|
||||||
import static org.briarproject.bramble.util.IoUtils.copyAndClose;
|
import static org.briarproject.bramble.util.IoUtils.copyAndClose;
|
||||||
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
import static org.briarproject.bramble.util.IoUtils.tryToClose;
|
||||||
@@ -113,16 +129,14 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
private final int maxLatency, maxIdleTime, socketTimeout;
|
private final int maxLatency, maxIdleTime, socketTimeout;
|
||||||
private final File torDirectory, torFile, geoIpFile, obfs4File, configFile;
|
private final File torDirectory, torFile, geoIpFile, obfs4File, configFile;
|
||||||
private final File doneFile, cookieFile;
|
private final File doneFile, cookieFile;
|
||||||
private final ConnectionStatus connectionStatus;
|
|
||||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
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 Socket controlSocket = null;
|
||||||
private volatile TorControlConnection controlConnection = null;
|
private volatile TorControlConnection controlConnection = null;
|
||||||
private volatile Settings settings = null;
|
private volatile Settings settings = null;
|
||||||
|
|
||||||
protected volatile boolean running = false;
|
|
||||||
|
|
||||||
protected abstract int getProcessId();
|
protected abstract int getProcessId();
|
||||||
|
|
||||||
protected abstract long getLastUpdateTime();
|
protected abstract long getLastUpdateTime();
|
||||||
@@ -159,7 +173,6 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
configFile = new File(torDirectory, "torrc");
|
configFile = new File(torDirectory, "torrc");
|
||||||
doneFile = new File(torDirectory, "done");
|
doneFile = new File(torDirectory, "done");
|
||||||
cookieFile = new File(torDirectory, ".tor/control_auth_cookie");
|
cookieFile = new File(torDirectory, ".tor/control_auth_cookie");
|
||||||
connectionStatus = new ConnectionStatus();
|
|
||||||
// Don't execute more than one connection status check at a time
|
// Don't execute more than one connection status check at a time
|
||||||
connectionStatusExecutor =
|
connectionStatusExecutor =
|
||||||
new PoliteExecutor("TorPlugin", ioExecutor, 1);
|
new PoliteExecutor("TorPlugin", ioExecutor, 1);
|
||||||
@@ -190,7 +203,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Load the settings
|
// Load the settings
|
||||||
settings = callback.getSettings();
|
settings = migrateSettings(callback.getSettings());
|
||||||
// Install or update the assets if necessary
|
// Install or update the assets if necessary
|
||||||
if (!assetsAreUpToDate()) installAssets();
|
if (!assetsAreUpToDate()) installAssets();
|
||||||
if (cookieFile.exists() && !cookieFile.delete())
|
if (cookieFile.exists() && !cookieFile.delete())
|
||||||
@@ -258,7 +271,6 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
// Tell Tor to exit when the control connection is closed
|
// Tell Tor to exit when the control connection is closed
|
||||||
controlConnection.takeOwnership();
|
controlConnection.takeOwnership();
|
||||||
controlConnection.resetConf(singletonList(OWNER));
|
controlConnection.resetConf(singletonList(OWNER));
|
||||||
running = true;
|
|
||||||
// Register to receive events from the Tor process
|
// Register to receive events from the Tor process
|
||||||
controlConnection.setEventHandler(this);
|
controlConnection.setEventHandler(this);
|
||||||
controlConnection.setEvents(asList(EVENTS));
|
controlConnection.setEvents(asList(EVENTS));
|
||||||
@@ -266,11 +278,12 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
String phase = controlConnection.getInfo("status/bootstrap-phase");
|
String phase = controlConnection.getInfo("status/bootstrap-phase");
|
||||||
if (phase != null && phase.contains("PROGRESS=100")) {
|
if (phase != null && phase.contains("PROGRESS=100")) {
|
||||||
LOG.info("Tor has already bootstrapped");
|
LOG.info("Tor has already bootstrapped");
|
||||||
connectionStatus.setBootstrapped();
|
state.setBootstrapped();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new PluginException(e);
|
throw new PluginException(e);
|
||||||
}
|
}
|
||||||
|
state.setStarted();
|
||||||
// Check whether we're online
|
// Check whether we're online
|
||||||
updateConnectionStatus(networkManager.getNetworkStatus(),
|
updateConnectionStatus(networkManager.getNetworkStatus(),
|
||||||
batteryManager.isCharging());
|
batteryManager.isCharging());
|
||||||
@@ -278,6 +291,18 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
bind();
|
bind();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Remove after a reasonable migration period (added 2020-06-25)
|
||||||
|
private Settings migrateSettings(Settings settings) {
|
||||||
|
int network = settings.getInt(PREF_TOR_NETWORK,
|
||||||
|
DEFAULT_PREF_TOR_NETWORK);
|
||||||
|
if (network == PREF_TOR_NETWORK_NEVER) {
|
||||||
|
settings.putInt(PREF_TOR_NETWORK, DEFAULT_PREF_TOR_NETWORK);
|
||||||
|
settings.putBoolean(PREF_PLUGIN_ENABLE, false);
|
||||||
|
callback.mergeSettings(settings);
|
||||||
|
}
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean assetsAreUpToDate() {
|
private boolean assetsAreUpToDate() {
|
||||||
return doneFile.lastModified() > getLastUpdateTime();
|
return doneFile.lastModified() > getLastUpdateTime();
|
||||||
}
|
}
|
||||||
@@ -393,11 +418,11 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
tryToClose(ss, LOG, WARNING);
|
tryToClose(ss, LOG, WARNING);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!running) {
|
if (!state.setServerSocket(ss)) {
|
||||||
|
LOG.info("Closing redundant server socket");
|
||||||
tryToClose(ss, LOG, WARNING);
|
tryToClose(ss, LOG, WARNING);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
socket = ss;
|
|
||||||
// Store the port number
|
// Store the port number
|
||||||
String localPort = String.valueOf(ss.getLocalPort());
|
String localPort = String.valueOf(ss.getLocalPort());
|
||||||
Settings s = new Settings();
|
Settings s = new Settings();
|
||||||
@@ -412,7 +437,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void publishHiddenService(String port) {
|
private void publishHiddenService(String port) {
|
||||||
if (!running) return;
|
if (!state.isTorRunning()) return;
|
||||||
LOG.info("Creating hidden service");
|
LOG.info("Creating hidden service");
|
||||||
String privKey = settings.get(HS_PRIVKEY);
|
String privKey = settings.get(HS_PRIVKEY);
|
||||||
Map<Integer, String> portLines = singletonMap(80, "127.0.0.1:" + port);
|
Map<Integer, String> portLines = singletonMap(80, "127.0.0.1:" + port);
|
||||||
@@ -450,14 +475,15 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void acceptContactConnections(ServerSocket ss) {
|
private void acceptContactConnections(ServerSocket ss) {
|
||||||
while (running) {
|
while (true) {
|
||||||
Socket s;
|
Socket s;
|
||||||
try {
|
try {
|
||||||
s = ss.accept();
|
s = ss.accept();
|
||||||
s.setSoTimeout(socketTimeout);
|
s.setSoTimeout(socketTimeout);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// This is expected when the socket is closed
|
// This is expected when the server socket is closed
|
||||||
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
|
LOG.info("Server socket closed");
|
||||||
|
state.clearServerSocket(ss);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LOG.info("Connection received");
|
LOG.info("Connection received");
|
||||||
@@ -467,10 +493,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void enableNetwork(boolean enable) throws IOException {
|
protected void enableNetwork(boolean enable) throws IOException {
|
||||||
if (!running) return;
|
state.enableNetwork(enable);
|
||||||
connectionStatus.enableNetwork(enable);
|
|
||||||
controlConnection.setConf("DisableNetwork", enable ? "0" : "1");
|
controlConnection.setConf("DisableNetwork", enable ? "0" : "1");
|
||||||
if (!enable) callback.transportDisabled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enableBridges(boolean enable, boolean needsMeek)
|
private void enableBridges(boolean enable, boolean needsMeek)
|
||||||
@@ -494,9 +518,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
running = false;
|
ServerSocket ss = state.setStopped();
|
||||||
tryToClose(socket, LOG, WARNING);
|
tryToClose(ss, LOG, WARNING);
|
||||||
callback.transportDisabled();
|
|
||||||
if (controlSocket != null && controlConnection != null) {
|
if (controlSocket != null && controlConnection != null) {
|
||||||
try {
|
try {
|
||||||
LOG.info("Stopping Tor");
|
LOG.info("Stopping Tor");
|
||||||
@@ -510,8 +533,13 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isRunning() {
|
public State getState() {
|
||||||
return running && connectionStatus.isConnected();
|
return state.getState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getReasonsDisabled() {
|
||||||
|
return state.getReasonsDisabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -527,7 +555,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
public void poll(Collection<Pair<TransportProperties, ConnectionHandler>>
|
||||||
properties) {
|
properties) {
|
||||||
if (!isRunning()) return;
|
if (getState() != ACTIVE) return;
|
||||||
backoff.increment();
|
backoff.increment();
|
||||||
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
for (Pair<TransportProperties, ConnectionHandler> p : properties) {
|
||||||
connect(p.getFirst(), p.getSecond());
|
connect(p.getFirst(), p.getSecond());
|
||||||
@@ -546,7 +574,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||||
if (!isRunning()) return null;
|
if (getState() != ACTIVE) return null;
|
||||||
String bestOnion = null;
|
String bestOnion = null;
|
||||||
String onion2 = p.get(PROP_ONION_V2);
|
String onion2 = p.get(PROP_ONION_V2);
|
||||||
String onion3 = p.get(PROP_ONION_V3);
|
String onion3 = p.get(PROP_ONION_V3);
|
||||||
@@ -634,8 +662,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
new TorTransportConnection(this, s));
|
new TorTransportConnection(this, s));
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// This is expected when the socket is closed
|
// This is expected when the server socket is closed
|
||||||
if (LOG.isLoggable(INFO)) LOG.info(e.toString());
|
LOG.info("Rendezvous server socket closed");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Map<Integer, String> portLines =
|
Map<Integer, String> portLines =
|
||||||
@@ -663,10 +691,9 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void circuitStatus(String status, String id, String path) {
|
public void circuitStatus(String status, String id, String path) {
|
||||||
if (status.equals("BUILT") &&
|
if (status.equals("BUILT") &&
|
||||||
connectionStatus.getAndSetCircuitBuilt()) {
|
state.getAndSetCircuitBuilt()) {
|
||||||
LOG.info("First circuit built");
|
LOG.info("First circuit built");
|
||||||
backoff.reset();
|
backoff.reset();
|
||||||
if (isRunning()) callback.transportEnabled();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -697,9 +724,8 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
public void message(String severity, String msg) {
|
public void message(String severity, String msg) {
|
||||||
if (LOG.isLoggable(INFO)) LOG.info(severity + " " + msg);
|
if (LOG.isLoggable(INFO)) LOG.info(severity + " " + msg);
|
||||||
if (severity.equals("NOTICE") && msg.startsWith("Bootstrapped 100%")) {
|
if (severity.equals("NOTICE") && msg.startsWith("Bootstrapped 100%")) {
|
||||||
connectionStatus.setBootstrapped();
|
state.setBootstrapped();
|
||||||
backoff.reset();
|
backoff.reset();
|
||||||
if (isRunning()) callback.transportEnabled();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -736,7 +762,7 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
private void disableNetwork() {
|
private void disableNetwork() {
|
||||||
connectionStatusExecutor.execute(() -> {
|
connectionStatusExecutor.execute(() -> {
|
||||||
try {
|
try {
|
||||||
enableNetwork(false);
|
if (state.isTorRunning()) enableNetwork(false);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
logException(LOG, WARNING, ex);
|
logException(LOG, WARNING, ex);
|
||||||
}
|
}
|
||||||
@@ -746,63 +772,90 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
private void updateConnectionStatus(NetworkStatus status,
|
private void updateConnectionStatus(NetworkStatus status,
|
||||||
boolean charging) {
|
boolean charging) {
|
||||||
connectionStatusExecutor.execute(() -> {
|
connectionStatusExecutor.execute(() -> {
|
||||||
if (!running) return;
|
if (!state.isTorRunning()) return;
|
||||||
boolean online = status.isConnected();
|
boolean online = status.isConnected();
|
||||||
boolean wifi = status.isWifi();
|
boolean wifi = status.isWifi();
|
||||||
String country = locationUtils.getCurrentCountry();
|
String country = locationUtils.getCurrentCountry();
|
||||||
boolean blocked =
|
boolean blocked =
|
||||||
circumventionProvider.isTorProbablyBlocked(country);
|
circumventionProvider.isTorProbablyBlocked(country);
|
||||||
|
boolean enabledByUser = settings.getBoolean(PREF_PLUGIN_ENABLE,
|
||||||
|
DEFAULT_PREF_PLUGIN_ENABLE);
|
||||||
int network = settings.getInt(PREF_TOR_NETWORK,
|
int network = settings.getInt(PREF_TOR_NETWORK,
|
||||||
PREF_TOR_NETWORK_AUTOMATIC);
|
DEFAULT_PREF_TOR_NETWORK);
|
||||||
boolean useMobile = settings.getBoolean(PREF_TOR_MOBILE, true);
|
boolean useMobile = settings.getBoolean(PREF_TOR_MOBILE,
|
||||||
|
DEFAULT_PREF_TOR_MOBILE);
|
||||||
boolean onlyWhenCharging =
|
boolean onlyWhenCharging =
|
||||||
settings.getBoolean(PREF_TOR_ONLY_WHEN_CHARGING, false);
|
settings.getBoolean(PREF_TOR_ONLY_WHEN_CHARGING,
|
||||||
|
DEFAULT_PREF_TOR_ONLY_WHEN_CHARGING);
|
||||||
boolean bridgesWork = circumventionProvider.doBridgesWork(country);
|
boolean bridgesWork = circumventionProvider.doBridgesWork(country);
|
||||||
boolean automatic = network == PREF_TOR_NETWORK_AUTOMATIC;
|
boolean automatic = network == PREF_TOR_NETWORK_AUTOMATIC;
|
||||||
|
|
||||||
if (LOG.isLoggable(INFO)) {
|
if (LOG.isLoggable(INFO)) {
|
||||||
LOG.info("Online: " + online + ", wifi: " + wifi);
|
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);
|
else LOG.info("Country code: " + country);
|
||||||
LOG.info("Charging: " + charging);
|
LOG.info("Charging: " + charging);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
int reasonsDisabled = 0;
|
||||||
if (!online) {
|
boolean enableNetwork = false, enableBridges = false;
|
||||||
LOG.info("Disabling network, device is offline");
|
boolean useMeek = false, enableConnectionPadding = false;
|
||||||
enableNetwork(false);
|
|
||||||
} else if (!charging && onlyWhenCharging) {
|
if (!online) {
|
||||||
LOG.info("Disabling network, device is on battery");
|
LOG.info("Disabling network, device is offline");
|
||||||
enableNetwork(false);
|
} else {
|
||||||
} else if (network == PREF_TOR_NETWORK_NEVER ||
|
if (!enabledByUser) {
|
||||||
(!useMobile && !wifi)) {
|
LOG.info("User has disabled Tor");
|
||||||
LOG.info("Disabling network, device is using mobile data");
|
reasonsDisabled |= REASON_USER;
|
||||||
enableNetwork(false);
|
}
|
||||||
} else if (automatic && blocked && !bridgesWork) {
|
if (!charging && onlyWhenCharging) {
|
||||||
LOG.info("Disabling network, country is blocked");
|
LOG.info("Configured not to use battery");
|
||||||
enableNetwork(false);
|
reasonsDisabled |= REASON_BATTERY;
|
||||||
} else if (network == PREF_TOR_NETWORK_WITH_BRIDGES ||
|
}
|
||||||
(automatic && bridgesWork)) {
|
if (!useMobile && !wifi) {
|
||||||
if (circumventionProvider.needsMeek(country)) {
|
LOG.info("Configured not to use mobile data");
|
||||||
LOG.info("Enabling network, using meek bridges");
|
reasonsDisabled |= REASON_MOBILE_DATA;
|
||||||
enableBridges(true, true);
|
}
|
||||||
|
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 {
|
} else {
|
||||||
LOG.info("Enabling network, using obfs4 bridges");
|
LOG.info("Not using bridges");
|
||||||
enableBridges(true, false);
|
}
|
||||||
|
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);
|
state.setReasonsDisabled(reasonsDisabled);
|
||||||
} else {
|
|
||||||
LOG.info("Disabling connection padding");
|
try {
|
||||||
enableConnectionPadding(false);
|
if (enableNetwork) {
|
||||||
|
enableBridges(enableBridges, useMeek);
|
||||||
|
enableConnectionPadding(enableConnectionPadding);
|
||||||
}
|
}
|
||||||
|
enableNetwork(enableNetwork);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
}
|
}
|
||||||
@@ -810,33 +863,96 @@ abstract class TorPlugin implements DuplexPlugin, EventHandler, EventListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void enableConnectionPadding(boolean enable) throws IOException {
|
private void enableConnectionPadding(boolean enable) throws IOException {
|
||||||
if (!running) return;
|
|
||||||
controlConnection.setConf("ConnectionPadding", enable ? "1" : "0");
|
controlConnection.setConf("ConnectionPadding", enable ? "1" : "0");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ConnectionStatus {
|
@ThreadSafe
|
||||||
|
@NotNullByDefault
|
||||||
|
protected class PluginState {
|
||||||
|
|
||||||
// All of the following are locking: this
|
@GuardedBy("this")
|
||||||
private boolean networkEnabled = false;
|
private boolean started = false,
|
||||||
private boolean bootstrapped = false, circuitBuilt = false;
|
stopped = false,
|
||||||
|
networkInitialised = false,
|
||||||
|
networkEnabled = false,
|
||||||
|
bootstrapped = false,
|
||||||
|
circuitBuilt = false,
|
||||||
|
settingsChecked = false;
|
||||||
|
|
||||||
private synchronized void setBootstrapped() {
|
@GuardedBy("this")
|
||||||
bootstrapped = true;
|
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;
|
boolean firstCircuit = !circuitBuilt;
|
||||||
circuitBuilt = true;
|
circuitBuilt = true;
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
return firstCircuit;
|
return firstCircuit;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void enableNetwork(boolean enable) {
|
synchronized void enableNetwork(boolean enable) {
|
||||||
|
networkInitialised = true;
|
||||||
networkEnabled = enable;
|
networkEnabled = enable;
|
||||||
if (!enable) circuitBuilt = false;
|
if (!enable) circuitBuilt = false;
|
||||||
|
callback.pluginStateChanged(getState());
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized boolean isConnected() {
|
synchronized void setReasonsDisabled(int reasonsDisabled) {
|
||||||
return networkEnabled && bootstrapped && circuitBuilt;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
|||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||||
@@ -269,11 +269,11 @@ class RendezvousPollerImpl implements RendezvousPoller, Service, EventListener {
|
|||||||
} else if (e instanceof PendingContactRemovedEvent) {
|
} else if (e instanceof PendingContactRemovedEvent) {
|
||||||
PendingContactRemovedEvent p = (PendingContactRemovedEvent) e;
|
PendingContactRemovedEvent p = (PendingContactRemovedEvent) e;
|
||||||
removePendingContactAsync(p.getId());
|
removePendingContactAsync(p.getId());
|
||||||
} else if (e instanceof TransportEnabledEvent) {
|
} else if (e instanceof TransportActiveEvent) {
|
||||||
TransportEnabledEvent t = (TransportEnabledEvent) e;
|
TransportActiveEvent t = (TransportActiveEvent) e;
|
||||||
addTransportAsync(t.getTransportId());
|
addTransportAsync(t.getTransportId());
|
||||||
} else if (e instanceof TransportDisabledEvent) {
|
} else if (e instanceof TransportInactiveEvent) {
|
||||||
TransportDisabledEvent t = (TransportDisabledEvent) e;
|
TransportInactiveEvent t = (TransportInactiveEvent) e;
|
||||||
removeTransportAsync(t.getTransportId());
|
removeTransportAsync(t.getTransportId());
|
||||||
} else if (e instanceof RendezvousConnectionOpenedEvent) {
|
} else if (e instanceof RendezvousConnectionOpenedEvent) {
|
||||||
RendezvousConnectionOpenedEvent r =
|
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.lifecycle.IoExecutor;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.TorConstants;
|
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.DevConfig;
|
||||||
import org.briarproject.bramble.api.reporting.DevReporter;
|
import org.briarproject.bramble.api.reporting.DevReporter;
|
||||||
import org.briarproject.bramble.util.IoUtils;
|
import org.briarproject.bramble.util.IoUtils;
|
||||||
@@ -92,8 +92,8 @@ class DevReporterImpl implements DevReporter, EventListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void eventOccurred(Event e) {
|
public void eventOccurred(Event e) {
|
||||||
if (e instanceof TransportEnabledEvent) {
|
if (e instanceof TransportActiveEvent) {
|
||||||
TransportEnabledEvent t = (TransportEnabledEvent) e;
|
TransportActiveEvent t = (TransportActiveEvent) e;
|
||||||
if (t.getTransportId().equals(TorConstants.ID))
|
if (t.getTransportId().equals(TorConstants.ID))
|
||||||
ioExecutor.execute(this::sendReports);
|
ioExecutor.execute(this::sendReports);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|||||||
import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent;
|
import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||||
import org.briarproject.bramble.api.sync.Ack;
|
import org.briarproject.bramble.api.sync.Ack;
|
||||||
import org.briarproject.bramble.api.sync.Message;
|
import org.briarproject.bramble.api.sync.Message;
|
||||||
import org.briarproject.bramble.api.sync.Offer;
|
import org.briarproject.bramble.api.sync.Offer;
|
||||||
@@ -238,6 +239,9 @@ class DuplexOutgoingSession implements SyncSession, EventListener {
|
|||||||
} else if (e instanceof CloseSyncConnectionsEvent) {
|
} else if (e instanceof CloseSyncConnectionsEvent) {
|
||||||
CloseSyncConnectionsEvent c = (CloseSyncConnectionsEvent) e;
|
CloseSyncConnectionsEvent c = (CloseSyncConnectionsEvent) e;
|
||||||
if (c.getTransportId().equals(transportId)) interrupt();
|
if (c.getTransportId().equals(transportId)) interrupt();
|
||||||
|
} else if (e instanceof TransportInactiveEvent) {
|
||||||
|
TransportInactiveEvent t = (TransportInactiveEvent) e;
|
||||||
|
if (t.getTransportId().equals(transportId)) interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
|||||||
import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent;
|
import org.briarproject.bramble.api.lifecycle.event.LifecycleEvent;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||||
import org.briarproject.bramble.api.sync.Ack;
|
import org.briarproject.bramble.api.sync.Ack;
|
||||||
import org.briarproject.bramble.api.sync.Message;
|
import org.briarproject.bramble.api.sync.Message;
|
||||||
import org.briarproject.bramble.api.sync.SyncRecordWriter;
|
import org.briarproject.bramble.api.sync.SyncRecordWriter;
|
||||||
@@ -131,6 +132,9 @@ class SimplexOutgoingSession implements SyncSession, EventListener {
|
|||||||
} else if (e instanceof CloseSyncConnectionsEvent) {
|
} else if (e instanceof CloseSyncConnectionsEvent) {
|
||||||
CloseSyncConnectionsEvent c = (CloseSyncConnectionsEvent) e;
|
CloseSyncConnectionsEvent c = (CloseSyncConnectionsEvent) e;
|
||||||
if (c.getTransportId().equals(transportId)) interrupt();
|
if (c.getTransportId().equals(transportId)) interrupt();
|
||||||
|
} else if (e instanceof TransportInactiveEvent) {
|
||||||
|
TransportInactiveEvent t = (TransportInactiveEvent) e;
|
||||||
|
if (t.getTransportId().equals(transportId)) interrupt();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,15 +24,21 @@ class StreamReaderFactoryImpl implements StreamReaderFactory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream createStreamReader(InputStream in, StreamContext ctx) {
|
public InputStream createStreamReader(InputStream in, StreamContext ctx) {
|
||||||
return new StreamReaderImpl(
|
return new StreamReaderImpl(streamDecrypterFactory
|
||||||
streamDecrypterFactory.createStreamDecrypter(in, ctx));
|
.createStreamDecrypter(in, ctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream createContactExchangeStreamReader(InputStream in,
|
public InputStream createContactExchangeStreamReader(InputStream in,
|
||||||
SecretKey headerKey) {
|
SecretKey headerKey) {
|
||||||
return new StreamReaderImpl(
|
return new StreamReaderImpl(streamDecrypterFactory
|
||||||
streamDecrypterFactory.createContactExchangeStreamDecrypter(in,
|
.createContactExchangeStreamDecrypter(in, headerKey));
|
||||||
headerKey));
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream createLogStreamReader(InputStream in,
|
||||||
|
SecretKey headerKey) {
|
||||||
|
return new StreamReaderImpl(streamDecrypterFactory
|
||||||
|
.createLogStreamDecrypter(in, headerKey));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,15 +26,21 @@ class StreamWriterFactoryImpl implements StreamWriterFactory {
|
|||||||
@Override
|
@Override
|
||||||
public StreamWriter createStreamWriter(OutputStream out,
|
public StreamWriter createStreamWriter(OutputStream out,
|
||||||
StreamContext ctx) {
|
StreamContext ctx) {
|
||||||
return new StreamWriterImpl(
|
return new StreamWriterImpl(streamEncrypterFactory
|
||||||
streamEncrypterFactory.createStreamEncrypter(out, ctx));
|
.createStreamEncrypter(out, ctx));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StreamWriter createContactExchangeStreamWriter(OutputStream out,
|
public StreamWriter createContactExchangeStreamWriter(OutputStream out,
|
||||||
SecretKey headerKey) {
|
SecretKey headerKey) {
|
||||||
return new StreamWriterImpl(
|
return new StreamWriterImpl(streamEncrypterFactory
|
||||||
streamEncrypterFactory.createContactExchangeStreamDecrypter(out,
|
.createContactExchangeStreamEncrypter(out, headerKey));
|
||||||
headerKey));
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StreamWriter createLogStreamWriter(OutputStream out,
|
||||||
|
SecretKey headerKey) {
|
||||||
|
return new StreamWriterImpl(streamEncrypterFactory
|
||||||
|
.createLogStreamEncrypter(out, headerKey));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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.duplex.DuplexTransportConnection;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionClosedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
import org.briarproject.bramble.api.plugin.event.ConnectionOpenedEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||||
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
import org.briarproject.bramble.api.plugin.simplex.SimplexPlugin;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
import org.briarproject.bramble.api.properties.TransportPropertyManager;
|
||||||
@@ -331,7 +331,7 @@ public class PollerImplTest extends BrambleMockTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPollsOnTransportEnabled() throws Exception {
|
public void testPollsOnTransportActivated() throws Exception {
|
||||||
DuplexPlugin plugin = context.mock(DuplexPlugin.class);
|
DuplexPlugin plugin = context.mock(DuplexPlugin.class);
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
@@ -370,7 +370,7 @@ public class PollerImplTest extends BrambleMockTestCase {
|
|||||||
pairOf(equal(properties), any(ConnectionHandler.class)))));
|
pairOf(equal(properties), any(ConnectionHandler.class)))));
|
||||||
}});
|
}});
|
||||||
|
|
||||||
poller.eventOccurred(new TransportEnabledEvent(transportId));
|
poller.eventOccurred(new TransportActiveEvent(transportId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -411,11 +411,11 @@ public class PollerImplTest extends BrambleMockTestCase {
|
|||||||
// All contacts are connected, so don't poll the plugin
|
// All contacts are connected, so don't poll the plugin
|
||||||
}});
|
}});
|
||||||
|
|
||||||
poller.eventOccurred(new TransportEnabledEvent(transportId));
|
poller.eventOccurred(new TransportActiveEvent(transportId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCancelsPollingOnTransportDisabled() {
|
public void testCancelsPollingOnTransportDeactivated() {
|
||||||
Plugin plugin = context.mock(Plugin.class);
|
Plugin plugin = context.mock(Plugin.class);
|
||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
@@ -433,12 +433,12 @@ public class PollerImplTest extends BrambleMockTestCase {
|
|||||||
oneOf(scheduler).schedule(with(any(Runnable.class)), with(0L),
|
oneOf(scheduler).schedule(with(any(Runnable.class)), with(0L),
|
||||||
with(MILLISECONDS));
|
with(MILLISECONDS));
|
||||||
will(returnValue(future));
|
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);
|
oneOf(future).cancel(false);
|
||||||
}});
|
}});
|
||||||
|
|
||||||
poller.eventOccurred(new TransportEnabledEvent(transportId));
|
poller.eventOccurred(new TransportActiveEvent(transportId));
|
||||||
poller.eventOccurred(new TransportDisabledEvent(transportId));
|
poller.eventOccurred(new TransportInactiveEvent(transportId));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void expectReschedule(Plugin plugin) {
|
private void expectReschedule(Plugin plugin) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.data.BdfList;
|
|||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.Backoff;
|
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.PluginCallback;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
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 java.util.concurrent.TimeUnit.SECONDS;
|
||||||
import static org.briarproject.bramble.api.keyagreement.KeyAgreementConstants.COMMIT_LENGTH;
|
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.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.briarproject.bramble.plugin.tcp.LanTcpPlugin.areAddressesInSameNetwork;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
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 propertiesLatch = new CountDownLatch(2);
|
||||||
private final CountDownLatch connectionsLatch = new CountDownLatch(1);
|
private final CountDownLatch connectionsLatch = new CountDownLatch(1);
|
||||||
private final TransportProperties local = new TransportProperties();
|
private final TransportProperties local = new TransportProperties();
|
||||||
|
private final Settings settings = new Settings();
|
||||||
|
|
||||||
|
private Callback() {
|
||||||
|
settings.putBoolean(PREF_PLUGIN_ENABLE, true);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Settings getSettings() {
|
public Settings getSettings() {
|
||||||
return new Settings();
|
return settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -324,11 +331,7 @@ public class LanTcpPluginTest extends BrambleTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void transportEnabled() {
|
public void pluginStateChanged(State newState) {
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void transportDisabled() {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -17,8 +17,8 @@ import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
|||||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
import org.briarproject.bramble.api.plugin.duplex.DuplexPlugin;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportDisabledEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportActiveEvent;
|
||||||
import org.briarproject.bramble.api.plugin.event.TransportEnabledEvent;
|
import org.briarproject.bramble.api.plugin.event.TransportInactiveEvent;
|
||||||
import org.briarproject.bramble.api.properties.TransportProperties;
|
import org.briarproject.bramble.api.properties.TransportProperties;
|
||||||
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
import org.briarproject.bramble.api.rendezvous.KeyMaterialSource;
|
||||||
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
import org.briarproject.bramble.api.rendezvous.RendezvousEndpoint;
|
||||||
@@ -178,10 +178,10 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
|||||||
rendezvousPoller.startService();
|
rendezvousPoller.startService();
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Enable the transport - no endpoints should be created yet
|
// Activate the transport - no endpoints should be created yet
|
||||||
expectGetPlugin();
|
expectGetPlugin();
|
||||||
|
|
||||||
rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId));
|
rendezvousPoller.eventOccurred(new TransportActiveEvent(transportId));
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Add the pending contact - endpoint should be created and polled
|
// Add the pending contact - endpoint should be created and polled
|
||||||
@@ -212,8 +212,8 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
|||||||
new PendingContactRemovedEvent(pendingContact.getId()));
|
new PendingContactRemovedEvent(pendingContact.getId()));
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Disable the transport - endpoint is already closed
|
// Deactivate the transport - endpoint is already closed
|
||||||
rendezvousPoller.eventOccurred(new TransportDisabledEvent(transportId));
|
rendezvousPoller.eventOccurred(new TransportInactiveEvent(transportId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -230,10 +230,10 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
|||||||
rendezvousPoller.startService();
|
rendezvousPoller.startService();
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Enable the transport - no endpoints should be created yet
|
// Activate the transport - no endpoints should be created yet
|
||||||
expectGetPlugin();
|
expectGetPlugin();
|
||||||
|
|
||||||
rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId));
|
rendezvousPoller.eventOccurred(new TransportActiveEvent(transportId));
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Add the pending contact - endpoint should be created and polled
|
// Add the pending contact - endpoint should be created and polled
|
||||||
@@ -269,12 +269,12 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
|||||||
new PendingContactRemovedEvent(pendingContact.getId()));
|
new PendingContactRemovedEvent(pendingContact.getId()));
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Disable the transport - endpoint is already closed
|
// Deactivate the transport - endpoint is already closed
|
||||||
rendezvousPoller.eventOccurred(new TransportDisabledEvent(transportId));
|
rendezvousPoller.eventOccurred(new TransportInactiveEvent(transportId));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreatesAndClosesEndpointsWhenTransportIsEnabledAndDisabled()
|
public void testCreatesAndClosesEndpointsWhenTransportIsActivatedAndDeactivated()
|
||||||
throws Exception {
|
throws Exception {
|
||||||
long beforeExpiry = pendingContact.getTimestamp();
|
long beforeExpiry = pendingContact.getTimestamp();
|
||||||
|
|
||||||
@@ -292,19 +292,19 @@ public class RendezvousPollerImplTest extends BrambleMockTestCase {
|
|||||||
new PendingContactAddedEvent(pendingContact));
|
new PendingContactAddedEvent(pendingContact));
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Enable the transport - endpoint should be created
|
// Activate the transport - endpoint should be created
|
||||||
expectGetPlugin();
|
expectGetPlugin();
|
||||||
expectCreateEndpoint();
|
expectCreateEndpoint();
|
||||||
expectStateChangedEvent(WAITING_FOR_CONNECTION);
|
expectStateChangedEvent(WAITING_FOR_CONNECTION);
|
||||||
|
|
||||||
rendezvousPoller.eventOccurred(new TransportEnabledEvent(transportId));
|
rendezvousPoller.eventOccurred(new TransportActiveEvent(transportId));
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Disable the transport - endpoint should be closed
|
// Deactivate the transport - endpoint should be closed
|
||||||
expectCloseEndpoint();
|
expectCloseEndpoint();
|
||||||
expectStateChangedEvent(OFFLINE);
|
expectStateChangedEvent(OFFLINE);
|
||||||
|
|
||||||
rendezvousPoller.eventOccurred(new TransportDisabledEvent(transportId));
|
rendezvousPoller.eventOccurred(new TransportInactiveEvent(transportId));
|
||||||
context.assertIsSatisfied();
|
context.assertIsSatisfied();
|
||||||
|
|
||||||
// Remove the pending contact - endpoint is already closed
|
// Remove the pending contact - endpoint is already closed
|
||||||
|
|||||||
@@ -45,9 +45,9 @@ public class DesktopPluginModule extends PluginModule {
|
|||||||
ioExecutor, random, eventBus, timeoutMonitor, backoffFactory);
|
ioExecutor, random, eventBus, timeoutMonitor, backoffFactory);
|
||||||
DuplexPluginFactory modem = new ModemPluginFactory(ioExecutor,
|
DuplexPluginFactory modem = new ModemPluginFactory(ioExecutor,
|
||||||
reliabilityFactory);
|
reliabilityFactory);
|
||||||
DuplexPluginFactory lan = new LanTcpPluginFactory(ioExecutor,
|
DuplexPluginFactory lan = new LanTcpPluginFactory(ioExecutor, eventBus,
|
||||||
backoffFactory);
|
backoffFactory);
|
||||||
DuplexPluginFactory wan = new WanTcpPluginFactory(ioExecutor,
|
DuplexPluginFactory wan = new WanTcpPluginFactory(ioExecutor, eventBus,
|
||||||
backoffFactory, shutdownManager);
|
backoffFactory, shutdownManager);
|
||||||
Collection<DuplexPluginFactory> duplex =
|
Collection<DuplexPluginFactory> duplex =
|
||||||
asList(bluetooth, modem, lan, wan);
|
asList(bluetooth, modem, lan, wan);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import org.briarproject.bramble.api.Pair;
|
|||||||
import org.briarproject.bramble.api.data.BdfList;
|
import org.briarproject.bramble.api.data.BdfList;
|
||||||
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
import org.briarproject.bramble.api.keyagreement.KeyAgreementListener;
|
||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
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.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
import org.briarproject.bramble.api.plugin.ConnectionHandler;
|
||||||
import org.briarproject.bramble.api.plugin.PluginCallback;
|
import org.briarproject.bramble.api.plugin.PluginCallback;
|
||||||
@@ -23,9 +24,16 @@ import java.util.Collection;
|
|||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.logging.Logger;
|
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.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.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.LogUtils.logException;
|
||||||
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
||||||
|
|
||||||
@@ -44,8 +52,8 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
|||||||
private final PluginCallback callback;
|
private final PluginCallback callback;
|
||||||
private final int maxLatency;
|
private final int maxLatency;
|
||||||
private final AtomicBoolean used = new AtomicBoolean(false);
|
private final AtomicBoolean used = new AtomicBoolean(false);
|
||||||
|
private final PluginState state = new PluginState();
|
||||||
|
|
||||||
private volatile boolean running = false;
|
|
||||||
private volatile Modem modem = null;
|
private volatile Modem modem = null;
|
||||||
|
|
||||||
ModemPlugin(ModemFactory modemFactory, SerialPortList serialPortList,
|
ModemPlugin(ModemFactory modemFactory, SerialPortList serialPortList,
|
||||||
@@ -75,6 +83,7 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
|||||||
@Override
|
@Override
|
||||||
public void start() throws PluginException {
|
public void start() throws PluginException {
|
||||||
if (used.getAndSet(true)) throw new IllegalStateException();
|
if (used.getAndSet(true)) throw new IllegalStateException();
|
||||||
|
state.setStarted();
|
||||||
for (String portName : serialPortList.getPortNames()) {
|
for (String portName : serialPortList.getPortNames()) {
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Trying to initialise modem on " + portName);
|
LOG.info("Trying to initialise modem on " + portName);
|
||||||
@@ -83,18 +92,20 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
|||||||
if (!modem.start()) continue;
|
if (!modem.start()) continue;
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Initialised modem on " + portName);
|
LOG.info("Initialised modem on " + portName);
|
||||||
running = true;
|
state.setInitialised();
|
||||||
return;
|
return;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
LOG.warning("Failed to initialised modem");
|
||||||
|
state.setFailed();
|
||||||
throw new PluginException();
|
throw new PluginException();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
running = false;
|
state.setStopped();
|
||||||
if (modem != null) {
|
if (modem != null) {
|
||||||
try {
|
try {
|
||||||
modem.stop();
|
modem.stop();
|
||||||
@@ -105,8 +116,13 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isRunning() {
|
public State getState() {
|
||||||
return running;
|
return state.getState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getReasonsDisabled() {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -125,8 +141,8 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
|||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean resetModem() {
|
private void resetModem() {
|
||||||
if (!running) return false;
|
if (getState() != ACTIVE) return;
|
||||||
for (String portName : serialPortList.getPortNames()) {
|
for (String portName : serialPortList.getPortNames()) {
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Trying to initialise modem on " + portName);
|
LOG.info("Trying to initialise modem on " + portName);
|
||||||
@@ -135,18 +151,18 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
|||||||
if (!modem.start()) continue;
|
if (!modem.start()) continue;
|
||||||
if (LOG.isLoggable(INFO))
|
if (LOG.isLoggable(INFO))
|
||||||
LOG.info("Initialised modem on " + portName);
|
LOG.info("Initialised modem on " + portName);
|
||||||
return true;
|
return;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logException(LOG, WARNING, e);
|
logException(LOG, WARNING, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
running = false;
|
LOG.warning("Failed to initialise modem");
|
||||||
return false;
|
state.setFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DuplexTransportConnection createConnection(TransportProperties p) {
|
public DuplexTransportConnection createConnection(TransportProperties p) {
|
||||||
if (!running) return null;
|
if (getState() != ACTIVE) return null;
|
||||||
// Get the ISO 3166 code for the caller's country
|
// Get the ISO 3166 code for the caller's country
|
||||||
String fromIso = callback.getLocalProperties().get("iso3166");
|
String fromIso = callback.getLocalProperties().get("iso3166");
|
||||||
if (isNullOrEmpty(fromIso)) return null;
|
if (isNullOrEmpty(fromIso)) return null;
|
||||||
@@ -232,4 +248,41 @@ class ModemPlugin implements DuplexPlugin, Modem.Callback {
|
|||||||
if (exception) resetModem();
|
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 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.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
@@ -33,6 +35,7 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
|||||||
@Test
|
@Test
|
||||||
public void testModemCreation() throws Exception {
|
public void testModemCreation() throws Exception {
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
|
oneOf(callback).pluginStateChanged(ENABLING);
|
||||||
oneOf(serialPortList).getPortNames();
|
oneOf(serialPortList).getPortNames();
|
||||||
will(returnValue(new String[] {"foo", "bar", "baz"}));
|
will(returnValue(new String[] {"foo", "bar", "baz"}));
|
||||||
// First call to createModem() returns false
|
// First call to createModem() returns false
|
||||||
@@ -50,6 +53,7 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
|||||||
will(returnValue(modem));
|
will(returnValue(modem));
|
||||||
oneOf(modem).start();
|
oneOf(modem).start();
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
|
oneOf(callback).pluginStateChanged(ACTIVE);
|
||||||
}});
|
}});
|
||||||
|
|
||||||
plugin.start();
|
plugin.start();
|
||||||
@@ -65,12 +69,14 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
// start()
|
// start()
|
||||||
|
oneOf(callback).pluginStateChanged(ENABLING);
|
||||||
oneOf(serialPortList).getPortNames();
|
oneOf(serialPortList).getPortNames();
|
||||||
will(returnValue(new String[] {"foo"}));
|
will(returnValue(new String[] {"foo"}));
|
||||||
oneOf(modemFactory).createModem(plugin, "foo");
|
oneOf(modemFactory).createModem(plugin, "foo");
|
||||||
will(returnValue(modem));
|
will(returnValue(modem));
|
||||||
oneOf(modem).start();
|
oneOf(modem).start();
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
|
oneOf(callback).pluginStateChanged(ACTIVE);
|
||||||
// createConnection()
|
// createConnection()
|
||||||
oneOf(callback).getLocalProperties();
|
oneOf(callback).getLocalProperties();
|
||||||
will(returnValue(local));
|
will(returnValue(local));
|
||||||
@@ -93,12 +99,14 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
// start()
|
// start()
|
||||||
|
oneOf(callback).pluginStateChanged(ENABLING);
|
||||||
oneOf(serialPortList).getPortNames();
|
oneOf(serialPortList).getPortNames();
|
||||||
will(returnValue(new String[] {"foo"}));
|
will(returnValue(new String[] {"foo"}));
|
||||||
oneOf(modemFactory).createModem(plugin, "foo");
|
oneOf(modemFactory).createModem(plugin, "foo");
|
||||||
will(returnValue(modem));
|
will(returnValue(modem));
|
||||||
oneOf(modem).start();
|
oneOf(modem).start();
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
|
oneOf(callback).pluginStateChanged(ACTIVE);
|
||||||
// createConnection()
|
// createConnection()
|
||||||
oneOf(callback).getLocalProperties();
|
oneOf(callback).getLocalProperties();
|
||||||
will(returnValue(local));
|
will(returnValue(local));
|
||||||
@@ -121,12 +129,14 @@ public class ModemPluginTest extends BrambleMockTestCase {
|
|||||||
|
|
||||||
context.checking(new Expectations() {{
|
context.checking(new Expectations() {{
|
||||||
// start()
|
// start()
|
||||||
|
oneOf(callback).pluginStateChanged(ENABLING);
|
||||||
oneOf(serialPortList).getPortNames();
|
oneOf(serialPortList).getPortNames();
|
||||||
will(returnValue(new String[] {"foo"}));
|
will(returnValue(new String[] {"foo"}));
|
||||||
oneOf(modemFactory).createModem(plugin, "foo");
|
oneOf(modemFactory).createModem(plugin, "foo");
|
||||||
will(returnValue(modem));
|
will(returnValue(modem));
|
||||||
oneOf(modem).start();
|
oneOf(modem).start();
|
||||||
will(returnValue(true));
|
will(returnValue(true));
|
||||||
|
oneOf(callback).pluginStateChanged(ACTIVE);
|
||||||
// createConnection()
|
// createConnection()
|
||||||
oneOf(callback).getLocalProperties();
|
oneOf(callback).getLocalProperties();
|
||||||
will(returnValue(local));
|
will(returnValue(local));
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ import javax.net.SocketFactory;
|
|||||||
import static java.util.Collections.singletonList;
|
import static java.util.Collections.singletonList;
|
||||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||||
import static java.util.logging.Logger.getLogger;
|
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.deleteTestDirectory;
|
||||||
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
import static org.briarproject.bramble.test.TestUtils.getTestDirectory;
|
||||||
import static org.briarproject.bramble.test.TestUtils.isOptionalTestEnabled;
|
import static org.briarproject.bramble.test.TestUtils.isOptionalTestEnabled;
|
||||||
@@ -141,10 +142,10 @@ public class BridgeTest extends BrambleTestCase {
|
|||||||
plugin.start();
|
plugin.start();
|
||||||
long start = clock.currentTimeMillis();
|
long start = clock.currentTimeMillis();
|
||||||
while (clock.currentTimeMillis() - start < TIMEOUT) {
|
while (clock.currentTimeMillis() - start < TIMEOUT) {
|
||||||
if (plugin.isRunning()) return;
|
if (plugin.getState() == ACTIVE) return;
|
||||||
clock.sleep(500);
|
clock.sleep(500);
|
||||||
}
|
}
|
||||||
if (!plugin.isRunning()) {
|
if (plugin.getState() != ACTIVE) {
|
||||||
fail("Could not connect to Tor within timeout.");
|
fail("Could not connect to Tor within timeout.");
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package org.briarproject.bramble.plugin.tor;
|
package org.briarproject.bramble.plugin.tor;
|
||||||
|
|
||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
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.PluginCallback;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
import org.briarproject.bramble.api.plugin.TransportConnectionReader;
|
||||||
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
import org.briarproject.bramble.api.plugin.TransportConnectionWriter;
|
||||||
@@ -30,11 +31,7 @@ public class TestPluginCallback implements PluginCallback {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void transportEnabled() {
|
public void pluginStateChanged(State state) {
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void transportDisabled() {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ android {
|
|||||||
defaultConfig {
|
defaultConfig {
|
||||||
minSdkVersion 16
|
minSdkVersion 16
|
||||||
targetSdkVersion 28
|
targetSdkVersion 28
|
||||||
versionCode 10207
|
versionCode 10209
|
||||||
versionName "1.2.7"
|
versionName "1.2.9"
|
||||||
applicationId "org.briarproject.briar.android"
|
applicationId "org.briarproject.briar.android"
|
||||||
buildConfigField "String", "GitHash",
|
buildConfigField "String", "GitHash",
|
||||||
"\"${getStdout(['git', 'rev-parse', '--short=7', 'HEAD'], 'No commit hash')}\""
|
"\"${getStdout(['git', 'rev-parse', '--short=7', 'HEAD'], 'No commit hash')}\""
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import android.content.SharedPreferences;
|
|||||||
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
import org.briarproject.bramble.api.crypto.CryptoComponent;
|
||||||
import org.briarproject.bramble.api.db.DatabaseConfig;
|
import org.briarproject.bramble.api.db.DatabaseConfig;
|
||||||
import org.briarproject.bramble.api.identity.IdentityManager;
|
import org.briarproject.bramble.api.identity.IdentityManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.Localizer;
|
import org.briarproject.briar.android.Localizer;
|
||||||
import org.briarproject.briar.android.util.UiUtils;
|
import org.briarproject.briar.android.util.UiUtils;
|
||||||
@@ -17,8 +18,8 @@ class BriarAccountManager extends AndroidAccountManager {
|
|||||||
@Inject
|
@Inject
|
||||||
BriarAccountManager(DatabaseConfig databaseConfig, CryptoComponent crypto,
|
BriarAccountManager(DatabaseConfig databaseConfig, CryptoComponent crypto,
|
||||||
IdentityManager identityManager, SharedPreferences prefs,
|
IdentityManager identityManager, SharedPreferences prefs,
|
||||||
Application app) {
|
PersistentLogManager logManager, Application app) {
|
||||||
super(databaseConfig, crypto, identityManager, prefs, app);
|
super(databaseConfig, crypto, identityManager, prefs, logManager, app);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import org.briarproject.bramble.api.keyagreement.PayloadEncoder;
|
|||||||
import org.briarproject.bramble.api.keyagreement.PayloadParser;
|
import org.briarproject.bramble.api.keyagreement.PayloadParser;
|
||||||
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
import org.briarproject.bramble.api.lifecycle.LifecycleManager;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
import org.briarproject.bramble.api.plugin.PluginManager;
|
import org.briarproject.bramble.api.plugin.PluginManager;
|
||||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||||
import org.briarproject.bramble.api.system.AndroidExecutor;
|
import org.briarproject.bramble.api.system.AndroidExecutor;
|
||||||
@@ -56,6 +57,7 @@ import org.briarproject.briar.api.privategroup.invitation.GroupInvitationManager
|
|||||||
import org.briarproject.briar.api.test.TestDataCreator;
|
import org.briarproject.briar.api.test.TestDataCreator;
|
||||||
|
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.logging.Formatter;
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
@@ -165,6 +167,10 @@ public interface AndroidComponent
|
|||||||
|
|
||||||
FeatureFlags featureFlags();
|
FeatureFlags featureFlags();
|
||||||
|
|
||||||
|
PersistentLogManager persistentLogManager();
|
||||||
|
|
||||||
|
Formatter formatter();
|
||||||
|
|
||||||
void inject(SignInReminderReceiver briarService);
|
void inject(SignInReminderReceiver briarService);
|
||||||
|
|
||||||
void inject(BriarService briarService);
|
void inject(BriarService briarService);
|
||||||
|
|||||||
@@ -164,7 +164,7 @@ class AndroidNotificationManagerImpl implements AndroidNotificationManager,
|
|||||||
nc.setLockscreenVisibility(VISIBILITY_SECRET);
|
nc.setLockscreenVisibility(VISIBILITY_SECRET);
|
||||||
nc.enableVibration(true);
|
nc.enableVibration(true);
|
||||||
nc.enableLights(true);
|
nc.enableLights(true);
|
||||||
nc.setLightColor(getColor(appContext, R.color.briar_green_light));
|
nc.setLightColor(getColor(appContext, R.color.briar_lime_400));
|
||||||
notificationManager.createNotificationChannel(nc);
|
notificationManager.createNotificationChannel(nc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import org.briarproject.bramble.util.StringUtils;
|
|||||||
import org.briarproject.briar.android.account.LockManagerImpl;
|
import org.briarproject.briar.android.account.LockManagerImpl;
|
||||||
import org.briarproject.briar.android.keyagreement.ContactExchangeModule;
|
import org.briarproject.briar.android.keyagreement.ContactExchangeModule;
|
||||||
import org.briarproject.briar.android.login.LoginModule;
|
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.android.viewmodel.ViewModelModule;
|
||||||
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
import org.briarproject.briar.api.android.AndroidNotificationManager;
|
||||||
import org.briarproject.briar.api.android.DozeWatchdog;
|
import org.briarproject.briar.api.android.DozeWatchdog;
|
||||||
@@ -76,6 +77,7 @@ import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
|||||||
@Module(includes = {
|
@Module(includes = {
|
||||||
ContactExchangeModule.class,
|
ContactExchangeModule.class,
|
||||||
LoginModule.class,
|
LoginModule.class,
|
||||||
|
NavDrawerModule.class,
|
||||||
ViewModelModule.class
|
ViewModelModule.class
|
||||||
})
|
})
|
||||||
public class AppModule {
|
public class AppModule {
|
||||||
@@ -134,8 +136,8 @@ public class AppModule {
|
|||||||
TimeoutMonitor timeoutMonitor) {
|
TimeoutMonitor timeoutMonitor) {
|
||||||
Context appContext = app.getApplicationContext();
|
Context appContext = app.getApplicationContext();
|
||||||
DuplexPluginFactory bluetooth = new AndroidBluetoothPluginFactory(
|
DuplexPluginFactory bluetooth = new AndroidBluetoothPluginFactory(
|
||||||
ioExecutor, androidExecutor, appContext, random, eventBus,
|
ioExecutor, scheduler, androidExecutor, appContext, random,
|
||||||
clock, timeoutMonitor, backoffFactory);
|
eventBus, clock, timeoutMonitor, backoffFactory);
|
||||||
DuplexPluginFactory tor = new AndroidTorPluginFactory(ioExecutor,
|
DuplexPluginFactory tor = new AndroidTorPluginFactory(ioExecutor,
|
||||||
scheduler, appContext, networkManager, locationUtils, eventBus,
|
scheduler, appContext, networkManager, locationUtils, eventBus,
|
||||||
torSocketFactory, backoffFactory, resourceProvider,
|
torSocketFactory, backoffFactory, resourceProvider,
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import org.acra.ReportingInteractionMode;
|
|||||||
import org.acra.annotation.ReportsCrashes;
|
import org.acra.annotation.ReportsCrashes;
|
||||||
import org.briarproject.bramble.BrambleAndroidEagerSingletons;
|
import org.briarproject.bramble.BrambleAndroidEagerSingletons;
|
||||||
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
import org.briarproject.bramble.BrambleCoreEagerSingletons;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
import org.briarproject.briar.BriarCoreEagerSingletons;
|
import org.briarproject.briar.BriarCoreEagerSingletons;
|
||||||
import org.briarproject.briar.BuildConfig;
|
import org.briarproject.briar.BuildConfig;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
@@ -28,6 +29,8 @@ import org.briarproject.briar.android.reporting.BriarReportSenderFactory;
|
|||||||
import org.briarproject.briar.android.reporting.DevReportActivity;
|
import org.briarproject.briar.android.reporting.DevReportActivity;
|
||||||
import org.briarproject.briar.android.util.UiUtils;
|
import org.briarproject.briar.android.util.UiUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.logging.Handler;
|
import java.util.logging.Handler;
|
||||||
import java.util.logging.LogRecord;
|
import java.util.logging.LogRecord;
|
||||||
@@ -36,6 +39,7 @@ import java.util.logging.Logger;
|
|||||||
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
|
import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND;
|
||||||
import static java.util.logging.Level.FINE;
|
import static java.util.logging.Level.FINE;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.acra.ReportField.ANDROID_VERSION;
|
import static org.acra.ReportField.ANDROID_VERSION;
|
||||||
import static org.acra.ReportField.APP_VERSION_CODE;
|
import static org.acra.ReportField.APP_VERSION_CODE;
|
||||||
@@ -54,6 +58,7 @@ import static org.acra.ReportField.REPORT_ID;
|
|||||||
import static org.acra.ReportField.STACK_TRACE;
|
import static org.acra.ReportField.STACK_TRACE;
|
||||||
import static org.acra.ReportField.USER_APP_START_DATE;
|
import static org.acra.ReportField.USER_APP_START_DATE;
|
||||||
import static org.acra.ReportField.USER_CRASH_DATE;
|
import static org.acra.ReportField.USER_CRASH_DATE;
|
||||||
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||||
|
|
||||||
@ReportsCrashes(
|
@ReportsCrashes(
|
||||||
@@ -120,9 +125,19 @@ public class BriarApplicationImpl extends Application
|
|||||||
rootLogger.addHandler(logHandler);
|
rootLogger.addHandler(logHandler);
|
||||||
rootLogger.setLevel(IS_DEBUG_BUILD ? FINE : INFO);
|
rootLogger.setLevel(IS_DEBUG_BUILD ? FINE : INFO);
|
||||||
|
|
||||||
|
applicationComponent = createApplicationComponent();
|
||||||
|
|
||||||
|
PersistentLogManager logManager =
|
||||||
|
applicationComponent.persistentLogManager();
|
||||||
|
File logDir = getDir("log", MODE_PRIVATE);
|
||||||
|
try {
|
||||||
|
rootLogger.addHandler(logManager.createLogHandler(logDir));
|
||||||
|
} catch (IOException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
}
|
||||||
|
|
||||||
LOG.info("Created");
|
LOG.info("Created");
|
||||||
|
|
||||||
applicationComponent = createApplicationComponent();
|
|
||||||
EmojiManager.install(new GoogleEmojiProvider());
|
EmojiManager.install(new GoogleEmojiProvider());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,8 +8,6 @@ import org.briarproject.briar.android.controller.BriarController;
|
|||||||
import org.briarproject.briar.android.controller.BriarControllerImpl;
|
import org.briarproject.briar.android.controller.BriarControllerImpl;
|
||||||
import org.briarproject.briar.android.controller.DbController;
|
import org.briarproject.briar.android.controller.DbController;
|
||||||
import org.briarproject.briar.android.controller.DbControllerImpl;
|
import org.briarproject.briar.android.controller.DbControllerImpl;
|
||||||
import org.briarproject.briar.android.navdrawer.NavDrawerController;
|
|
||||||
import org.briarproject.briar.android.navdrawer.NavDrawerControllerImpl;
|
|
||||||
|
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
import dagger.Provides;
|
import dagger.Provides;
|
||||||
@@ -58,14 +56,6 @@ public class ActivityModule {
|
|||||||
return dbController;
|
return dbController;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ActivityScope
|
|
||||||
@Provides
|
|
||||||
NavDrawerController provideNavDrawerController(
|
|
||||||
NavDrawerControllerImpl navDrawerController) {
|
|
||||||
activity.addLifecycleController(navDrawerController);
|
|
||||||
return navDrawerController;
|
|
||||||
}
|
|
||||||
|
|
||||||
@ActivityScope
|
@ActivityScope
|
||||||
@Provides
|
@Provides
|
||||||
BriarServiceConnection provideBriarServiceConnection() {
|
BriarServiceConnection provideBriarServiceConnection() {
|
||||||
|
|||||||
@@ -16,5 +16,7 @@ public interface RequestCodes {
|
|||||||
int REQUEST_KEYGUARD_UNLOCK = 12;
|
int REQUEST_KEYGUARD_UNLOCK = 12;
|
||||||
int REQUEST_ATTACH_IMAGE = 13;
|
int REQUEST_ATTACH_IMAGE = 13;
|
||||||
int REQUEST_SAVE_ATTACHMENT = 14;
|
int REQUEST_SAVE_ATTACHMENT = 14;
|
||||||
|
int REQUEST_EXPORT_LOG = 15;
|
||||||
|
int REQUEST_EXPORT_OLD_LOG = 16;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.briarproject.briar.android.contact.add.remote;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface.OnClickListener;
|
import android.content.DialogInterface.OnClickListener;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@@ -38,12 +37,10 @@ import androidx.lifecycle.ViewModelProviders;
|
|||||||
import static android.view.View.INVISIBLE;
|
import static android.view.View.INVISIBLE;
|
||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
import static android.widget.Toast.LENGTH_LONG;
|
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 java.util.Objects.requireNonNull;
|
||||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||||
import static org.briarproject.bramble.util.StringUtils.utf8IsTooLong;
|
import static org.briarproject.bramble.util.StringUtils.utf8IsTooLong;
|
||||||
|
import static org.briarproject.briar.android.util.UiUtils.getDialogIcon;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
@@ -199,9 +196,7 @@ public class NicknameFragment extends BaseFragment {
|
|||||||
private void showWarningDialog(String name1, String name2) {
|
private void showWarningDialog(String name1, String name2) {
|
||||||
Context ctx = requireContext();
|
Context ctx = requireContext();
|
||||||
Builder b = new Builder(ctx, R.style.BriarDialogTheme);
|
Builder b = new Builder(ctx, R.style.BriarDialogTheme);
|
||||||
Drawable icon = getDrawable(ctx, R.drawable.alerts_and_states_error);
|
b.setIcon(getDialogIcon(ctx, R.drawable.alerts_and_states_error));
|
||||||
setTint(requireNonNull(icon), getColor(ctx, R.color.color_primary));
|
|
||||||
b.setIcon(icon);
|
|
||||||
b.setTitle(getString(R.string.duplicate_link_dialog_title));
|
b.setTitle(getString(R.string.duplicate_link_dialog_title));
|
||||||
b.setMessage(
|
b.setMessage(
|
||||||
getString(R.string.duplicate_link_dialog_text_3, name1, name2));
|
getString(R.string.duplicate_link_dialog_text_3, name1, name2));
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ public class PendingContactListActivity extends BriarActivity
|
|||||||
list.showProgressBar();
|
list.showProgressBar();
|
||||||
|
|
||||||
offlineSnackbar = new BriarSnackbarBuilder()
|
offlineSnackbar = new BriarSnackbarBuilder()
|
||||||
.setBackgroundColor(R.color.briar_red)
|
.setBackgroundColor(R.color.briar_red_500)
|
||||||
.make(list, R.string.offline_state, LENGTH_INDEFINITE);
|
.make(list, R.string.offline_state, LENGTH_INDEFINITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,11 +46,11 @@ class PendingContactViewHolder extends ViewHolder {
|
|||||||
});
|
});
|
||||||
|
|
||||||
int color = ContextCompat
|
int color = ContextCompat
|
||||||
.getColor(status.getContext(), R.color.briar_green);
|
.getColor(status.getContext(), R.color.briar_lime_600);
|
||||||
switch (item.getState()) {
|
switch (item.getState()) {
|
||||||
case WAITING_FOR_CONNECTION:
|
case WAITING_FOR_CONNECTION:
|
||||||
color = ContextCompat
|
color = ContextCompat.getColor(status.getContext(),
|
||||||
.getColor(status.getContext(), R.color.briar_yellow);
|
R.color.briar_orange_500);
|
||||||
status.setText(R.string.waiting_for_contact_to_come_online);
|
status.setText(R.string.waiting_for_contact_to_come_online);
|
||||||
break;
|
break;
|
||||||
case OFFLINE:
|
case OFFLINE:
|
||||||
@@ -64,7 +64,7 @@ class PendingContactViewHolder extends ViewHolder {
|
|||||||
break;
|
break;
|
||||||
case FAILED:
|
case FAILED:
|
||||||
color = ContextCompat
|
color = ContextCompat
|
||||||
.getColor(status.getContext(), R.color.briar_red);
|
.getColor(status.getContext(), R.color.briar_red_500);
|
||||||
status.setText(R.string.adding_contact_failed);
|
status.setText(R.string.adding_contact_failed);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ class ConversationMessageViewHolder extends ConversationItemViewHolder {
|
|||||||
|
|
||||||
// remember original status text color
|
// remember original status text color
|
||||||
timeColor = time.getCurrentTextColor();
|
timeColor = time.getCurrentTextColor();
|
||||||
timeColorBubble = getColor(v.getContext(), R.color.briar_white);
|
timeColorBubble =
|
||||||
|
getColor(v.getContext(), R.color.msg_status_bubble_foreground);
|
||||||
|
|
||||||
// clone constraint sets from layout files
|
// clone constraint sets from layout files
|
||||||
textConstraints.clone(v.getContext(),
|
textConstraints.clone(v.getContext(),
|
||||||
|
|||||||
@@ -230,7 +230,11 @@ class ConversationVisitor implements
|
|||||||
R.layout.list_item_conversation_notice_out, text, r);
|
R.layout.list_item_conversation_notice_out, text, r);
|
||||||
} else {
|
} else {
|
||||||
String text;
|
String text;
|
||||||
if (r.isContact()) {
|
if (r.wasAnswered()) {
|
||||||
|
text = ctx.getString(
|
||||||
|
R.string.introduction_request_answered_received,
|
||||||
|
contactName.getValue(), name);
|
||||||
|
} else if (r.isContact()) {
|
||||||
text = ctx.getString(
|
text = ctx.getString(
|
||||||
R.string.introduction_request_exists_received,
|
R.string.introduction_request_exists_received,
|
||||||
contactName.getValue(), name);
|
contactName.getValue(), name);
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.briarproject.briar.android.conversation;
|
|||||||
|
|
||||||
import android.content.DialogInterface.OnClickListener;
|
import android.content.DialogInterface.OnClickListener;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.drawable.Drawable;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.transition.Fade;
|
import android.transition.Fade;
|
||||||
import android.transition.Transition;
|
import android.transition.Transition;
|
||||||
@@ -35,8 +34,6 @@ import androidx.annotation.Nullable;
|
|||||||
import androidx.annotation.RequiresApi;
|
import androidx.annotation.RequiresApi;
|
||||||
import androidx.appcompat.app.AlertDialog.Builder;
|
import androidx.appcompat.app.AlertDialog.Builder;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
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.Fragment;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.fragment.app.FragmentStatePagerAdapter;
|
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 java.util.Objects.requireNonNull;
|
||||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_SAVE_ATTACHMENT;
|
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.formatDateAbsolute;
|
||||||
|
import static org.briarproject.briar.android.util.UiUtils.getDialogIcon;
|
||||||
|
|
||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
@@ -278,10 +276,7 @@ public class ImageActivity extends BriarActivity
|
|||||||
Builder builder = new Builder(this, R.style.BriarDialogTheme);
|
Builder builder = new Builder(this, R.style.BriarDialogTheme);
|
||||||
builder.setTitle(getString(R.string.dialog_title_save_image));
|
builder.setTitle(getString(R.string.dialog_title_save_image));
|
||||||
builder.setMessage(getString(R.string.dialog_message_save_image));
|
builder.setMessage(getString(R.string.dialog_message_save_image));
|
||||||
Drawable icon = ContextCompat.getDrawable(this, R.drawable.ic_security);
|
builder.setIcon(getDialogIcon(this, R.drawable.ic_security));
|
||||||
DrawableCompat.setTint(requireNonNull(icon),
|
|
||||||
ContextCompat.getColor(this, R.color.color_primary));
|
|
||||||
builder.setIcon(icon);
|
|
||||||
builder.setPositiveButton(R.string.save_image, okListener);
|
builder.setPositiveButton(R.string.save_image, okListener);
|
||||||
builder.setNegativeButton(R.string.cancel, null);
|
builder.setNegativeButton(R.string.cancel, null);
|
||||||
builder.show();
|
builder.show();
|
||||||
@@ -304,7 +299,7 @@ public class ImageActivity extends BriarActivity
|
|||||||
int stringRes = error ?
|
int stringRes = error ?
|
||||||
R.string.save_image_error : R.string.save_image_success;
|
R.string.save_image_error : R.string.save_image_success;
|
||||||
int colorRes = error ?
|
int colorRes = error ?
|
||||||
R.color.briar_red : R.color.briar_primary;
|
R.color.briar_red_500 : R.color.briar_primary;
|
||||||
new BriarSnackbarBuilder()
|
new BriarSnackbarBuilder()
|
||||||
.setBackgroundColor(colorRes)
|
.setBackgroundColor(colorRes)
|
||||||
.make(layout, stringRes, LENGTH_LONG)
|
.make(layout, stringRes, LENGTH_LONG)
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -79,13 +80,13 @@ public class ContactExchangeActivity extends KeyAgreementActivity {
|
|||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
private void contactExchangeFailed() {
|
private void contactExchangeFailed() {
|
||||||
showErrorFragment(R.string.connection_error_explanation);
|
showErrorFragment();
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@Override
|
@Override
|
||||||
public void keyAgreementFailed() {
|
public void keyAgreementFailed() {
|
||||||
showErrorFragment(R.string.connection_error_explanation);
|
showErrorFragment();
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@@ -103,7 +104,7 @@ public class ContactExchangeActivity extends KeyAgreementActivity {
|
|||||||
@UiThread
|
@UiThread
|
||||||
@Override
|
@Override
|
||||||
public void keyAgreementAborted(boolean remoteAborted) {
|
public void keyAgreementAborted(boolean remoteAborted) {
|
||||||
showErrorFragment(R.string.connection_error_explanation);
|
showErrorFragment();
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
@@ -112,4 +113,10 @@ public class ContactExchangeActivity extends KeyAgreementActivity {
|
|||||||
startContactExchange(result);
|
startContactExchange(result);
|
||||||
return getString(R.string.exchanging_contact_details);
|
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.os.Bundle;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.event.Event;
|
||||||
import org.briarproject.bramble.api.event.EventBus;
|
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.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
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.BluetoothEnabledEvent;
|
||||||
|
import org.briarproject.bramble.api.plugin.event.TransportStateEvent;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.activity.BriarActivity;
|
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.Manifest.permission.CAMERA;
|
||||||
import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
|
import static android.bluetooth.BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE;
|
||||||
import static android.bluetooth.BluetoothAdapter.ACTION_SCAN_MODE_CHANGED;
|
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.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
|
||||||
import static android.bluetooth.BluetoothAdapter.STATE_ON;
|
|
||||||
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
|
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_BLUETOOTH_DISCOVERABLE;
|
||||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PERMISSION_CAMERA_LOCATION;
|
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
|
@ParametersNotNullByDefault
|
||||||
public abstract class KeyAgreementActivity extends BriarActivity implements
|
public abstract class KeyAgreementActivity extends BriarActivity implements
|
||||||
BaseFragmentListener, IntroScreenSeenListener,
|
BaseFragmentListener, IntroScreenSeenListener,
|
||||||
KeyAgreementEventListener {
|
KeyAgreementEventListener, EventListener {
|
||||||
|
|
||||||
private enum BluetoothState {
|
private enum BluetoothDecision {
|
||||||
UNKNOWN, NO_ADAPTER, WAITING, REFUSED, ENABLED, DISCOVERABLE
|
/**
|
||||||
|
* 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 {
|
private enum Permission {
|
||||||
@@ -62,11 +95,14 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(KeyAgreementActivity.class.getName());
|
getLogger(KeyAgreementActivity.class.getName());
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
EventBus eventBus;
|
EventBus eventBus;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PluginManager pluginManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set to true in onPostResume() and false in onPause(). This prevents the
|
* Set to true in onPostResume() and false in onPause(). This prevents the
|
||||||
* QR code fragment from being shown if onRequestPermissionsResult() is
|
* 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.
|
* https://issuetracker.google.com/issues/37067655.
|
||||||
*/
|
*/
|
||||||
private boolean isResumed = false;
|
private boolean isResumed = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set to true when the continue button is clicked, and false when the QR
|
* 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
|
* code fragment is shown. This prevents the QR code fragment from being
|
||||||
* shown automatically before the continue button has been clicked.
|
* shown automatically before the continue button has been clicked.
|
||||||
*/
|
*/
|
||||||
private boolean continueClicked = false;
|
private boolean continueClicked = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Records whether the Bluetooth adapter was already enabled before we
|
* Records whether the Bluetooth adapter was already enabled before we
|
||||||
* asked for Bluetooth discoverability, so we know whether to broadcast a
|
* asked for Bluetooth discoverability, so we know whether to broadcast a
|
||||||
* {@link BluetoothEnabledEvent}.
|
* {@link BluetoothEnabledEvent}.
|
||||||
*/
|
*/
|
||||||
private boolean wasAdapterEnabled = false;
|
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 cameraPermission = Permission.UNKNOWN;
|
||||||
private Permission locationPermission = Permission.UNKNOWN;
|
private Permission locationPermission = Permission.UNKNOWN;
|
||||||
private BluetoothState bluetoothState = BluetoothState.UNKNOWN;
|
private BluetoothDecision bluetoothDecision = BluetoothDecision.UNKNOWN;
|
||||||
private BroadcastReceiver bluetoothReceiver = null;
|
private BroadcastReceiver bluetoothReceiver = null;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -96,20 +147,17 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
component.inject(this);
|
component.inject(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("ConstantConditions")
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable Bundle state) {
|
public void onCreate(@Nullable Bundle state) {
|
||||||
super.onCreate(state);
|
super.onCreate(state);
|
||||||
setContentView(R.layout.activity_fragment_container_toolbar);
|
setContentView(R.layout.activity_fragment_container_toolbar);
|
||||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
|
||||||
if (state == null) {
|
if (state == null) {
|
||||||
showInitialFragment(IntroFragment.newInstance());
|
showInitialFragment(IntroFragment.newInstance());
|
||||||
}
|
}
|
||||||
IntentFilter filter = new IntentFilter();
|
IntentFilter filter = new IntentFilter(ACTION_SCAN_MODE_CHANGED);
|
||||||
filter.addAction(ACTION_STATE_CHANGED);
|
|
||||||
filter.addAction(ACTION_SCAN_MODE_CHANGED);
|
|
||||||
bluetoothReceiver = new BluetoothStateReceiver();
|
bluetoothReceiver = new BluetoothStateReceiver();
|
||||||
registerReceiver(bluetoothReceiver, filter);
|
registerReceiver(bluetoothReceiver, filter);
|
||||||
}
|
}
|
||||||
@@ -122,18 +170,17 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
if (item.getItemId() == android.R.id.home) {
|
||||||
case android.R.id.home:
|
onBackPressed();
|
||||||
onBackPressed();
|
return true;
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
}
|
||||||
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
|
eventBus.addListener(this);
|
||||||
// Permissions may have been granted manually while we were stopped
|
// Permissions may have been granted manually while we were stopped
|
||||||
cameraPermission = Permission.UNKNOWN;
|
cameraPermission = Permission.UNKNOWN;
|
||||||
locationPermission = Permission.UNKNOWN;
|
locationPermission = Permission.UNKNOWN;
|
||||||
@@ -150,11 +197,22 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
|
|
||||||
private void showQrCodeFragmentIfAllowed() {
|
private void showQrCodeFragmentIfAllowed() {
|
||||||
if (isResumed && continueClicked && areEssentialPermissionsGranted()) {
|
if (isResumed && continueClicked && areEssentialPermissionsGranted()) {
|
||||||
if (bluetoothState == BluetoothState.UNKNOWN ||
|
if (isWifiReady() && isBluetoothReady()) {
|
||||||
bluetoothState == BluetoothState.ENABLED) {
|
LOG.info("Wifi and Bluetooth are ready");
|
||||||
requestBluetoothDiscoverable();
|
|
||||||
} else if (bluetoothState != BluetoothState.WAITING) {
|
|
||||||
showQrCodeFragment();
|
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);
|
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
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
isResumed = false;
|
isResumed = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onStop() {
|
||||||
|
super.onStop();
|
||||||
|
eventBus.removeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showNextScreen() {
|
public void showNextScreen() {
|
||||||
continueClicked = true;
|
continueClicked = true;
|
||||||
if (checkPermissions()) showQrCodeFragmentIfAllowed();
|
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
|
@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 (request == REQUEST_BLUETOOTH_DISCOVERABLE) {
|
||||||
if (result == RESULT_CANCELED) {
|
if (result == RESULT_CANCELED) {
|
||||||
setBluetoothState(BluetoothState.REFUSED);
|
LOG.info("Bluetooth discoverability was refused");
|
||||||
|
bluetoothDecision = BluetoothDecision.REFUSED;
|
||||||
} else {
|
} else {
|
||||||
// If Bluetooth is already discoverable, show the QR code -
|
LOG.info("Bluetooth discoverability was accepted");
|
||||||
// otherwise wait for the state or scan mode to change
|
bluetoothDecision = BluetoothDecision.ACCEPTED;
|
||||||
BluetoothAdapter bt = BluetoothAdapter.getDefaultAdapter();
|
if (!wasAdapterEnabled) {
|
||||||
if (bt == null) throw new AssertionError();
|
LOG.info("Bluetooth adapter was enabled by us");
|
||||||
if (bt.getScanMode() == SCAN_MODE_CONNECTABLE_DISCOVERABLE)
|
eventBus.broadcast(new BluetoothEnabledEvent());
|
||||||
setBluetoothState(BluetoothState.DISCOVERABLE);
|
wasAdapterEnabled = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
showQrCodeFragmentIfAllowed();
|
||||||
} else super.onActivityResult(request, result, data);
|
} else super.onActivityResult(request, result, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,7 +336,12 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
continueClicked = false;
|
continueClicked = false;
|
||||||
// If we return to the intro fragment, ask for Bluetooth
|
// If we return to the intro fragment, ask for Bluetooth
|
||||||
// discoverability again before showing the QR code fragment
|
// 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
|
// FIXME #824
|
||||||
FragmentManager fm = getSupportFragmentManager();
|
FragmentManager fm = getSupportFragmentManager();
|
||||||
if (fm.findFragmentByTag(KeyAgreementFragment.TAG) == null) {
|
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() {
|
private boolean checkPermissions() {
|
||||||
if (areEssentialPermissionsGranted()) return true;
|
if (areEssentialPermissionsGranted()) return true;
|
||||||
// If the camera permission has been permanently denied, ask the
|
// If the camera permission has been permanently denied, ask the
|
||||||
@@ -335,24 +443,30 @@ public abstract class KeyAgreementActivity extends BriarActivity implements
|
|||||||
permission);
|
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 {
|
private class BluetoothStateReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
String action = intent.getAction();
|
LOG.info("Bluetooth scan mode changed");
|
||||||
if (ACTION_STATE_CHANGED.equals(action)) {
|
showQrCodeFragmentIfAllowed();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package org.briarproject.briar.android.navdrawer;
|
package org.briarproject.briar.android.navdrawer;
|
||||||
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@@ -23,6 +22,7 @@ import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
|||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
||||||
import org.briarproject.bramble.api.plugin.LanTcpConstants;
|
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.TorConstants;
|
||||||
import org.briarproject.bramble.api.plugin.TransportId;
|
import org.briarproject.bramble.api.plugin.TransportId;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
@@ -30,7 +30,6 @@ import org.briarproject.briar.android.activity.ActivityComponent;
|
|||||||
import org.briarproject.briar.android.activity.BriarActivity;
|
import org.briarproject.briar.android.activity.BriarActivity;
|
||||||
import org.briarproject.briar.android.blog.FeedFragment;
|
import org.briarproject.briar.android.blog.FeedFragment;
|
||||||
import org.briarproject.briar.android.contact.ContactListFragment;
|
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.forum.ForumListFragment;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment;
|
import org.briarproject.briar.android.fragment.BaseFragment;
|
||||||
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
import org.briarproject.briar.android.fragment.BaseFragment.BaseFragmentListener;
|
||||||
@@ -44,9 +43,11 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.ColorRes;
|
||||||
|
import androidx.annotation.DrawableRes;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.StringRes;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
import androidx.appcompat.app.ActionBarDrawerToggle;
|
import androidx.appcompat.app.ActionBarDrawerToggle;
|
||||||
import androidx.appcompat.widget.Toolbar;
|
import androidx.appcompat.widget.Toolbar;
|
||||||
@@ -55,6 +56,8 @@ import androidx.core.content.ContextCompat;
|
|||||||
import androidx.drawerlayout.widget.DrawerLayout;
|
import androidx.drawerlayout.widget.DrawerLayout;
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.fragment.app.FragmentTransaction;
|
import androidx.fragment.app.FragmentTransaction;
|
||||||
|
import androidx.lifecycle.ViewModelProvider;
|
||||||
|
import androidx.lifecycle.ViewModelProviders;
|
||||||
|
|
||||||
import static android.view.View.GONE;
|
import static android.view.View.GONE;
|
||||||
import static android.view.View.VISIBLE;
|
import static android.view.View.VISIBLE;
|
||||||
@@ -64,7 +67,11 @@ import static androidx.fragment.app.FragmentManager.POP_BACK_STACK_INCLUSIVE;
|
|||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING;
|
import static org.briarproject.bramble.api.lifecycle.LifecycleManager.LifecycleState.RUNNING;
|
||||||
|
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.STARTING_STOPPING;
|
||||||
import static org.briarproject.briar.android.BriarService.EXTRA_STARTUP_FAILED;
|
import static org.briarproject.briar.android.BriarService.EXTRA_STARTUP_FAILED;
|
||||||
|
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PASSWORD;
|
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_PASSWORD;
|
||||||
import static org.briarproject.briar.android.navdrawer.IntentRouter.handleExternalIntent;
|
import static org.briarproject.briar.android.navdrawer.IntentRouter.handleExternalIntent;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.getDaysUntilExpiry;
|
import static org.briarproject.briar.android.util.UiUtils.getDaysUntilExpiry;
|
||||||
@@ -72,8 +79,7 @@ import static org.briarproject.briar.android.util.UiUtils.getDaysUntilExpiry;
|
|||||||
@MethodsNotNullByDefault
|
@MethodsNotNullByDefault
|
||||||
@ParametersNotNullByDefault
|
@ParametersNotNullByDefault
|
||||||
public class NavDrawerActivity extends BriarActivity implements
|
public class NavDrawerActivity extends BriarActivity implements
|
||||||
BaseFragmentListener, TransportStateListener,
|
BaseFragmentListener, OnNavigationItemSelectedListener {
|
||||||
OnNavigationItemSelectedListener {
|
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
getLogger(NavDrawerActivity.class.getName());
|
getLogger(NavDrawerActivity.class.getName());
|
||||||
@@ -91,10 +97,13 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
public static Uri SIGN_OUT_URI =
|
public static Uri SIGN_OUT_URI =
|
||||||
Uri.parse("briar-content://org.briarproject.briar/sign-out");
|
Uri.parse("briar-content://org.briarproject.briar/sign-out");
|
||||||
|
|
||||||
|
private NavDrawerViewModel navDrawerViewModel;
|
||||||
|
private PluginViewModel pluginViewModel;
|
||||||
private ActionBarDrawerToggle drawerToggle;
|
private ActionBarDrawerToggle drawerToggle;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
NavDrawerController controller;
|
ViewModelProvider.Factory viewModelFactory;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
LifecycleManager lifecycleManager;
|
LifecycleManager lifecycleManager;
|
||||||
|
|
||||||
@@ -115,6 +124,19 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
exitIfStartupFailed(getIntent());
|
exitIfStartupFailed(getIntent());
|
||||||
setContentView(R.layout.activity_nav_drawer);
|
setContentView(R.layout.activity_nav_drawer);
|
||||||
|
|
||||||
|
ViewModelProvider provider =
|
||||||
|
ViewModelProviders.of(this, viewModelFactory);
|
||||||
|
navDrawerViewModel = provider.get(NavDrawerViewModel.class);
|
||||||
|
pluginViewModel = provider.get(PluginViewModel.class);
|
||||||
|
|
||||||
|
if (IS_DEBUG_BUILD) {
|
||||||
|
navDrawerViewModel.showExpiryWarning()
|
||||||
|
.observe(this, this::showExpiryWarning);
|
||||||
|
}
|
||||||
|
navDrawerViewModel.shouldAskForDozeWhitelisting().observe(this, ask -> {
|
||||||
|
if (ask) showDozeDialog(getString(R.string.setup_doze_intro));
|
||||||
|
});
|
||||||
|
|
||||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
drawerLayout = findViewById(R.id.drawer_layout);
|
drawerLayout = findViewById(R.id.drawer_layout);
|
||||||
navigation = findViewById(R.id.navigation);
|
navigation = findViewById(R.id.navigation);
|
||||||
@@ -131,7 +153,7 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
drawerLayout.addDrawerListener(drawerToggle);
|
drawerLayout.addDrawerListener(drawerToggle);
|
||||||
navigation.setNavigationItemSelectedListener(this);
|
navigation.setNavigationItemSelectedListener(this);
|
||||||
|
|
||||||
initializeTransports(getLayoutInflater());
|
initializeTransports();
|
||||||
transportsView.setAdapter(transportsAdapter);
|
transportsView.setAdapter(transportsAdapter);
|
||||||
|
|
||||||
lockManager.isLockable().observe(this, this::setLockVisible);
|
lockManager.isLockable().observe(this, this::setLockVisible);
|
||||||
@@ -149,17 +171,10 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressLint("NewApi")
|
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
updateTransports();
|
|
||||||
lockManager.checkIfLockable();
|
lockManager.checkIfLockable();
|
||||||
controller.showExpiryWarning(new UiResultHandler<Boolean>(this) {
|
if (IS_DEBUG_BUILD) navDrawerViewModel.checkExpiryWarning();
|
||||||
@Override
|
|
||||||
public void onResultUi(Boolean expiry) {
|
|
||||||
if (expiry) showExpiryWarning();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -167,16 +182,7 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
@Nullable Intent data) {
|
@Nullable Intent data) {
|
||||||
super.onActivityResult(request, result, data);
|
super.onActivityResult(request, result, data);
|
||||||
if (request == REQUEST_PASSWORD && result == RESULT_OK) {
|
if (request == REQUEST_PASSWORD && result == RESULT_OK) {
|
||||||
controller.shouldAskForDozeWhitelisting(this,
|
navDrawerViewModel.checkDozeWhitelisting();
|
||||||
new UiResultHandler<Boolean>(this) {
|
|
||||||
@Override
|
|
||||||
public void onResultUi(Boolean ask) {
|
|
||||||
if (ask) {
|
|
||||||
showDozeDialog(
|
|
||||||
getString(R.string.setup_doze_intro));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,55 +352,38 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
if (item != null) item.setVisible(visible);
|
if (item != null) item.setVisible(visible);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showExpiryWarning() {
|
private void showExpiryWarning(boolean show) {
|
||||||
int daysUntilExpiry = getDaysUntilExpiry();
|
long daysUntilExpiry = getDaysUntilExpiry();
|
||||||
if (daysUntilExpiry < 0) signOut();
|
if (daysUntilExpiry < 0) {
|
||||||
|
signOut();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// show expiry warning text
|
|
||||||
ViewGroup expiryWarning = findViewById(R.id.expiryWarning);
|
ViewGroup expiryWarning = findViewById(R.id.expiryWarning);
|
||||||
TextView expiryWarningText =
|
if (show) {
|
||||||
expiryWarning.findViewById(R.id.expiryWarningText);
|
// show expiry warning text
|
||||||
// make close button functional
|
TextView expiryWarningText =
|
||||||
ImageView expiryWarningClose =
|
expiryWarning.findViewById(R.id.expiryWarningText);
|
||||||
expiryWarning.findViewById(R.id.expiryWarningClose);
|
String text = getResources().getQuantityString(
|
||||||
|
R.plurals.expiry_warning, (int) daysUntilExpiry,
|
||||||
expiryWarningText.setText(getResources()
|
(int) daysUntilExpiry);
|
||||||
.getQuantityString(R.plurals.expiry_warning,
|
expiryWarningText.setText(text);
|
||||||
daysUntilExpiry, daysUntilExpiry));
|
// make close button functional
|
||||||
|
ImageView expiryWarningClose =
|
||||||
expiryWarningClose.setOnClickListener(v -> {
|
expiryWarning.findViewById(R.id.expiryWarningClose);
|
||||||
controller.expiryWarningDismissed();
|
expiryWarningClose.setOnClickListener(v ->
|
||||||
|
navDrawerViewModel.expiryWarningDismissed());
|
||||||
|
expiryWarning.setVisibility(VISIBLE);
|
||||||
|
} else {
|
||||||
expiryWarning.setVisibility(GONE);
|
expiryWarning.setVisibility(GONE);
|
||||||
});
|
}
|
||||||
|
|
||||||
expiryWarning.setVisibility(VISIBLE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeTransports(LayoutInflater inflater) {
|
private void initializeTransports() {
|
||||||
transports = new ArrayList<>(3);
|
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() {
|
transportsAdapter = new BaseAdapter() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCount() {
|
public int getCount() {
|
||||||
return transports.size();
|
return transports.size();
|
||||||
@@ -417,63 +406,67 @@ public class NavDrawerActivity extends BriarActivity implements
|
|||||||
if (convertView != null) {
|
if (convertView != null) {
|
||||||
view = convertView;
|
view = convertView;
|
||||||
} else {
|
} else {
|
||||||
|
LayoutInflater inflater = getLayoutInflater();
|
||||||
view = inflater.inflate(R.layout.list_item_transport,
|
view = inflater.inflate(R.layout.list_item_transport,
|
||||||
parent, false);
|
parent, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
Transport t = getItem(position);
|
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);
|
ImageView icon = view.findViewById(R.id.imageView);
|
||||||
icon.setImageDrawable(ContextCompat
|
icon.setImageDrawable(ContextCompat.getDrawable(
|
||||||
.getDrawable(NavDrawerActivity.this, t.iconId));
|
NavDrawerActivity.this, t.iconDrawable));
|
||||||
icon.setColorFilter(c);
|
icon.setColorFilter(ContextCompat.getColor(
|
||||||
|
NavDrawerActivity.this, t.iconColor));
|
||||||
|
|
||||||
TextView text = view.findViewById(R.id.textView);
|
TextView text = view.findViewById(R.id.textView);
|
||||||
text.setText(getString(t.textId));
|
text.setText(getString(t.label));
|
||||||
|
|
||||||
return view;
|
return view;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
transports.add(createTransport(TorConstants.ID,
|
||||||
|
R.drawable.transport_tor, R.string.transport_tor));
|
||||||
|
transports.add(createTransport(LanTcpConstants.ID,
|
||||||
|
R.drawable.transport_lan, R.string.transport_lan));
|
||||||
|
transports.add(createTransport(BluetoothConstants.ID,
|
||||||
|
R.drawable.transport_bt, R.string.transport_bt));
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@ColorRes
|
||||||
private void setTransport(TransportId id, boolean enabled) {
|
private int getIconColor(State state) {
|
||||||
if (transports == null || transportsAdapter == null) return;
|
if (state == ACTIVE) return R.color.briar_lime_400;
|
||||||
for (Transport t : transports) {
|
else if (state == ENABLING) return R.color.briar_orange_500;
|
||||||
if (t.id.equals(id)) {
|
else return android.R.color.tertiary_text_light;
|
||||||
t.enabled = enabled;
|
|
||||||
transportsAdapter.notifyDataSetChanged();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateTransports() {
|
private Transport createTransport(TransportId id,
|
||||||
if (transports == null || transportsAdapter == null) return;
|
@DrawableRes int iconDrawable, @StringRes int label) {
|
||||||
for (Transport t : transports) {
|
int iconColor = getIconColor(STARTING_STOPPING);
|
||||||
t.enabled = controller.isTransportRunning(t.id);
|
Transport transport = new Transport(iconDrawable, label, iconColor);
|
||||||
}
|
pluginViewModel.getPluginState(id).observe(this, state -> {
|
||||||
transportsAdapter.notifyDataSetChanged();
|
transport.iconColor = getIconColor(state);
|
||||||
}
|
transportsAdapter.notifyDataSetChanged();
|
||||||
|
});
|
||||||
@Override
|
return transport;
|
||||||
public void stateUpdate(TransportId id, boolean enabled) {
|
|
||||||
setTransport(id, enabled);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class Transport {
|
private static class Transport {
|
||||||
|
|
||||||
private TransportId id;
|
@DrawableRes
|
||||||
private boolean enabled;
|
private final int iconDrawable;
|
||||||
private int iconId;
|
@StringRes
|
||||||
private int textId;
|
private final int label;
|
||||||
|
|
||||||
|
@ColorRes
|
||||||
|
private int iconColor;
|
||||||
|
|
||||||
|
private Transport(@DrawableRes int iconDrawable, @StringRes int label,
|
||||||
|
@ColorRes int iconColor) {
|
||||||
|
this.iconDrawable = iconDrawable;
|
||||||
|
this.label = label;
|
||||||
|
this.iconColor = iconColor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,23 @@
|
|||||||
|
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);
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
@IntoMap
|
||||||
|
@ViewModelKey(PluginViewModel.class)
|
||||||
|
abstract ViewModel bindPluginViewModel(PluginViewModel pluginViewModel);
|
||||||
|
}
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
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.nullsafety.NotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
|
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
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.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.controller.BriarControllerImpl.DOZE_ASK_AGAIN;
|
||||||
|
import static org.briarproject.briar.android.settings.SettingsFragment.SETTINGS_NAMESPACE;
|
||||||
|
import static org.briarproject.briar.android.util.UiUtils.needsDozeWhitelisting;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public class NavDrawerViewModel extends AndroidViewModel {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(NavDrawerViewModel.class.getName());
|
||||||
|
|
||||||
|
private static final String EXPIRY_DATE_WARNING = "expiryDateWarning";
|
||||||
|
|
||||||
|
@DatabaseExecutor
|
||||||
|
private final Executor dbExecutor;
|
||||||
|
private final SettingsManager settingsManager;
|
||||||
|
|
||||||
|
private final MutableLiveData<Boolean> showExpiryWarning =
|
||||||
|
new MutableLiveData<>();
|
||||||
|
private final MutableLiveData<Boolean> shouldAskForDozeWhitelisting =
|
||||||
|
new MutableLiveData<>();
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
NavDrawerViewModel(Application app, @DatabaseExecutor Executor dbExecutor,
|
||||||
|
SettingsManager settingsManager) {
|
||||||
|
super(app);
|
||||||
|
this.dbExecutor = dbExecutor;
|
||||||
|
this.settingsManager = settingsManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
LiveData<Boolean> showExpiryWarning() {
|
||||||
|
return showExpiryWarning;
|
||||||
|
}
|
||||||
|
|
||||||
|
@UiThread
|
||||||
|
void checkExpiryWarning() {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package org.briarproject.briar.android.navdrawer;
|
||||||
|
|
||||||
|
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 java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.MutableLiveData;
|
||||||
|
import androidx.lifecycle.ViewModel;
|
||||||
|
|
||||||
|
import static java.util.logging.Level.INFO;
|
||||||
|
import static java.util.logging.Logger.getLogger;
|
||||||
|
import static org.briarproject.bramble.api.plugin.Plugin.State.STARTING_STOPPING;
|
||||||
|
|
||||||
|
@NotNullByDefault
|
||||||
|
public class PluginViewModel extends ViewModel implements EventListener {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(PluginViewModel.class.getName());
|
||||||
|
|
||||||
|
private final PluginManager pluginManager;
|
||||||
|
private final EventBus eventBus;
|
||||||
|
|
||||||
|
private final MutableLiveData<State> torPluginState =
|
||||||
|
new MutableLiveData<>();
|
||||||
|
private final MutableLiveData<State> wifiPluginState =
|
||||||
|
new MutableLiveData<>();
|
||||||
|
private final MutableLiveData<State> btPluginState =
|
||||||
|
new MutableLiveData<>();
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PluginViewModel(PluginManager pluginManager, EventBus eventBus) {
|
||||||
|
this.pluginManager = pluginManager;
|
||||||
|
this.eventBus = eventBus;
|
||||||
|
eventBus.addListener(this);
|
||||||
|
torPluginState.setValue(getTransportState(TorConstants.ID));
|
||||||
|
wifiPluginState.setValue(getTransportState(LanTcpConstants.ID));
|
||||||
|
btPluginState.setValue(getTransportState(BluetoothConstants.ID));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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<State> liveData = getPluginLiveData(id);
|
||||||
|
if (liveData != null) liveData.postValue(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LiveData<State> getPluginState(TransportId id) {
|
||||||
|
LiveData<State> liveData = getPluginLiveData(id);
|
||||||
|
if (liveData == null) throw new IllegalArgumentException();
|
||||||
|
return liveData;
|
||||||
|
}
|
||||||
|
|
||||||
|
private State getTransportState(TransportId id) {
|
||||||
|
Plugin plugin = pluginManager.getPlugin(id);
|
||||||
|
return plugin == null ? STARTING_STOPPING : plugin.getState();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private MutableLiveData<State> getPluginLiveData(TransportId id) {
|
||||||
|
if (id.equals(TorConstants.ID)) return torPluginState;
|
||||||
|
else if (id.equals(LanTcpConstants.ID)) return wifiPluginState;
|
||||||
|
else if (id.equals(BluetoothConstants.ID)) return btPluginState;
|
||||||
|
else return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -14,15 +14,21 @@ import android.os.Looper;
|
|||||||
import org.acra.builder.ReportBuilder;
|
import org.acra.builder.ReportBuilder;
|
||||||
import org.acra.builder.ReportPrimer;
|
import org.acra.builder.ReportPrimer;
|
||||||
import org.briarproject.bramble.api.Pair;
|
import org.briarproject.bramble.api.Pair;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
import org.briarproject.briar.BuildConfig;
|
import org.briarproject.briar.BuildConfig;
|
||||||
|
import org.briarproject.briar.android.AndroidComponent;
|
||||||
import org.briarproject.briar.android.BriarApplication;
|
import org.briarproject.briar.android.BriarApplication;
|
||||||
import org.briarproject.briar.android.logging.BriefLogFormatter;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Scanner;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.FutureTask;
|
import java.util.concurrent.FutureTask;
|
||||||
@@ -35,6 +41,7 @@ import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE;
|
|||||||
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
|
import static android.bluetooth.BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE;
|
||||||
import static android.content.Context.ACTIVITY_SERVICE;
|
import static android.content.Context.ACTIVITY_SERVICE;
|
||||||
import static android.content.Context.CONNECTIVITY_SERVICE;
|
import static android.content.Context.CONNECTIVITY_SERVICE;
|
||||||
|
import static android.content.Context.MODE_PRIVATE;
|
||||||
import static android.content.Context.WIFI_P2P_SERVICE;
|
import static android.content.Context.WIFI_P2P_SERVICE;
|
||||||
import static android.content.Context.WIFI_SERVICE;
|
import static android.content.Context.WIFI_SERVICE;
|
||||||
import static android.net.ConnectivityManager.TYPE_MOBILE;
|
import static android.net.ConnectivityManager.TYPE_MOBILE;
|
||||||
@@ -49,6 +56,8 @@ import static org.briarproject.bramble.util.StringUtils.isNullOrEmpty;
|
|||||||
|
|
||||||
public class BriarReportPrimer implements ReportPrimer {
|
public class BriarReportPrimer implements ReportPrimer {
|
||||||
|
|
||||||
|
private static final int MAX_PERSISTED_LOG_LINES = 1_000;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void primeReport(@NonNull Context ctx,
|
public void primeReport(@NonNull Context ctx,
|
||||||
@NonNull ReportBuilder builder) {
|
@NonNull ReportBuilder builder) {
|
||||||
@@ -79,13 +88,23 @@ public class BriarReportPrimer implements ReportPrimer {
|
|||||||
// Log
|
// Log
|
||||||
BriarApplication app =
|
BriarApplication app =
|
||||||
(BriarApplication) ctx.getApplicationContext();
|
(BriarApplication) ctx.getApplicationContext();
|
||||||
|
AndroidComponent appComponent = app.getApplicationComponent();
|
||||||
|
PersistentLogManager logManager =
|
||||||
|
appComponent.persistentLogManager();
|
||||||
|
Formatter formatter = appComponent.formatter();
|
||||||
|
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
Formatter formatter = new BriefLogFormatter();
|
|
||||||
for (LogRecord record : app.getRecentLogRecords()) {
|
for (LogRecord record : app.getRecentLogRecords()) {
|
||||||
sb.append(formatter.format(record)).append('\n');
|
sb.append(formatter.format(record));
|
||||||
}
|
}
|
||||||
customData.put("Log", sb.toString());
|
customData.put("Log", sb.toString());
|
||||||
|
|
||||||
|
customData.put("Persisted log",
|
||||||
|
getPersistedLog(ctx, logManager, false));
|
||||||
|
|
||||||
|
customData.put("Previous persisted log",
|
||||||
|
getPersistedLog(ctx, logManager, true));
|
||||||
|
|
||||||
// System memory
|
// System memory
|
||||||
Object o = ctx.getSystemService(ACTIVITY_SERVICE);
|
Object o = ctx.getSystemService(ACTIVITY_SERVICE);
|
||||||
ActivityManager am = (ActivityManager) o;
|
ActivityManager am = (ActivityManager) o;
|
||||||
@@ -185,12 +204,18 @@ public class BriarReportPrimer implements ReportPrimer {
|
|||||||
WifiInfo wifiInfo = wm.getConnectionInfo();
|
WifiInfo wifiInfo = wm.getConnectionInfo();
|
||||||
if (wifiInfo != null) {
|
if (wifiInfo != null) {
|
||||||
int ip = wifiInfo.getIpAddress(); // Nice API, Google
|
int ip = wifiInfo.getIpAddress(); // Nice API, Google
|
||||||
int ip1 = ip & 0xFF;
|
byte[] ipBytes = new byte[4];
|
||||||
int ip2 = (ip >> 8) & 0xFF;
|
ipBytes[0] = (byte) (ip & 0xFF);
|
||||||
int ip3 = (ip >> 16) & 0xFF;
|
ipBytes[1] = (byte) ((ip >> 8) & 0xFF);
|
||||||
int ip4 = (ip >> 24) & 0xFF;
|
ipBytes[2] = (byte) ((ip >> 16) & 0xFF);
|
||||||
String address = ip1 + "." + ip2 + "." + ip3 + "." + ip4;
|
ipBytes[3] = (byte) ((ip >> 24) & 0xFF);
|
||||||
customData.put("Wi-Fi address", scrubInetAddress(address));
|
try {
|
||||||
|
InetAddress address = InetAddress.getByAddress(ipBytes);
|
||||||
|
customData.put("Wi-Fi address",
|
||||||
|
scrubInetAddress(address));
|
||||||
|
} catch (UnknownHostException ignored) {
|
||||||
|
// Should only be thrown if address has illegal length
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,6 +269,27 @@ public class BriarReportPrimer implements ReportPrimer {
|
|||||||
|
|
||||||
return unmodifiableMap(customData);
|
return unmodifiableMap(customData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getPersistedLog(Context ctx,
|
||||||
|
PersistentLogManager logManager, boolean old) {
|
||||||
|
File logDir = ctx.getDir("log", MODE_PRIVATE);
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
try {
|
||||||
|
Scanner scanner = logManager.getPersistedLog(logDir, old);
|
||||||
|
LinkedList<String> lines = new LinkedList<>();
|
||||||
|
int numLines = 0;
|
||||||
|
while (scanner.hasNextLine()) {
|
||||||
|
lines.add(scanner.nextLine());
|
||||||
|
if (numLines == MAX_PERSISTED_LOG_LINES) lines.pollFirst();
|
||||||
|
else numLines++;
|
||||||
|
}
|
||||||
|
scanner.close();
|
||||||
|
for (String line : lines) sb.append(line).append('\n');
|
||||||
|
} catch (IOException e) {
|
||||||
|
sb.append("Could not recover persisted log: ").append(e);
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class SingleShotAndroidExecutor extends Thread {
|
private static class SingleShotAndroidExecutor extends Thread {
|
||||||
|
|||||||
@@ -1,18 +1,65 @@
|
|||||||
package org.briarproject.briar.android.settings;
|
package org.briarproject.briar.android.settings;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import org.briarproject.bramble.api.lifecycle.IoExecutor;
|
||||||
|
import org.briarproject.bramble.api.logging.PersistentLogManager;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.briar.R;
|
import org.briarproject.briar.R;
|
||||||
import org.briarproject.briar.android.activity.ActivityComponent;
|
import org.briarproject.briar.android.activity.ActivityComponent;
|
||||||
import org.briarproject.briar.android.activity.BriarActivity;
|
import org.briarproject.briar.android.activity.BriarActivity;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.RequiresApi;
|
||||||
import androidx.appcompat.app.ActionBar;
|
import androidx.appcompat.app.ActionBar;
|
||||||
|
|
||||||
|
import static android.content.Intent.ACTION_CREATE_DOCUMENT;
|
||||||
|
import static android.content.Intent.CATEGORY_OPENABLE;
|
||||||
|
import static android.content.Intent.EXTRA_TITLE;
|
||||||
|
import static android.os.Build.VERSION.SDK_INT;
|
||||||
|
import static android.os.Environment.DIRECTORY_DOWNLOADS;
|
||||||
|
import static android.os.Environment.getExternalStoragePublicDirectory;
|
||||||
|
import static android.widget.Toast.LENGTH_LONG;
|
||||||
|
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.activity.RequestCodes.REQUEST_EXPORT_LOG;
|
||||||
|
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_EXPORT_OLD_LOG;
|
||||||
|
|
||||||
|
@MethodsNotNullByDefault
|
||||||
|
@ParametersNotNullByDefault
|
||||||
public class SettingsActivity extends BriarActivity {
|
public class SettingsActivity extends BriarActivity {
|
||||||
|
|
||||||
|
private static final Logger LOG =
|
||||||
|
getLogger(SettingsActivity.class.getName());
|
||||||
|
|
||||||
|
private static final String LOG_EXPORT_FILENAME = "briar-log.txt";
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
@IoExecutor
|
||||||
|
Executor ioExecutor;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
PersistentLogManager logManager;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle bundle) {
|
public void onCreate(@Nullable Bundle bundle) {
|
||||||
super.onCreate(bundle);
|
super.onCreate(bundle);
|
||||||
|
|
||||||
ActionBar actionBar = getSupportActionBar();
|
ActionBar actionBar = getSupportActionBar();
|
||||||
@@ -37,4 +84,96 @@ public class SettingsActivity extends BriarActivity {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onActivityResult(int request, int result,
|
||||||
|
@Nullable Intent data) {
|
||||||
|
super.onActivityResult(request, result, data);
|
||||||
|
if (request == REQUEST_EXPORT_LOG && result == RESULT_OK &&
|
||||||
|
data != null && data.getData() != null) {
|
||||||
|
exportLog(false, data.getData());
|
||||||
|
} else if (request == REQUEST_EXPORT_OLD_LOG && result == RESULT_OK &&
|
||||||
|
data != null && data.getData() != null) {
|
||||||
|
exportLog(true, data.getData());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void exportLog(boolean old, Uri uri) {
|
||||||
|
copyLog(old, () -> getOutputStream(uri));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyLog(boolean old, OutputStreamProvider osp) {
|
||||||
|
ioExecutor.execute(() -> {
|
||||||
|
try {
|
||||||
|
File logDir = getApplication().getDir("log", MODE_PRIVATE);
|
||||||
|
Scanner scanner = logManager.getPersistedLog(logDir, old);
|
||||||
|
if (!scanner.hasNextLine()) {
|
||||||
|
scanner.close();
|
||||||
|
runOnUiThreadUnlessDestroyed(() ->
|
||||||
|
Toast.makeText(getApplication(), "Log is empty",
|
||||||
|
LENGTH_LONG).show());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
PrintWriter w = new PrintWriter(osp.getOutputStream());
|
||||||
|
while (scanner.hasNextLine()) w.println(scanner.nextLine());
|
||||||
|
w.flush();
|
||||||
|
w.close();
|
||||||
|
scanner.close();
|
||||||
|
runOnUiThreadUnlessDestroyed(() ->
|
||||||
|
Toast.makeText(getApplication(), "Log exported",
|
||||||
|
LENGTH_LONG).show());
|
||||||
|
} catch (IOException e) {
|
||||||
|
logException(LOG, WARNING, e);
|
||||||
|
runOnUiThreadUnlessDestroyed(() ->
|
||||||
|
Toast.makeText(getApplication(), "Failed to export log",
|
||||||
|
LENGTH_LONG).show());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void onExportLogClick(boolean old) {
|
||||||
|
if (SDK_INT >= 19) {
|
||||||
|
Intent intent = getExportLogIntent();
|
||||||
|
int request = old ? REQUEST_EXPORT_OLD_LOG : REQUEST_EXPORT_LOG;
|
||||||
|
startActivityForResult(intent, request);
|
||||||
|
} else {
|
||||||
|
exportLog(old);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiresApi(api = 19)
|
||||||
|
private Intent getExportLogIntent() {
|
||||||
|
Intent intent = new Intent(ACTION_CREATE_DOCUMENT);
|
||||||
|
intent.addCategory(CATEGORY_OPENABLE);
|
||||||
|
intent.setType("text/plain");
|
||||||
|
intent.putExtra(EXTRA_TITLE, LOG_EXPORT_FILENAME);
|
||||||
|
return intent;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void exportLog(boolean old) {
|
||||||
|
File file = getLogOutputFile();
|
||||||
|
copyLog(old, () -> getOutputStream(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
private File getLogOutputFile() {
|
||||||
|
File path = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS);
|
||||||
|
//noinspection ResultOfMethodCallIgnored
|
||||||
|
path.mkdirs();
|
||||||
|
return new File(path, LOG_EXPORT_FILENAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
private OutputStream getOutputStream(File file) throws IOException {
|
||||||
|
return new FileOutputStream(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private OutputStream getOutputStream(Uri uri) throws IOException {
|
||||||
|
OutputStream os =
|
||||||
|
getApplication().getContentResolver().openOutputStream(uri);
|
||||||
|
if (os == null) throw new IOException();
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
private interface OutputStreamProvider {
|
||||||
|
OutputStream getOutputStream() throws IOException;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package org.briarproject.briar.android.settings;
|
package org.briarproject.briar.android.settings;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.TargetApi;
|
import android.annotation.TargetApi;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -21,6 +22,7 @@ import org.briarproject.bramble.api.event.EventListener;
|
|||||||
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.MethodsNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.ParametersNotNullByDefault;
|
||||||
import org.briarproject.bramble.api.plugin.BluetoothConstants;
|
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.TorConstants;
|
||||||
import org.briarproject.bramble.api.settings.Settings;
|
import org.briarproject.bramble.api.settings.Settings;
|
||||||
import org.briarproject.bramble.api.settings.SettingsManager;
|
import org.briarproject.bramble.api.settings.SettingsManager;
|
||||||
@@ -41,6 +43,7 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.StringRes;
|
import androidx.annotation.StringRes;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.core.text.TextUtilsCompat;
|
import androidx.core.text.TextUtilsCompat;
|
||||||
@@ -72,10 +75,15 @@ import static android.widget.Toast.LENGTH_SHORT;
|
|||||||
import static androidx.core.view.ViewCompat.LAYOUT_DIRECTION_LTR;
|
import static androidx.core.view.ViewCompat.LAYOUT_DIRECTION_LTR;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.PREF_BT_ENABLE;
|
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.TorConstants.DEFAULT_PREF_TOR_MOBILE;
|
||||||
|
import static org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_PREF_TOR_NETWORK;
|
||||||
|
import static org.briarproject.bramble.api.plugin.TorConstants.DEFAULT_PREF_TOR_ONLY_WHEN_CHARGING;
|
||||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_MOBILE;
|
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;
|
||||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_AUTOMATIC;
|
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_AUTOMATIC;
|
||||||
|
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_NETWORK_NEVER;
|
||||||
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_ONLY_WHEN_CHARGING;
|
import static org.briarproject.bramble.api.plugin.TorConstants.PREF_TOR_ONLY_WHEN_CHARGING;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
import static org.briarproject.bramble.util.LogUtils.logDuration;
|
||||||
import static org.briarproject.bramble.util.LogUtils.logException;
|
import static org.briarproject.bramble.util.LogUtils.logException;
|
||||||
@@ -84,6 +92,7 @@ import static org.briarproject.briar.android.BriarApplication.ENTRY_ACTIVITY;
|
|||||||
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
import static org.briarproject.briar.android.TestingConstants.IS_DEBUG_BUILD;
|
||||||
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_RINGTONE;
|
import static org.briarproject.briar.android.activity.RequestCodes.REQUEST_RINGTONE;
|
||||||
import static org.briarproject.briar.android.navdrawer.NavDrawerActivity.SIGN_OUT_URI;
|
import static org.briarproject.briar.android.navdrawer.NavDrawerActivity.SIGN_OUT_URI;
|
||||||
|
import static org.briarproject.briar.android.util.UiUtils.getCountryDisplayName;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.hasScreenLock;
|
import static org.briarproject.briar.android.util.UiUtils.hasScreenLock;
|
||||||
import static org.briarproject.briar.android.util.UiUtils.triggerFeedback;
|
import static org.briarproject.briar.android.util.UiUtils.triggerFeedback;
|
||||||
import static org.briarproject.briar.api.android.AndroidNotificationManager.BLOG_CHANNEL_ID;
|
import static org.briarproject.briar.api.android.AndroidNotificationManager.BLOG_CHANNEL_ID;
|
||||||
@@ -105,24 +114,34 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
implements EventListener, OnPreferenceChangeListener {
|
implements EventListener, OnPreferenceChangeListener {
|
||||||
|
|
||||||
public static final String SETTINGS_NAMESPACE = "android-ui";
|
public static final String SETTINGS_NAMESPACE = "android-ui";
|
||||||
public static final String BT_NAMESPACE = BluetoothConstants.ID.getString();
|
|
||||||
public static final String TOR_NAMESPACE = TorConstants.ID.getString();
|
|
||||||
public static final String LANGUAGE = "pref_key_language";
|
public static final String LANGUAGE = "pref_key_language";
|
||||||
public static final String PREF_SCREEN_LOCK = "pref_key_lock";
|
public static final String PREF_SCREEN_LOCK = "pref_key_lock";
|
||||||
public static final String PREF_SCREEN_LOCK_TIMEOUT =
|
public static final String PREF_SCREEN_LOCK_TIMEOUT =
|
||||||
"pref_key_lock_timeout";
|
"pref_key_lock_timeout";
|
||||||
public static final String NOTIFY_SIGN_IN = "pref_key_notify_sign_in";
|
public static final String NOTIFY_SIGN_IN = "pref_key_notify_sign_in";
|
||||||
public static final String TOR_NETWORK = "pref_key_tor_network";
|
|
||||||
public static final String TOR_MOBILE = "pref_key_tor_mobile_data";
|
private static final String BT_NAMESPACE =
|
||||||
public static final String TOR_ONLY_WHEN_CHARGING =
|
BluetoothConstants.ID.getString();
|
||||||
|
private static final String BT_ENABLE = "pref_key_bluetooth";
|
||||||
|
|
||||||
|
private static final String WIFI_NAMESPACE = LanTcpConstants.ID.getString();
|
||||||
|
private static final String WIFI_ENABLE = "pref_key_wifi";
|
||||||
|
|
||||||
|
private static final String TOR_NAMESPACE = TorConstants.ID.getString();
|
||||||
|
private static final String TOR_ENABLE = "pref_key_tor_enable";
|
||||||
|
private static final String TOR_NETWORK = "pref_key_tor_network";
|
||||||
|
private static final String TOR_MOBILE = "pref_key_tor_mobile_data";
|
||||||
|
private static final String TOR_ONLY_WHEN_CHARGING =
|
||||||
"pref_key_tor_only_when_charging";
|
"pref_key_tor_only_when_charging";
|
||||||
|
|
||||||
private static final Logger LOG =
|
private static final Logger LOG =
|
||||||
Logger.getLogger(SettingsFragment.class.getName());
|
getLogger(SettingsFragment.class.getName());
|
||||||
|
|
||||||
private SettingsActivity listener;
|
private SettingsActivity listener;
|
||||||
private ListPreference language;
|
private ListPreference language;
|
||||||
private ListPreference enableBluetooth;
|
private SwitchPreference enableBluetooth;
|
||||||
|
private SwitchPreference enableWifi;
|
||||||
|
private SwitchPreference enableTor;
|
||||||
private ListPreference torNetwork;
|
private ListPreference torNetwork;
|
||||||
private SwitchPreference torMobile;
|
private SwitchPreference torMobile;
|
||||||
private SwitchPreference torOnlyWhenCharging;
|
private SwitchPreference torOnlyWhenCharging;
|
||||||
@@ -137,7 +156,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
private Preference notifySound;
|
private Preference notifySound;
|
||||||
|
|
||||||
// Fields that are accessed from background threads must be volatile
|
// Fields that are accessed from background threads must be volatile
|
||||||
private volatile Settings settings, btSettings, torSettings;
|
private volatile Settings settings, btSettings, wifiSettings, torSettings;
|
||||||
private volatile boolean settingsLoaded = false;
|
private volatile boolean settingsLoaded = false;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@@ -163,28 +182,23 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
public void onCreatePreferences(Bundle bundle, String s) {
|
public void onCreatePreferences(Bundle bundle, String s) {
|
||||||
addPreferencesFromResource(R.xml.settings);
|
addPreferencesFromResource(R.xml.settings);
|
||||||
|
|
||||||
language = (ListPreference) findPreference(LANGUAGE);
|
language = findPreference(LANGUAGE);
|
||||||
setLanguageEntries();
|
setLanguageEntries();
|
||||||
ListPreference theme =
|
ListPreference theme = findPreference("pref_key_theme");
|
||||||
(ListPreference) findPreference("pref_key_theme");
|
enableBluetooth = findPreference(BT_ENABLE);
|
||||||
enableBluetooth = (ListPreference) findPreference("pref_key_bluetooth");
|
enableWifi = findPreference(WIFI_ENABLE);
|
||||||
torNetwork = (ListPreference) findPreference(TOR_NETWORK);
|
enableTor = findPreference(TOR_ENABLE);
|
||||||
torMobile = (SwitchPreference) findPreference(TOR_MOBILE);
|
torNetwork = findPreference(TOR_NETWORK);
|
||||||
torOnlyWhenCharging =
|
torMobile = findPreference(TOR_MOBILE);
|
||||||
(SwitchPreference) findPreference(TOR_ONLY_WHEN_CHARGING);
|
torOnlyWhenCharging = findPreference(TOR_ONLY_WHEN_CHARGING);
|
||||||
screenLock = (SwitchPreference) findPreference(PREF_SCREEN_LOCK);
|
screenLock = findPreference(PREF_SCREEN_LOCK);
|
||||||
screenLockTimeout =
|
screenLockTimeout = findPreference(PREF_SCREEN_LOCK_TIMEOUT);
|
||||||
(ListPreference) findPreference(PREF_SCREEN_LOCK_TIMEOUT);
|
notifyPrivateMessages =
|
||||||
notifyPrivateMessages = (SwitchPreference) findPreference(
|
findPreference("pref_key_notify_private_messages");
|
||||||
"pref_key_notify_private_messages");
|
notifyGroupMessages = findPreference("pref_key_notify_group_messages");
|
||||||
notifyGroupMessages = (SwitchPreference) findPreference(
|
notifyForumPosts = findPreference("pref_key_notify_forum_posts");
|
||||||
"pref_key_notify_group_messages");
|
notifyBlogPosts = findPreference("pref_key_notify_blog_posts");
|
||||||
notifyForumPosts = (SwitchPreference) findPreference(
|
notifyVibration = findPreference("pref_key_notify_vibration");
|
||||||
"pref_key_notify_forum_posts");
|
|
||||||
notifyBlogPosts = (SwitchPreference) findPreference(
|
|
||||||
"pref_key_notify_blog_posts");
|
|
||||||
notifyVibration = (SwitchPreference) findPreference(
|
|
||||||
"pref_key_notify_vibration");
|
|
||||||
notifySound = findPreference("pref_key_notify_sound");
|
notifySound = findPreference("pref_key_notify_sound");
|
||||||
|
|
||||||
language.setOnPreferenceChangeListener(this);
|
language.setOnPreferenceChangeListener(this);
|
||||||
@@ -194,8 +208,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
UiUtils.setTheme(getActivity(), (String) newValue);
|
UiUtils.setTheme(getActivity(), (String) newValue);
|
||||||
// bring up parent activity, so it can change its theme as well
|
// bring up parent activity, so it can change its theme as well
|
||||||
// upstream bug: https://issuetracker.google.com/issues/38352704
|
// upstream bug: https://issuetracker.google.com/issues/38352704
|
||||||
Intent intent =
|
Intent intent = new Intent(getActivity(), ENTRY_ACTIVITY);
|
||||||
new Intent(getActivity(), ENTRY_ACTIVITY);
|
|
||||||
intent.setFlags(
|
intent.setFlags(
|
||||||
FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK);
|
FLAG_ACTIVITY_CLEAR_TASK | FLAG_ACTIVITY_NEW_TASK);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
@@ -207,6 +220,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
enableBluetooth.setOnPreferenceChangeListener(this);
|
enableBluetooth.setOnPreferenceChangeListener(this);
|
||||||
|
enableWifi.setOnPreferenceChangeListener(this);
|
||||||
|
enableTor.setOnPreferenceChangeListener(this);
|
||||||
torNetwork.setOnPreferenceChangeListener(this);
|
torNetwork.setOnPreferenceChangeListener(this);
|
||||||
torMobile.setOnPreferenceChangeListener(this);
|
torMobile.setOnPreferenceChangeListener(this);
|
||||||
torOnlyWhenCharging.setOnPreferenceChangeListener(this);
|
torOnlyWhenCharging.setOnPreferenceChangeListener(this);
|
||||||
@@ -238,8 +253,25 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
throw new RuntimeException("Boom!");
|
throw new RuntimeException("Boom!");
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
findPreference("pref_key_export_log").setOnPreferenceClickListener(
|
||||||
|
preference -> {
|
||||||
|
((SettingsActivity) requireActivity())
|
||||||
|
.onExportLogClick(false);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
findPreference("pref_key_export_old_log")
|
||||||
|
.setOnPreferenceClickListener(
|
||||||
|
preference -> {
|
||||||
|
((SettingsActivity) requireActivity())
|
||||||
|
.onExportLogClick(true);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
findPreference("pref_key_explode").setVisible(false);
|
findPreference("pref_key_explode").setVisible(false);
|
||||||
|
findPreference("pref_key_export_log").setVisible(false);
|
||||||
|
findPreference("pref_key_export_old_log").setVisible(false);
|
||||||
findPreference("pref_key_test_data").setVisible(false);
|
findPreference("pref_key_test_data").setVisible(false);
|
||||||
PreferenceGroup testing =
|
PreferenceGroup testing =
|
||||||
findPreference("pref_key_explode").getParent();
|
findPreference("pref_key_explode").getParent();
|
||||||
@@ -249,8 +281,9 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
public View onCreateView(LayoutInflater inflater,
|
||||||
Bundle savedInstanceState) {
|
@Nullable ViewGroup container,
|
||||||
|
@Nullable Bundle savedInstanceState) {
|
||||||
View view = super.onCreateView(inflater, container, savedInstanceState);
|
View view = super.onCreateView(inflater, container, savedInstanceState);
|
||||||
ColorDrawable divider = new ColorDrawable(
|
ColorDrawable divider = new ColorDrawable(
|
||||||
ContextCompat.getColor(requireContext(), R.color.divider));
|
ContextCompat.getColor(requireContext(), R.color.divider));
|
||||||
@@ -317,6 +350,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
return direction == LAYOUT_DIRECTION_LTR;
|
return direction == LAYOUT_DIRECTION_LTR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressLint("StringFormatInvalid")
|
||||||
private void setTorNetworkSummary(int torNetworkSetting) {
|
private void setTorNetworkSummary(int torNetworkSetting) {
|
||||||
if (torNetworkSetting != PREF_TOR_NETWORK_AUTOMATIC) {
|
if (torNetworkSetting != PREF_TOR_NETWORK_AUTOMATIC) {
|
||||||
torNetwork.setSummary("%s"); // use setting value
|
torNetwork.setSummary("%s"); // use setting value
|
||||||
@@ -325,17 +359,13 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
|
|
||||||
// Look up country name in the user's chosen language if available
|
// Look up country name in the user's chosen language if available
|
||||||
String country = locationUtils.getCurrentCountry();
|
String country = locationUtils.getCurrentCountry();
|
||||||
String countryName = country;
|
String countryName = getCountryDisplayName(country);
|
||||||
for (Locale locale : Locale.getAvailableLocales()) {
|
|
||||||
if (locale.getCountry().equalsIgnoreCase(country)) {
|
|
||||||
countryName = locale.getDisplayCountry();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
boolean blocked =
|
boolean blocked =
|
||||||
circumventionProvider.isTorProbablyBlocked(country);
|
circumventionProvider.isTorProbablyBlocked(country);
|
||||||
boolean useBridges = circumventionProvider.doBridgesWork(country);
|
boolean useBridges = circumventionProvider.doBridgesWork(country);
|
||||||
String setting = getString(R.string.tor_network_setting_without_bridges);
|
String setting =
|
||||||
|
getString(R.string.tor_network_setting_without_bridges);
|
||||||
if (blocked && useBridges) {
|
if (blocked && useBridges) {
|
||||||
setting = getString(R.string.tor_network_setting_with_bridges);
|
setting = getString(R.string.tor_network_setting_with_bridges);
|
||||||
} else if (blocked) {
|
} else if (blocked) {
|
||||||
@@ -352,6 +382,7 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
long start = now();
|
long start = now();
|
||||||
settings = settingsManager.getSettings(SETTINGS_NAMESPACE);
|
settings = settingsManager.getSettings(SETTINGS_NAMESPACE);
|
||||||
btSettings = settingsManager.getSettings(BT_NAMESPACE);
|
btSettings = settingsManager.getSettings(BT_NAMESPACE);
|
||||||
|
wifiSettings = settingsManager.getSettings(WIFI_NAMESPACE);
|
||||||
torSettings = settingsManager.getSettings(TOR_NAMESPACE);
|
torSettings = settingsManager.getSettings(TOR_NAMESPACE);
|
||||||
settingsLoaded = true;
|
settingsLoaded = true;
|
||||||
logDuration(LOG, "Loading settings", start);
|
logDuration(LOG, "Loading settings", start);
|
||||||
@@ -362,26 +393,50 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Remove after a reasonable migration period (added 2020-06-25)
|
||||||
|
private Settings migrateTorSettings(Settings s) {
|
||||||
|
int network = s.getInt(PREF_TOR_NETWORK, DEFAULT_PREF_TOR_NETWORK);
|
||||||
|
if (network == PREF_TOR_NETWORK_NEVER) {
|
||||||
|
s.putInt(PREF_TOR_NETWORK, DEFAULT_PREF_TOR_NETWORK);
|
||||||
|
s.putBoolean(PREF_PLUGIN_ENABLE, false);
|
||||||
|
// We don't need to save the migrated settings - the Tor plugin is
|
||||||
|
// responsible for that. This code just handles the case where the
|
||||||
|
// settings are loaded before the plugin migrates them.
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
private void displaySettings() {
|
private void displaySettings() {
|
||||||
listener.runOnUiThreadUnlessDestroyed(() -> {
|
listener.runOnUiThreadUnlessDestroyed(() -> {
|
||||||
// due to events, we might try to display before a load completed
|
// due to events, we might try to display before a load completed
|
||||||
if (!settingsLoaded) return;
|
if (!settingsLoaded) return;
|
||||||
|
|
||||||
boolean btEnabledSetting =
|
boolean btEnabledSetting = btSettings.getBoolean(PREF_PLUGIN_ENABLE,
|
||||||
btSettings.getBoolean(PREF_BT_ENABLE, false);
|
BluetoothConstants.DEFAULT_PREF_PLUGIN_ENABLE);
|
||||||
enableBluetooth.setValue(Boolean.toString(btEnabledSetting));
|
enableBluetooth.setChecked(btEnabledSetting);
|
||||||
|
|
||||||
|
boolean wifiEnabledSetting =
|
||||||
|
wifiSettings.getBoolean(PREF_PLUGIN_ENABLE,
|
||||||
|
LanTcpConstants.DEFAULT_PREF_PLUGIN_ENABLE);
|
||||||
|
enableWifi.setChecked(wifiEnabledSetting);
|
||||||
|
|
||||||
|
boolean torEnabledSetting =
|
||||||
|
torSettings.getBoolean(PREF_PLUGIN_ENABLE,
|
||||||
|
TorConstants.DEFAULT_PREF_PLUGIN_ENABLE);
|
||||||
|
enableTor.setChecked(torEnabledSetting);
|
||||||
|
|
||||||
int torNetworkSetting = torSettings.getInt(PREF_TOR_NETWORK,
|
int torNetworkSetting = torSettings.getInt(PREF_TOR_NETWORK,
|
||||||
PREF_TOR_NETWORK_AUTOMATIC);
|
DEFAULT_PREF_TOR_NETWORK);
|
||||||
torNetwork.setValue(Integer.toString(torNetworkSetting));
|
torNetwork.setValue(Integer.toString(torNetworkSetting));
|
||||||
setTorNetworkSummary(torNetworkSetting);
|
setTorNetworkSummary(torNetworkSetting);
|
||||||
|
|
||||||
boolean torMobileSetting =
|
boolean torMobileSetting = torSettings.getBoolean(PREF_TOR_MOBILE,
|
||||||
torSettings.getBoolean(PREF_TOR_MOBILE, true);
|
DEFAULT_PREF_TOR_MOBILE);
|
||||||
torMobile.setChecked(torMobileSetting);
|
torMobile.setChecked(torMobileSetting);
|
||||||
|
|
||||||
boolean torChargingSetting =
|
boolean torChargingSetting =
|
||||||
torSettings.getBoolean(PREF_TOR_ONLY_WHEN_CHARGING, false);
|
torSettings.getBoolean(PREF_TOR_ONLY_WHEN_CHARGING,
|
||||||
|
DEFAULT_PREF_TOR_ONLY_WHEN_CHARGING);
|
||||||
torOnlyWhenCharging.setChecked(torChargingSetting);
|
torOnlyWhenCharging.setChecked(torChargingSetting);
|
||||||
|
|
||||||
displayScreenLockSetting();
|
displayScreenLockSetting();
|
||||||
@@ -443,6 +498,8 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
// - pref_key_lock (screenLock -> displayScreenLockSetting())
|
// - pref_key_lock (screenLock -> displayScreenLockSetting())
|
||||||
// - pref_key_lock_timeout (screenLockTimeout)
|
// - pref_key_lock_timeout (screenLockTimeout)
|
||||||
enableBluetooth.setEnabled(enabled);
|
enableBluetooth.setEnabled(enabled);
|
||||||
|
enableWifi.setEnabled(enabled);
|
||||||
|
enableTor.setEnabled(enabled);
|
||||||
torNetwork.setEnabled(enabled);
|
torNetwork.setEnabled(enabled);
|
||||||
torMobile.setEnabled(enabled);
|
torMobile.setEnabled(enabled);
|
||||||
torOnlyWhenCharging.setEnabled(enabled);
|
torOnlyWhenCharging.setEnabled(enabled);
|
||||||
@@ -545,8 +602,14 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
languageChanged((String) newValue);
|
languageChanged((String) newValue);
|
||||||
return false;
|
return false;
|
||||||
} else if (preference == enableBluetooth) {
|
} else if (preference == enableBluetooth) {
|
||||||
boolean btSetting = Boolean.valueOf((String) newValue);
|
boolean btSetting = (Boolean) newValue;
|
||||||
storeBluetoothSettings(btSetting);
|
storeBluetoothSetting(btSetting);
|
||||||
|
} else if (preference == enableWifi) {
|
||||||
|
boolean wifiSetting = (Boolean) newValue;
|
||||||
|
storeWifiSetting(wifiSetting);
|
||||||
|
} else if (preference == enableTor) {
|
||||||
|
boolean torEnabledSetting = (Boolean) newValue;
|
||||||
|
storeTorEnabledSetting(torEnabledSetting);
|
||||||
} else if (preference == torNetwork) {
|
} else if (preference == torNetwork) {
|
||||||
int torNetworkSetting = Integer.valueOf((String) newValue);
|
int torNetworkSetting = Integer.valueOf((String) newValue);
|
||||||
storeTorNetworkSetting(torNetworkSetting);
|
storeTorNetworkSetting(torNetworkSetting);
|
||||||
@@ -610,6 +673,12 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
builder.show();
|
builder.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void storeTorEnabledSetting(boolean torEnabledSetting) {
|
||||||
|
Settings s = new Settings();
|
||||||
|
s.putBoolean(PREF_PLUGIN_ENABLE, torEnabledSetting);
|
||||||
|
mergeSettings(s, TOR_NAMESPACE);
|
||||||
|
}
|
||||||
|
|
||||||
private void storeTorNetworkSetting(int torNetworkSetting) {
|
private void storeTorNetworkSetting(int torNetworkSetting) {
|
||||||
Settings s = new Settings();
|
Settings s = new Settings();
|
||||||
s.putInt(PREF_TOR_NETWORK, torNetworkSetting);
|
s.putInt(PREF_TOR_NETWORK, torNetworkSetting);
|
||||||
@@ -628,12 +697,18 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
mergeSettings(s, TOR_NAMESPACE);
|
mergeSettings(s, TOR_NAMESPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void storeBluetoothSettings(boolean btSetting) {
|
private void storeBluetoothSetting(boolean btSetting) {
|
||||||
Settings s = new Settings();
|
Settings s = new Settings();
|
||||||
s.putBoolean(PREF_BT_ENABLE, btSetting);
|
s.putBoolean(PREF_PLUGIN_ENABLE, btSetting);
|
||||||
mergeSettings(s, BT_NAMESPACE);
|
mergeSettings(s, BT_NAMESPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void storeWifiSetting(boolean wifiSetting) {
|
||||||
|
Settings s = new Settings();
|
||||||
|
s.putBoolean(PREF_PLUGIN_ENABLE, wifiSetting);
|
||||||
|
mergeSettings(s, WIFI_NAMESPACE);
|
||||||
|
}
|
||||||
|
|
||||||
private void storeSettings(Settings s) {
|
private void storeSettings(Settings s) {
|
||||||
mergeSettings(s, SETTINGS_NAMESPACE);
|
mergeSettings(s, SETTINGS_NAMESPACE);
|
||||||
}
|
}
|
||||||
@@ -696,9 +771,13 @@ public class SettingsFragment extends PreferenceFragmentCompat
|
|||||||
LOG.info("Bluetooth settings updated");
|
LOG.info("Bluetooth settings updated");
|
||||||
btSettings = s.getSettings();
|
btSettings = s.getSettings();
|
||||||
displaySettings();
|
displaySettings();
|
||||||
|
} else if (namespace.equals(WIFI_NAMESPACE)) {
|
||||||
|
LOG.info("Wifi settings updated");
|
||||||
|
wifiSettings = s.getSettings();
|
||||||
|
displaySettings();
|
||||||
} else if (namespace.equals(TOR_NAMESPACE)) {
|
} else if (namespace.equals(TOR_NAMESPACE)) {
|
||||||
LOG.info("Tor settings updated");
|
LOG.info("Tor settings updated");
|
||||||
torSettings = s.getSettings();
|
torSettings = migrateTorSettings(s.getSettings());
|
||||||
displaySettings();
|
displaySettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class BriarNotificationBuilder extends NotificationCompat.Builder {
|
|||||||
// https://issuetracker.google.com/issues/36961721
|
// https://issuetracker.google.com/issues/36961721
|
||||||
setAutoCancel(true);
|
setAutoCancel(true);
|
||||||
|
|
||||||
setLights(ContextCompat.getColor(context, R.color.briar_green_light),
|
setLights(ContextCompat.getColor(context, R.color.briar_lime_400),
|
||||||
750, 500);
|
750, 500);
|
||||||
if (SDK_INT >= 21) setVisibility(VISIBILITY_PRIVATE);
|
if (SDK_INT >= 21) setVisibility(VISIBILITY_PRIVATE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import android.app.KeyguardManager;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface.OnClickListener;
|
import android.content.DialogInterface.OnClickListener;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.graphics.drawable.Drawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
@@ -38,9 +39,12 @@ import org.briarproject.briar.R;
|
|||||||
import org.briarproject.briar.android.view.ArticleMovementMethod;
|
import org.briarproject.briar.android.view.ArticleMovementMethod;
|
||||||
import org.briarproject.briar.android.widget.LinkDialogFragment;
|
import org.briarproject.briar.android.widget.LinkDialogFragment;
|
||||||
|
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
import androidx.annotation.AttrRes;
|
import androidx.annotation.AttrRes;
|
||||||
import androidx.annotation.ColorInt;
|
import androidx.annotation.ColorInt;
|
||||||
import androidx.annotation.ColorRes;
|
import androidx.annotation.ColorRes;
|
||||||
|
import androidx.annotation.DrawableRes;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.annotation.RequiresApi;
|
import androidx.annotation.RequiresApi;
|
||||||
import androidx.annotation.UiThread;
|
import androidx.annotation.UiThread;
|
||||||
@@ -79,7 +83,10 @@ import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
|
|||||||
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
|
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_NO;
|
||||||
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
|
import static androidx.appcompat.app.AppCompatDelegate.MODE_NIGHT_YES;
|
||||||
import static androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode;
|
import static androidx.appcompat.app.AppCompatDelegate.setDefaultNightMode;
|
||||||
|
import static androidx.core.content.ContextCompat.getColor;
|
||||||
|
import static androidx.core.content.ContextCompat.getDrawable;
|
||||||
import static androidx.core.content.ContextCompat.getSystemService;
|
import static androidx.core.content.ContextCompat.getSystemService;
|
||||||
|
import static androidx.core.graphics.drawable.DrawableCompat.setTint;
|
||||||
import static androidx.core.view.ViewCompat.LAYOUT_DIRECTION_RTL;
|
import static androidx.core.view.ViewCompat.LAYOUT_DIRECTION_RTL;
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
import static java.util.concurrent.TimeUnit.DAYS;
|
import static java.util.concurrent.TimeUnit.DAYS;
|
||||||
@@ -152,10 +159,9 @@ public class UiUtils {
|
|||||||
return DateUtils.formatDateTime(ctx, time, flags);
|
return DateUtils.formatDateTime(ctx, time, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int getDaysUntilExpiry() {
|
public static long getDaysUntilExpiry() {
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
long daysBeforeExpiry = (EXPIRY_DATE - now) / DAYS.toMillis(1);
|
return (EXPIRY_DATE - now) / DAYS.toMillis(1);
|
||||||
return (int) daysBeforeExpiry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SpannableStringBuilder getTeaser(Context ctx, Spanned text) {
|
public static SpannableStringBuilder getTeaser(Context ctx, Spanned text) {
|
||||||
@@ -401,4 +407,20 @@ public class UiUtils {
|
|||||||
return ctx.getResources().getConfiguration().getLayoutDirection() ==
|
return ctx.getResources().getConfiguration().getLayoutDirection() ==
|
||||||
LAYOUT_DIRECTION_RTL;
|
LAYOUT_DIRECTION_RTL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getCountryDisplayName(String isoCode) {
|
||||||
|
for (Locale locale : Locale.getAvailableLocales()) {
|
||||||
|
if (locale.getCountry().equalsIgnoreCase(isoCode)) {
|
||||||
|
return locale.getDisplayCountry();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Name is unknown
|
||||||
|
return isoCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Drawable getDialogIcon(Context ctx, @DrawableRes int resId) {
|
||||||
|
Drawable icon = getDrawable(ctx, resId);
|
||||||
|
setTint(requireNonNull(icon), getColor(ctx, R.color.color_primary));
|
||||||
|
return icon;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
C7.58,20,4,16.42,4,12 S7.58,4,12,4 S20,7.58,20,12 S16.42,20,12,20 Z"/>
|
C7.58,20,4,16.42,4,12 S7.58,4,12,4 S20,7.58,20,12 S16.42,20,12,20 Z"/>
|
||||||
|
|
||||||
<path
|
<path
|
||||||
android:fillColor="#95d220"
|
android:fillColor="#82c91e"
|
||||||
android:pathData="M10.8972,19.9503 C6.5514,19.3493,3.43091,15.2154,4.0625,10.896
|
android:pathData="M10.8972,19.9503 C6.5514,19.3493,3.43091,15.2154,4.0625,10.896
|
||||||
C4.55452,7.53099,7.09451,4.8236,10.394,4.14714
|
C4.55452,7.53099,7.09451,4.8236,10.394,4.14714
|
||||||
C14.2569,3.35517,18.1698,5.54347,19.5236,9.25295
|
C14.2569,3.35517,18.1698,5.54347,19.5236,9.25295
|
||||||
|
|||||||
@@ -17,12 +17,12 @@
|
|||||||
android:fillColor="#ffffff"
|
android:fillColor="#ffffff"
|
||||||
android:pathData="M175.3 16.5l-9.8 0 0 -5.7c0 -1.4 -1.2 -2.6 -2.6 -2.6l-19.3 0c-1.4 0 -2.6 1.2 -2.6 2.6l0 14.4c0 1.4 1.2 2.6 2.6 2.6l15.1 0 0 17.3c0 2.4 2 4.4 4.4 4.4l12.2 0c2.4 0 4.4 -2 4.4 -4.4l0 -24.2c0.1 -2.4 -1.9 -4.4 -4.4 -4.4zm-12.4 -5.9l-9.6 6 -9.6 -6 19.2 0zm-19.4 14.8l0 -12.3 9.8 6.1 9.8 -6.1 0 12.3 -19.6 0zm28.6 21.2l-5.8 0 0 -1.5 5.8 0 0 1.5zm5 -4.6l-15.8 0 0 -14.2 1.6 0c1.4 0 2.6 -1.2 2.6 -2.6l0 -4.1 11.6 0 0 20.9z"/>
|
android:pathData="M175.3 16.5l-9.8 0 0 -5.7c0 -1.4 -1.2 -2.6 -2.6 -2.6l-19.3 0c-1.4 0 -2.6 1.2 -2.6 2.6l0 14.4c0 1.4 1.2 2.6 2.6 2.6l15.1 0 0 17.3c0 2.4 2 4.4 4.4 4.4l12.2 0c2.4 0 4.4 -2 4.4 -4.4l0 -24.2c0.1 -2.4 -1.9 -4.4 -4.4 -4.4zm-12.4 -5.9l-9.6 6 -9.6 -6 19.2 0zm-19.4 14.8l0 -12.3 9.8 6.1 9.8 -6.1 0 12.3 -19.6 0zm28.6 21.2l-5.8 0 0 -1.5 5.8 0 0 1.5zm5 -4.6l-15.8 0 0 -14.2 1.6 0c1.4 0 2.6 -1.2 2.6 -2.6l0 -4.1 11.6 0 0 20.9z"/>
|
||||||
<path
|
<path
|
||||||
android:fillColor="#ff0000"
|
android:fillColor="#db3b21"
|
||||||
android:pathData="M101.4 17.8l2 2 7.4 -7.3 7.3 7.3 2.1 -2 -7.4 -7.4 7.4 -7.3 -2.1 -2.1 -7.3 7.4 -7.4 -7.4 -2 2.1 7.3 7.3z"/>
|
android:pathData="M101.4 17.8l2 2 7.4 -7.3 7.3 7.3 2.1 -2 -7.4 -7.4 7.4 -7.3 -2.1 -2.1 -7.3 7.4 -7.4 -7.4 -2 2.1 7.3 7.3z"/>
|
||||||
<path
|
<path
|
||||||
android:fillColor="#ff0000"
|
android:fillColor="#db3b21"
|
||||||
android:pathData="M176 17.8l2.1 2 7.3 -7.3 7.4 7.3 2 -2 -7.3 -7.4 7.3 -7.3 -2 -2.1 -7.4 7.4 -7.3 -7.4 -2.1 2.1 7.3 7.3z"/>
|
android:pathData="M176 17.8l2.1 2 7.3 -7.3 7.4 7.3 2 -2 -7.3 -7.4 7.3 -7.3 -2 -2.1 -7.4 7.4 -7.3 -7.4 -2.1 2.1 7.3 7.3z"/>
|
||||||
<path
|
<path
|
||||||
android:fillColor="#08b124"
|
android:fillColor="#67a60f"
|
||||||
android:pathData="M35.8 18.8l0 0L52.5 2.1 50.5 0 35.6 14.8 28.5 7.7l-2.1 2.1 9.2 9.1z"/>
|
android:pathData="M35.8 18.8l0 0L52.5 2.1 50.5 0 35.6 14.8 28.5 7.7l-2.1 2.1 9.2 9.1z"/>
|
||||||
</vector>
|
</vector>
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
android:radius="32dp"/>
|
android:radius="32dp"/>
|
||||||
|
|
||||||
<solid
|
<solid
|
||||||
android:color="@color/briar_green"/>
|
android:color="@color/briar_lime_600"/>
|
||||||
|
|
||||||
</shape>
|
</shape>
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ C7.58,20,4,16.42,4,12 S7.58,4,12,4 S20,7.58,20,12 S16.42,20,12,20 Z"/>
|
|||||||
<path
|
<path
|
||||||
android:pathData="M0,0 L24,0 L24,24 L0,24 Z"/>
|
android:pathData="M0,0 L24,0 L24,24 L0,24 Z"/>
|
||||||
<path
|
<path
|
||||||
android:fillColor="#95d220"
|
android:fillColor="#82c91e"
|
||||||
android:pathData="M10.8972,19.9503 C6.5514,19.3493,3.43091,15.2154,4.0625,10.896
|
android:pathData="M10.8972,19.9503 C6.5514,19.3493,3.43091,15.2154,4.0625,10.896
|
||||||
C4.55452,7.53099,7.09451,4.8236,10.394,4.14714
|
C4.55452,7.53099,7.09451,4.8236,10.394,4.14714
|
||||||
C14.2569,3.35517,18.1698,5.54347,19.5236,9.25295
|
C14.2569,3.35517,18.1698,5.54347,19.5236,9.25295
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
android:viewportWidth="24">
|
android:viewportWidth="24">
|
||||||
|
|
||||||
<path
|
<path
|
||||||
android:fillColor="#2D3E50"
|
android:fillColor="#2e3d4f"
|
||||||
android:pathData="M10.8972,19.9503 C6.5514,19.3493,3.43091,15.2154,4.0625,10.896
|
android:pathData="M10.8972,19.9503 C6.5514,19.3493,3.43091,15.2154,4.0625,10.896
|
||||||
C4.55452,7.53099,7.09451,4.8236,10.394,4.14714
|
C4.55452,7.53099,7.09451,4.8236,10.394,4.14714
|
||||||
C14.2569,3.35517,18.1698,5.54347,19.5236,9.25295
|
C14.2569,3.35517,18.1698,5.54347,19.5236,9.25295
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
android:viewportWidth="24">
|
android:viewportWidth="24">
|
||||||
|
|
||||||
<path
|
<path
|
||||||
android:fillColor="#95D220"
|
android:fillColor="#82c91e"
|
||||||
android:pathData="M10.8972,19.9503 C6.5514,19.3493,3.43091,15.2154,4.0625,10.896
|
android:pathData="M10.8972,19.9503 C6.5514,19.3493,3.43091,15.2154,4.0625,10.896
|
||||||
C4.55452,7.53099,7.09451,4.8236,10.394,4.14714
|
C4.55452,7.53099,7.09451,4.8236,10.394,4.14714
|
||||||
C14.2569,3.35517,18.1698,5.54347,19.5236,9.25295
|
C14.2569,3.35517,18.1698,5.54347,19.5236,9.25295
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:width="24dp"
|
android:width="24dp"
|
||||||
android:height="24dp"
|
android:height="24dp"
|
||||||
android:tint="#2A93C6"
|
android:tint="#418cd8"
|
||||||
android:viewportHeight="24.0"
|
android:viewportHeight="24.0"
|
||||||
android:viewportWidth="24.0">
|
android:viewportWidth="24.0">
|
||||||
<path
|
<path
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:width="24dp"
|
android:width="24dp"
|
||||||
android:height="24dp"
|
android:height="24dp"
|
||||||
android:tint="#2A93C6"
|
android:tint="#418cd8"
|
||||||
android:viewportHeight="24.0"
|
android:viewportHeight="24.0"
|
||||||
android:viewportWidth="24.0">
|
android:viewportWidth="24.0">
|
||||||
<path
|
<path
|
||||||
|
|||||||
@@ -4,6 +4,6 @@
|
|||||||
android:viewportHeight="24.0"
|
android:viewportHeight="24.0"
|
||||||
android:viewportWidth="24.0">
|
android:viewportWidth="24.0">
|
||||||
<path
|
<path
|
||||||
android:fillColor="#808080"
|
android:fillColor="#707070"
|
||||||
android:pathData="M21,5v6.59l-3,-3.01 -4,4.01 -4,-4 -4,4 -3,-3.01L3,5c0,-1.1 0.9,-2 2,-2h14c1.1,0 2,0.9 2,2zM18,11.42l3,3.01L21,19c0,1.1 -0.9,2 -2,2L5,21c-1.1,0 -2,-0.9 -2,-2v-6.58l3,2.99 4,-4 4,4 4,-3.99z"/>
|
android:pathData="M21,5v6.59l-3,-3.01 -4,4.01 -4,-4 -4,4 -3,-3.01L3,5c0,-1.1 0.9,-2 2,-2h14c1.1,0 2,0.9 2,2zM18,11.42l3,3.01L21,19c0,1.1 -0.9,2 -2,2L5,21c-1.1,0 -2,-0.9 -2,-2v-6.58l3,2.99 4,-4 4,4 4,-3.99z"/>
|
||||||
</vector>
|
</vector>
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user