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

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