2022-01-22 22:10:45 +08:00
|
|
|
package common
|
2018-06-10 22:50:03 +08:00
|
|
|
|
|
|
|
import (
|
2024-08-16 09:19:18 +08:00
|
|
|
"errors"
|
2022-01-26 12:01:14 +08:00
|
|
|
"fmt"
|
2021-08-29 22:19:22 +08:00
|
|
|
"strings"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/geodata"
|
|
|
|
"github.com/metacubex/mihomo/component/geodata/router"
|
|
|
|
"github.com/metacubex/mihomo/component/mmdb"
|
|
|
|
"github.com/metacubex/mihomo/component/resolver"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/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
|
2024-08-16 09:19:18 +08:00
|
|
|
country string
|
|
|
|
adapter string
|
|
|
|
noResolveIP bool
|
|
|
|
isSourceIP bool
|
|
|
|
geodata bool
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|
|
|
|
|
2024-03-02 17:41:04 +08:00
|
|
|
var _ C.Rule = (*GEOIP)(nil)
|
|
|
|
|
2018-06-10 22:50:03 +08:00
|
|
|
func (g *GEOIP) RuleType() C.RuleType {
|
2024-04-01 18:16:34 +08:00
|
|
|
if g.isSourceIP {
|
|
|
|
return C.SrcGEOIP
|
|
|
|
}
|
2018-06-10 22:50:03 +08:00
|
|
|
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
|
2024-04-01 18:16:34 +08:00
|
|
|
if g.isSourceIP {
|
|
|
|
ip = metadata.SrcIP
|
|
|
|
}
|
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
|
|
|
|
2024-03-02 17:41:04 +08:00
|
|
|
if g.country == "lan" {
|
2022-03-17 17:41:02 +08:00
|
|
|
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
|
|
|
}
|
2024-03-02 17:41:04 +08:00
|
|
|
|
|
|
|
for _, code := range metadata.DstGeoIP {
|
|
|
|
if g.country == code {
|
|
|
|
return true, g.adapter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 17:29:09 +08:00
|
|
|
if !C.GeodataMode {
|
2024-04-01 18:16:34 +08:00
|
|
|
if g.isSourceIP {
|
|
|
|
codes := mmdb.IPInstance().LookupCode(ip.AsSlice())
|
|
|
|
for _, code := range codes {
|
|
|
|
if g.country == code {
|
|
|
|
return true, g.adapter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, g.adapter
|
|
|
|
}
|
|
|
|
|
2024-03-02 17:41:04 +08:00
|
|
|
if metadata.DstGeoIP != nil {
|
|
|
|
return false, g.adapter
|
|
|
|
}
|
2024-03-12 03:14:25 +08:00
|
|
|
metadata.DstGeoIP = mmdb.IPInstance().LookupCode(ip.AsSlice())
|
2024-03-02 17:41:04 +08:00
|
|
|
for _, code := range metadata.DstGeoIP {
|
|
|
|
if g.country == code {
|
2023-07-17 10:33:20 +08:00
|
|
|
return true, g.adapter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, g.adapter
|
2022-03-16 00:43:08 +08:00
|
|
|
}
|
2024-03-02 17:41:04 +08:00
|
|
|
|
2024-08-16 09:19:18 +08:00
|
|
|
matcher, err := g.GetIPMatcher()
|
|
|
|
if err != nil {
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
match := matcher.Match(ip)
|
2024-04-01 18:16:34 +08:00
|
|
|
if match && !g.isSourceIP {
|
2024-03-02 17:41:04 +08:00
|
|
|
metadata.DstGeoIP = append(metadata.DstGeoIP, g.country)
|
|
|
|
}
|
|
|
|
return match, 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
|
|
|
|
}
|
|
|
|
|
2024-08-16 09:19:18 +08:00
|
|
|
func (g *GEOIP) GetIPMatcher() (router.IPMatcher, error) {
|
|
|
|
if g.geodata {
|
|
|
|
geoIPMatcher, err := geodata.LoadGeoIPMatcher(g.country)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("[GeoIP] %w", err)
|
|
|
|
}
|
|
|
|
return geoIPMatcher, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("geoip country not set")
|
2022-02-04 23:33:36 +08:00
|
|
|
}
|
2022-01-26 12:01:14 +08:00
|
|
|
|
2022-05-17 16:47:21 +08:00
|
|
|
func (g *GEOIP) GetRecodeSize() int {
|
2024-08-16 09:19:18 +08:00
|
|
|
if matcher, err := g.GetIPMatcher(); err == nil {
|
|
|
|
return matcher.Count()
|
|
|
|
}
|
|
|
|
return 0
|
2022-05-17 16:47:21 +08:00
|
|
|
}
|
|
|
|
|
2024-04-01 18:16:34 +08:00
|
|
|
func NewGEOIP(country string, adapter string, isSrc, 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
|
|
|
|
}
|
2024-03-02 17:41:04 +08:00
|
|
|
country = strings.ToLower(country)
|
2023-01-09 21:07:31 +08:00
|
|
|
|
2024-08-16 09:19:18 +08:00
|
|
|
geoip := &GEOIP{
|
|
|
|
Base: &Base{},
|
|
|
|
country: country,
|
|
|
|
adapter: adapter,
|
|
|
|
noResolveIP: noResolveIP,
|
|
|
|
isSourceIP: isSrc,
|
|
|
|
}
|
2024-03-02 17:41:04 +08:00
|
|
|
if !C.GeodataMode || country == "lan" {
|
2022-03-16 17:29:09 +08:00
|
|
|
return geoip, nil
|
|
|
|
}
|
|
|
|
|
2024-08-16 09:19:18 +08:00
|
|
|
geoip.geodata = true
|
|
|
|
geoIPMatcher, err := geoip.GetIPMatcher() // test load
|
2022-01-26 12:01:14 +08:00
|
|
|
if err != nil {
|
2024-08-16 09:19:18 +08:00
|
|
|
return nil, err
|
2022-01-26 12:01:14 +08:00
|
|
|
}
|
|
|
|
|
2024-08-16 09:19:18 +08:00
|
|
|
log.Infoln("Finished initial GeoIP rule %s => %s, records: %d", country, adapter, geoIPMatcher.Count())
|
2021-11-17 16:03:47 +08:00
|
|
|
return geoip, nil
|
2018-06-10 22:50:03 +08:00
|
|
|
}
|