diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-03-07 20:18:07 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-07-05 21:41:01 +0200 |
commit | 56825456261abc1c321ee1aa68e5e9389487edf3 (patch) | |
tree | 35e07d7db15378745794787ffe669e69abf5a2c4 /tunnel/src/main | |
parent | 1ff676630ddfc17633042769ec24d5abd30c93da (diff) |
WIP: add valid and preferred lifetime to dhcp leases
Diffstat (limited to 'tunnel/src/main')
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/Dhcp.java | 31 | ||||
-rw-r--r-- | tunnel/src/main/java/com/wireguard/android/backend/GoBackend.java | 19 |
2 files changed, 35 insertions, 15 deletions
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<InetNetwork> addresses; + private Set<Lease> leases = new LinkedHashSet<>(); - Dhcp(Set<InetNetwork> 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<InetNetwork> getAddresses() { - return addresses; + public final Set<Lease> 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<InetNetwork> 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<InetNetwork> leases) throws PackageManager.NameNotFoundException { + private VpnService.Builder getBuilder(final String name, @Nullable final Config config, final VpnService service, @Nullable final Set<Dhcp.Lease> 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()); } } |