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 +++++++------ tunnel/tools/libwg-go/dhcp.go | 9 ++++--- .../android/databinding/BindingAdapters.kt | 7 +++++ ui/src/main/res/layout/tunnel_detail_fragment.xml | 2 +- 5 files changed, 48 insertions(+), 20 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 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()); } } diff --git a/tunnel/tools/libwg-go/dhcp.go b/tunnel/tools/libwg-go/dhcp.go index b7919151..4af5b0cb 100644 --- a/tunnel/tools/libwg-go/dhcp.go +++ b/tunnel/tools/libwg-go/dhcp.go @@ -12,6 +12,7 @@ import ( "github.com/insomniacslk/dhcp/iana" gen "golang.zx2c4.com/wireguard/android/gen" + "google.golang.org/protobuf/types/known/durationpb" ) const ( @@ -131,13 +132,13 @@ func getAddressesFromReply(reply *dhcpv6.Message) []*gen.Lease{ ianaOpts := iana.Options.Get(dhcpv6.OptionIAAddr) for _, opt := range ianaOpts { - addr :=opt.(*dhcpv6.OptIAAddress).IPv6Addr + addrOpt := opt.(*dhcpv6.OptIAAddress) lease := &gen.Lease{ Address: &gen.InetAddress{ - Address: addr, + Address: addrOpt.IPv6Addr, }, - // PreferredLifetime: , - // ValidLifetime: , + PreferredLifetime: durationpb.New(addrOpt.PreferredLifetime), + ValidLifetime: durationpb.New(addrOpt.ValidLifetime), } leases = append(leases, lease) } diff --git a/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt b/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt index afba41cb..6b2040e1 100644 --- a/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt +++ b/ui/src/main/java/com/wireguard/android/databinding/BindingAdapters.kt @@ -20,6 +20,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.wireguard.android.BR import com.wireguard.android.R +import com.wireguard.android.backend.Dhcp; import com.wireguard.android.databinding.ObservableKeyedRecyclerViewAdapter.RowConfigurationHandler import com.wireguard.android.widget.ToggleSwitch import com.wireguard.android.widget.ToggleSwitch.OnBeforeCheckedChangeListener @@ -169,6 +170,12 @@ object BindingAdapters { view.text = if (strings != null) Attribute.join(strings) else "" } + @JvmStatic + @BindingAdapter("android:text") + fun setDhcpLeaseSetText(view: TextView, dhcp: Dhcp?) { + view.text = if (dhcp?.leases != null) Attribute.join(dhcp.leases.map { it }) else "" + } + @JvmStatic fun tryParseInt(s: String?): Int { if (s == null) diff --git a/ui/src/main/res/layout/tunnel_detail_fragment.xml b/ui/src/main/res/layout/tunnel_detail_fragment.xml index 425b364d..9b17a06c 100644 --- a/ui/src/main/res/layout/tunnel_detail_fragment.xml +++ b/ui/src/main/res/layout/tunnel_detail_fragment.xml @@ -171,7 +171,7 @@ android:nextFocusDown="@id/dns_servers_text" android:nextFocusForward="@id/dns_servers_text" android:onClick="@{ClipboardUtils::copyTextView}" - android:text="@{tunnel.dhcp.addresses}" + android:text="@{tunnel.dhcp}" android:textAppearance="?attr/textAppearanceBodyLarge" android:visibility="@{tunnel.dhcp == null ? android.view.View.GONE : android.view.View.VISIBLE}" app:layout_constraintStart_toStartOf="parent" -- cgit v1.2.3