clash/rules/common/geosite.go

75 lines
1.6 KiB
Go
Raw Normal View History

package common
2021-11-17 08:03:47 +00:00
import (
"fmt"
2022-11-21 02:33:42 +00:00
2023-11-03 13:01:45 +00:00
"github.com/metacubex/mihomo/component/geodata"
_ "github.com/metacubex/mihomo/component/geodata/memconservative"
"github.com/metacubex/mihomo/component/geodata/router"
_ "github.com/metacubex/mihomo/component/geodata/standard"
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
2021-11-17 08:03:47 +00:00
)
type GEOSITE struct {
2022-03-12 17:21:23 +00:00
*Base
2022-05-17 08:47:21 +00:00
country string
adapter string
matcher router.DomainMatcher
2022-05-17 08:47:21 +00:00
recodeSize int
2021-11-17 08:03:47 +00:00
}
func (gs *GEOSITE) RuleType() C.RuleType {
return C.GEOSITE
}
func (gs *GEOSITE) Match(metadata *C.Metadata) (bool, string) {
2023-02-18 09:18:58 +00:00
domain := metadata.RuleHost()
if len(domain) == 0 {
2022-11-21 02:33:42 +00:00
return false, ""
}
return gs.matcher.ApplyDomain(domain), gs.adapter
2021-11-17 08:03:47 +00:00
}
func (gs *GEOSITE) Adapter() string {
return gs.adapter
}
func (gs *GEOSITE) Payload() string {
return gs.country
}
func (gs *GEOSITE) GetDomainMatcher() router.DomainMatcher {
2021-11-17 08:03:47 +00:00
return gs.matcher
}
2022-05-17 08:47:21 +00:00
func (gs *GEOSITE) GetRecodeSize() int {
return gs.recodeSize
}
2022-03-17 15:24:07 +00:00
func NewGEOSITE(country string, adapter string) (*GEOSITE, error) {
2023-01-09 13:07:31 +00:00
if err := geodata.InitGeoSite(); err != nil {
log.Errorln("can't initial GeoSite: %s", err)
return nil, err
2022-05-30 13:26:41 +00:00
}
2022-05-17 08:47:21 +00:00
matcher, size, err := geodata.LoadGeoSiteMatcher(country)
2021-11-17 08:03:47 +00:00
if err != nil {
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
}
2022-05-17 08:47:21 +00:00
log.Infoln("Start initial GeoSite rule %s => %s, records: %d", country, adapter, size)
2021-11-17 08:03:47 +00:00
geoSite := &GEOSITE{
2022-05-17 08:47:21 +00:00
Base: &Base{},
country: country,
adapter: adapter,
matcher: matcher,
recodeSize: size,
2021-11-17 08:03:47 +00:00
}
return geoSite, nil
}
2022-03-12 17:21:23 +00:00
2023-01-09 13:07:31 +00:00
var _ C.Rule = (*GEOSITE)(nil)