Skip to content

Commit

Permalink
feat: implement DNS rules to match any outbound
Browse files Browse the repository at this point in the history
  • Loading branch information
Restia-Ashbell committed Sep 28, 2024
1 parent 86c22a8 commit c173a67
Showing 1 changed file with 4 additions and 32 deletions.
36 changes: 4 additions & 32 deletions app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import io.nekohasekai.sagernet.fmt.ssh.SSHBean
import io.nekohasekai.sagernet.fmt.tuic.TuicBean
import io.nekohasekai.sagernet.fmt.v2ray.StandardV2RayBean
import io.nekohasekai.sagernet.fmt.wireguard.WireGuardBean
import io.nekohasekai.sagernet.ktx.isIpAddress
import io.nekohasekai.sagernet.ktx.mkPort
import io.nekohasekai.sagernet.utils.PackageCache
import moe.matsuri.nb4a.*
Expand All @@ -29,7 +28,6 @@ import moe.matsuri.nb4a.proxy.shadowtls.buildSingBoxOutboundShadowTLSBean
import moe.matsuri.nb4a.utils.JavaUtil.gson
import moe.matsuri.nb4a.utils.Util
import moe.matsuri.nb4a.utils.listByLineOrComma
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import org.json.JSONArray
import org.json.JSONObject

Expand Down Expand Up @@ -148,7 +146,6 @@ fun buildConfig(
}.toHashSet().toList()).associateBy { it.id }
val buildSelector = !forTest && group?.isSelector == true && !forExport
val userDNSRuleList = mutableListOf<DNSRule_DefaultOptions>()
val domainListDNSDirectForce = mutableListOf<String>()
val bypassDNSBeans = hashSetOf<AbstractBean>()
val isVPN = DataStore.serviceMode == Key.MODE_VPN
val bind = if (!forTest && DataStore.allowAccess) "0.0.0.0" else LOCALHOST
Expand Down Expand Up @@ -398,13 +395,6 @@ fun buildConfig(
} catch (_: Exception) {
}

// domain_strategy
pastEntity?.requireBean()?.apply {
// don't loopback
if (defaultServerDomainStrategy != "" && !serverAddress.isIpAddress()) {
domainListDNSDirectForce.add("full:$serverAddress")
}
}
currentOutbound["domain_strategy"] =
if (forTest) "" else defaultServerDomainStrategy

Expand Down Expand Up @@ -645,22 +635,6 @@ fun buildConfig(
serverAddr = toString()
}
}

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

remoteDns.forEach {
var address = it
if (address.contains("://")) {
address = address.substringAfter("://")
}
"https://$address".toHttpUrlOrNull()?.apply {
if (!host.isIpAddress()) {
domainListDNSDirectForce.add("full:$host")
}
}
}

// remote dns obj
Expand Down Expand Up @@ -750,13 +724,11 @@ fun buildConfig(
disable_cache = true
})
}
// force bypass (always top DNS rule)
if (domainListDNSDirectForce.isNotEmpty()) {
dns.rules.add(0, DNSRule_DefaultOptions().apply {
makeSingBoxRule(domainListDNSDirectForce.toHashSet().toList())
// any outbound dns
dns.rules.add(0, DNSRule_DefaultOptions().apply {
outbound = listOf("any")
server = "dns-direct"
})
}
})
}
}.let {
ConfigBuildResult(
Expand Down

0 comments on commit c173a67

Please sign in to comment.