summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-05-26 18:03:44 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2021-05-26 18:07:28 +0200
commitfe61522f2aceea1b2681fe8493a54d321b952218 (patch)
tree8920334e8b6ee4632a7d9427a98a2465f3a286d1
parent24ded8070f0c584b80ce692c41fc56e52a3ab315 (diff)
tunnel: retry DNS resolution for 10 seconds
This has several problems: 1) it blocks the main thread; 2) it doesn't distinguish between a permanent error and a transient one; 3) the 10 seconds is hard coded; 4) there's no way for the user to cancel it. We'll have to improve this. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/BackendException.java3
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java21
-rw-r--r--ui/src/main/java/com/wireguard/android/util/ErrorMessages.kt3
-rw-r--r--ui/src/main/res/values/strings.xml1
4 files changed, 26 insertions, 2 deletions
diff --git a/tunnel/src/main/java/com/wireguard/android/backend/BackendException.java b/tunnel/src/main/java/com/wireguard/android/backend/BackendException.java
index b64ebb47..52d084cd 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/BackendException.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/BackendException.java
@@ -55,6 +55,7 @@ public final class BackendException extends Exception {
VPN_NOT_AUTHORIZED,
UNABLE_TO_START_VPN,
TUN_CREATION_ERROR,
- GO_ACTIVATION_ERROR_CODE
+ GO_ACTIVATION_ERROR_CODE,
+ DNS_RESOLUTION_FAILURE,
}
}
diff --git a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
index 8b9213db..dfe217a3 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -16,6 +16,7 @@ import com.wireguard.android.backend.BackendException.Reason;
import com.wireguard.android.backend.Tunnel.State;
import com.wireguard.android.util.SharedLibraryLoader;
import com.wireguard.config.Config;
+import com.wireguard.config.InetEndpoint;
import com.wireguard.config.InetNetwork;
import com.wireguard.config.Peer;
import com.wireguard.crypto.Key;
@@ -40,6 +41,7 @@ import androidx.collection.ArraySet;
*/
@NonNullForAll
public final class GoBackend implements Backend {
+ private static final int DNS_RESOLUTION_RETRIES = 10;
private static final String TAG = "WireGuard/GoBackend";
@Nullable private static AlwaysOnCallback alwaysOnCallback;
private static GhettoCompletableFuture<VpnService> vpnService = new GhettoCompletableFuture<>();
@@ -234,6 +236,25 @@ public final class GoBackend implements Backend {
return;
}
+
+ dnsRetry: for (int i = 0; i < DNS_RESOLUTION_RETRIES; ++i) {
+ // Pre-resolve IPs so they're cached when building the userspace string
+ for (final Peer peer : config.getPeers()) {
+ final InetEndpoint ep = peer.getEndpoint().orElse(null);
+ if (ep == null)
+ continue;
+ if (ep.getResolved().orElse(null) == null) {
+ if (i < DNS_RESOLUTION_RETRIES - 1) {
+ Log.w(TAG, "DNS host \"" + ep.getHost() + "\" failed to resolve; trying again");
+ Thread.sleep(1000);
+ continue dnsRetry;
+ } else
+ throw new BackendException(Reason.DNS_RESOLUTION_FAILURE, ep.getHost());
+ }
+ }
+ break;
+ }
+
// Build config
final String goConfig = config.toWgUserspaceString();
diff --git a/ui/src/main/java/com/wireguard/android/util/ErrorMessages.kt b/ui/src/main/java/com/wireguard/android/util/ErrorMessages.kt
index 1ee0dafc..d8d53b09 100644
--- a/ui/src/main/java/com/wireguard/android/util/ErrorMessages.kt
+++ b/ui/src/main/java/com/wireguard/android/util/ErrorMessages.kt
@@ -36,7 +36,8 @@ object ErrorMessages {
BackendException.Reason.VPN_NOT_AUTHORIZED to R.string.vpn_not_authorized_error,
BackendException.Reason.UNABLE_TO_START_VPN to R.string.vpn_start_error,
BackendException.Reason.TUN_CREATION_ERROR to R.string.tun_create_error,
- BackendException.Reason.GO_ACTIVATION_ERROR_CODE to R.string.tunnel_on_error
+ BackendException.Reason.GO_ACTIVATION_ERROR_CODE to R.string.tunnel_on_error,
+ BackendException.Reason.DNS_RESOLUTION_FAILURE to R.string.tunnel_dns_failure
)
private val KFE_FORMAT_MAP = mapOf(
Key.Format.BASE64 to R.string.key_length_explanation_base64,
diff --git a/ui/src/main/res/values/strings.xml b/ui/src/main/res/values/strings.xml
index 812b2192..bb4466aa 100644
--- a/ui/src/main/res/values/strings.xml
+++ b/ui/src/main/res/values/strings.xml
@@ -214,6 +214,7 @@
<string name="tunnel_list_placeholder">Add a tunnel using the blue button</string>
<string name="tunnel_name">Tunnel Name</string>
<string name="tunnel_on_error">Unable to turn tunnel on (wgTurnOn returned %d)</string>
+ <string name="tunnel_dns_failure">Unable to resolve DNS hostname: ā€œ%sā€</string>
<string name="tunnel_rename_error">Unable to rename tunnel: %s</string>
<string name="tunnel_rename_success">Successfully renamed tunnel to ā€œ%sā€</string>
<string name="type_name_go_userspace">Go userspace</string>