2019-09-15 13:36:45 +08:00
|
|
|
package dns
|
|
|
|
|
2020-01-11 21:07:01 +08:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2020-09-28 22:17:10 +08:00
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
2021-07-01 22:49:29 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
"github.com/Dreamacro/clash/rule/geodata"
|
|
|
|
"github.com/Dreamacro/clash/rule/geodata/router"
|
|
|
|
_ "github.com/Dreamacro/clash/rule/geodata/standard"
|
2020-01-11 21:07:01 +08:00
|
|
|
)
|
2019-09-15 13:36:45 +08:00
|
|
|
|
2021-07-06 15:07:05 +08:00
|
|
|
var multiGeoIPMatcher *router.MultiGeoIPMatcher
|
2021-07-01 22:49:29 +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 {
|
2021-07-06 15:07:05 +08:00
|
|
|
if multiGeoIPMatcher == nil {
|
2021-09-01 18:29:48 +08:00
|
|
|
countryCode := gf.code
|
2021-07-06 15:07:05 +08:00
|
|
|
countryCodePrivate := "private"
|
2021-07-01 22:49:29 +08:00
|
|
|
geoLoader, err := geodata.GetGeoDataLoader("standard")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("[GeoIPFilter] GetGeoDataLoader error: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-01 18:29:48 +08:00
|
|
|
recordsCN, err := geoLoader.LoadGeoIP(countryCode)
|
2021-07-01 22:49:29 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Errorln("[GeoIPFilter] LoadGeoIP error: %s", err.Error())
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:07:05 +08:00
|
|
|
recordsPrivate, err := geoLoader.LoadGeoIP(countryCodePrivate)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("[GeoIPFilter] LoadGeoIP error: %s", err.Error())
|
|
|
|
return false
|
2021-07-01 22:49:29 +08:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:07:05 +08:00
|
|
|
geoips := []*router.GeoIP{
|
|
|
|
{
|
2021-09-01 18:29:48 +08:00
|
|
|
CountryCode: countryCode,
|
2021-07-06 15:07:05 +08:00
|
|
|
Cidr: recordsCN,
|
|
|
|
ReverseMatch: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CountryCode: countryCodePrivate,
|
|
|
|
Cidr: recordsPrivate,
|
|
|
|
ReverseMatch: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
multiGeoIPMatcher, err = router.NewMultiGeoIPMatcher(geoips)
|
2021-07-01 22:49:29 +08:00
|
|
|
|
|
|
|
if err != nil {
|
2021-07-06 15:07:05 +08:00
|
|
|
log.Errorln("[GeoIPFilter] NewMultiGeoIPMatcher error: %s", err.Error())
|
2021-07-01 22:49:29 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:07:05 +08:00
|
|
|
return !multiGeoIPMatcher.ApplyIp(ip)
|
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-07-06 15:07:05 +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
|
|
|
|
}
|