2018-05-02 17:29:58 +02:00
|
|
|
/*
|
2018-05-02 21:40:01 +02:00
|
|
|
* Copyright © 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
|
2018-07-06 04:09:48 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2018-05-02 17:29:58 +02:00
|
|
|
*/
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android;
|
|
|
|
|
2018-07-25 16:11:37 +02:00
|
|
|
import android.content.Context;
|
2018-07-28 18:27:45 +02:00
|
|
|
import android.content.Intent;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.content.SharedPreferences;
|
2018-07-25 16:11:37 +02:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.pm.Signature;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.os.AsyncTask;
|
2018-07-28 18:27:45 +02:00
|
|
|
import android.os.Build;
|
2018-01-01 09:06:37 +01:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.preference.PreferenceManager;
|
2018-07-13 02:10:35 +02:00
|
|
|
import android.support.annotation.Nullable;
|
2018-06-01 07:34:00 +02:00
|
|
|
import android.support.v7.app.AppCompatDelegate;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
import com.wireguard.android.backend.Backend;
|
2018-02-07 19:19:20 +01:00
|
|
|
import com.wireguard.android.backend.GoBackend;
|
2018-01-01 09:06:37 +01:00
|
|
|
import com.wireguard.android.backend.WgQuickBackend;
|
|
|
|
import com.wireguard.android.configStore.FileConfigStore;
|
|
|
|
import com.wireguard.android.model.TunnelManager;
|
|
|
|
import com.wireguard.android.util.AsyncWorker;
|
|
|
|
import com.wireguard.android.util.RootShell;
|
2018-01-08 07:34:55 +01:00
|
|
|
import com.wireguard.android.util.ToolsInstaller;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
2018-07-25 16:11:37 +02:00
|
|
|
import org.acra.ACRA;
|
|
|
|
import org.acra.annotation.AcraCore;
|
|
|
|
import org.acra.annotation.AcraHttpSender;
|
|
|
|
import org.acra.data.StringFormat;
|
|
|
|
import org.acra.sender.HttpSender;
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
2018-02-07 19:19:20 +01:00
|
|
|
import java.io.File;
|
2018-06-07 04:12:42 +02:00
|
|
|
import java.lang.ref.WeakReference;
|
2018-07-25 16:11:37 +02:00
|
|
|
import java.security.cert.CertificateFactory;
|
|
|
|
import java.security.cert.X509Certificate;
|
2018-07-26 03:17:22 +02:00
|
|
|
|
|
|
|
import java9.util.concurrent.CompletableFuture;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
2018-07-25 16:11:37 +02:00
|
|
|
|
|
|
|
@AcraCore(reportFormat = StringFormat.JSON,
|
|
|
|
buildConfigClass = BuildConfig.class,
|
2018-07-26 03:17:22 +02:00
|
|
|
logcatArguments = {"-b", "all", "-d", "-v", "threadtime", "*:V"},
|
|
|
|
excludeMatchingSharedPreferencesKeys = {"last_used_tunnel", "enabled_configs"})
|
2018-07-25 16:11:37 +02:00
|
|
|
@AcraHttpSender(uri = "https://crashreport.zx2c4.com/android/report",
|
|
|
|
basicAuthLogin = "6RCovLxEVCTXGiW5",
|
|
|
|
basicAuthPassword = "O7I3sVa5ULVdiC51",
|
|
|
|
httpMethod = HttpSender.Method.POST,
|
|
|
|
compress = true)
|
2018-01-01 09:06:37 +01:00
|
|
|
public class Application extends android.app.Application {
|
2018-07-13 02:10:35 +02:00
|
|
|
@SuppressWarnings("NullableProblems") private static WeakReference<Application> weakSelf;
|
|
|
|
@SuppressWarnings("NullableProblems") private AsyncWorker asyncWorker;
|
|
|
|
@SuppressWarnings("NullableProblems") private RootShell rootShell;
|
|
|
|
@SuppressWarnings("NullableProblems") private SharedPreferences sharedPreferences;
|
|
|
|
@SuppressWarnings("NullableProblems") private ToolsInstaller toolsInstaller;
|
|
|
|
@SuppressWarnings("NullableProblems") private TunnelManager tunnelManager;
|
|
|
|
@Nullable private Backend backend;
|
2018-07-26 03:17:22 +02:00
|
|
|
private final CompletableFuture<Backend> futureBackend = new CompletableFuture<>();
|
2018-06-14 04:59:24 +02:00
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public Application() {
|
|
|
|
weakSelf = new WeakReference<>(this);
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-07-25 16:11:37 +02:00
|
|
|
/* The ACRA password can be trivially reverse engineered and is open source anyway,
|
|
|
|
* so there's no point in trying to protect it. However, we do want to at least
|
|
|
|
* prevent innocent self-builders from uploading stuff to our crash reporter. So, we
|
|
|
|
* check the DN of the certs that signed the apk, without even bothering to try
|
|
|
|
* validating that they're authentic. It's a good enough heuristic.
|
|
|
|
*/
|
2018-07-29 15:46:01 +02:00
|
|
|
@SuppressWarnings("deprecation")
|
2018-07-26 13:28:34 +02:00
|
|
|
@Nullable
|
|
|
|
private static String getInstallSource(final Context context) {
|
2018-07-25 16:11:37 +02:00
|
|
|
if (BuildConfig.DEBUG)
|
2018-07-26 13:28:34 +02:00
|
|
|
return null;
|
2018-07-25 16:11:37 +02:00
|
|
|
try {
|
|
|
|
final CertificateFactory cf = CertificateFactory.getInstance("X509");
|
|
|
|
for (final Signature sig : context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES).signatures) {
|
|
|
|
try {
|
|
|
|
for (final String category : ((X509Certificate) cf.generateCertificate(new ByteArrayInputStream(sig.toByteArray()))).getSubjectDN().getName().split(", *")) {
|
|
|
|
final String[] parts = category.split("=", 2);
|
|
|
|
if (!"O".equals(parts[0]))
|
|
|
|
continue;
|
|
|
|
switch (parts[1]) {
|
|
|
|
case "Google Inc.":
|
2018-07-26 13:28:34 +02:00
|
|
|
return "Play Store";
|
2018-07-25 16:11:37 +02:00
|
|
|
case "fdroid.org":
|
2018-07-26 13:28:34 +02:00
|
|
|
return "F-Droid";
|
2018-07-25 16:11:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (final Exception ignored) { }
|
|
|
|
}
|
|
|
|
} catch (final Exception ignored) { }
|
2018-07-26 13:28:34 +02:00
|
|
|
return null;
|
2018-07-25 16:11:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void attachBaseContext(final Context context) {
|
|
|
|
super.attachBaseContext(context);
|
2018-07-28 18:27:45 +02:00
|
|
|
|
|
|
|
if (BuildConfig.MIN_SDK_VERSION > Build.VERSION.SDK_INT) {
|
|
|
|
final Intent intent = new Intent(Intent.ACTION_MAIN);
|
|
|
|
intent.addCategory(Intent.CATEGORY_HOME);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
startActivity(intent);
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
2018-07-26 13:28:34 +02:00
|
|
|
final String installSource = getInstallSource(context);
|
|
|
|
if (installSource != null) {
|
2018-07-25 16:11:37 +02:00
|
|
|
ACRA.init(this);
|
2018-07-26 13:28:34 +02:00
|
|
|
ACRA.getErrorReporter().putCustomData("installSource", installSource);
|
|
|
|
}
|
2018-07-25 16:11:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static Application get() {
|
|
|
|
return weakSelf.get();
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static AsyncWorker getAsyncWorker() {
|
|
|
|
return get().asyncWorker;
|
|
|
|
}
|
2018-01-08 07:34:55 +01:00
|
|
|
|
2018-06-07 04:35:07 +02:00
|
|
|
public static Backend getBackend() {
|
2018-06-14 04:59:24 +02:00
|
|
|
final Application app = get();
|
2018-07-26 03:17:22 +02:00
|
|
|
synchronized (app.futureBackend) {
|
2018-06-14 04:59:24 +02:00
|
|
|
if (app.backend == null) {
|
2018-07-26 03:17:22 +02:00
|
|
|
Backend backend = null;
|
2018-06-14 04:59:24 +02:00
|
|
|
if (new File("/sys/module/wireguard").exists()) {
|
|
|
|
try {
|
|
|
|
app.rootShell.start();
|
2018-07-26 03:17:22 +02:00
|
|
|
backend = new WgQuickBackend(app.getApplicationContext());
|
|
|
|
} catch (final Exception ignored) {
|
2018-07-13 02:10:35 +02:00
|
|
|
}
|
2018-06-14 04:59:24 +02:00
|
|
|
}
|
2018-07-26 03:17:22 +02:00
|
|
|
if (backend == null)
|
|
|
|
backend = new GoBackend(app.getApplicationContext());
|
|
|
|
app.backend = backend;
|
2018-06-14 04:59:24 +02:00
|
|
|
}
|
|
|
|
return app.backend;
|
|
|
|
}
|
2018-06-07 04:35:07 +02:00
|
|
|
}
|
|
|
|
|
2018-07-26 03:17:22 +02:00
|
|
|
public static CompletableFuture<Backend> getBackendAsync() {
|
|
|
|
return get().futureBackend;
|
2018-06-07 04:12:42 +02:00
|
|
|
}
|
2018-06-07 03:27:06 +02:00
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static RootShell getRootShell() {
|
|
|
|
return get().rootShell;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static SharedPreferences getSharedPreferences() {
|
|
|
|
return get().sharedPreferences;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static ToolsInstaller getToolsInstaller() {
|
|
|
|
return get().toolsInstaller;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
public static TunnelManager getTunnelManager() {
|
|
|
|
return get().tunnelManager;
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
|
2018-07-26 03:17:22 +02:00
|
|
|
asyncWorker = new AsyncWorker(AsyncTask.SERIAL_EXECUTOR, new Handler(Looper.getMainLooper()));
|
2018-06-07 04:12:42 +02:00
|
|
|
rootShell = new RootShell(getApplicationContext());
|
|
|
|
toolsInstaller = new ToolsInstaller(getApplicationContext());
|
|
|
|
|
|
|
|
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
|
|
|
AppCompatDelegate.setDefaultNightMode(
|
|
|
|
sharedPreferences.getBoolean("dark_theme", false) ?
|
|
|
|
AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
|
|
|
|
|
2018-07-26 03:17:22 +02:00
|
|
|
tunnelManager = new TunnelManager(new FileConfigStore(getApplicationContext()));
|
2018-06-07 04:12:42 +02:00
|
|
|
tunnelManager.onCreate();
|
2018-07-25 16:11:37 +02:00
|
|
|
|
2018-07-26 03:17:22 +02:00
|
|
|
asyncWorker.supplyAsync(Application::getBackend).thenAccept(backend -> {
|
|
|
|
futureBackend.complete(backend);
|
2018-07-26 13:28:34 +02:00
|
|
|
if (ACRA.isInitialised()) {
|
|
|
|
ACRA.getErrorReporter().putCustomData("backend", backend.getClass().getSimpleName());
|
|
|
|
asyncWorker.supplyAsync(backend::getVersion).thenAccept(version ->
|
|
|
|
ACRA.getErrorReporter().putCustomData("backendVersion", version));
|
|
|
|
}
|
2018-07-25 16:11:37 +02:00
|
|
|
});
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
}
|