2021-07-01 22:49:29 +08:00
|
|
|
|
package rules
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2021-10-29 00:52:44 +08:00
|
|
|
|
"github.com/Dreamacro/clash/component/geodata"
|
|
|
|
|
"github.com/Dreamacro/clash/component/geodata/router"
|
2021-07-01 22:49:29 +08:00
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
|
"github.com/Dreamacro/clash/log"
|
2021-10-29 00:52:44 +08:00
|
|
|
|
|
|
|
|
|
_ "github.com/Dreamacro/clash/component/geodata/standard"
|
2021-07-01 22:49:29 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type GEOSITE struct {
|
2021-08-31 21:46:04 +08:00
|
|
|
|
country string
|
|
|
|
|
adapter string
|
|
|
|
|
ruleExtra *C.RuleExtra
|
|
|
|
|
matcher *router.DomainMatcher
|
2021-07-01 22:49:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gs *GEOSITE) RuleType() C.RuleType {
|
|
|
|
|
return C.GEOSITE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gs *GEOSITE) Match(metadata *C.Metadata) bool {
|
|
|
|
|
if metadata.AddrType != C.AtypDomainName {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
domain := metadata.Host
|
|
|
|
|
return gs.matcher.ApplyDomain(domain)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gs *GEOSITE) Adapter() string {
|
|
|
|
|
return gs.adapter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gs *GEOSITE) Payload() string {
|
|
|
|
|
return gs.country
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (gs *GEOSITE) ShouldResolveIP() bool {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 21:46:04 +08:00
|
|
|
|
func (gs *GEOSITE) RuleExtra() *C.RuleExtra {
|
|
|
|
|
return gs.ruleExtra
|
2021-07-01 22:49:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-31 21:46:04 +08:00
|
|
|
|
func NewGEOSITE(country string, adapter string, ruleExtra *C.RuleExtra) (*GEOSITE, error) {
|
2021-07-01 22:49:29 +08:00
|
|
|
|
geoLoaderName := "standard"
|
|
|
|
|
geoLoader, err := geodata.GetGeoDataLoader(geoLoaderName)
|
|
|
|
|
if err != nil {
|
2021-10-15 14:11:14 +08:00
|
|
|
|
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
|
2021-07-01 22:49:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
domains, err := geoLoader.LoadGeoSite(country)
|
|
|
|
|
if err != nil {
|
2021-10-15 14:11:14 +08:00
|
|
|
|
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
|
2021-07-01 22:49:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 22:37:30 +08:00
|
|
|
|
/**
|
|
|
|
|
linear: linear algorithm
|
|
|
|
|
matcher, err := router.NewDomainMatcher(domains)
|
|
|
|
|
mph:minimal perfect hash algorithm
|
|
|
|
|
*/
|
2021-07-01 22:49:29 +08:00
|
|
|
|
matcher, err := router.NewMphMatcherGroup(domains)
|
|
|
|
|
if err != nil {
|
2021-10-15 14:11:14 +08:00
|
|
|
|
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
|
2021-07-01 22:49:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Infoln("Start initial GeoSite rule %s => %s, records: %d", country, adapter, len(domains))
|
|
|
|
|
|
|
|
|
|
geoSite := &GEOSITE{
|
2021-08-31 21:46:04 +08:00
|
|
|
|
country: country,
|
|
|
|
|
adapter: adapter,
|
|
|
|
|
ruleExtra: ruleExtra,
|
|
|
|
|
matcher: matcher,
|
2021-07-01 22:49:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return geoSite, nil
|
|
|
|
|
}
|