github.com/metacubex/mihomo@v1.18.5/rules/common/domain_regex.go (about)

     1  package common
     2  
     3  import (
     4  	C "github.com/metacubex/mihomo/constant"
     5  
     6  	"github.com/dlclark/regexp2"
     7  )
     8  
     9  type DomainRegex struct {
    10  	*Base
    11  	regex   *regexp2.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  	match, _ := dr.regex.MatchString(domain)
    22  	return match, dr.adapter
    23  }
    24  
    25  func (dr *DomainRegex) Adapter() string {
    26  	return dr.adapter
    27  }
    28  
    29  func (dr *DomainRegex) Payload() string {
    30  	return dr.regex.String()
    31  }
    32  
    33  func NewDomainRegex(regex string, adapter string) (*DomainRegex, error) {
    34  	r, err := regexp2.Compile(regex, regexp2.IgnoreCase)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return &DomainRegex{
    39  		Base:    &Base{},
    40  		regex:   r,
    41  		adapter: adapter,
    42  	}, nil
    43  }
    44  
    45  //var _ C.Rule = (*DomainRegex)(nil)