diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-03-07 20:18:07 +0100 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2023-08-19 00:30:43 +0200 |
commit | 4910edc398a9ad74414d5b198286055f182dea97 (patch) | |
tree | ef74873a0aba3042f1485378576726a5964a14dd | |
parent | ca0f785800b76f900bdfda5c35b82b847956f309 (diff) |
WIP: add valid and preferred lifetime to dhcp leases
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<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()); } } 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 @@ -170,6 +171,12 @@ object BindingAdapters { } @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) return 0 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" |