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
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
"github.com/Dreamacro/clash/component/geodata"
|
2022-06-04 02:58:14 +08:00
|
|
|
_ "github.com/Dreamacro/clash/component/geodata/memconservative"
|
2021-11-17 16:03:47 +08:00
|
|
|
"github.com/Dreamacro/clash/component/geodata/router"
|
2022-06-04 02:58:14 +08:00
|
|
|
_ "github.com/Dreamacro/clash/component/geodata/standard"
|
2021-11-17 16:03:47 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
2022-11-21 10:33:42 +08:00
|
|
|
"github.com/Dreamacro/clash/transport/socks5"
|
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
|
|
|
|
matcher *router.DomainMatcher
|
|
|
|
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) {
|
2022-11-21 10:33:42 +08:00
|
|
|
if metadata.AddrType() != socks5.AtypDomainName {
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
domain := metadata.Host
|
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
|
|
|
return gs.matcher.ApplyDomain(domain), gs.adapter
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *GEOSITE) Adapter() string {
|
|
|
|
return gs.adapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *GEOSITE) Payload() string {
|
|
|
|
return gs.country
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gs *GEOSITE) GetDomainMatcher() *router.DomainMatcher {
|
|
|
|
return gs.matcher
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:47:21 +08:00
|
|
|
func (gs *GEOSITE) GetRecodeSize() int {
|
|
|
|
return gs.recodeSize
|
|
|
|
}
|
|
|
|
|
2022-03-17 23:24:07 +08:00
|
|
|
func NewGEOSITE(country string, adapter string) (*GEOSITE, error) {
|
2022-05-30 21:26:41 +08:00
|
|
|
if !initFlag {
|
2022-06-10 13:36:09 +08:00
|
|
|
if err := geodata.InitGeoSite(); err != nil {
|
2022-05-30 21:26:41 +08:00
|
|
|
log.Errorln("can't initial GeoSite: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
initFlag = true
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:47:21 +08:00
|
|
|
matcher, size, err := geodata.LoadGeoSiteMatcher(country)
|
2021-11-17 16:03:47 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("load GeoSite data error, %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:47:21 +08:00
|
|
|
log.Infoln("Start initial GeoSite rule %s => %s, records: %d", country, adapter, size)
|
2021-11-17 16:03:47 +08:00
|
|
|
|
|
|
|
geoSite := &GEOSITE{
|
2022-05-17 16:47:21 +08:00
|
|
|
Base: &Base{},
|
|
|
|
country: country,
|
|
|
|
adapter: adapter,
|
|
|
|
matcher: matcher,
|
|
|
|
recodeSize: size,
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return geoSite, nil
|
|
|
|
}
|
2022-03-13 01:21:23 +08:00
|
|
|
|
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
|
|
|
//var _ C.Rule = (*GEOSITE)(nil)
|