github.com/yaling888/clash@v1.53.0/rule/geoip.go (about) 1 package rules 2 3 import ( 4 "strings" 5 6 "github.com/yaling888/clash/component/mmdb" 7 "github.com/yaling888/clash/component/resolver" 8 C "github.com/yaling888/clash/constant" 9 ) 10 11 type GEOIP struct { 12 *Base 13 country string 14 adapter string 15 noResolveIP bool 16 } 17 18 func (g *GEOIP) RuleType() C.RuleType { 19 return C.GEOIP 20 } 21 22 func (g *GEOIP) Match(metadata *C.Metadata) bool { 23 ip := metadata.DstIP 24 if !ip.IsValid() { 25 return false 26 } 27 28 if strings.EqualFold(g.country, "LAN") { 29 return ip.IsPrivate() || 30 ip.IsUnspecified() || 31 ip.IsLoopback() || 32 ip.IsMulticast() || 33 ip.IsLinkLocalUnicast() || 34 resolver.IsFakeBroadcastIP(ip) 35 } 36 37 record, _ := mmdb.Instance().Country(ip.AsSlice()) 38 return strings.EqualFold(record.Country.IsoCode, g.country) 39 } 40 41 func (g *GEOIP) Adapter() string { 42 return g.adapter 43 } 44 45 func (g *GEOIP) Payload() string { 46 return g.country 47 } 48 49 func (g *GEOIP) ShouldResolveIP() bool { 50 return !g.noResolveIP 51 } 52 53 func (g *GEOIP) GetCountry() string { 54 return g.country 55 } 56 57 func NewGEOIP(country string, adapter string, noResolveIP bool) *GEOIP { 58 geoip := &GEOIP{ 59 Base: &Base{}, 60 country: country, 61 adapter: adapter, 62 noResolveIP: noResolveIP, 63 } 64 65 return geoip 66 } 67 68 var _ C.Rule = (*GEOIP)(nil)