github.com/chwjbn/xclash@v0.2.0/dns/filters.go (about)

     1  package dns
     2  
     3  import (
     4  	"net"
     5  	"strings"
     6  
     7  	"github.com/chwjbn/xclash/component/mmdb"
     8  	"github.com/chwjbn/xclash/component/trie"
     9  )
    10  
    11  type fallbackIPFilter interface {
    12  	Match(net.IP) bool
    13  }
    14  
    15  type geoipFilter struct {
    16  	code string
    17  }
    18  
    19  func (gf *geoipFilter) Match(ip net.IP) bool {
    20  	record, _ := mmdb.Instance().Country(ip)
    21  	return !strings.EqualFold(record.Country.IsoCode, gf.code) && !ip.IsPrivate()
    22  }
    23  
    24  type ipnetFilter struct {
    25  	ipnet *net.IPNet
    26  }
    27  
    28  func (inf *ipnetFilter) Match(ip net.IP) bool {
    29  	return inf.ipnet.Contains(ip)
    30  }
    31  
    32  type fallbackDomainFilter interface {
    33  	Match(domain string) bool
    34  }
    35  
    36  type domainFilter struct {
    37  	tree *trie.DomainTrie
    38  }
    39  
    40  func NewDomainFilter(domains []string) *domainFilter {
    41  	df := domainFilter{tree: trie.New()}
    42  	for _, domain := range domains {
    43  		df.tree.Insert(domain, "")
    44  	}
    45  	return &df
    46  }
    47  
    48  func (df *domainFilter) Match(domain string) bool {
    49  	return df.tree.Search(domain) != nil
    50  }