2018-05-02 17:29:58 +02:00
|
|
|
/*
|
2019-01-02 01:57:15 +01:00
|
|
|
* Copyright © 2017-2019 WireGuard LLC. 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;
|
|
|
|
|
2020-03-08 09:46:07 +01:00
|
|
|
import android.app.PendingIntent;
|
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;
|
|
|
|
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;
|
2020-02-15 14:22:06 +01:00
|
|
|
import android.os.StrictMode;
|
2019-10-18 11:28:41 +02:00
|
|
|
import android.util.Log;
|
|
|
|
|
2019-10-12 14:46:04 +02:00
|
|
|
import androidx.preference.PreferenceManager;
|
2018-12-18 11:45:37 +01:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.appcompat.app.AppCompatDelegate;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
2020-03-08 09:46:07 +01:00
|
|
|
import com.wireguard.android.activity.MainActivity;
|
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;
|
2020-03-08 09:46:07 +01:00
|
|
|
import com.wireguard.android.util.ExceptionLoggers;
|
2019-10-13 20:48:09 +02:00
|
|
|
import com.wireguard.android.util.ModuleLoader;
|
2018-01-01 09:06:37 +01:00
|
|
|
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-06-07 04:12:42 +02:00
|
|
|
import java.lang.ref.WeakReference;
|
2019-10-18 11:28:41 +02:00
|
|
|
import java.util.Locale;
|
2018-07-26 03:17:22 +02:00
|
|
|
|
|
|
|
import java9.util.concurrent.CompletableFuture;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
public class Application extends android.app.Application {
|
2019-10-18 11:28:41 +02:00
|
|
|
private static final String TAG = "WireGuard/" + Application.class.getSimpleName();
|
|
|
|
public static final String USER_AGENT;
|
|
|
|
|
|
|
|
static {
|
|
|
|
String preferredAbi = "unknown ABI";
|
|
|
|
if (Build.SUPPORTED_ABIS.length > 0)
|
|
|
|
preferredAbi = Build.SUPPORTED_ABIS[0];
|
|
|
|
USER_AGENT = String.format(Locale.ENGLISH, "WireGuard/%s (Android %d; %s; %s; %s %s; %s)", BuildConfig.VERSION_NAME, Build.VERSION.SDK_INT, preferredAbi, Build.BOARD, Build.MANUFACTURER, Build.MODEL, Build.FINGERPRINT);
|
|
|
|
}
|
|
|
|
|
2018-07-13 02:10:35 +02:00
|
|
|
@SuppressWarnings("NullableProblems") private static WeakReference<Application> weakSelf;
|
2018-09-06 01:33:42 +02:00
|
|
|
private final CompletableFuture<Backend> futureBackend = new CompletableFuture<>();
|
2018-07-13 02:10:35 +02:00
|
|
|
@SuppressWarnings("NullableProblems") private AsyncWorker asyncWorker;
|
2018-09-06 01:33:42 +02:00
|
|
|
@Nullable private Backend backend;
|
2018-07-13 02:10:35 +02:00
|
|
|
@SuppressWarnings("NullableProblems") private RootShell rootShell;
|
|
|
|
@SuppressWarnings("NullableProblems") private SharedPreferences sharedPreferences;
|
|
|
|
@SuppressWarnings("NullableProblems") private ToolsInstaller toolsInstaller;
|
2019-10-13 20:48:09 +02:00
|
|
|
@SuppressWarnings("NullableProblems") private ModuleLoader moduleLoader;
|
2018-07-13 02:10:35 +02:00
|
|
|
@SuppressWarnings("NullableProblems") private TunnelManager tunnelManager;
|
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-09-06 01:33:42 +02:00
|
|
|
public static Application get() {
|
|
|
|
return weakSelf.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static AsyncWorker getAsyncWorker() {
|
|
|
|
return get().asyncWorker;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Backend getBackend() {
|
|
|
|
final Application app = get();
|
|
|
|
synchronized (app.futureBackend) {
|
|
|
|
if (app.backend == null) {
|
|
|
|
Backend backend = null;
|
2019-10-13 20:48:09 +02:00
|
|
|
boolean didStartRootShell = false;
|
2020-02-28 08:46:31 +01:00
|
|
|
if (!ModuleLoader.isModuleLoaded() && app.moduleLoader.moduleMightExist()) {
|
2018-09-06 01:33:42 +02:00
|
|
|
try {
|
|
|
|
app.rootShell.start();
|
2019-10-13 20:48:09 +02:00
|
|
|
didStartRootShell = true;
|
|
|
|
app.moduleLoader.loadModule();
|
|
|
|
} catch (final Exception ignored) {
|
|
|
|
}
|
|
|
|
}
|
2020-02-28 08:46:31 +01:00
|
|
|
if (ModuleLoader.isModuleLoaded()) {
|
2019-10-13 20:48:09 +02:00
|
|
|
try {
|
|
|
|
if (!didStartRootShell)
|
|
|
|
app.rootShell.start();
|
2018-09-06 01:33:42 +02:00
|
|
|
backend = new WgQuickBackend(app.getApplicationContext());
|
|
|
|
} catch (final Exception ignored) {
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 09:46:07 +01:00
|
|
|
if (backend == null) {
|
2020-03-09 02:52:57 +01:00
|
|
|
backend = new GoBackend(app.getApplicationContext());
|
2020-03-08 09:46:07 +01:00
|
|
|
GoBackend.setAlwaysOnCallback(() -> {
|
|
|
|
get().tunnelManager.restoreState(true).whenComplete(ExceptionLoggers.D);
|
|
|
|
});
|
|
|
|
}
|
2018-09-06 01:33:42 +02:00
|
|
|
app.backend = backend;
|
|
|
|
}
|
|
|
|
return app.backend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static CompletableFuture<Backend> getBackendAsync() {
|
|
|
|
return get().futureBackend;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static RootShell getRootShell() {
|
|
|
|
return get().rootShell;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static SharedPreferences getSharedPreferences() {
|
|
|
|
return get().sharedPreferences;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ToolsInstaller getToolsInstaller() {
|
|
|
|
return get().toolsInstaller;
|
|
|
|
}
|
2019-10-13 20:48:09 +02:00
|
|
|
public static ModuleLoader getModuleLoader() {
|
|
|
|
return get().moduleLoader;
|
|
|
|
}
|
2018-09-06 01:33:42 +02:00
|
|
|
|
|
|
|
public static TunnelManager getTunnelManager() {
|
|
|
|
return get().tunnelManager;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2020-02-15 14:22:06 +01:00
|
|
|
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
|
|
|
|
}
|
2018-07-25 16:11:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-07 04:12:42 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2019-10-18 11:28:41 +02:00
|
|
|
Log.i(TAG, USER_AGENT);
|
2018-06-07 04:12:42 +02:00
|
|
|
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());
|
2019-10-13 20:48:09 +02:00
|
|
|
moduleLoader = new ModuleLoader(getApplicationContext());
|
2018-06-07 04:12:42 +02:00
|
|
|
|
|
|
|
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
|
2019-10-12 14:58:47 +02:00
|
|
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(
|
|
|
|
sharedPreferences.getBoolean("dark_theme", false) ?
|
|
|
|
AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO);
|
|
|
|
} else {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
|
|
|
|
}
|
2018-06-07 04:12:42 +02:00
|
|
|
|
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-12-08 03:16:23 +01:00
|
|
|
asyncWorker.supplyAsync(Application::getBackend).thenAccept(futureBackend::complete);
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
}
|