do prestart dns checkings 10 times faster in parallel

This commit is contained in:
Hiddify
2024-02-12 07:59:55 +01:00
parent f29b26399c
commit df750c3f9c
2 changed files with 15 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ package config
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
@@ -12,6 +13,7 @@ import (
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
dns "github.com/sagernet/sing-dns"
"github.com/sagernet/sing/common/batch"
)
const (
@@ -470,11 +472,19 @@ func BuildConfig(opt ConfigOptions, input option.Options) (*option.Options, erro
if len(directDNSDomains) > 0 {
trickDnsDomains := []string{}
directDNSDomains = removeDuplicateStr(directDNSDomains)
b, _ := batch.New(context.Background(), batch.WithConcurrencyNum[bool](10))
for _, d := range directDNSDomains {
if isBlockedDomain(d) {
trickDnsDomains = append(trickDnsDomains, d)
b.Go(d, func() (bool, error) {
return isBlockedDomain(d), nil
})
}
b.Wait()
for domain, isBlock := range b.Result() {
if isBlock.Value {
trickDnsDomains = append(trickDnsDomains, domain)
}
}
trickDomains := strings.Join(trickDnsDomains, ",")
trickRule := Rule{Domains: trickDomains, Outbound: OutboundBypassTag}
trickDnsRule := trickRule.MakeDNSRule()