diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-06-02 22:50:10 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-08-19 00:30:43 +0200 |
commit | ca0f785800b76f900bdfda5c35b82b847956f309 (patch) | |
tree | f59770b9e8ba55cb6bd8bedaf21d3a9481604e54 /tunnel/src/main/java/com/wireguard/android | |
parent | b60241655f0e6bc557806df672fd9728d5687011 (diff) |
WIP: DHCP handler
Diffstat (limited to 'tunnel/src/main/java/com/wireguard/android')
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java | 44 |
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) { |