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

     1  package rules
     2  
     3  import (
     4  	"strings"
     5  
     6  	C "github.com/yaling888/clash/constant"
     7  )
     8  
     9  type UserAgent struct {
    10  	*Base
    11  	ua      string
    12  	adapter string
    13  }
    14  
    15  func (d *UserAgent) RuleType() C.RuleType {
    16  	return C.UserAgent
    17  }
    18  
    19  func (d *UserAgent) Match(metadata *C.Metadata) bool {
    20  	if metadata.Type != C.MITM || metadata.UserAgent == "" {
    21  		return false
    22  	}
    23  
    24  	return strings.Contains(metadata.UserAgent, d.ua)
    25  }
    26  
    27  func (d *UserAgent) Adapter() string {
    28  	return d.adapter
    29  }
    30  
    31  func (d *UserAgent) Payload() string {
    32  	return d.ua
    33  }
    34  
    35  func (d *UserAgent) ShouldResolveIP() bool {
    36  	return false
    37  }
    38  
    39  func NewUserAgent(ua string, adapter string) (*UserAgent, error) {
    40  	ua = strings.Trim(ua, "*")
    41  	if ua == "" {
    42  		return nil, errPayload
    43  	}
    44  
    45  	return &UserAgent{
    46  		Base:    &Base{},
    47  		ua:      ua,
    48  		adapter: adapter,
    49  	}, nil
    50  }
    51  
    52  var _ C.Rule = (*UserAgent)(nil)