mihomo/rules/common/geoip.go

225 lines
4.4 KiB
Go
Raw Normal View History

package common
2018-06-10 22:50:03 +08:00
import (
"errors"
"fmt"
"net/netip"
"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"
"golang.org/x/exp/slices"
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
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" {
return g.isLan(ip), g.adapter
2024-03-02 17:41:04 +08:00
}
2024-09-09 16:08:48 +08:00
if geodata.GeodataMode() {
if g.isSourceIP {
if slices.Contains(metadata.SrcGeoIP, g.country) {
return true, g.adapter
}
} else {
if slices.Contains(metadata.DstGeoIP, g.country) {
return true, g.adapter
}
}
matcher, err := g.getIPMatcher()
if err != nil {
return false, ""
2024-03-02 17:41:04 +08:00
}
match := matcher.Match(ip)
if match {
if g.isSourceIP {
metadata.SrcGeoIP = append(metadata.SrcGeoIP, g.country)
} else {
metadata.DstGeoIP = append(metadata.DstGeoIP, g.country)
}
}
return match, g.adapter
2022-03-16 00:43:08 +08:00
}
2024-03-02 17:41:04 +08:00
if g.isSourceIP {
if metadata.SrcGeoIP != nil {
return slices.Contains(metadata.SrcGeoIP, g.country), g.adapter
}
} else {
if metadata.DstGeoIP != nil {
return slices.Contains(metadata.DstGeoIP, g.country), g.adapter
}
}
codes := mmdb.IPInstance().LookupCode(ip.AsSlice())
if g.isSourceIP {
metadata.SrcGeoIP = codes
} else {
metadata.DstGeoIP = codes
}
if slices.Contains(codes, g.country) {
return true, g.adapter
}
return false, ""
}
// MatchIp implements C.IpMatcher
func (g *GEOIP) MatchIp(ip netip.Addr) bool {
if !ip.IsValid() {
return false
}
if g.country == "lan" {
return g.isLan(ip)
2024-03-02 17:41:04 +08:00
}
2024-09-09 16:08:48 +08:00
if geodata.GeodataMode() {
matcher, err := g.getIPMatcher()
if err != nil {
return false
}
return matcher.Match(ip)
}
codes := mmdb.IPInstance().LookupCode(ip.AsSlice())
return slices.Contains(codes, g.country)
}
// MatchIp implements C.IpMatcher
func (g dnsFallbackFilter) MatchIp(ip netip.Addr) bool {
if !ip.IsValid() {
return false
}
if g.isLan(ip) { // compatible with original behavior
return false
}
2024-09-09 16:08:48 +08:00
if geodata.GeodataMode() {
matcher, err := g.getIPMatcher()
if err != nil {
return false
}
return !matcher.Match(ip)
}
codes := mmdb.IPInstance().LookupCode(ip.AsSlice())
return !slices.Contains(codes, g.country)
}
type dnsFallbackFilter struct {
*GEOIP
}
func (g *GEOIP) DnsFallbackFilter() C.IpMatcher { // for dns.fallback-filter.geoip
return dnsFallbackFilter{GEOIP: g}
}
func (g *GEOIP) isLan(ip netip.Addr) bool {
return ip.IsPrivate() ||
ip.IsUnspecified() ||
ip.IsLoopback() ||
ip.IsMulticast() ||
ip.IsLinkLocalUnicast() ||
resolver.IsFakeBroadcastIP(ip)
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) {
2024-09-09 16:08:48 +08:00
if geodata.GeodataMode() {
return g.getIPMatcher()
}
return nil, errors.New("not geodata mode")
}
func (g *GEOIP) getIPMatcher() (router.IPMatcher, error) {
geoIPMatcher, err := geodata.LoadGeoIPMatcher(g.country)
if err != nil {
return nil, fmt.Errorf("[GeoIP] %w", err)
}
return geoIPMatcher, nil
}
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) {
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-09-09 16:08:48 +08:00
if country == "lan" {
2022-03-16 17:29:09 +08:00
return geoip, nil
}
2024-09-09 16:08:48 +08:00
if err := geodata.InitGeoIP(); err != nil {
log.Errorln("can't initial GeoIP: %s", err)
return nil, err
}
if geodata.GeodataMode() {
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
}