Clash.Meta/rules/common/geoip.go

153 lines
3.0 KiB
Go
Raw Normal View History

package common
2018-06-10 22:50:03 +08:00
import (
"errors"
"fmt"
"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 {
Merge remote-tracking branch 'yaling888/with-tun' into Alpha # Conflicts: # .github/workflows/codeql-analysis.yml # .github/workflows/linter.yml # .github/workflows/release.yml # Makefile # README.md # adapter/outbound/vless.go # component/geodata/memconservative/cache.go # component/geodata/router/condition.go # component/geodata/router/condition_geoip.go # component/geodata/standard/standard.go # component/geodata/utils.go # config/config.go # config/initial.go # constant/metadata.go # constant/path.go # constant/rule.go # constant/rule_extra.go # dns/client.go # dns/filters.go # dns/resolver.go # go.mod # go.sum # hub/executor/executor.go # hub/route/configs.go # listener/listener.go # listener/tproxy/tproxy_linux_iptables.go # listener/tun/dev/dev.go # listener/tun/dev/dev_darwin.go # listener/tun/dev/dev_linux.go # listener/tun/dev/dev_windows.go # listener/tun/dev/wintun/config.go # listener/tun/dev/wintun/dll_windows.go # listener/tun/dev/wintun/session_windows.go # listener/tun/dev/wintun/wintun_windows.go # listener/tun/ipstack/commons/dns.go # listener/tun/ipstack/gvisor/tun.go # listener/tun/ipstack/gvisor/tundns.go # listener/tun/ipstack/gvisor/utils.go # listener/tun/ipstack/stack_adapter.go # listener/tun/ipstack/system/dns.go # listener/tun/ipstack/system/tcp.go # listener/tun/ipstack/system/tun.go # listener/tun/tun_adapter.go # main.go # rule/common/base.go # rule/common/domain.go # rule/common/domain_keyword.go # rule/common/domain_suffix.go # rule/common/final.go # rule/common/geoip.go # rule/common/geosite.go # rule/common/ipcidr.go # rule/common/port.go # rule/parser.go # rule/process.go # test/go.mod # test/go.sum # transport/vless/xtls.go # tunnel/tunnel.go
2022-03-17 17:41:02 +08:00
*Base
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 {
if g.isSourceIP {
return C.SrcGEOIP
}
2018-06-10 22:50:03 +08:00
return C.GEOIP
}
func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) {
ip := metadata.DstIP
if g.isSourceIP {
ip = metadata.SrcIP
}
2022-04-20 01:52:51 +08:00
if !ip.IsValid() {
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" {
Merge remote-tracking branch 'yaling888/with-tun' into Alpha # Conflicts: # .github/workflows/codeql-analysis.yml # .github/workflows/linter.yml # .github/workflows/release.yml # Makefile # README.md # adapter/outbound/vless.go # component/geodata/memconservative/cache.go # component/geodata/router/condition.go # component/geodata/router/condition_geoip.go # component/geodata/standard/standard.go # component/geodata/utils.go # config/config.go # config/initial.go # constant/metadata.go # constant/path.go # constant/rule.go # constant/rule_extra.go # dns/client.go # dns/filters.go # dns/resolver.go # go.mod # go.sum # hub/executor/executor.go # hub/route/configs.go # listener/listener.go # listener/tproxy/tproxy_linux_iptables.go # listener/tun/dev/dev.go # listener/tun/dev/dev_darwin.go # listener/tun/dev/dev_linux.go # listener/tun/dev/dev_windows.go # listener/tun/dev/wintun/config.go # listener/tun/dev/wintun/dll_windows.go # listener/tun/dev/wintun/session_windows.go # listener/tun/dev/wintun/wintun_windows.go # listener/tun/ipstack/commons/dns.go # listener/tun/ipstack/gvisor/tun.go # listener/tun/ipstack/gvisor/tundns.go # listener/tun/ipstack/gvisor/utils.go # listener/tun/ipstack/stack_adapter.go # listener/tun/ipstack/system/dns.go # listener/tun/ipstack/system/tcp.go # listener/tun/ipstack/system/tun.go # listener/tun/tun_adapter.go # main.go # rule/common/base.go # rule/common/domain.go # rule/common/domain_keyword.go # rule/common/domain_suffix.go # rule/common/final.go # rule/common/geoip.go # rule/common/geosite.go # rule/common/ipcidr.go # rule/common/port.go # rule/parser.go # rule/process.go # test/go.mod # test/go.sum # transport/vless/xtls.go # tunnel/tunnel.go
2022-03-17 17:41:02 +08:00
return ip.IsPrivate() ||
ip.IsUnspecified() ||
ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsLinkLocalUnicast() ||
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 {
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 {
return true, g.adapter
}
}
return false, g.adapter
2022-03-16 00:43:08 +08:00
}
2024-03-02 17:41:04 +08:00
matcher, err := g.GetIPMatcher()
if err != nil {
return false, ""
}
match := matcher.Match(ip)
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
}
func (g *GEOIP) ShouldResolveIP() bool {
return !g.noResolveIP
}
2021-11-17 16:03:47 +08:00
func (g *GEOIP) GetCountry() string {
return g.country
}
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-05-17 16:47:21 +08:00
func (g *GEOIP) GetRecodeSize() int {
if matcher, err := g.GetIPMatcher(); err == nil {
return matcher.Count()
}
return 0
2022-05-17 16:47:21 +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
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
}
geoip.geodata = true
geoIPMatcher, err := geoip.GetIPMatcher() // test load
if err != nil {
return nil, err
}
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
}