diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-03-09 00:52:33 +0800 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2020-03-09 00:58:32 +0800 |
commit | afd75cc4cf7dc59a4d31a133b56d6e045bbe8786 (patch) | |
tree | e643979ec5dacbe1159486c127d6a5cc5ddc10ed /app/src/main/java/com/wireguard/android/backend | |
parent | 453a1aaa6523c19697b6463d492bc54346e89690 (diff) |
ErrorMessages: do not use R from backend
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/android/backend')
4 files changed, 45 insertions, 32 deletions
diff --git a/app/src/main/java/com/wireguard/android/backend/Backend.java b/app/src/main/java/com/wireguard/android/backend/Backend.java index 7569b5fa..84e32eb2 100644 --- a/app/src/main/java/com/wireguard/android/backend/Backend.java +++ b/app/src/main/java/com/wireguard/android/backend/Backend.java @@ -42,13 +42,6 @@ public interface Backend { Statistics getStatistics(Tunnel tunnel) throws Exception; /** - * Determine type name of underlying backend. - * - * @return Type name - */ - String getTypePrettyName(); - - /** * Determine version of underlying backend. * * @return The version of the backend. diff --git a/app/src/main/java/com/wireguard/android/backend/BackendException.java b/app/src/main/java/com/wireguard/android/backend/BackendException.java new file mode 100644 index 00000000..2994e02a --- /dev/null +++ b/app/src/main/java/com/wireguard/android/backend/BackendException.java @@ -0,0 +1,31 @@ +/* + * Copyright © 2020 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.wireguard.android.backend; + +public final class BackendException extends Exception { + public enum Reason { + UNKNOWN_KERNEL_MODULE_NAME, + WG_QUICK_CONFIG_ERROR_CODE, + TUNNEL_MISSING_CONFIG, + VPN_NOT_AUTHORIZED, + UNABLE_TO_START_VPN, + TUN_CREATION_ERROR, + GO_ACTIVATION_ERROR_CODE + + } + private final Reason reason; + private final Object[] format; + public BackendException(final Reason reason, final Object ...format) { + this.reason = reason; + this.format = format; + } + public Reason getReason() { + return reason; + } + public Object[] getFormat() { + return format; + } +} diff --git a/app/src/main/java/com/wireguard/android/backend/GoBackend.java b/app/src/main/java/com/wireguard/android/backend/GoBackend.java index 6bbf5a3b..00c1d217 100644 --- a/app/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -14,10 +14,8 @@ import androidx.annotation.Nullable; import androidx.collection.ArraySet; import android.util.Log; -import com.wireguard.android.Application; -import com.wireguard.android.R; +import com.wireguard.android.backend.BackendException.Reason; import com.wireguard.android.backend.Tunnel.State; -import com.wireguard.android.util.ExceptionLoggers; import com.wireguard.android.util.SharedLibraryLoader; import com.wireguard.config.Config; import com.wireguard.config.InetNetwork; @@ -28,7 +26,6 @@ import com.wireguard.crypto.KeyFormatException; import java.net.InetAddress; import java.util.Collections; import java.util.HashSet; -import java.util.Objects; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -131,11 +128,6 @@ public final class GoBackend implements Backend { } @Override - public String getTypePrettyName() { - return context.getString(R.string.type_name_go_userspace); - } - - @Override public String getVersion() { return wgVersion(); } @@ -171,10 +163,11 @@ public final class GoBackend implements Backend { Log.i(TAG, "Bringing tunnel " + tunnel.getName() + " " + state); if (state == State.UP) { - Objects.requireNonNull(config, context.getString(R.string.no_config_error)); + if (config == null) + throw new BackendException(Reason.TUNNEL_MISSING_CONFIG); if (VpnService.prepare(context) != null) - throw new Exception(context.getString(R.string.vpn_not_authorized_error)); + throw new BackendException(Reason.VPN_NOT_AUTHORIZED); final VpnService service; if (!vpnService.isDone()) @@ -183,7 +176,9 @@ public final class GoBackend implements Backend { try { service = vpnService.get(2, TimeUnit.SECONDS); } catch (final TimeoutException e) { - throw new Exception(context.getString(R.string.vpn_start_error), e); + final Exception be = new BackendException(Reason.UNABLE_TO_START_VPN); + be.initCause(e); + throw be; } service.setOwner(this); @@ -225,12 +220,12 @@ public final class GoBackend implements Backend { builder.setBlocking(true); try (final ParcelFileDescriptor tun = builder.establish()) { if (tun == null) - throw new Exception(context.getString(R.string.tun_create_error)); + throw new BackendException(Reason.TUN_CREATION_ERROR); Log.d(TAG, "Go backend v" + wgVersion()); currentTunnelHandle = wgTurnOn(tunnel.getName(), tun.detachFd(), goConfig); } if (currentTunnelHandle < 0) - throw new Exception(context.getString(R.string.tunnel_on_error, currentTunnelHandle)); + throw new BackendException(Reason.GO_ACTIVATION_ERROR_CODE, currentTunnelHandle); currentTunnel = tunnel; currentConfig = config; diff --git a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java index e04e6658..5769da66 100644 --- a/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java +++ b/app/src/main/java/com/wireguard/android/backend/WgQuickBackend.java @@ -5,12 +5,13 @@ package com.wireguard.android.backend; -import android.content.Context; import androidx.annotation.Nullable; + +import android.content.Context; import android.util.Log; import com.wireguard.android.Application; -import com.wireguard.android.R; +import com.wireguard.android.backend.BackendException.Reason; import com.wireguard.android.backend.Tunnel.State; import com.wireguard.config.Config; import com.wireguard.crypto.Key; @@ -40,13 +41,11 @@ public final class WgQuickBackend implements Backend { private static final String TAG = "WireGuard/" + WgQuickBackend.class.getSimpleName(); private final File localTemporaryDir; - private final Context context; private final Map<Tunnel, Config> runningConfigs = new HashMap<>(); private final Set<TunnelStateChangeNotificationReceiver> notifiers = new HashSet<>(); public WgQuickBackend(final Context context) { localTemporaryDir = new File(context.getCacheDir(), "tmp"); - this.context = context; } @Override @@ -93,16 +92,11 @@ public final class WgQuickBackend implements Backend { } @Override - public String getTypePrettyName() { - return context.getString(R.string.type_name_kernel_module); - } - - @Override public String getVersion() throws Exception { final List<String> output = new ArrayList<>(); if (Application.getRootShell() .run(output, "cat /sys/module/wireguard/version") != 0 || output.isEmpty()) - throw new Exception(context.getString(R.string.module_version_error)); + throw new BackendException(Reason.UNKNOWN_KERNEL_MODULE_NAME); return output.get(0); } @@ -150,7 +144,7 @@ public final class WgQuickBackend implements Backend { // noinspection ResultOfMethodCallIgnored tempFile.delete(); if (result != 0) - throw new Exception(context.getString(R.string.tunnel_config_error, result)); + throw new BackendException(Reason.WG_QUICK_CONFIG_ERROR_CODE, result); if (state == State.UP) runningConfigs.put(tunnel, config); |