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

     1  package common
     2  
     3  import (
     4  	"strings"
     5  
     6  	C "github.com/metacubex/mihomo/constant"
     7  	"golang.org/x/net/idna"
     8  )
     9  
    10  type DomainSuffix struct {
    11  	*Base
    12  	suffix  string
    13  	adapter string
    14  }
    15  
    16  func (ds *DomainSuffix) RuleType() C.RuleType {
    17  	return C.DomainSuffix
    18  }
    19  
    20  func (ds *DomainSuffix) Match(metadata *C.Metadata) (bool, string) {
    21  	domain := metadata.RuleHost()
    22  	return strings.HasSuffix(domain, "."+ds.suffix) || domain == ds.suffix, ds.adapter
    23  }
    24  
    25  func (ds *DomainSuffix) Adapter() string {
    26  	return ds.adapter
    27  }
    28  
    29  func (ds *DomainSuffix) Payload() string {
    30  	return ds.suffix
    31  }
    32  
    33  func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
    34  	punycode, _ := idna.ToASCII(strings.ToLower(suffix))
    35  	return &DomainSuffix{
    36  		Base:    &Base{},
    37  		suffix:  punycode,
    38  		adapter: adapter,
    39  	}
    40  }
    41  
    42  //var _ C.Rule = (*DomainSuffix)(nil)