blob: 16c3e6a3b2ae5cbf9a48bb647c3f1b69cbb2c68c (
plain)
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
|
/*
* Copyright © 2017-2023 WireGuard LLC. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package com.wireguard.android.viewmodel
import android.net.Uri
import android.os.Build
import android.os.Parcel
import android.os.Parcelable
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableList
import com.wireguard.android.BR
import com.wireguard.config.Attribute
import com.wireguard.config.BadConfigException
import com.wireguard.config.Interface
import com.wireguard.crypto.Key
import com.wireguard.crypto.KeyFormatException
import com.wireguard.crypto.KeyPair
object Constants {
const val HTTP_PROXY_NONE = "None"
const val HTTP_PROXY_MANUAL = "Manual"
const val HTTP_PROXY_PAC = "Proxy Auto-Config"
}
class InterfaceProxy : BaseObservable, Parcelable {
@get:Bindable
val excludedApplications: ObservableList<String> = ObservableArrayList()
@get:Bindable
val includedApplications: ObservableList<String> = ObservableArrayList()
@get:Bindable
var addresses: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.addresses)
}
@get:Bindable
var dnsServers: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.dnsServers)
}
@get:Bindable
var listenPort: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.listenPort)
}
@get:Bindable
var mtu: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.mtu)
}
@get:Bindable
var httpProxyMenu: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.httpProxyMenu)
notifyPropertyChanged(BR.httpProxyManualVisibility)
notifyPropertyChanged(BR.httpProxyPacVisibility)
}
@get:Bindable
var httpProxyManualVisibility: Int = 0
get() = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) android.view.View.GONE else (if (httpProxyMenu == Constants.HTTP_PROXY_MANUAL) android.view.View.VISIBLE else android.view.View.GONE)
@get:Bindable
var httpProxyHostname: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.httpProxyHostname)
}
@get:Bindable
var httpProxyPort: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.httpProxyPort)
}
@get:Bindable
var httpProxyPac: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.httpProxyPac)
}
@get:Bindable
var httpProxyPacVisibility: Int = 0
get() = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) android.view.View.GONE else (if (httpProxyMenu == Constants.HTTP_PROXY_PAC) android.view.View.VISIBLE else android.view.View.GONE)
@get:Bindable
var privateKey: String = ""
set(value) {
field = value
notifyPropertyChanged(BR.privateKey)
notifyPropertyChanged(BR.publicKey)
}
@get:Bindable
val publicKey: String
get() = try {
KeyPair(Key.fromBase64(privateKey)).publicKey.toBase64()
} catch (ignored: KeyFormatException) {
""
}
private constructor(parcel: Parcel) {
addresses = parcel.readString() ?: ""
dnsServers = parcel.readString() ?: ""
parcel.readStringList(excludedApplications)
parcel.readStringList(includedApplications)
listenPort = parcel.readString() ?: ""
mtu = parcel.readString() ?: ""
httpProxyMenu = parcel.readString() ?: ""
httpProxyHostname = parcel.readString() ?: ""
httpProxyPort = parcel.readString() ?: ""
httpProxyPac = parcel.readString() ?: ""
privateKey = parcel.readString() ?: ""
}
constructor(other: Interface) {
addresses = Attribute.join(other.addresses)
val dnsServerStrings = other.dnsServers.map { it.hostAddress }.plus(other.dnsSearchDomains)
dnsServers = Attribute.join(dnsServerStrings)
excludedApplications.addAll(other.excludedApplications)
includedApplications.addAll(other.includedApplications)
listenPort = other.listenPort.map { it.toString() }.orElse("")
mtu = other.mtu.map { it.toString() }.orElse("")
httpProxyHostname = other.httpProxy.map { if (it.getHost().startsWith('[') && it.getHost().endsWith(']')) it.getHost().substring(1, it.getHost().length-1) else it.getHost() }.orElse("")
httpProxyPort = other.httpProxy.map { if (it.getPort() <= 0) "8080" else it.getPort().toString() }.orElse("")
httpProxyPac = other.httpProxy.map { it.getPacFileUrl().toString() }.orElse("")
httpProxyMenu = other.httpProxy.map { if (it.getPacFileUrl() != null && it.getPacFileUrl() != Uri.EMPTY) Constants.HTTP_PROXY_PAC else if (it.getHost() != "") Constants.HTTP_PROXY_MANUAL else Constants.HTTP_PROXY_NONE }.orElse(Constants.HTTP_PROXY_NONE)
val keyPair = other.keyPair
privateKey = keyPair.privateKey.toBase64()
}
constructor()
override fun describeContents() = 0
fun generateKeyPair() {
val keyPair = KeyPair()
privateKey = keyPair.privateKey.toBase64()
notifyPropertyChanged(BR.privateKey)
notifyPropertyChanged(BR.publicKey)
}
@Throws(BadConfigException::class)
fun resolve(): Interface {
val builder = Interface.Builder()
if (addresses.isNotEmpty()) builder.parseAddresses(addresses)
if (dnsServers.isNotEmpty()) builder.parseDnsServers(dnsServers)
if (excludedApplications.isNotEmpty()) builder.excludeApplications(excludedApplications)
if (includedApplications.isNotEmpty()) builder.includeApplications(includedApplications)
if (listenPort.isNotEmpty()) builder.parseListenPort(listenPort)
if (mtu.isNotEmpty()) builder.parseMtu(mtu)
if (Constants.HTTP_PROXY_MANUAL.equals(httpProxyMenu) && httpProxyHostname.isNotEmpty()) {
var httpProxy: String
if (httpProxyHostname.contains(":")) {
httpProxy = "[" + httpProxyHostname + "]"
} else {
httpProxy = httpProxyHostname
}
if (httpProxyPort.isNotEmpty()) {
httpProxy += ":" + httpProxyPort;
}
builder.parseHttpProxy(httpProxy)
} else if (Constants.HTTP_PROXY_PAC.equals(httpProxyMenu) && httpProxyPac.isNotEmpty()) {
builder.parseHttpProxy("pac:" + httpProxyPac)
}
if (privateKey.isNotEmpty()) builder.parsePrivateKey(privateKey)
return builder.build()
}
override fun writeToParcel(dest: Parcel, flags: Int) {
dest.writeString(addresses)
dest.writeString(dnsServers)
dest.writeStringList(excludedApplications)
dest.writeStringList(includedApplications)
dest.writeString(listenPort)
dest.writeString(mtu)
dest.writeString(httpProxyMenu)
dest.writeString(httpProxyHostname)
dest.writeString(httpProxyPort)
dest.writeString(httpProxyPac)
dest.writeString(privateKey)
}
private class InterfaceProxyCreator : Parcelable.Creator<InterfaceProxy> {
override fun createFromParcel(parcel: Parcel): InterfaceProxy {
return InterfaceProxy(parcel)
}
override fun newArray(size: Int): Array<InterfaceProxy?> {
return arrayOfNulls(size)
}
}
companion object {
@JvmField
val CREATOR: Parcelable.Creator<InterfaceProxy> = InterfaceProxyCreator()
}
}
|