From c3ed06e99dd8b26cc11f15c425c93e21b50f8647 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Wed, 29 Mar 2023 13:24:26 +0800 Subject: [PATCH] chore: sniffer use TrieSet --- common/utils/strings.go | 1 + component/sniffer/dispatcher.go | 12 ++++++------ component/trie/sskv.go | 4 ++-- config/config.go | 25 ++++--------------------- hub/executor/executor.go | 8 ++++---- 5 files changed, 17 insertions(+), 33 deletions(-) diff --git a/common/utils/strings.go b/common/utils/strings.go index 04ed7a2b..5d5ae596 100644 --- a/common/utils/strings.go +++ b/common/utils/strings.go @@ -1,4 +1,5 @@ package utils + func Reverse(s string) string { a := []rune(s) for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 { diff --git a/component/sniffer/dispatcher.go b/component/sniffer/dispatcher.go index 97d448ce..3fe40bff 100644 --- a/component/sniffer/dispatcher.go +++ b/component/sniffer/dispatcher.go @@ -28,8 +28,8 @@ var Dispatcher *SnifferDispatcher type SnifferDispatcher struct { enable bool sniffers map[sniffer.Sniffer]SnifferConfig - forceDomain *trie.DomainTrie[struct{}] - skipSNI *trie.DomainTrie[struct{}] + forceDomain *trie.Set + skipSNI *trie.Set skipList *cache.LruCache[string, uint8] rwMux sync.RWMutex forceDnsMapping bool @@ -37,7 +37,7 @@ type SnifferDispatcher struct { } func (sd *SnifferDispatcher) TCPSniff(conn *N.BufferedConn, metadata *C.Metadata) { - if (metadata.Host == "" && sd.parsePureIp) || sd.forceDomain.Search(metadata.Host) != nil || (metadata.DNSMode == C.DNSMapping && sd.forceDnsMapping) { + if (metadata.Host == "" && sd.parsePureIp) || sd.forceDomain.Has(metadata.Host) || (metadata.DNSMode == C.DNSMapping && sd.forceDnsMapping) { port, err := strconv.ParseUint(metadata.DstPort, 10, 16) if err != nil { log.Debugln("[Sniffer] Dst port is error") @@ -74,7 +74,7 @@ func (sd *SnifferDispatcher) TCPSniff(conn *N.BufferedConn, metadata *C.Metadata log.Debugln("[Sniffer] All sniffing sniff failed with from [%s:%s] to [%s:%s]", metadata.SrcIP, metadata.SrcPort, metadata.String(), metadata.DstPort) return } else { - if sd.skipSNI.Search(host) != nil { + if sd.skipSNI.Has(host) { log.Debugln("[Sniffer] Skip sni[%s]", host) return } @@ -166,8 +166,8 @@ func NewCloseSnifferDispatcher() (*SnifferDispatcher, error) { return &dispatcher, nil } -func NewSnifferDispatcher(snifferConfig map[sniffer.Type]SnifferConfig, forceDomain *trie.DomainTrie[struct{}], - skipSNI *trie.DomainTrie[struct{}], +func NewSnifferDispatcher(snifferConfig map[sniffer.Type]SnifferConfig, + forceDomain *trie.Set, skipSNI *trie.Set, forceDnsMapping bool, parsePureIp bool) (*SnifferDispatcher, error) { dispatcher := SnifferDispatcher{ enable: true, diff --git a/component/trie/sskv.go b/component/trie/sskv.go index 757822b3..34020812 100644 --- a/component/trie/sskv.go +++ b/component/trie/sskv.go @@ -44,7 +44,7 @@ func NewDomainTrieSet(keys []string) *Set { reserveDomains = append(reserveDomains, reserveDomain) } sort.Strings(reserveDomains) - keys=reserveDomains + keys = reserveDomains ss := &Set{} lIdx := 0 @@ -79,7 +79,7 @@ func NewDomainTrieSet(keys []string) *Set { // Has query for a key and return whether it presents in the Set. func (ss *Set) Has(key string) bool { - key=utils.Reverse(key) + key = utils.Reverse(key) // no more labels in this node // skip character matching // go to next level diff --git a/config/config.go b/config/config.go index f036097e..b6ad5145 100644 --- a/config/config.go +++ b/config/config.go @@ -136,9 +136,8 @@ type IPTables struct { type Sniffer struct { Enable bool Sniffers map[snifferTypes.Type]SNIFF.SnifferConfig - Reverses *trie.DomainTrie[struct{}] - ForceDomain *trie.DomainTrie[struct{}] - SkipDomain *trie.DomainTrie[struct{}] + ForceDomain *trie.Set + SkipDomain *trie.Set ForceDnsMapping bool ParsePureIp bool } @@ -1340,24 +1339,8 @@ func parseSniffer(snifferRaw RawSniffer) (*Sniffer, error) { } sniffer.Sniffers = loadSniffer - sniffer.ForceDomain = trie.New[struct{}]() - for _, domain := range snifferRaw.ForceDomain { - err := sniffer.ForceDomain.Insert(domain, struct{}{}) - if err != nil { - return nil, fmt.Errorf("error domian[%s] in force-domain, error:%v", domain, err) - } - } - sniffer.ForceDomain.Optimize() - - sniffer.SkipDomain = trie.New[struct{}]() - for _, domain := range snifferRaw.SkipDomain { - err := sniffer.SkipDomain.Insert(domain, struct{}{}) - if err != nil { - return nil, fmt.Errorf("error domian[%s] in force-domain, error:%v", domain, err) - } - } - sniffer.SkipDomain.Optimize() - + sniffer.ForceDomain = trie.NewDomainTrieSet(snifferRaw.ForceDomain) + sniffer.SkipDomain = trie.NewDomainTrieSet(snifferRaw.SkipDomain) return sniffer, nil } diff --git a/hub/executor/executor.go b/hub/executor/executor.go index 19612fee..228b1ea9 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -198,7 +198,7 @@ func updateDNS(c *config.DNS, ruleProvider map[string]provider.RuleProvider, gen case "domain-set": if p, ok := ruleProvider[key]; ok { domainSetPolicies[p] = nameservers - } + } case "geosite": // TODO: } @@ -221,9 +221,9 @@ func updateDNS(c *config.DNS, ruleProvider map[string]provider.RuleProvider, gen Domain: c.FallbackFilter.Domain, GeoSite: c.FallbackFilter.GeoSite, }, - Default: c.DefaultNameserver, - Policy: c.NameServerPolicy, - ProxyServer: c.ProxyServerNameserver, + Default: c.DefaultNameserver, + Policy: c.NameServerPolicy, + ProxyServer: c.ProxyServerNameserver, DomainSetPolicy: domainSetPolicies, }