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 8fd587895..216ca9921 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt @@ -18,6 +18,7 @@ object AppConfig { const val PREF_SNIFFING_ENABLED = "pref_sniffing_enabled" 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_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/dto/V2rayConfig.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt index 675fed2bf..5d05a1782 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/V2rayConfig.kt @@ -20,7 +20,7 @@ data class V2rayConfig( val api: Any? = null, val transport: Any? = null, val reverse: Any? = null, - val fakedns: Any? = null, + var fakedns: FakednsBean? = null, val browserForwarder: Any? = null) { companion object { const val DEFAULT_PORT = 443 @@ -57,7 +57,7 @@ data class V2rayConfig( val network: String? = null) data class SniffingBean(var enabled: Boolean, - val destOverride: List, + val destOverride: ArrayList, val metadataOnly: Boolean? = null) } @@ -78,7 +78,7 @@ data class V2rayConfig( val address: String? = null, val port: Int? = null, /*Freedom*/ - val domainStrategy: String? = null, + var domainStrategy: String? = null, val redirect: String? = null, val userLevel: Int? = null, /*Loopback*/ @@ -376,9 +376,9 @@ data class V2rayConfig( val tag: String? = null ) { data class ServersBean(var address: String = "", - var port: Int = 0, - var domains: List?, - var expectIPs: List?, + var port: Int? = null, + var domains: List? = null, + var expectIPs: List? = null, val clientIp: String? = null) } @@ -416,6 +416,9 @@ data class V2rayConfig( var bufferSize: Int? = null) } + data class FakednsBean(var ipPool: String = "198.18.0.0/15", + var poolSize: Int = 10000) // roughly 10 times smaller than total ip pool + fun getProxyOutbound(): OutboundBean? { outbounds.forEach { outbound -> if (outbound.protocol.equals(EConfigType.VMESS.name, true) || diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt index eb4495a21..e897050dc 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt @@ -67,6 +67,7 @@ object V2rayConfigUtil { routing(v2rayConfig) + fakedns(v2rayConfig) if (settingsStorage?.decodeBool(AppConfig.PREF_LOCAL_DNS_ENABLED) == true) { customLocalDns(v2rayConfig) @@ -97,8 +98,17 @@ object V2rayConfigUtil { } } v2rayConfig.inbounds[0].port = 10808 - v2rayConfig.inbounds[0].sniffing?.enabled = settingsStorage?.decodeBool(AppConfig.PREF_SNIFFING_ENABLED) + val fakedns = settingsStorage?.decodeBool(AppConfig.PREF_FAKE_DNS_ENABLED) + ?: false + val sniffAllTlsAndHttp = settingsStorage?.decodeBool(AppConfig.PREF_SNIFFING_ENABLED) ?: true + v2rayConfig.inbounds[0].sniffing?.enabled = fakedns || sniffAllTlsAndHttp + if (!sniffAllTlsAndHttp) { + v2rayConfig.inbounds[0].sniffing?.destOverride?.clear() + } + if (fakedns) { + v2rayConfig.inbounds[0].sniffing?.destOverride?.add("fakedns") + } //v2rayConfig.inbounds[1].port = httpPort @@ -115,6 +125,15 @@ object V2rayConfigUtil { return true } + private fun fakedns(v2rayConfig: V2rayConfig) { + if (settingsStorage?.decodeBool(AppConfig.PREF_FAKE_DNS_ENABLED) == true) { + v2rayConfig.fakedns = V2rayConfig.FakednsBean() + v2rayConfig.outbounds.filter { it.protocol == "freedom" }.forEach { + it.settings?.domainStrategy = "UseIP" + } + } + } + /** * routing */ @@ -245,27 +264,27 @@ object V2rayConfigUtil { val hosts = mutableMapOf() val servers = ArrayList() val remoteDns = Utils.getRemoteDnsServers() - - remoteDns.forEach { - servers.add(it) - } - val domesticDns = Utils.getDomesticDnsServers() val geositeCn = arrayListOf("geosite:cn") val geoipCn = arrayListOf("geoip:cn") - - val agDomain = userRule2Domian(settingsStorage?.decodeString(AppConfig.PREF_V2RAY_ROUTING_AGENT) + val proxyDomain = userRule2Domian(settingsStorage?.decodeString(AppConfig.PREF_V2RAY_ROUTING_AGENT) ?: "") - if (agDomain.size > 0) { - servers.add(V2rayConfig.DnsBean.ServersBean(remoteDns.first(), 53, agDomain, null)) - } - - val dirDomain = userRule2Domian(settingsStorage?.decodeString(AppConfig.PREF_V2RAY_ROUTING_DIRECT) + val directDomain = userRule2Domian(settingsStorage?.decodeString(AppConfig.PREF_V2RAY_ROUTING_DIRECT) ?: "") - if (dirDomain.size > 0) { - servers.add(V2rayConfig.DnsBean.ServersBean(domesticDns.first(), 53, dirDomain, geoipCn)) - } + if (settingsStorage?.decodeBool(AppConfig.PREF_FAKE_DNS_ENABLED) == true) { + // fakedns with all domains to make it always top priority + servers.add(V2rayConfig.DnsBean.ServersBean(address = "fakedns", domains = geositeCn.plus(proxyDomain).plus(directDomain))) + } + remoteDns.forEach { + servers.add(it) + } + if (proxyDomain.size > 0) { + servers.add(V2rayConfig.DnsBean.ServersBean(remoteDns.first(), 53, proxyDomain, null)) + } + if (directDomain.size > 0) { + servers.add(V2rayConfig.DnsBean.ServersBean(domesticDns.first(), 53, directDomain, geoipCn)) + } val routingMode = settingsStorage?.decodeString(AppConfig.PREF_ROUTING_MODE) ?: "0" if (routingMode == "2" || routingMode == "3") { servers.add(V2rayConfig.DnsBean.ServersBean(domesticDns.first(), 53, geositeCn, geoipCn)) 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 464f3e9ad..20205078a 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 @@ -39,6 +39,7 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application AppConfig.PREF_SPEED_ENABLED, AppConfig.PREF_PROXY_SHARING, AppConfig.PREF_LOCAL_DNS_ENABLED, + AppConfig.PREF_FAKE_DNS_ENABLED, AppConfig.PREF_FORWARD_IPV6, AppConfig.PREF_PER_APP_PROXY, AppConfig.PREF_BYPASS_APPS, -> {