From 56825456261abc1c321ee1aa68e5e9389487edf3 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Tue, 7 Mar 2023 20:18:07 +0100 Subject: WIP: add valid and preferred lifetime to dhcp leases --- .../java/com/wireguard/android/backend/Dhcp.java | 31 ++++++++++++++++++---- .../com/wireguard/android/backend/GoBackend.java | 19 +++++++------ 2 files changed, 35 insertions(+), 15 deletions(-) (limited to 'tunnel/src/main') diff --git a/tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java b/tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java index 59a3e69c..4cb2a5a0 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java @@ -8,6 +8,7 @@ package com.wireguard.android.backend; import com.wireguard.config.InetNetwork; import com.wireguard.util.NonNullForAll; +import java.util.LinkedHashSet; import java.util.Set; /** @@ -15,14 +16,34 @@ import java.util.Set; */ @NonNullForAll public class Dhcp { - private Set addresses; + private Set leases = new LinkedHashSet<>(); - Dhcp(Set addresses) { - this.addresses = addresses; + public class Lease { + private InetNetwork address; + private long valid; + private long preferred; + + public Lease(InetNetwork address, long valid, long preferred) { + this.address = address; + this.valid = valid; + this.preferred = preferred; + } + + public final InetNetwork getAddress() { + return this.address; + } + + public String toString() { + return address.toString() + " (valid " + this.valid + "s, preferred " + this.preferred + "s)"; + } + } + + public void addLease(InetNetwork address, long valid, long preferred) { + this.leases.add(new Lease(address, valid, preferred)); } - public Set getAddresses() { - return addresses; + public final Set getLeases() { + return leases; } public String toString() { 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 327383cd..040a316d 100644 --- a/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java +++ b/tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java @@ -81,7 +81,6 @@ import java.net.URL; import java.time.Instant; import java.nio.ByteOrder; import java.util.Collections; -import java.util.LinkedHashSet; import java.util.List; import java.util.Optional; import java.util.Set; @@ -427,23 +426,22 @@ public final class GoBackend implements Backend { DhcpResponse resp = stub.dhcp(request); Log.i(TAG, "Dhcp: " + resp.getError().getMessage()); - Set addresses = new LinkedHashSet<>(); + Dhcp dhcp = new Dhcp(); + if (resp.getLeasesList() != null) { for (final Lease lease: resp.getLeasesList()) { try { InetAddress addr = InetAddress.getByAddress(lease.getAddress().getAddress().toByteArray()); - Log.i(TAG, "Lease: " + addr); - addresses.add(new InetNetwork(addr, 128)); + Log.i(TAG, "Lease: " + addr + " " + lease.getValidLifetime().getSeconds() + " " + lease.getPreferredLifetime().getSeconds()); + dhcp.addLease(new InetNetwork(addr, 128), lease.getValidLifetime().getSeconds(), lease.getPreferredLifetime().getSeconds()); } catch (UnknownHostException ex) { // Ignore } } } - Dhcp dhcp = new Dhcp(addresses); - // Replace the vpn tunnel - final VpnService.Builder builder = getBuilder(currentTunnel.getName(), currentConfig, service, dhcp.getAddresses()); + final VpnService.Builder builder = getBuilder(currentTunnel.getName(), currentConfig, service, dhcp.getLeases()); Log.i(TAG, "Builder: " + builder); @@ -555,7 +553,7 @@ public final class GoBackend implements Backend { Log.i(TAG, "Exit streamReverse"); } - private VpnService.Builder getBuilder(final String name, @Nullable final Config config, final VpnService service, @Nullable final Set leases) throws PackageManager.NameNotFoundException { + private VpnService.Builder getBuilder(final String name, @Nullable final Config config, final VpnService service, @Nullable final Set leases) throws PackageManager.NameNotFoundException { Log.i(TAG, "Builder 1"); final VpnService.Builder builder = service.getBuilder(); Log.i(TAG, "Builder 2"); @@ -571,8 +569,9 @@ public final class GoBackend implements Backend { Log.i(TAG, "Builder 5"); if (leases != null) { - for (final InetNetwork lease: leases) { - builder.addAddress(lease.getAddress(), lease.getMask()); + for (final Dhcp.Lease lease: leases) { + InetNetwork addr = lease.getAddress(); + builder.addAddress(addr.getAddress(), addr.getMask()); } } -- cgit v1.2.3