github.com/igoogolx/clash@v1.19.8/rule/geoip.go (about)

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