mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-09-23 16:30:25 +00:00
Reduce rule matching memory allocations
This commit is contained in:
@@ -129,7 +129,7 @@ func (t *DNSTransport) updateDNSServers(routeConfig *router.Config, dnsConfig *n
|
||||
}
|
||||
myResolvers = append(myResolvers, myResolver)
|
||||
}
|
||||
routes[domain.WithTrailingDot()] = myResolvers
|
||||
routes[mDNS.CanonicalName(domain.WithTrailingDot())] = myResolvers
|
||||
}
|
||||
hosts := make(map[string][]netip.Addr)
|
||||
for domain, addresses := range dnsConfig.Hosts {
|
||||
@@ -275,14 +275,25 @@ func (t *DNSTransport) PreferredDomain(domain string) bool {
|
||||
if t.acceptSearchDomain && len(searchDomains) > 0 && mDNS.CountLabel(domain) == 1 {
|
||||
return true
|
||||
}
|
||||
canonicalDomain := mDNS.CanonicalName(domain)
|
||||
for suffix := range routes {
|
||||
if mDNS.IsSubDomain(suffix, domain) {
|
||||
if matchDomainSuffix(canonicalDomain, suffix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func matchDomainSuffix(domain string, suffix string) bool {
|
||||
if suffix == "." || suffix == "" {
|
||||
return true
|
||||
}
|
||||
if !strings.HasSuffix(domain, suffix) {
|
||||
return false
|
||||
}
|
||||
return len(domain) == len(suffix) || domain[len(domain)-len(suffix)-1] == '.'
|
||||
}
|
||||
|
||||
func (t *DNSTransport) Exchange(ctx context.Context, message *mDNS.Msg) (*mDNS.Msg, error) {
|
||||
done := make(chan struct{})
|
||||
var (
|
||||
@@ -375,8 +386,9 @@ func (t *DNSTransport) exchangeOnce(ctx context.Context, message *mDNS.Msg, allo
|
||||
}
|
||||
return
|
||||
}
|
||||
canonicalName := mDNS.CanonicalName(question.Name)
|
||||
for domainSuffix, transports := range routes {
|
||||
if mDNS.IsSubDomain(domainSuffix, question.Name) {
|
||||
if matchDomainSuffix(canonicalName, domainSuffix) {
|
||||
if len(transports) == 0 {
|
||||
callback(&mDNS.Msg{
|
||||
MsgHdr: mDNS.MsgHdr{
|
||||
|
||||
@@ -953,7 +953,7 @@ func (t *Endpoint) PreferredDomain(metadata *adapter.InboundContext, domain stri
|
||||
}
|
||||
}
|
||||
for _, suffix := range t.routeSuffixes.Load() {
|
||||
if mDNS.IsSubDomain(suffix, domain) {
|
||||
if matchDomainSuffix(domain, suffix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -1000,7 +1000,7 @@ func (t *Endpoint) onReconfig(cfg *wgcfg.Config, routerCfg *router.Config, dnsCf
|
||||
}
|
||||
routeSuffixes := make([]string, 0, len(dnsCfg.Routes))
|
||||
for fqdn := range dnsCfg.Routes {
|
||||
routeSuffixes = append(routeSuffixes, fqdn.WithoutTrailingDot())
|
||||
routeSuffixes = append(routeSuffixes, strings.ToLower(fqdn.WithoutTrailingDot()))
|
||||
}
|
||||
t.routeDomains.Store(routeDomains)
|
||||
t.routeSuffixes.Store(routeSuffixes)
|
||||
|
||||
Reference in New Issue
Block a user