1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
* Copyright © 2017-2023 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
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
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.config.InetNetwork
import com.wireguard.crypto.Key
import java.util.Optional
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
/**
* Encapsulates the volatile and nonvolatile state of a WireGuard tunnel.
*/
class ObservableTunnel internal constructor(
private val manager: TunnelManager,
private var name: String,
config: Config?,
state: Tunnel.State
) : BaseObservable(), Keyed<String>, Tunnel {
override val key
get() = name
@Bindable
override fun getName() = name
suspend fun setNameAsync(name: String): String = withContext(Dispatchers.Main.immediate) {
if (name != this@ObservableTunnel.name)
manager.setTunnelName(this@ObservableTunnel, name)
else
this@ObservableTunnel.name
}
fun onNameChanged(name: String): String {
this.name = name
notifyPropertyChanged(BR.name)
return name
}
@get:Bindable
var state = state
private set
override fun onStateChange(newState: Tunnel.State) {
onStateChanged(newState)
}
fun onStateChanged(state: Tunnel.State): Tunnel.State {
if (state != Tunnel.State.UP) {
onStatisticsChanged(null)
onDhcpChanged(null)
Application.getCoroutineScope().launch {
onPeersReset()
}
} 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)
return state
}
suspend fun setStateAsync(state: Tunnel.State): Tunnel.State = withContext(Dispatchers.Main.immediate) {
if (state != this@ObservableTunnel.state)
manager.setTunnelState(this@ObservableTunnel, state)
else
this@ObservableTunnel.state
}
private var configDetail: ConfigDetail? = if (config != null) ConfigDetail(config) else null
@get:Bindable
var config = config
get() {
if (field == null)
// Opportunistically fetch this if we don't have a cached one, and rely on data bindings to update it eventually
applicationScope.launch {
try {
manager.getTunnelConfig(this@ObservableTunnel)
} catch (e: Throwable) {
Log.e(TAG, Log.getStackTraceString(e))
}
}
return field
}
private set
suspend fun getConfigAsync(): Config = withContext(Dispatchers.Main.immediate) {
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) {
this@ObservableTunnel.config.let {
if (config != it)
manager.setTunnelConfig(this@ObservableTunnel, config)
else
it
}
}
fun onConfigChanged(config: Config?): ConfigDetail? {
this.configDetail = ConfigDetail(config)
this.config = config
notifyPropertyChanged(BR.config)
return configDetail
}
@get:Bindable
var statistics: Statistics? = null
get() {
if (field == null || field?.isStale != false)
// Opportunistically fetch this if we don't have a cached one, and rely on data bindings to update it eventually
applicationScope.launch {
try {
manager.getTunnelStatistics(this@ObservableTunnel)
} catch (e: Throwable) {
Log.e(TAG, Log.getStackTraceString(e))
}
}
return field
}
private set
suspend fun getStatisticsAsync(): Statistics = withContext(Dispatchers.Main.immediate) {
statistics.let {
if (it == null || it.isStale)
manager.getTunnelStatistics(this@ObservableTunnel)
else
it
}
}
fun onStatisticsChanged(statistics: Statistics?): Statistics? {
this.statistics = statistics
notifyPropertyChanged(BR.statistics)
return statistics
}
@get:Bindable
var dhcp: Dhcp? = null
private set
override fun onDhcpChange(newDhcp: Dhcp) {
onDhcpChanged(newDhcp)
}
fun onDhcpChanged(dhcp: Dhcp?): Dhcp? {
this.dhcp = dhcp
notifyPropertyChanged(BR.dhcp)
return dhcp
}
// 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 {
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)
}
}
suspend fun deleteAsync() = manager.delete(this)
companion object {
private const val TAG = "WireGuard/ObservableTunnel"
}
}
|