github.com/yaling888/clash@v1.53.0/rule/ipset.go (about)

     1  package rules
     2  
     3  import (
     4  	"github.com/phuslu/log"
     5  
     6  	"github.com/yaling888/clash/component/ipset"
     7  	C "github.com/yaling888/clash/constant"
     8  )
     9  
    10  type IPSet struct {
    11  	*Base
    12  	name        string
    13  	adapter     string
    14  	noResolveIP bool
    15  }
    16  
    17  func (f *IPSet) RuleType() C.RuleType {
    18  	return C.IPSet
    19  }
    20  
    21  func (f *IPSet) Match(metadata *C.Metadata) bool {
    22  	if !metadata.DstIP.IsValid() {
    23  		return false
    24  	}
    25  	exist, err := ipset.Test(f.name, metadata.DstIP)
    26  	if err != nil {
    27  		log.Warn().Err(err).Str("name", f.name).Msg("[Matcher] check ipset failed")
    28  		return false
    29  	}
    30  	return exist
    31  }
    32  
    33  func (f *IPSet) Adapter() string {
    34  	return f.adapter
    35  }
    36  
    37  func (f *IPSet) Payload() string {
    38  	return f.name
    39  }
    40  
    41  func (f *IPSet) ShouldResolveIP() bool {
    42  	return !f.noResolveIP
    43  }
    44  
    45  func NewIPSet(name string, adapter string, noResolveIP bool) (*IPSet, error) {
    46  	if err := ipset.Verify(name); err != nil {
    47  		return nil, err
    48  	}
    49  
    50  	return &IPSet{
    51  		name:        name,
    52  		adapter:     adapter,
    53  		noResolveIP: noResolveIP,
    54  	}, nil
    55  }
    56  
    57  var _ C.Rule = (*IPSet)(nil)