Skip to content

Commit

Permalink
fix dns routing
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Mar 22, 2023
1 parent faf964e commit 03c99d7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
33 changes: 21 additions & 12 deletions app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ fun buildConfig(
val uidListDNSDirect = mutableListOf<Int>()
val domainListDNSRemote = mutableListOf<String>()
val domainListDNSDirect = mutableListOf<String>()
val domainListDNSDirectForce = mutableListOf<String>()
val domainListDNSBlock = mutableListOf<String>()
val bypassDNSBeans = hashSetOf<AbstractBean>()
val isVPN = DataStore.serviceMode == Key.MODE_VPN
Expand Down Expand Up @@ -404,7 +405,7 @@ fun buildConfig(
pastEntity?.requireBean()?.apply {
// don't loopback
if (currentDomainStrategy != "" && !serverAddress.isIpAddress()) {
domainListDNSDirect.add("full:$serverAddress")
domainListDNSDirectForce.add("full:$serverAddress")
}
}
if (forTest) {
Expand Down Expand Up @@ -628,7 +629,7 @@ fun buildConfig(
}

if (!serverAddr.isIpAddress()) {
domainListDNSDirect.add("full:${serverAddr}")
domainListDNSDirectForce.add("full:${serverAddr}")
}
}

Expand All @@ -639,7 +640,7 @@ fun buildConfig(
}
"https://$address".toHttpUrlOrNull()?.apply {
if (!host.isIpAddress()) {
domainListDNSDirect.add("full:$host")
domainListDNSDirectForce.add("full:$host")
}
}
}
Expand Down Expand Up @@ -675,6 +676,14 @@ fun buildConfig(
address = "rcode://success"
tag = "dns-block"
})
if (domainListDNSDirectForce.isNotEmpty()) {
dns.rules.add(
DNSRule_DefaultOptions().apply {
makeSingBoxRule(domainListDNSDirectForce.toHashSet().toList())
server = "dns-direct"
}
)
}

// dns object user rules
if (enableDnsRouting) {
Expand All @@ -696,15 +705,15 @@ fun buildConfig(
}
)
}
}
if (domainListDNSBlock.isNotEmpty()) {
dns.rules.add(
DNSRule_DefaultOptions().apply {
makeSingBoxRule(domainListDNSBlock.toHashSet().toList())
server = "dns-block"
disable_cache = true
}
)
if (domainListDNSBlock.isNotEmpty()) {
dns.rules.add(
DNSRule_DefaultOptions().apply {
makeSingBoxRule(domainListDNSBlock.toHashSet().toList())
server = "dns-block"
disable_cache = true
}
)
}
}

// Disable DNS for test
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/moe/matsuri/nb4a/DNS.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ object DNS {
domain.plusAssign(it)
}
}
if (geosite?.isEmpty() == true) geosite = null
if (domain?.isEmpty() == true) domain = null
if (domain_suffix?.isEmpty() == true) domain_suffix = null
if (domain_regex?.isEmpty() == true) domain_regex = null
if (domain_keyword?.isEmpty() == true) domain_keyword = null
}

fun SingBoxOptions.Rule_DefaultOptions.makeSingBoxRule(list: List<String>, isIP: Boolean) {
Expand Down Expand Up @@ -70,5 +75,12 @@ object DNS {
domain.plusAssign(it)
}
}
if (ip_cidr?.isEmpty() == true) geosite = null
if (geoip?.isEmpty() == true) geosite = null
if (geosite?.isEmpty() == true) geosite = null
if (domain?.isEmpty() == true) domain = null
if (domain_suffix?.isEmpty() == true) domain_suffix = null
if (domain_regex?.isEmpty() == true) domain_regex = null
if (domain_keyword?.isEmpty() == true) domain_keyword = null
}
}

0 comments on commit 03c99d7

Please sign in to comment.