chore: sniffer use TrieSet

This commit is contained in:
Skyxim 2023-03-29 13:24:26 +08:00
parent d52748165f
commit c3ed06e99d
5 changed files with 17 additions and 33 deletions

View File

@ -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 {

View File

@ -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,

View File

@ -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

View File

@ -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
}

View File

@ -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,
}