summaryrefslogtreecommitdiffhomepage
path: root/tunnel
diff options
context:
space:
mode:
authorMikael Magnusson <mikma@users.sourceforge.net>2023-06-02 22:50:10 +0200
committerMikael Magnusson <mikma@users.sourceforge.net>2023-06-02 23:48:11 +0200
commite2e31bef58cc00bf814239aef3d97fff69debc67 (patch)
tree0a83dc447fc1c5fac4a7f48308d8bd9944124047 /tunnel
parenteb0a693758b7bd6087440afc93f0b26a3a7dcc62 (diff)
WIP: DHCP handler
Diffstat (limited to 'tunnel')
-rw-r--r--tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java44
1 files changed, 44 insertions, 0 deletions
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 6bd42936..327383cd 100644
--- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
+++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java
@@ -18,6 +18,9 @@ import android.net.ProxyInfo;
import android.net.TrafficStats;
import android.net.Uri;
import android.os.Build;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.os.Process;
import android.system.OsConstants;
@@ -104,6 +107,7 @@ public final class GoBackend implements Backend {
private static final int DNS_RESOLUTION_RETRIES = 10;
private static final String TAG = "WireGuard/GoBackend";
private static final int STATS_TAG = 2;
+ private static final int MSG_DHCP_EXPIRE = 1;
@Nullable private static AlwaysOnCallback alwaysOnCallback;
private static GhettoCompletableFuture<VpnService> vpnService = new GhettoCompletableFuture<>();
private final Context context;
@@ -117,6 +121,8 @@ public final class GoBackend implements Backend {
private ManagedChannel channel;
private boolean obtainDhcpLease = false;
@Nullable private Bgp bgp;
+ private HandlerThread thread;
+ private Handler handler;
/**
* Public constructor for GoBackend.
@@ -455,6 +461,12 @@ public final class GoBackend implements Backend {
service.protect(wgGetSocketV6(currentTunnelHandle));
Log.i(TAG, "Dhcp done");
+ Message msg = Message.obtain(handler, MSG_DHCP_EXPIRE);
+
+ // TODO fetch expiration time from Dhcp leases
+ long delayMillis = 1000 * 3600 * 12 * 3 / 4; // Renew 3/4 * 12h
+ handler.sendMessageDelayed(msg, delayMillis);
+
bgp = new Bgp(this, channel, currentTunnel, currentTunnelHandle);
bgp.startServer();
@@ -708,6 +720,27 @@ public final class GoBackend implements Backend {
obtainDhcpLease = true;
+ thread = new HandlerThread("GoBackend HandlerThread");
+ thread.start();
+ handler = new Handler(thread.getLooper()) {
+ public void handleMessage(Message msg) {
+ switch (msg.what) {
+ case MSG_DHCP_EXPIRE:
+ try {
+ Log.w(TAG, "Before Dhcp");
+ Dhcp(service); // Renew addresses
+ Log.w(TAG, "After Dhcp");
+ } catch (Exception ex) {
+ Log.e(TAG, "DHCP failed: " + ex);
+ }
+ break;
+ default:
+ Log.w(TAG, "Unknown message: " + msg.what);
+ break;
+ }
+ }
+ };
+
NetworkRequest req = new NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN).build();
connectivityManager.requestNetwork(req, myNetworkCallback);
@@ -723,6 +756,11 @@ public final class GoBackend implements Backend {
currentTunnel = null;
currentTunnelHandle = -1;
currentConfig = null;
+ if (thread != null) {
+ handler = null;
+ thread.quit();
+ thread = null;
+ }
if (bgp != null) {
bgp.stopServer();
bgp = null;
@@ -802,6 +840,12 @@ public final class GoBackend implements Backend {
owner.bgp.stopServer();
owner.bgp = null;
}
+ if (owner.thread != null) {
+ owner.handler = null;
+ owner.thread.quit();
+ owner.thread = null;
+ }
+
owner.stopHttpProxy();
final Tunnel tunnel = owner.currentTunnel;
if (tunnel != null) {