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"
|
2023-07-14 22:28:24 +08:00
|
|
|
"strings"
|
2022-04-20 01:52:51 +08:00
|
|
|
|
2022-01-26 12:01:14 +08:00
|
|
|
"github.com/Dreamacro/clash/component/geodata"
|
2021-11-17 16:03:47 +08:00
|
|
|
"github.com/Dreamacro/clash/component/geodata/router"
|
2022-03-16 00:43:08 +08:00
|
|
|
"github.com/Dreamacro/clash/component/mmdb"
|
2020-09-28 22:17:10 +08:00
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2022-03-16 17:33:08 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-01-26 12:01:14 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
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 {
|
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-01-26 12:01:14 +08:00
|
|
|
var geoIPMatcher *router.GeoIPMatcher
|
|
|
|
|
2022-04-20 01:52:51 +08:00
|
|
|
func (gf *geoipFilter) Match(ip netip.Addr) bool {
|
2022-03-16 17:33:08 +08:00
|
|
|
if !C.GeodataMode {
|
2023-07-17 10:33:20 +08:00
|
|
|
codes := mmdb.Instance().LookupCode(ip.AsSlice())
|
|
|
|
for _, code := range codes {
|
|
|
|
if !strings.EqualFold(code, gf.code) && !ip.IsPrivate() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2022-03-16 00:43:08 +08:00
|
|
|
}
|
|
|
|
|
2022-01-26 12:01:14 +08:00
|
|
|
if geoIPMatcher == nil {
|
2023-03-23 18:35:37 +08:00
|
|
|
var err error
|
|
|
|
geoIPMatcher, _, err = geodata.LoadGeoIPMatcher("CN")
|
2022-01-26 12:01:14 +08:00
|
|
|
if err != nil {
|
2023-03-23 18:35:37 +08:00
|
|
|
log.Errorln("[GeoIPFilter] LoadGeoIPMatcher error: %s", err.Error())
|
2022-01-26 12:01:14 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 01:52:51 +08:00
|
|
|
return !geoIPMatcher.Match(ip.AsSlice())
|
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)
|
|
|
|
}
|
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 {
|
2022-11-02 22:28:18 +08:00
|
|
|
tree *trie.DomainTrie[struct{}]
|
2020-09-28 22:17:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDomainFilter(domains []string) *domainFilter {
|
2022-11-02 22:28:18 +08:00
|
|
|
df := domainFilter{tree: trie.New[struct{}]()}
|
2020-09-28 22:17:10 +08:00
|
|
|
for _, domain := range domains {
|
2022-11-02 22:28:18 +08:00
|
|
|
_ = df.tree.Insert(domain, struct{}{})
|
2020-09-28 22:17:10 +08:00
|
|
|
}
|
2022-11-30 19:42:05 +08:00
|
|
|
df.tree.Optimize()
|
2020-09-28 22:17:10 +08:00
|
|
|
return &df
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *domainFilter) Match(domain string) bool {
|
|
|
|
return df.tree.Search(domain) != nil
|
|
|
|
}
|
2021-11-17 16:03:47 +08:00
|
|
|
|
|
|
|
type geoSiteFilter struct {
|
|
|
|
matchers []*router.DomainMatcher
|
|
|
|
}
|
|
|
|
|
2023-01-21 14:40:36 +08:00
|
|
|
func NewGeoSite(group string) (fallbackDomainFilter, error) {
|
2023-03-18 22:33:39 +08:00
|
|
|
if err := geodata.InitGeoSite(); err != nil {
|
|
|
|
log.Errorln("can't initial GeoSite: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-21 14:40:36 +08:00
|
|
|
matcher, _, err := geodata.LoadGeoSiteMatcher(group)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filter := &geoSiteFilter{
|
|
|
|
matchers: []*router.DomainMatcher{matcher},
|
|
|
|
}
|
|
|
|
return filter, nil
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
func (gsf *geoSiteFilter) Match(domain string) bool {
|
|
|
|
for _, matcher := range gsf.matchers {
|
|
|
|
if matcher.ApplyDomain(domain) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|