summaryrefslogtreecommitdiffhomepage
path: root/android/src/main
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-10-29 17:23:49 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2020-12-04 13:18:17 +0000
commitc76918ec8a65c5beb3517d3bf9641b94e6bf0f52 (patch)
treea3357d5062dca7b6b24898b3ff04aa1d597369d1 /android/src/main
parent2d2f11e4cbe1577176aace4ef08a085145c44969 (diff)
downloadmullvadvpn-c76918ec8a65c5beb3517d3bf9641b94e6bf0f52.tar.xz
mullvadvpn-c76918ec8a65c5beb3517d3bf9641b94e6bf0f52.zip
Create `CustomDns` helper class
Diffstat (limited to 'android/src/main')
-rw-r--r--android/src/main/kotlin/net/mullvad/mullvadvpn/service/CustomDns.kt68
1 files changed, 68 insertions, 0 deletions
diff --git a/android/src/main/kotlin/net/mullvad/mullvadvpn/service/CustomDns.kt b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/CustomDns.kt
new file mode 100644
index 0000000000..95a4be3cb9
--- /dev/null
+++ b/android/src/main/kotlin/net/mullvad/mullvadvpn/service/CustomDns.kt
@@ -0,0 +1,68 @@
+package net.mullvad.mullvadvpn.service
+
+import java.net.InetAddress
+import java.util.ArrayList
+import kotlin.properties.Delegates.observable
+import net.mullvad.mullvadvpn.model.DnsOptions
+import net.mullvad.talpid.util.EventNotifier
+
+class CustomDns(val daemon: MullvadDaemon, val settingsListener: SettingsListener) {
+ private var enabled by observable(false) { _, oldValue, newValue ->
+ if (oldValue != newValue) {
+ onEnabledChanged.notify(newValue)
+ }
+ }
+
+ private var dnsServers by observable<ArrayList<InetAddress>>(ArrayList()) { _, _, servers ->
+ onDnsServersChanged.notify(servers.toList())
+ }
+
+ val onEnabledChanged = EventNotifier(false)
+ val onDnsServersChanged = EventNotifier<List<InetAddress>>(emptyList())
+
+ init {
+ settingsListener.dnsOptionsNotifier.subscribe(this) { dnsOptions ->
+ enabled = dnsOptions.custom
+ dnsServers = ArrayList(dnsOptions.addresses)
+ }
+ }
+
+ fun onDestroy() {
+ settingsListener.dnsOptionsNotifier.unsubscribe(this)
+ }
+
+ fun enable() {
+ synchronized(this) {
+ changeDnsOptions(true, dnsServers)
+ }
+ }
+
+ fun disable() {
+ synchronized(this) {
+ changeDnsOptions(false, dnsServers)
+ }
+ }
+
+ fun addDnsServer(server: InetAddress) {
+ synchronized(this) {
+ if (!dnsServers.contains(server)) {
+ dnsServers.add(server)
+ changeDnsOptions(enabled, dnsServers)
+ }
+ }
+ }
+
+ fun removeDnsServer(server: InetAddress) {
+ synchronized(this) {
+ if (dnsServers.remove(server)) {
+ changeDnsOptions(enabled, dnsServers)
+ }
+ }
+ }
+
+ private fun changeDnsOptions(enable: Boolean, dnsServers: ArrayList<InetAddress>) {
+ val options = DnsOptions(enable, dnsServers)
+
+ daemon.setDnsOptions(options)
+ }
+}