github.com/yaling888/clash@v1.53.0/rule/geosite.go (about) 1 package rules 2 3 import ( 4 "fmt" 5 6 "github.com/phuslu/log" 7 8 "github.com/yaling888/clash/component/geodata" 9 "github.com/yaling888/clash/component/geodata/router" 10 C "github.com/yaling888/clash/constant" 11 ) 12 13 type GEOSITE struct { 14 *Base 15 country string 16 adapter string 17 matcher *router.DomainMatcher 18 } 19 20 func (gs *GEOSITE) RuleType() C.RuleType { 21 return C.GEOSITE 22 } 23 24 func (gs *GEOSITE) Match(metadata *C.Metadata) bool { 25 domain := metadata.Host 26 if domain == "" { 27 return false 28 } 29 return gs.matcher.ApplyDomain(domain) 30 } 31 32 func (gs *GEOSITE) Adapter() string { 33 return gs.adapter 34 } 35 36 func (gs *GEOSITE) Payload() string { 37 return gs.country 38 } 39 40 func (gs *GEOSITE) ShouldResolveIP() bool { 41 return false 42 } 43 44 func (gs *GEOSITE) GetDomainMatcher() *router.DomainMatcher { 45 return gs.matcher 46 } 47 48 func NewGEOSITE(country string, adapter string) (*GEOSITE, error) { 49 matcher, recordsCount, err := geodata.LoadProviderByCode(country) 50 if err != nil { 51 return nil, fmt.Errorf("load GeoSite data error, %w", err) 52 } 53 54 count := fmt.Sprintf("%d", recordsCount) 55 if recordsCount == 0 { 56 count = "from cache" 57 } 58 if adapter == C.ScriptRuleGeoSiteTarget { 59 adapter = "Script" 60 } 61 62 log.Info(). 63 Str("country", country). 64 Str("proxy", adapter). 65 Str("records", count). 66 Msg("[Config] initial GeoSite rule") 67 68 geoSite := &GEOSITE{ 69 Base: &Base{}, 70 country: country, 71 adapter: adapter, 72 matcher: matcher, 73 } 74 75 return geoSite, nil 76 } 77 78 var _ C.Rule = (*GEOSITE)(nil)