mirror of
https://gitclone.com/github.com/MetaCubeX/Clash.Meta
synced 2024-11-15 21:51:23 +08:00
52 lines
994 B
Go
52 lines
994 B
Go
package dns
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
|
|
"github.com/Dreamacro/clash/component/mmdb"
|
|
"github.com/Dreamacro/clash/component/trie"
|
|
C "github.com/Dreamacro/clash/constant"
|
|
)
|
|
|
|
type fallbackIPFilter interface {
|
|
Match(net.IP) bool
|
|
}
|
|
|
|
type geoipFilter struct {
|
|
code string
|
|
}
|
|
|
|
func (gf *geoipFilter) Match(ip net.IP) bool {
|
|
record, _ := mmdb.Instance().Country(ip)
|
|
return !strings.EqualFold(record.Country.IsoCode, gf.code) && !ip.IsPrivate() && !ip.Equal(C.TunBroadcastAddr)
|
|
}
|
|
|
|
type ipnetFilter struct {
|
|
ipnet *net.IPNet
|
|
}
|
|
|
|
func (inf *ipnetFilter) Match(ip net.IP) bool {
|
|
return inf.ipnet.Contains(ip)
|
|
}
|
|
|
|
type fallbackDomainFilter interface {
|
|
Match(domain string) bool
|
|
}
|
|
|
|
type domainFilter struct {
|
|
tree *trie.DomainTrie
|
|
}
|
|
|
|
func NewDomainFilter(domains []string) *domainFilter {
|
|
df := domainFilter{tree: trie.New()}
|
|
for _, domain := range domains {
|
|
df.tree.Insert(domain, "")
|
|
}
|
|
return &df
|
|
}
|
|
|
|
func (df *domainFilter) Match(domain string) bool {
|
|
return df.tree.Search(domain) != nil
|
|
}
|