diff options
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt | 102 |
1 files changed, 89 insertions, 13 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 ef22f846..9684ffab 100644 --- a/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt +++ b/ui/src/main/java/com/wireguard/android/model/ObservableTunnel.kt @@ -7,6 +7,7 @@ package com.wireguard.android.model import android.util.Log import androidx.databinding.BaseObservable import androidx.databinding.Bindable +import com.wireguard.android.Application import com.wireguard.android.BR import com.wireguard.android.backend.Dhcp import com.wireguard.android.backend.Statistics @@ -14,10 +15,12 @@ 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.android.viewmodel.PeerDetail import com.wireguard.config.Config import com.wireguard.config.InetEndpoint -import com.wireguard.crypto.Key; -import java.util.Optional; +import com.wireguard.config.InetNetwork +import com.wireguard.crypto.Key +import java.util.Optional import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -63,10 +66,12 @@ class ObservableTunnel internal constructor( if (state != Tunnel.State.UP) { onStatisticsChanged(null) onDhcpChanged(null) - onEndpointChange(null, null) + Application.getCoroutineScope().launch { + onPeersReset() + } } else { configDetail?.peers?.forEach { - var endpoint: InetEndpoint? = it.peer.endpoint.orElse(null) + var endpoint: InetEndpoint? = it.peer?.endpoint?.orElse(null) it.endpoint = Optional.ofNullable(endpoint?.getResolved()?.get()) } } @@ -118,8 +123,8 @@ class ObservableTunnel internal constructor( } fun onConfigChanged(config: Config?): ConfigDetail? { - this.config = config this.configDetail = ConfigDetail(config) + this.config = config notifyPropertyChanged(BR.config) return configDetail } @@ -171,18 +176,89 @@ class ObservableTunnel internal constructor( return dhcp } - override fun onEndpointChange(publicKey: Key?, newEndpoint: InetEndpoint?) { + // Remove dynamic peers, and reset static peers + fun onPeersReset() { + Log.i(TAG, "ObservableTunnel onPeersReset") + var toRemove: MutableList<PeerDetail> = ArrayList() + + configDetail?.peers?.forEach { + if (it.peer == null) { + toRemove.add(it) + } else { + it.endpoint = Optional.empty() + } + } + + toRemove.forEach { + Log.i(TAG, "ObservableTunnel remove " + it) + configDetail?.peers?.remove(it) + } + } + + override fun onEndpointChange(publicKey: Key, newEndpoint: InetEndpoint?) { + Application.getCoroutineScope().launch { + onEndpointChanged(publicKey, newEndpoint) + } + } + + private fun onEndpointChanged(publicKey: Key, newEndpoint: InetEndpoint?) { + Log.i(TAG, "ObservableTunnel onEndpointChange " + newEndpoint) + var peer: PeerDetail? = null + 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() - } + if (it.publicKey.equals(publicKey) == true) { + Log.i(TAG, "ObservableTunnel peer " + it + ", " + it.peer) + peer = it; + } + } + + if (peer == null) { + Log.i(TAG, "ObservableTunnel create peer " + publicKey) + peer = PeerDetail(publicKey) + configDetail?.peers?.add(peer) + } + + var peer2: PeerDetail = peer!! + + if (newEndpoint != null) { + peer2.endpoint = newEndpoint.getResolved() + } else { + var peer3 = peer2.peer + peer2.endpoint = if (peer3 != null) peer3.endpoint else Optional.empty() + } + } + + fun lookupPeer(publicKey: Key): PeerDetail { + configDetail?.peers?.forEach { + if (it.publicKey.equals(publicKey) == true) { + Log.i(TAG, "ObservableTunnel peer " + it + ", " + it.peer) + return it } } + + Log.i(TAG, "ObservableTunnel create peer " + publicKey) + var peer: PeerDetail = PeerDetail(publicKey) + configDetail?.peers?.add(peer) + + return peer + } + + override fun onAllowedIpsChange(publicKey: Key, addNetworks: List<InetNetwork>?, removeNetworks: List<InetNetwork>?) { + Application.getCoroutineScope().launch { + onAllowedIpsChanged(publicKey, addNetworks, removeNetworks) + } + } + + private fun onAllowedIpsChanged(publicKey: Key, addNetworks: List<InetNetwork>?, removeNetworks: List<InetNetwork>?) { + var peer: PeerDetail = lookupPeer(publicKey) + + removeNetworks?.let() { + peer.allowedIps.removeAll(removeNetworks) + } + addNetworks?.let() { + peer.allowedIps.addAll(addNetworks) + } } |