From e83208465f1945095e988a9a52a657fe43ab0c8a Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Fri, 7 May 2021 20:42:41 -0400 Subject: [PATCH] Add vpn dns preference setting Vpn Dns can now be changed separately from remote Dns --- .../main/kotlin/com/v2ray/ang/AppConfig.kt | 1 + .../com/v2ray/ang/service/V2RayVpnService.kt | 10 +++-- .../main/kotlin/com/v2ray/ang/util/Utils.kt | 45 +++++++------------ .../v2ray/ang/viewmodel/SettingsViewModel.kt | 1 + 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt index 216ca992..70b966b7 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt @@ -19,6 +19,7 @@ object AppConfig { const val PREF_PROXY_SHARING = "pref_proxy_sharing_enabled" const val PREF_LOCAL_DNS_ENABLED = "pref_local_dns_enabled" const val PREF_FAKE_DNS_ENABLED = "pref_fake_dns_enabled" + const val PREF_VPN_DNS = "pref_vpn_dns" const val PREF_REMOTE_DNS = "pref_remote_dns" const val PREF_DOMESTIC_DNS = "pref_domestic_dns" const val PREF_FORWARD_IPV6 = "pref_forward_ipv6" diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt index efc410ff..0a21a313 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/service/V2RayVpnService.kt @@ -122,10 +122,12 @@ class V2RayVpnService : VpnService(), ServiceControl { } if(!enableLocalDns) { - Utils.getRemoteDnsServers() - .forEach { - builder.addDnsServer(it) - } + Utils.getVpnDnsServers() + .forEach { + if (Utils.isPureIpAddress(it)) { + builder.addDnsServer(it) + } + } } builder.setSession(V2RayServiceManager.currentConfig?.remarks.orEmpty()) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt index a619d7bf..b905a354 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt @@ -15,7 +15,6 @@ import android.content.ClipData import android.content.Intent import android.net.Uri import android.os.SystemClock -import android.text.TextUtils import android.util.Log import android.util.Patterns import android.webkit.URLUtil @@ -123,41 +122,31 @@ object Utils { /** * get remote dns servers from preference */ - fun getRemoteDnsServers(): ArrayList { + fun getRemoteDnsServers(): List { val remoteDns = settingsStorage?.decodeString(AppConfig.PREF_REMOTE_DNS) ?: AppConfig.DNS_AGENT - val ret = ArrayList() - if (!TextUtils.isEmpty(remoteDns)) { - remoteDns - .split(",") - .forEach { - if (Utils.isPureIpAddress(it)) { - ret.add(it) - } - } - } - if (ret.size == 0) { - ret.add(AppConfig.DNS_AGENT) + val ret = remoteDns.split(",").filter { isPureIpAddress(it) || it.startsWith("https") } + if (ret.isEmpty()) { + return listOf(AppConfig.DNS_AGENT) } return ret } + fun getVpnDnsServers(): List { + val vpnDns = settingsStorage?.decodeString(AppConfig.PREF_VPN_DNS) + ?: settingsStorage?.decodeString(AppConfig.PREF_REMOTE_DNS) + ?: AppConfig.DNS_AGENT + return vpnDns.split(",").filter { isPureIpAddress(it) } + // allow empty, in that case dns will use system default + } + /** * get remote dns servers from preference */ - fun getDomesticDnsServers(): ArrayList { + fun getDomesticDnsServers(): List { val domesticDns = settingsStorage?.decodeString(AppConfig.PREF_DOMESTIC_DNS) ?: AppConfig.DNS_DIRECT - val ret = ArrayList() - if (!TextUtils.isEmpty(domesticDns)) { - domesticDns - .split(",") - .forEach { - if (Utils.isPureIpAddress(it)) { - ret.add(it) - } - } - } - if (ret.size == 0) { - ret.add(AppConfig.DNS_DIRECT) + val ret = domesticDns.split(",").filter { isPureIpAddress(it) || it.startsWith("https") } + if (ret.isEmpty()) { + return listOf(AppConfig.DNS_DIRECT) } return ret } @@ -391,7 +380,7 @@ object Utils { val command = "/system/bin/ping -c 3 $url" val process = Runtime.getRuntime().exec(command) val allText = process.inputStream.bufferedReader().use { it.readText() } - if (!TextUtils.isEmpty(allText)) { + if (allText.isNotBlank()) { val tempInfo = allText.substring(allText.indexOf("min/avg/max/mdev") + 19) val temps = tempInfo.split("/".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray() if (temps.count() > 0 && temps[0].length < 10) { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/SettingsViewModel.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/SettingsViewModel.kt index 20205078..1cecc9b9 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/SettingsViewModel.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/SettingsViewModel.kt @@ -27,6 +27,7 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application Log.d(AppConfig.ANG_PACKAGE, "Observe settings changed: $key") when(key) { AppConfig.PREF_MODE, + AppConfig.PREF_VPN_DNS, AppConfig.PREF_REMOTE_DNS, AppConfig.PREF_DOMESTIC_DNS, AppConfig.PREF_ROUTING_DOMAIN_STRATEGY,