From 6141eafb86c9dc72d39784b86d2136b25ea269f6 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Sat, 13 Mar 2021 21:58:21 +0000 Subject: dhcp WIP: implement wgOnEvent WIP: debug WIP: netstack: implement dhcp and ipvlan WIP: implement dhcp in tunnel WIP: add dhcp information to detail --- .../android/fragment/TunnelDetailFragment.kt | 4 +++ .../wireguard/android/model/ObservableTunnel.kt | 30 ++++++++++++++++++++ .../com/wireguard/android/model/TunnelManager.kt | 5 ++++ ui/src/main/res/layout/tunnel_detail_fragment.xml | 32 ++++++++++++++++++++-- ui/src/main/res/values/strings.xml | 2 ++ 5 files changed, 71 insertions(+), 2 deletions(-) (limited to 'ui/src') diff --git a/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt b/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt index fad1b5d4..f2cf9040 100644 --- a/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt +++ b/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.kt @@ -104,6 +104,10 @@ class TunnelDetailFragment : BaseFragment() { lastState = state var now = LocalDateTime.now(ZoneId.of("UTC")) try { + binding.dhcpInfoText.text = tunnel.getDhcpInfo().get() + binding.dhcpInfoLabel.visibility = View.VISIBLE + binding.dhcpInfoText.visibility = View.VISIBLE + val statistics = tunnel.getStatisticsAsync() for (i in 0 until binding.peersLayout.childCount) { val peer: TunnelDetailPeerBinding = DataBindingUtil.getBinding(binding.peersLayout.getChildAt(i)) diff --git a/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt b/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt index 252e8759..edf8feaf 100644 --- a/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt +++ b/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt @@ -8,6 +8,7 @@ import android.util.Log import androidx.databinding.BaseObservable import androidx.databinding.Bindable import com.wireguard.android.BR +import com.wireguard.android.backend.DhcpInfo import com.wireguard.android.backend.Statistics import com.wireguard.android.backend.Tunnel import com.wireguard.android.databinding.Keyed @@ -136,6 +137,35 @@ class ObservableTunnel internal constructor( return statistics } + @get:Bindable + var dhcpInfo: DhcpInfo? = null + get() { + if (field == null || field?.isStale != false) + applicationScope.launch { + try { + manager.getTunnelDhcpInfo(this@ObservableTunnel) + } catch (e: Throwable) { + Log.e(TAG, Log.getStackTraceString(e)) + } + } + return field + } + private set + + suspend fun getDhcpInfo(): DhcpInfo = withContext(Dispatchers.Main.immediate) { + dhcpInfo.let { + if (it == null || it.isStale) + manager.getTunnelDhcpInfo(this@ObservableTunnel) + else + it + } + } + + fun onDhcpInfoChanged(dhcpInfo: DhcpInfo?): DhcpInfo? { + this.dhcpInfo = dhcpInfo + notifyPropertyChanged(BR.dhcpInfo) + return dhcpInfo + } suspend fun deleteAsync() = manager.delete(this) diff --git a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt index ec796164..ac39ac4a 100644 --- a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt +++ b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt @@ -17,6 +17,7 @@ import com.wireguard.android.Application.Companion.getBackend import com.wireguard.android.Application.Companion.getTunnelManager import com.wireguard.android.BR import com.wireguard.android.R +import com.wireguard.android.backend.DhcpInfo import com.wireguard.android.backend.Statistics import com.wireguard.android.backend.Tunnel import com.wireguard.android.configStore.ConfigStore @@ -240,6 +241,10 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() { } } + suspend fun getTunnelDhcpInfo(tunnel: ObservableTunnel): DhcpInfo = withContext(Dispatchers.Main.immediate) { + tunnel.onDhcpInfoChanged(withContext(Dispatchers.IO) { getBackend().getDhcpInfo(tunnel) })!! + } + suspend fun getTunnelState(tunnel: ObservableTunnel): Tunnel.State = withContext(Dispatchers.Main.immediate) { tunnel.onStateChanged(withContext(Dispatchers.IO) { getBackend().getState(tunnel) }) } diff --git a/ui/src/main/res/layout/tunnel_detail_fragment.xml b/ui/src/main/res/layout/tunnel_detail_fragment.xml index aef71e18..c004fc5a 100644 --- a/ui/src/main/res/layout/tunnel_detail_fragment.xml +++ b/ui/src/main/res/layout/tunnel_detail_fragment.xml @@ -322,14 +322,42 @@ android:layout_height="wrap_content" android:contentDescription="@string/applications" android:nextFocusUp="@id/mtu_text" - android:nextFocusDown="@id/peers_layout" - android:nextFocusForward="@id/peers_layout" + android:nextFocusDown="@id/dhcp_info_text" + android:nextFocusForward="@id/dhcp_info_text" android:onClick="@{ClipboardUtils::copyTextView}" android:text="@{config.interface.includedApplications.isEmpty() ? @plurals/n_excluded_applications(config.interface.excludedApplications.size(), config.interface.excludedApplications.size()) : @plurals/n_included_applications(config.interface.includedApplications.size(), config.interface.includedApplications.size())}" android:visibility="@{config.interface.includedApplications.isEmpty() && config.interface.excludedApplications.isEmpty() ? android.view.View.GONE : android.view.View.VISIBLE}" app:layout_constraintTop_toBottomOf="@+id/applications_label" app:layout_constraintStart_toStartOf="parent" tools:text="8 excluded" /> + + + + + diff --git a/ui/src/main/res/values/strings.xml b/ui/src/main/res/values/strings.xml index 3b32ab79..4da4e8fe 100644 --- a/ui/src/main/res/values/strings.xml +++ b/ui/src/main/res/values/strings.xml @@ -248,4 +248,6 @@ Authenticate to view private key Authentication failure Authentication failure: %s + DHCP information + Addresses: %s\nIPv6 prefixes: %s\nDNS servers: %s -- cgit v1.2.3