2022-01-22 22:10:45 +08:00
|
|
|
package common
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
import (
|
2022-01-26 12:01:14 +08:00
|
|
|
"fmt"
|
2021-08-29 22:19:22 +08:00
|
|
|
"strings"
|
|
|
|
|
2022-11-21 10:33:42 +08:00
|
|
|
"github.com/Dreamacro/clash/component/geodata"
|
|
|
|
"github.com/Dreamacro/clash/component/geodata/router"
|
2022-03-17 17:41:02 +08:00
|
|
|
"github.com/Dreamacro/clash/component/mmdb"
|
|
|
|
"github.com/Dreamacro/clash/component/resolver"
|
2018-06-10 22:50:03 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-01-26 12:01:14 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2018-08-01 00:18:29 +08:00
|
|
|
)
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
type GEOIP struct {
|
2022-03-17 17:41:02 +08:00
|
|
|
*Base
|
2022-03-17 23:24:07 +08:00
|
|
|
country string
|
|
|
|
adapter string
|
|
|
|
noResolveIP bool
|
2022-01-26 12:01:14 +08:00
|
|
|
geoIPMatcher *router.GeoIPMatcher
|
2022-05-17 16:47:21 +08:00
|
|
|
recodeSize int
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GEOIP) RuleType() C.RuleType {
|
|
|
|
return C.GEOIP
|
|
|
|
}
|
|
|
|
|
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 (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
|
2019-10-28 00:02:23 +08:00
|
|
|
ip := metadata.DstIP
|
2022-04-20 01:52:51 +08:00
|
|
|
if !ip.IsValid() {
|
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 false, ""
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2021-08-18 13:26:23 +08:00
|
|
|
|
2022-03-17 17:41:02 +08:00
|
|
|
if strings.EqualFold(g.country, "LAN") {
|
|
|
|
return ip.IsPrivate() ||
|
|
|
|
ip.IsUnspecified() ||
|
|
|
|
ip.IsLoopback() ||
|
|
|
|
ip.IsMulticast() ||
|
|
|
|
ip.IsLinkLocalUnicast() ||
|
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
|
|
|
resolver.IsFakeBroadcastIP(ip), g.adapter
|
2021-08-18 13:26:23 +08:00
|
|
|
}
|
2022-03-16 17:29:09 +08:00
|
|
|
if !C.GeodataMode {
|
2023-07-14 22:28:24 +08:00
|
|
|
code := mmdb.Instance().LookupCode(ip.AsSlice())
|
|
|
|
return strings.EqualFold(code, g.country), g.adapter
|
2022-03-16 00:43:08 +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
|
|
|
return g.geoIPMatcher.Match(ip.AsSlice()), g.adapter
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GEOIP) Adapter() string {
|
|
|
|
return g.adapter
|
|
|
|
}
|
|
|
|
|
2018-06-20 22:41:02 +08:00
|
|
|
func (g *GEOIP) Payload() string {
|
|
|
|
return g.country
|
|
|
|
}
|
|
|
|
|
2020-07-27 11:57:55 +08:00
|
|
|
func (g *GEOIP) ShouldResolveIP() bool {
|
|
|
|
return !g.noResolveIP
|
2019-10-28 00:02:23 +08:00
|
|
|
}
|
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
func (g *GEOIP) GetCountry() string {
|
|
|
|
return g.country
|
|
|
|
}
|
|
|
|
|
2022-02-04 23:33:36 +08:00
|
|
|
func (g *GEOIP) GetIPMatcher() *router.GeoIPMatcher {
|
|
|
|
return g.geoIPMatcher
|
|
|
|
}
|
2022-01-26 12:01:14 +08:00
|
|
|
|
2022-05-17 16:47:21 +08:00
|
|
|
func (g *GEOIP) GetRecodeSize() int {
|
|
|
|
return g.recodeSize
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:41:02 +08:00
|
|
|
func NewGEOIP(country string, adapter string, noResolveIP bool) (*GEOIP, error) {
|
2023-01-09 21:07:31 +08:00
|
|
|
if err := geodata.InitGeoIP(); err != nil {
|
|
|
|
log.Errorln("can't initial GeoIP: %s", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-17 00:33:03 +08:00
|
|
|
if !C.GeodataMode || strings.EqualFold(country, "LAN") {
|
2022-03-16 17:29:09 +08:00
|
|
|
geoip := &GEOIP{
|
2022-03-17 17:41:02 +08:00
|
|
|
Base: &Base{},
|
2022-03-16 17:29:09 +08:00
|
|
|
country: country,
|
|
|
|
adapter: adapter,
|
|
|
|
noResolveIP: noResolveIP,
|
|
|
|
}
|
|
|
|
return geoip, nil
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:47:21 +08:00
|
|
|
geoIPMatcher, size, err := geodata.LoadGeoIPMatcher(country)
|
2022-01-26 12:01:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("[GeoIP] %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2022-05-17 16:47:21 +08:00
|
|
|
log.Infoln("Start initial GeoIP rule %s => %s, records: %d", country, adapter, size)
|
2019-10-28 00:02:23 +08:00
|
|
|
geoip := &GEOIP{
|
2022-03-17 23:24:07 +08:00
|
|
|
Base: &Base{},
|
2022-01-26 12:01:14 +08:00
|
|
|
country: country,
|
|
|
|
adapter: adapter,
|
|
|
|
noResolveIP: noResolveIP,
|
|
|
|
geoIPMatcher: geoIPMatcher,
|
2022-05-17 16:47:21 +08:00
|
|
|
recodeSize: size,
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2021-11-17 16:03:47 +08:00
|
|
|
return geoip, nil
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
2022-03-17 17:41:02 +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 = (*GEOIP)(nil)
|