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