github.com/kelleygo/clashcore@v1.0.2/rules/common/domain_regex.go (about)

     1  package common
     2  
     3  import (
     4  	"regexp"
     5  
     6  	C "github.com/kelleygo/clashcore/constant"
     7  )
     8  
     9  type DomainRegex struct {
    10  	*Base
    11  	regex   *regexp.Regexp
    12  	adapter string
    13  }
    14  
    15  func (dr *DomainRegex) RuleType() C.RuleType {
    16  	return C.DomainRegex
    17  }
    18  
    19  func (dr *DomainRegex) Match(metadata *C.Metadata) (bool, string) {
    20  	domain := metadata.RuleHost()
    21  	return dr.regex.MatchString(domain), dr.adapter
    22  }
    23  
    24  func (dr *DomainRegex) Adapter() string {
    25  	return dr.adapter
    26  }
    27  
    28  func (dr *DomainRegex) Payload() string {
    29  	return dr.regex.String()
    30  }
    31  
    32  func NewDomainRegex(regex string, adapter string) (*DomainRegex, error) {
    33  	r, err := regexp.Compile(regex)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	return &DomainRegex{
    38  		Base:    &Base{},
    39  		regex:   r,
    40  		adapter: adapter,
    41  	}, nil
    42  }
    43  
    44  //var _ C.Rule = (*DomainRegex)(nil)