github.com/chwjbn/xclash@v0.2.0/rule/geoip.go (about)

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