mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 11:42:43 +08:00
chore: sniffer use TrieSet
This commit is contained in:
parent
d52748165f
commit
c3ed06e99d
|
@ -1,4 +1,5 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
func Reverse(s string) string {
|
func Reverse(s string) string {
|
||||||
a := []rune(s)
|
a := []rune(s)
|
||||||
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
|
for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 {
|
||||||
|
|
|
@ -28,8 +28,8 @@ var Dispatcher *SnifferDispatcher
|
||||||
type SnifferDispatcher struct {
|
type SnifferDispatcher struct {
|
||||||
enable bool
|
enable bool
|
||||||
sniffers map[sniffer.Sniffer]SnifferConfig
|
sniffers map[sniffer.Sniffer]SnifferConfig
|
||||||
forceDomain *trie.DomainTrie[struct{}]
|
forceDomain *trie.Set
|
||||||
skipSNI *trie.DomainTrie[struct{}]
|
skipSNI *trie.Set
|
||||||
skipList *cache.LruCache[string, uint8]
|
skipList *cache.LruCache[string, uint8]
|
||||||
rwMux sync.RWMutex
|
rwMux sync.RWMutex
|
||||||
forceDnsMapping bool
|
forceDnsMapping bool
|
||||||
|
@ -37,7 +37,7 @@ type SnifferDispatcher struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sd *SnifferDispatcher) TCPSniff(conn *N.BufferedConn, metadata *C.Metadata) {
|
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)
|
port, err := strconv.ParseUint(metadata.DstPort, 10, 16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debugln("[Sniffer] Dst port is error")
|
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)
|
log.Debugln("[Sniffer] All sniffing sniff failed with from [%s:%s] to [%s:%s]", metadata.SrcIP, metadata.SrcPort, metadata.String(), metadata.DstPort)
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
if sd.skipSNI.Search(host) != nil {
|
if sd.skipSNI.Has(host) {
|
||||||
log.Debugln("[Sniffer] Skip sni[%s]", host)
|
log.Debugln("[Sniffer] Skip sni[%s]", host)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -166,8 +166,8 @@ func NewCloseSnifferDispatcher() (*SnifferDispatcher, error) {
|
||||||
return &dispatcher, nil
|
return &dispatcher, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSnifferDispatcher(snifferConfig map[sniffer.Type]SnifferConfig, forceDomain *trie.DomainTrie[struct{}],
|
func NewSnifferDispatcher(snifferConfig map[sniffer.Type]SnifferConfig,
|
||||||
skipSNI *trie.DomainTrie[struct{}],
|
forceDomain *trie.Set, skipSNI *trie.Set,
|
||||||
forceDnsMapping bool, parsePureIp bool) (*SnifferDispatcher, error) {
|
forceDnsMapping bool, parsePureIp bool) (*SnifferDispatcher, error) {
|
||||||
dispatcher := SnifferDispatcher{
|
dispatcher := SnifferDispatcher{
|
||||||
enable: true,
|
enable: true,
|
||||||
|
|
|
@ -44,7 +44,7 @@ func NewDomainTrieSet(keys []string) *Set {
|
||||||
reserveDomains = append(reserveDomains, reserveDomain)
|
reserveDomains = append(reserveDomains, reserveDomain)
|
||||||
}
|
}
|
||||||
sort.Strings(reserveDomains)
|
sort.Strings(reserveDomains)
|
||||||
keys=reserveDomains
|
keys = reserveDomains
|
||||||
ss := &Set{}
|
ss := &Set{}
|
||||||
lIdx := 0
|
lIdx := 0
|
||||||
|
|
||||||
|
@ -79,7 +79,7 @@ func NewDomainTrieSet(keys []string) *Set {
|
||||||
|
|
||||||
// Has query for a key and return whether it presents in the Set.
|
// Has query for a key and return whether it presents in the Set.
|
||||||
func (ss *Set) Has(key string) bool {
|
func (ss *Set) Has(key string) bool {
|
||||||
key=utils.Reverse(key)
|
key = utils.Reverse(key)
|
||||||
// no more labels in this node
|
// no more labels in this node
|
||||||
// skip character matching
|
// skip character matching
|
||||||
// go to next level
|
// go to next level
|
||||||
|
|
|
@ -136,9 +136,8 @@ type IPTables struct {
|
||||||
type Sniffer struct {
|
type Sniffer struct {
|
||||||
Enable bool
|
Enable bool
|
||||||
Sniffers map[snifferTypes.Type]SNIFF.SnifferConfig
|
Sniffers map[snifferTypes.Type]SNIFF.SnifferConfig
|
||||||
Reverses *trie.DomainTrie[struct{}]
|
ForceDomain *trie.Set
|
||||||
ForceDomain *trie.DomainTrie[struct{}]
|
SkipDomain *trie.Set
|
||||||
SkipDomain *trie.DomainTrie[struct{}]
|
|
||||||
ForceDnsMapping bool
|
ForceDnsMapping bool
|
||||||
ParsePureIp bool
|
ParsePureIp bool
|
||||||
}
|
}
|
||||||
|
@ -1340,24 +1339,8 @@ func parseSniffer(snifferRaw RawSniffer) (*Sniffer, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sniffer.Sniffers = loadSniffer
|
sniffer.Sniffers = loadSniffer
|
||||||
sniffer.ForceDomain = trie.New[struct{}]()
|
sniffer.ForceDomain = trie.NewDomainTrieSet(snifferRaw.ForceDomain)
|
||||||
for _, domain := range snifferRaw.ForceDomain {
|
sniffer.SkipDomain = trie.NewDomainTrieSet(snifferRaw.SkipDomain)
|
||||||
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()
|
|
||||||
|
|
||||||
return sniffer, nil
|
return sniffer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -198,7 +198,7 @@ func updateDNS(c *config.DNS, ruleProvider map[string]provider.RuleProvider, gen
|
||||||
case "domain-set":
|
case "domain-set":
|
||||||
if p, ok := ruleProvider[key]; ok {
|
if p, ok := ruleProvider[key]; ok {
|
||||||
domainSetPolicies[p] = nameservers
|
domainSetPolicies[p] = nameservers
|
||||||
}
|
}
|
||||||
case "geosite":
|
case "geosite":
|
||||||
// TODO:
|
// TODO:
|
||||||
}
|
}
|
||||||
|
@ -221,9 +221,9 @@ func updateDNS(c *config.DNS, ruleProvider map[string]provider.RuleProvider, gen
|
||||||
Domain: c.FallbackFilter.Domain,
|
Domain: c.FallbackFilter.Domain,
|
||||||
GeoSite: c.FallbackFilter.GeoSite,
|
GeoSite: c.FallbackFilter.GeoSite,
|
||||||
},
|
},
|
||||||
Default: c.DefaultNameserver,
|
Default: c.DefaultNameserver,
|
||||||
Policy: c.NameServerPolicy,
|
Policy: c.NameServerPolicy,
|
||||||
ProxyServer: c.ProxyServerNameserver,
|
ProxyServer: c.ProxyServerNameserver,
|
||||||
DomainSetPolicy: domainSetPolicies,
|
DomainSetPolicy: domainSetPolicies,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user