Skip to content

Commit

Permalink
Optimize routing
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Apr 11, 2024
1 parent b132b0d commit fc2e4ff
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 18 deletions.
5 changes: 3 additions & 2 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/AppConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object AppConfig {
const val TASKER_EXTRA_BUNDLE_GUID = "tasker_extra_bundle_guid"
const val TASKER_DEFAULT_GUID = "Default"

const val TAG_AGENT = "proxy"
const val TAG_PROXY = "proxy"
const val TAG_DIRECT = "direct"
const val TAG_BLOCKED = "block"
const val TAG_FRAGMENT = "fragment"
Expand All @@ -78,8 +78,9 @@ object AppConfig {
const val promotionUrl = "aHR0cHM6Ly85LjIzNDQ1Ni54eXovYWJjLmh0bWw="
const val geoUrl = "https://github.com/Loyalsoldier/v2ray-rules-dat/releases/latest/download/"

const val DNS_AGENT = "1.1.1.1"
const val DNS_PROXY = "1.1.1.1"
const val DNS_DIRECT = "223.5.5.5"
const val DNS_VPN = "1.1.1.1"

const val PORT_LOCAL_DNS = "10853"
const val PORT_SOCKS = "10808"
Expand Down
4 changes: 2 additions & 2 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/dto/ServerConfig.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.v2ray.ang.dto

import com.v2ray.ang.AppConfig.TAG_AGENT
import com.v2ray.ang.AppConfig.TAG_PROXY
import com.v2ray.ang.AppConfig.TAG_BLOCKED
import com.v2ray.ang.AppConfig.TAG_DIRECT
import com.v2ray.ang.util.Utils
Expand Down Expand Up @@ -58,7 +58,7 @@ data class ServerConfig(

fun getAllOutboundTags(): MutableList<String> {
if (configType != EConfigType.CUSTOM) {
return mutableListOf(TAG_AGENT, TAG_DIRECT, TAG_BLOCKED)
return mutableListOf(TAG_PROXY, TAG_DIRECT, TAG_BLOCKED)
}
fullConfig?.let { config ->
return config.outbounds.map { it.tag }.toMutableList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class RoutingSettingsFragment : Fragment() {
var tag = ""
when (requireArguments().getString(routing_arg)) {
AppConfig.PREF_V2RAY_ROUTING_AGENT -> {
tag = AppConfig.TAG_AGENT
tag = AppConfig.TAG_PROXY
}
AppConfig.PREF_V2RAY_ROUTING_DIRECT -> {
tag = AppConfig.TAG_DIRECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SettingsActivity : BaseActivity() {
remoteDns?.setOnPreferenceChangeListener { _, any ->
// remoteDns.summary = any as String
val nval = any as String
remoteDns?.summary = if (nval == "") AppConfig.DNS_AGENT else nval
remoteDns?.summary = if (nval == "") AppConfig.DNS_PROXY else nval
true
}
domesticDns?.setOnPreferenceChangeListener { _, any ->
Expand Down Expand Up @@ -213,7 +213,7 @@ class SettingsActivity : BaseActivity() {
autoUpdateInterval?.isEnabled = defaultSharedPreferences.getBoolean(AppConfig.SUBSCRIPTION_AUTO_UPDATE, false)

if (TextUtils.isEmpty(remoteDnsString)) {
remoteDnsString = AppConfig.DNS_AGENT
remoteDnsString = AppConfig.DNS_PROXY
}
if (TextUtils.isEmpty(domesticDns?.summary)) {
domesticDns?.summary = AppConfig.DNS_DIRECT
Expand Down
8 changes: 4 additions & 4 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,18 @@ object Utils {
* get remote dns servers from preference
*/
fun getRemoteDnsServers(): List<String> {
val remoteDns = settingsStorage?.decodeString(AppConfig.PREF_REMOTE_DNS) ?: AppConfig.DNS_AGENT
val remoteDns = settingsStorage?.decodeString(AppConfig.PREF_REMOTE_DNS) ?: AppConfig.DNS_PROXY
val ret = remoteDns.split(",").filter { isPureIpAddress(it) || isCoreDNSAddress(it) }
if (ret.isEmpty()) {
return listOf(AppConfig.DNS_AGENT)
return listOf(AppConfig.DNS_PROXY)
}
return ret
}

fun getVpnDnsServers(): List<String> {
val vpnDns = settingsStorage?.decodeString(AppConfig.PREF_VPN_DNS)
?: settingsStorage?.decodeString(AppConfig.PREF_REMOTE_DNS)
?: AppConfig.DNS_AGENT
?: AppConfig.DNS_VPN
return vpnDns.split(",").filter { isPureIpAddress(it) }
// allow empty, in that case dns will use system default
}
Expand All @@ -160,7 +160,7 @@ object Utils {
*/
fun getDomesticDnsServers(): List<String> {
val domesticDns = settingsStorage?.decodeString(AppConfig.PREF_DOMESTIC_DNS) ?: AppConfig.DNS_DIRECT
val ret = domesticDns.split(",").filter { isPureIpAddress(it) || isCoreDNSAddress(it) }
val ret = domesticDns.split(",").filter { isPureIpAddress(it) }
if (ret.isEmpty()) {
return listOf(AppConfig.DNS_DIRECT)
}
Expand Down
17 changes: 13 additions & 4 deletions V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/V2rayConfigUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ object V2rayConfigUtil {
try {
routingUserRule(
settingsStorage?.decodeString(AppConfig.PREF_V2RAY_ROUTING_AGENT)
?: "", AppConfig.TAG_AGENT, v2rayConfig
?: "", AppConfig.TAG_PROXY, v2rayConfig
)
routingUserRule(
settingsStorage?.decodeString(AppConfig.PREF_V2RAY_ROUTING_DIRECT)
Expand All @@ -195,7 +195,7 @@ object V2rayConfigUtil {

// Hardcode googleapis.cn
val googleapisRoute = V2rayConfig.RoutingBean.RulesBean(
outboundTag = AppConfig.TAG_AGENT,
outboundTag = AppConfig.TAG_PROXY,
domain = arrayListOf("domain:googleapis.cn")
)

Expand Down Expand Up @@ -225,6 +225,15 @@ object V2rayConfigUtil {
v2rayConfig.routing.rules.add(globalDirect)
}
}

if(routingMode != ERoutingMode.GLOBAL_DIRECT.value) {
v2rayConfig.routing.rules.add(
V2rayConfig.RoutingBean.RulesBean(
outboundTag = AppConfig.TAG_PROXY,
port = "0-65535"
))
}

} catch (e: Exception) {
e.printStackTrace()
return false
Expand Down Expand Up @@ -340,7 +349,7 @@ object V2rayConfigUtil {
val remoteDns = Utils.getRemoteDnsServers()
if (v2rayConfig.inbounds.none { e -> e.protocol == "dokodemo-door" && e.tag == "dns-in" }) {
val dnsInboundSettings = V2rayConfig.InboundBean.InSettingsBean(
address = if (Utils.isPureIpAddress(remoteDns.first())) remoteDns.first() else "1.1.1.1",
address = if (Utils.isPureIpAddress(remoteDns.first())) remoteDns.first() else AppConfig.DNS_PROXY,
port = 53,
network = "tcp,udp"
)
Expand Down Expand Up @@ -477,7 +486,7 @@ object V2rayConfigUtil {
if (Utils.isPureIpAddress(remoteDns.first())) {
v2rayConfig.routing.rules.add(
0, V2rayConfig.RoutingBean.RulesBean(
outboundTag = AppConfig.TAG_AGENT,
outboundTag = AppConfig.TAG_PROXY,
port = "53",
ip = arrayListOf(remoteDns.first()),
domain = null
Expand Down
2 changes: 1 addition & 1 deletion V2rayNG/app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<string name="title_pref_routing_mode">预定义规则</string>
<string name="title_pref_routing_custom">自定义规则</string>

<string name="title_pref_remote_dns">远程DNS (可选)</string>
<string name="title_pref_remote_dns">远程DNS (udp/tcp/https/quic)(可选)</string>
<string name="summary_pref_remote_dns">DNS</string>

<string name="title_pref_vpn_dns">VPN DNS (仅支持 IPv4/v6)</string>
Expand Down
2 changes: 1 addition & 1 deletion V2rayNG/app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
<string name="title_pref_routing_mode">轉送模式</string>
<string name="title_pref_routing_custom">自訂轉送</string>

<string name="title_pref_remote_dns">遠端 DNS (可選)</string>
<string name="title_pref_remote_dns">遠端DNS (udp/tcp/https/quic)(可選)</string>
<string name="summary_pref_remote_dns">DNS</string>

<string name="title_pref_vpn_dns">VPN DNS (僅支援 IPv4/v6)</string>
Expand Down
2 changes: 1 addition & 1 deletion V2rayNG/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
<string name="title_pref_routing_mode">Predefined rules</string>
<string name="title_pref_routing_custom">Custom rules</string>

<string name="title_pref_remote_dns">Remote DNS (Optional)</string>
<string name="title_pref_remote_dns">Remote DNS (udp/tcp/https/quic)(Optional)</string>
<string name="summary_pref_remote_dns">DNS</string>

<string name="title_pref_vpn_dns">VPN DNS (only IPv4/v6)</string>
Expand Down

0 comments on commit fc2e4ff

Please sign in to comment.