diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-06-19 22:07:07 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-06-19 23:45:25 +0200 |
commit | e586a1d0baeea07432b173049e54947bca1ecef5 (patch) | |
tree | b48c46f69317d06f837646be84d09b0f9d9f2d34 /tunnel/src | |
parent | 5fc369349be0558444ba5d94854074469ea04d0d (diff) |
WIP: Delay DHCP renew until VPN network available
Diffstat (limited to 'tunnel/src')
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java | 42 |
1 files changed, 33 insertions, 9 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 11928d46..79b724e8 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -434,6 +434,7 @@ public final class GoBackend implements Backend { Log.i(TAG, "Dhcp: " + resp.getError().getMessage()); Dhcp dhcp = new Dhcp(); + long delayMillis = 1000 * 3600 * 12; // Max renew 12h if (resp.getLeasesList() != null) { for (final Lease lease: resp.getLeasesList()) { @@ -443,6 +444,8 @@ public final class GoBackend implements Backend { dhcp.addLease(new InetNetwork(addr, 128), Duration.ofSeconds(lease.getValidLifetime().getSeconds(), lease.getValidLifetime().getNanos()), Duration.ofSeconds(lease.getPreferredLifetime().getSeconds(), lease.getPreferredLifetime().getNanos())); + long leaseDelayMillis = lease.getValidLifetime().getSeconds() * 1000 + lease.getValidLifetime().getNanos() / 1000; + delayMillis = Math.min(delayMillis, leaseDelayMillis); } catch (UnknownHostException ex) { // Ignore } @@ -468,11 +471,8 @@ public final class GoBackend implements Backend { service.protect(wgGetSocketV6(currentTunnelHandle)); Log.i(TAG, "Dhcp done"); - // TODO fetch expiration time from Dhcp leases - long delayMillis = 1000 * 3600 * 12; // Renew 3/4 * 12h - //long delayMillis = lease.getValidLifetime().getSeconds() * 1000 + lease.getValidLifetime().getNanos() / 1000; - AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); + am.cancel(alarmListener); am.setWindow(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + delayMillis * 3 / 4, delayMillis / 4, null, alarmListener, handler); if (bgp != null) { @@ -682,7 +682,11 @@ public final class GoBackend implements Backend { activeNetwork = connectivityManager.getActiveNetwork(); - if (!connectivityManager.getNetworkCapabilities(activeNetwork).hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) { + if (activeNetwork == null) { + Log.w(TAG, "Null activeNetwork"); + activeNetwork = null; + } + else if (!connectivityManager.getNetworkCapabilities(activeNetwork).hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN)) { Log.w(TAG, "VPN network is active, null activeNetwork"); activeNetwork = null; } @@ -737,10 +741,16 @@ public final class GoBackend implements Backend { public void handleMessage(Message msg) { switch (msg.what) { case MSG_DHCP_EXPIRE: + Log.w(TAG, "DHCP expire: " + ((activeNetwork != null) ? activeNetwork : "disabled")); try { - Log.w(TAG, "Before Dhcp"); - Dhcp(service); // Renew addresses - Log.w(TAG, "After Dhcp"); + if (activeNetwork != null) { + Log.w(TAG, "DHCP before"); + Dhcp(service); // Renew addresses + Log.w(TAG, "DHCP after"); + } else { + Log.w(TAG, "DHCP delay obtain lease"); + obtainDhcpLease = true; + } } catch (Exception ex) { Log.e(TAG, "DHCP failed: " + ex); } @@ -907,7 +917,7 @@ public final class GoBackend implements Backend { @Override public void onAvailable(Network network) { Log.w(TAG, "VPN onAvailable: " + network); - if (obtainDhcpLease) { + if (obtainDhcpLease && activeNetwork != null) { Log.w(TAG, "Obtaindhcplease"); try { Log.w(TAG, "Before Dhcp"); @@ -928,6 +938,20 @@ public final class GoBackend implements Backend { } @Override + public void onLosing(Network network, int maxMsToLive) { + Log.w(TAG, "onLosing: " + network + " maxMsToLive: " + maxMsToLive); + // TODO release DHCP? + } + + @Override + public void onLost(Network network) { + Log.w(TAG, "onLost: " + network + " (" + activeNetwork + ")"); + if (network.equals(activeNetwork)) { + activeNetwork = null; + } + } + + @Override public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) { Log.w(TAG, "onLinkPropertiesChanged: " + network + " is default:" + (network.equals(activeNetwork))); if (network.equals(activeNetwork) && currentConfig != null && currentTunnelHandle > -1) { |