2022-01-22 22:10:45 +08:00
|
|
|
package common
|
2021-11-17 16:03:47 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-21 10:33:42 +08:00
|
|
|
|
2023-11-03 21:01:45 +08: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 16:03:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type GEOSITE struct {
|
2022-03-13 01:21:23 +08:00
|
|
|
*Base
|
2022-05-17 16:47:21 +08:00
|
|
|
country string
|
|
|
|
adapter string
|
|
|
|
recodeSize int
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *GEOSITE) RuleType() C.RuleType {
|
|
|
|
return C.GEOSITE
|
|
|
|
}
|
|
|
|
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 17:30:35 +08:00
|
|
|
func (gs *GEOSITE) Match(metadata *C.Metadata) (bool, string) {
|
2024-08-28 12:25:45 +08:00
|
|
|
return gs.MatchDomain(metadata.RuleHost()), gs.adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchDomain implements C.DomainMatcher
|
|
|
|
func (gs *GEOSITE) MatchDomain(domain string) bool {
|
2023-02-18 17:18:58 +08:00
|
|
|
if len(domain) == 0 {
|
2024-08-28 12:25:45 +08:00
|
|
|
return false
|
2022-11-21 10:33:42 +08:00
|
|
|
}
|
2024-08-16 09:19:18 +08:00
|
|
|
matcher, err := gs.GetDomainMatcher()
|
|
|
|
if err != nil {
|
2024-08-28 12:25:45 +08:00
|
|
|
return false
|
2024-08-16 09:19:18 +08:00
|
|
|
}
|
2024-08-28 12:25:45 +08:00
|
|
|
return matcher.ApplyDomain(domain)
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *GEOSITE) Adapter() string {
|
|
|
|
return gs.adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *GEOSITE) Payload() string {
|
|
|
|
return gs.country
|
|
|
|
}
|
|
|
|
|
2024-08-16 09:19:18 +08:00
|
|
|
func (gs *GEOSITE) GetDomainMatcher() (router.DomainMatcher, error) {
|
|
|
|
matcher, err := geodata.LoadGeoSiteMatcher(gs.country)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("load GeoSite data error, %w", err)
|
|
|
|
}
|
|
|
|
return matcher, nil
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
2022-05-17 16:47:21 +08:00
|
|
|
func (gs *GEOSITE) GetRecodeSize() int {
|
2024-08-16 09:19:18 +08:00
|
|
|
if matcher, err := gs.GetDomainMatcher(); err == nil {
|
|
|
|
return matcher.Count()
|
|
|
|
}
|
|
|
|
return 0
|
2022-05-17 16:47:21 +08:00
|
|
|
}
|
|
|
|
|
2022-03-17 23:24:07 +08:00
|
|
|
func NewGEOSITE(country string, adapter string) (*GEOSITE, error) {
|
2023-01-09 21:07:31 +08:00
|
|
|
if err := geodata.InitGeoSite(); err != nil {
|
|
|
|
log.Errorln("can't initial GeoSite: %s", err)
|
|
|
|
return nil, err
|
2022-05-30 21:26:41 +08:00
|
|
|
}
|
|
|
|
|
2024-08-16 09:19:18 +08:00
|
|
|
geoSite := &GEOSITE{
|
|
|
|
Base: &Base{},
|
|
|
|
country: country,
|
|
|
|
adapter: adapter,
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
2024-08-16 09:19:18 +08:00
|
|
|
matcher, err := geoSite.GetDomainMatcher() // test load
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
2024-08-16 09:19:18 +08:00
|
|
|
log.Infoln("Finished initial GeoSite rule %s => %s, records: %d", country, adapter, matcher.Count())
|
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
return geoSite, nil
|
|
|
|
}
|
2022-03-13 01:21:23 +08:00
|
|
|
|
2023-01-09 21:07:31 +08:00
|
|
|
var _ C.Rule = (*GEOSITE)(nil)
|