github.com/eagleql/xray-core@v1.4.4/common/strmatcher/matchers.go (about)

     1  package strmatcher
     2  
     3  import (
     4  	"regexp"
     5  	"strings"
     6  )
     7  
     8  type fullMatcher string
     9  
    10  func (m fullMatcher) Match(s string) bool {
    11  	return string(m) == s
    12  }
    13  
    14  func (m fullMatcher) String() string {
    15  	return "full:" + string(m)
    16  }
    17  
    18  type substrMatcher string
    19  
    20  func (m substrMatcher) Match(s string) bool {
    21  	return strings.Contains(s, string(m))
    22  }
    23  
    24  func (m substrMatcher) String() string {
    25  	return "keyword:" + string(m)
    26  }
    27  
    28  type domainMatcher string
    29  
    30  func (m domainMatcher) Match(s string) bool {
    31  	pattern := string(m)
    32  	if !strings.HasSuffix(s, pattern) {
    33  		return false
    34  	}
    35  	return len(s) == len(pattern) || s[len(s)-len(pattern)-1] == '.'
    36  }
    37  
    38  func (m domainMatcher) String() string {
    39  	return "domain:" + string(m)
    40  }
    41  
    42  type regexMatcher struct {
    43  	pattern *regexp.Regexp
    44  }
    45  
    46  func (m *regexMatcher) Match(s string) bool {
    47  	return m.pattern.MatchString(s)
    48  }
    49  
    50  func (m *regexMatcher) String() string {
    51  	return "regexp:" + m.pattern.String()
    52  }