2019-09-15 13:36:45 +08:00
|
|
|
package dns
|
|
|
|
|
2020-01-11 21:07:01 +08:00
|
|
|
import (
|
|
|
|
"net"
|
2021-08-29 22:19:22 +08:00
|
|
|
"strings"
|
2020-01-11 21:07:01 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/mmdb"
|
2020-09-28 22:17:10 +08:00
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2020-01-11 21:07:01 +08:00
|
|
|
)
|
2019-09-15 13:36:45 +08:00
|
|
|
|
2020-09-28 22:17:10 +08:00
|
|
|
type fallbackIPFilter interface {
|
2019-09-15 13:36:45 +08:00
|
|
|
Match(net.IP) bool
|
|
|
|
}
|
|
|
|
|
2021-08-25 15:15:13 +08:00
|
|
|
type geoipFilter struct {
|
|
|
|
code string
|
|
|
|
}
|
2019-09-15 13:36:45 +08:00
|
|
|
|
|
|
|
func (gf *geoipFilter) Match(ip net.IP) bool {
|
2020-01-11 21:07:01 +08:00
|
|
|
record, _ := mmdb.Instance().Country(ip)
|
2021-08-29 22:19:22 +08:00
|
|
|
return !strings.EqualFold(record.Country.IsoCode, gf.code) && !ip.IsPrivate()
|
2019-09-15 13:36:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type ipnetFilter struct {
|
|
|
|
ipnet *net.IPNet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inf *ipnetFilter) Match(ip net.IP) bool {
|
|
|
|
return inf.ipnet.Contains(ip)
|
|
|
|
}
|
2020-09-28 22:17:10 +08:00
|
|
|
|
|
|
|
type fallbackDomainFilter interface {
|
|
|
|
Match(domain string) bool
|
|
|
|
}
|
2021-10-10 23:44:09 +08:00
|
|
|
|
2020-09-28 22:17:10 +08:00
|
|
|
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
|
|
|
|
}
|