2019-09-15 05:36:45 +00:00
|
|
|
package dns
|
|
|
|
|
2020-01-11 13:07:01 +00:00
|
|
|
import (
|
2022-01-26 04:01:14 +00:00
|
|
|
"github.com/Dreamacro/clash/component/geodata"
|
2021-11-17 08:03:47 +00:00
|
|
|
"github.com/Dreamacro/clash/component/geodata/router"
|
2022-03-15 16:43:08 +00:00
|
|
|
"github.com/Dreamacro/clash/component/mmdb"
|
2020-09-28 14:17:10 +00:00
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2022-03-16 09:33:08 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-01-26 04:01:14 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
"net"
|
2022-03-15 16:43:08 +00:00
|
|
|
"strings"
|
2020-01-11 13:07:01 +00:00
|
|
|
)
|
2019-09-15 05:36:45 +00:00
|
|
|
|
2020-09-28 14:17:10 +00:00
|
|
|
type fallbackIPFilter interface {
|
2019-09-15 05:36:45 +00:00
|
|
|
Match(net.IP) bool
|
|
|
|
}
|
|
|
|
|
2021-08-25 07:15:13 +00:00
|
|
|
type geoipFilter struct {
|
|
|
|
code string
|
|
|
|
}
|
2019-09-15 05:36:45 +00:00
|
|
|
|
2022-01-26 04:01:14 +00:00
|
|
|
var geoIPMatcher *router.GeoIPMatcher
|
|
|
|
|
2019-09-15 05:36:45 +00:00
|
|
|
func (gf *geoipFilter) Match(ip net.IP) bool {
|
2022-03-16 09:33:08 +00:00
|
|
|
if !C.GeodataMode {
|
2022-03-15 16:43:08 +00:00
|
|
|
record, _ := mmdb.Instance().Country(ip)
|
|
|
|
return !strings.EqualFold(record.Country.IsoCode, gf.code) && !ip.IsPrivate()
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:01:14 +00:00
|
|
|
if geoIPMatcher == nil {
|
|
|
|
countryCode := "cn"
|
2022-02-04 16:51:06 +00:00
|
|
|
geoLoader, err := geodata.GetGeoDataLoader(geodata.LoaderName())
|
2022-01-26 04:01:14 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln("[GeoIPFilter] GetGeoDataLoader error: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
records, err := geoLoader.LoadGeoIP(countryCode)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("[GeoIPFilter] LoadGeoIP error: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
geoIP := &router.GeoIP{
|
|
|
|
CountryCode: countryCode,
|
|
|
|
Cidr: records,
|
|
|
|
ReverseMatch: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
geoIPMatcher, err = router.NewGeoIPMatcher(geoIP)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("[GeoIPFilter] NewGeoIPMatcher error: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !geoIPMatcher.Match(ip)
|
2019-09-15 05:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ipnetFilter struct {
|
|
|
|
ipnet *net.IPNet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inf *ipnetFilter) Match(ip net.IP) bool {
|
|
|
|
return inf.ipnet.Contains(ip)
|
|
|
|
}
|
2020-09-28 14:17:10 +00:00
|
|
|
|
|
|
|
type fallbackDomainFilter interface {
|
|
|
|
Match(domain string) bool
|
|
|
|
}
|
2021-10-10 15:44:09 +00:00
|
|
|
|
2020-09-28 14:17:10 +00:00
|
|
|
type domainFilter struct {
|
2022-04-05 20:25:53 +00:00
|
|
|
tree *trie.DomainTrie[bool]
|
2020-09-28 14:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDomainFilter(domains []string) *domainFilter {
|
2022-04-05 20:25:53 +00:00
|
|
|
df := domainFilter{tree: trie.New[bool]()}
|
2020-09-28 14:17:10 +00:00
|
|
|
for _, domain := range domains {
|
2022-04-05 20:25:53 +00:00
|
|
|
df.tree.Insert(domain, true)
|
2020-09-28 14:17:10 +00:00
|
|
|
}
|
|
|
|
return &df
|
|
|
|
}
|
|
|
|
|
|
|
|
func (df *domainFilter) Match(domain string) bool {
|
|
|
|
return df.tree.Search(domain) != nil
|
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|