summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/util
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2020-03-08 20:20:29 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2020-03-08 20:23:25 -0600
commit7a4af834c213672c65c89b14c75229bf5f2ea4ab (patch)
tree64fad45abe8f1586449b2f1090547d6d57f5b934 /app/src/main/java/com/wireguard/android/util
parent314a0d124dabd11353a120e66bc872f2843bcdb1 (diff)
Backend: do not use singletons
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/util')
-rw-r--r--app/src/main/java/com/wireguard/android/util/ModuleLoader.java15
-rw-r--r--app/src/main/java/com/wireguard/android/util/ToolsInstaller.java13
2 files changed, 16 insertions, 12 deletions
diff --git a/app/src/main/java/com/wireguard/android/util/ModuleLoader.java b/app/src/main/java/com/wireguard/android/util/ModuleLoader.java
index 7c943d26..bf094a5e 100644
--- a/app/src/main/java/com/wireguard/android/util/ModuleLoader.java
+++ b/app/src/main/java/com/wireguard/android/util/ModuleLoader.java
@@ -9,7 +9,6 @@ import android.content.Context;
import android.system.OsConstants;
import android.util.Base64;
-import com.wireguard.android.Application;
import com.wireguard.android.util.RootShell.RootShellException;
import net.i2p.crypto.eddsa.EdDSAEngine;
@@ -43,12 +42,16 @@ public class ModuleLoader {
private static final String MODULE_URL = "https://download.wireguard.com/android-module/%s";
private static final String MODULE_NAME = "wireguard-%s.ko";
+ private final RootShell rootShell;
+ private final String userAgent;
private final File moduleDir;
private final File tmpDir;
- public ModuleLoader(final Context context) {
+ public ModuleLoader(final Context context, final RootShell rootShell, final String userAgent) {
moduleDir = new File(context.getCacheDir(), "kmod");
tmpDir = new File(context.getCacheDir(), "tmp");
+ this.rootShell = rootShell;
+ this.userAgent = userAgent;
}
public boolean moduleMightExist() {
@@ -56,7 +59,7 @@ public class ModuleLoader {
}
public void loadModule() throws IOException, RootShellException {
- Application.getRootShell().run(null, String.format("insmod \"%s/wireguard-$(sha256sum /proc/version|cut -d ' ' -f 1).ko\"", moduleDir.getAbsolutePath()));
+ rootShell.run(null, String.format("insmod \"%s/wireguard-$(sha256sum /proc/version|cut -d ' ' -f 1).ko\"", moduleDir.getAbsolutePath()));
}
public static boolean isModuleLoaded() {
@@ -124,12 +127,12 @@ public class ModuleLoader {
public Integer download() throws IOException, RootShellException, NoSuchAlgorithmException {
final List<String> output = new ArrayList<>();
- Application.getRootShell().run(output, "sha256sum /proc/version|cut -d ' ' -f 1");
+ rootShell.run(output, "sha256sum /proc/version|cut -d ' ' -f 1");
if (output.size() != 1 || output.get(0).length() != 64)
throw new InvalidParameterException("Invalid sha256 of /proc/version");
final String moduleName = String.format(MODULE_NAME, output.get(0));
HttpURLConnection connection = (HttpURLConnection)new URL(MODULE_LIST_URL).openConnection();
- connection.setRequestProperty("User-Agent", Application.USER_AGENT);
+ connection.setRequestProperty("User-Agent", userAgent);
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
throw new IOException("Hash list could not be found");
@@ -146,7 +149,7 @@ public class ModuleLoader {
if (!modules.containsKey(moduleName))
return OsConstants.ENOENT;
connection = (HttpURLConnection)new URL(String.format(MODULE_URL, moduleName)).openConnection();
- connection.setRequestProperty("User-Agent", Application.USER_AGENT);
+ connection.setRequestProperty("User-Agent", userAgent);
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
throw new IOException("Module file could not be found, despite being on hash list");
diff --git a/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java b/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java
index f2fdeddc..5a75c158 100644
--- a/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java
+++ b/app/src/main/java/com/wireguard/android/util/ToolsInstaller.java
@@ -10,7 +10,6 @@ import androidx.annotation.Nullable;
import android.system.OsConstants;
import android.util.Log;
-import com.wireguard.android.Application;
import com.wireguard.android.BuildConfig;
import com.wireguard.android.util.RootShell.RootShellException;
@@ -39,14 +38,16 @@ public final class ToolsInstaller {
private static final String TAG = "WireGuard/" + ToolsInstaller.class.getSimpleName();
private final Context context;
+ private final RootShell rootShell;
private final File localBinaryDir;
private final Object lock = new Object();
@Nullable private Boolean areToolsAvailable;
@Nullable private Boolean installAsMagiskModule;
- public ToolsInstaller(final Context context) {
+ public ToolsInstaller(final Context context, final RootShell rootShell) {
localBinaryDir = new File(context.getCodeCacheDir(), "bin");
this.context = context;
+ this.rootShell = rootShell;
}
@Nullable
@@ -73,7 +74,7 @@ public final class ToolsInstaller {
}
script.append("exit ").append(OsConstants.EALREADY).append(';');
try {
- final int ret = Application.getRootShell().run(null, script.toString());
+ final int ret = rootShell.run(null, script.toString());
if (ret == OsConstants.EALREADY)
return willInstallAsMagiskModule() ? YES | MAGISK : YES | SYSTEM;
else
@@ -124,7 +125,7 @@ public final class ToolsInstaller {
script.append("trap - INT TERM EXIT;");
try {
- return Application.getRootShell().run(null, script.toString()) == 0 ? YES | MAGISK : ERROR;
+ return rootShell.run(null, script.toString()) == 0 ? YES | MAGISK : ERROR;
} catch (final IOException ignored) {
return ERROR;
} catch (final RootShellException e) {
@@ -146,7 +147,7 @@ public final class ToolsInstaller {
new File(localBinaryDir, name), destination, destination, destination));
}
try {
- return Application.getRootShell().run(null, script.toString()) == 0 ? YES | SYSTEM : ERROR;
+ return rootShell.run(null, script.toString()) == 0 ? YES | SYSTEM : ERROR;
} catch (final IOException ignored) {
return ERROR;
} catch (final RootShellException e) {
@@ -183,7 +184,7 @@ public final class ToolsInstaller {
synchronized (lock) {
if (installAsMagiskModule == null) {
try {
- installAsMagiskModule = Application.getRootShell().run(null, "[ -d /sbin/.magisk/mirror -a -d /sbin/.magisk/img -a ! -f /cache/.disable_magisk ]") == OsConstants.EXIT_SUCCESS;
+ installAsMagiskModule = rootShell.run(null, "[ -d /sbin/.magisk/mirror -a -d /sbin/.magisk/img -a ! -f /cache/.disable_magisk ]") == OsConstants.EXIT_SUCCESS;
} catch (final Exception ignored) {
installAsMagiskModule = false;
}