summaryrefslogtreecommitdiffhomepage
path: root/ui/src/main/java/com/wireguard/android/model
diff options
context:
space:
mode:
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/model')
-rw-r--r--ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt36
-rw-r--r--ui/src/main/java/com/wireguard/android/model/TunnelManager.kt5
2 files changed, 36 insertions, 5 deletions
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 49e080f9..d4a0b1c0 100644
--- a/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt
+++ b/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt
@@ -13,7 +13,11 @@ import com.wireguard.android.backend.Statistics
import com.wireguard.android.backend.Tunnel
import com.wireguard.android.databinding.Keyed
import com.wireguard.android.util.applicationScope
+import com.wireguard.android.viewmodel.ConfigDetail
import com.wireguard.config.Config
+import com.wireguard.config.InetEndpoint
+import com.wireguard.crypto.Key;
+import java.util.Optional;
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@@ -59,6 +63,12 @@ class ObservableTunnel internal constructor(
if (state != Tunnel.State.UP) {
onStatisticsChanged(null)
onDhcpChanged(null)
+ onEndpointChange(null, null)
+ } else {
+ configDetail?.peers?.forEach {
+ var endpoint: InetEndpoint? = it.peer.endpoint.orElse(null)
+ it.endpoint = Optional.ofNullable(endpoint?.getResolved()?.get())
+ }
}
this.state = state
notifyPropertyChanged(BR.state)
@@ -72,6 +82,7 @@ class ObservableTunnel internal constructor(
this@ObservableTunnel.state
}
+ private var configDetail: ConfigDetail? = if (config != null) ConfigDetail(config) else null
@get:Bindable
var config = config
@@ -90,7 +101,11 @@ class ObservableTunnel internal constructor(
private set
suspend fun getConfigAsync(): Config = withContext(Dispatchers.Main.immediate) {
- config ?: manager.getTunnelConfig(this@ObservableTunnel)
+ config ?: manager.getTunnelConfig(this@ObservableTunnel).config!!
+ }
+
+ suspend fun getConfigDetailAsync(): ConfigDetail = withContext(Dispatchers.Main.immediate) {
+ configDetail ?: manager.getTunnelConfig(this@ObservableTunnel)
}
suspend fun setConfigAsync(config: Config): Config = withContext(Dispatchers.Main.immediate) {
@@ -102,10 +117,11 @@ class ObservableTunnel internal constructor(
}
}
- fun onConfigChanged(config: Config?): Config? {
+ fun onConfigChanged(config: Config?): ConfigDetail? {
this.config = config
+ this.configDetail = ConfigDetail(config)
notifyPropertyChanged(BR.config)
- return config
+ return configDetail
}
@@ -155,6 +171,20 @@ class ObservableTunnel internal constructor(
return dhcp
}
+ override fun onEndpointChange(publicKey: Key?, newEndpoint: InetEndpoint?) {
+ Log.i(TAG, "ObservableTunnel onEndpointChange " + newEndpoint)
+ configDetail?.peers?.forEach {
+ Log.i(TAG, "ObservableTunnel peer " + it + ", " + it.peer)
+ if (publicKey == null || it.peer.publicKey.equals(publicKey)) {
+ if (newEndpoint == null) {
+ it.endpoint = it.peer.endpoint
+ } else {
+ it.endpoint = newEndpoint.getResolved()
+ }
+ }
+ }
+ }
+
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 e7bb751b..77d69f70 100644
--- a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt
+++ b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt
@@ -24,6 +24,7 @@ import com.wireguard.android.databinding.ObservableSortedKeyedArrayList
import com.wireguard.android.util.ErrorMessages
import com.wireguard.android.util.UserKnobs
import com.wireguard.android.util.applicationScope
+import com.wireguard.android.viewmodel.ConfigDetail
import com.wireguard.config.Config
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Dispatchers
@@ -94,7 +95,7 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() {
applicationScope.launch { UserKnobs.setLastUsedTunnel(value?.name) }
}
- suspend fun getTunnelConfig(tunnel: ObservableTunnel): Config = withContext(Dispatchers.Main.immediate) {
+ suspend fun getTunnelConfig(tunnel: ObservableTunnel): ConfigDetail = withContext(Dispatchers.Main.immediate) {
tunnel.onConfigChanged(withContext(Dispatchers.IO) { configStore.load(tunnel.name) })!!
}
@@ -155,7 +156,7 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() {
tunnel.onConfigChanged(withContext(Dispatchers.IO) {
getBackend().setState(tunnel, tunnel.state, config)
configStore.save(tunnel.name, config)
- })!!
+ })!!.config!!
}
suspend fun setTunnelName(tunnel: ObservableTunnel, name: String): String = withContext(Dispatchers.Main.immediate) {