2022-01-22 22:10:45 +08:00
|
|
|
package common
|
2021-11-17 16:03:47 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/component/geodata"
|
|
|
|
"github.com/Dreamacro/clash/component/geodata/router"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
|
|
|
|
2022-02-04 23:33:36 +08:00
|
|
|
_ "github.com/Dreamacro/clash/component/geodata/memconservative"
|
2021-11-17 16:03:47 +08:00
|
|
|
_ "github.com/Dreamacro/clash/component/geodata/standard"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GEOSITE struct {
|
2022-03-13 01:21:23 +08:00
|
|
|
*Base
|
|
|
|
country string
|
|
|
|
adapter string
|
|
|
|
matcher *router.DomainMatcher
|
2021-11-17 16:03:47 +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
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:43:08 +08:00
|
|
|
func (gs *GEOSITE) ShouldFindProcess() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
func (gs *GEOSITE) GetDomainMatcher() *router.DomainMatcher {
|
|
|
|
return gs.matcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGEOSITE(country string, adapter string, ruleExtra *C.RuleExtra) (*GEOSITE, error) {
|
|
|
|
matcher, recordsCount, err := geodata.LoadGeoSiteMatcher(country)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infoln("Start initial GeoSite rule %s => %s, records: %d", country, adapter, recordsCount)
|
|
|
|
|
|
|
|
geoSite := &GEOSITE{
|
2022-03-13 01:21:23 +08:00
|
|
|
Base: &Base{},
|
|
|
|
country: country,
|
|
|
|
adapter: adapter,
|
|
|
|
matcher: matcher,
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return geoSite, nil
|
|
|
|
}
|
2022-03-13 01:21:23 +08:00
|
|
|
|
|
|
|
var _ C.Rule = (*GEOSITE)(nil)
|