1
0
mirror of https://github.com/MetaCubeX/mihomo.git synced 2025-09-20 04:25:59 +08:00
Files
mihomo/dns/filters.go

65 lines
1.2 KiB
Go
Raw Normal View History

2019-09-15 13:36:45 +08:00
package dns
2020-01-11 21:07:01 +08:00
import (
2022-04-20 01:52:51 +08:00
"net/netip"
2021-10-28 00:06:55 +08:00
"strings"
2020-01-11 21:07:01 +08:00
"github.com/Dreamacro/clash/component/geodata/router"
2021-10-28 00:06:55 +08:00
"github.com/Dreamacro/clash/component/mmdb"
"github.com/Dreamacro/clash/component/trie"
2020-01-11 21:07:01 +08:00
)
2019-09-15 13:36:45 +08:00
type fallbackIPFilter interface {
2022-04-20 01:52:51 +08:00
Match(netip.Addr) bool
2019-09-15 13:36:45 +08:00
}
2021-08-25 15:15:13 +08:00
type geoipFilter struct {
code string
}
2019-09-15 13:36:45 +08:00
2022-04-20 01:52:51 +08:00
func (gf *geoipFilter) Match(ip netip.Addr) bool {
record, _ := mmdb.Instance().Country(ip.AsSlice())
2022-03-15 02:55:06 +08:00
return !strings.EqualFold(record.Country.IsoCode, gf.code) && !ip.IsPrivate()
2019-09-15 13:36:45 +08:00
}
type ipnetFilter struct {
2022-04-20 01:52:51 +08:00
ipnet *netip.Prefix
2019-09-15 13:36:45 +08:00
}
2022-04-20 01:52:51 +08:00
func (inf *ipnetFilter) Match(ip netip.Addr) bool {
2019-09-15 13:36:45 +08:00
return inf.ipnet.Contains(ip)
}
type fallbackDomainFilter interface {
Match(domain string) bool
}
type domainFilter struct {
2022-04-06 04:25:53 +08:00
tree *trie.DomainTrie[bool]
}
func NewDomainFilter(domains []string) *domainFilter {
2022-04-06 04:25:53 +08:00
df := domainFilter{tree: trie.New[bool]()}
for _, domain := range domains {
2022-04-20 01:52:51 +08:00
_ = df.tree.Insert(domain, true)
}
return &df
}
func (df *domainFilter) Match(domain string) bool {
return df.tree.Search(domain) != nil
}
type geoSiteFilter struct {
matchers []*router.DomainMatcher
}
func (gsf *geoSiteFilter) Match(domain string) bool {
for _, matcher := range gsf.matchers {
if matcher.ApplyDomain(domain) {
return true
}
}
return false
}