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

     1  package rules
     2  
     3  import (
     4  	"github.com/phuslu/log"
     5  
     6  	C "github.com/yaling888/clash/constant"
     7  )
     8  
     9  type Script struct {
    10  	*Base
    11  	matcher  C.Matcher
    12  	shortcut string
    13  	adapter  string
    14  }
    15  
    16  func (s *Script) RuleType() C.RuleType {
    17  	return C.Script
    18  }
    19  
    20  func (s *Script) Match(metadata *C.Metadata) bool {
    21  	rs, err := s.matcher.Match(metadata)
    22  	if err != nil {
    23  		log.Warn().Err(err).Str("name", s.matcher.Name()).Msg("[Matcher] match shortcut failed")
    24  		return false
    25  	}
    26  
    27  	return rs
    28  }
    29  
    30  func (s *Script) Adapter() string {
    31  	return s.adapter
    32  }
    33  
    34  func (s *Script) Payload() string {
    35  	return s.shortcut
    36  }
    37  
    38  func (s *Script) ShouldResolveIP() bool {
    39  	return false
    40  }
    41  
    42  func (s *Script) RuleExtra() *C.RuleExtra {
    43  	return nil
    44  }
    45  
    46  func (s *Script) SetMatcher(m C.Matcher) {
    47  	s.matcher = m
    48  }
    49  
    50  func NewScript(shortcut string, adapter string) (*Script, error) {
    51  	obj := &Script{
    52  		Base:     &Base{},
    53  		shortcut: shortcut,
    54  		adapter:  adapter,
    55  	}
    56  
    57  	return obj, nil
    58  }
    59  
    60  var _ C.Rule = (*Script)(nil)