github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/agents/agent.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package agents
     4  
     5  import (
     6  	"regexp"
     7  	"strings"
     8  )
     9  
    10  type Agent struct {
    11  	Code     string
    12  	Keywords []string // user agent keywords
    13  
    14  	suffixes []string // PTR suffixes
    15  	reg      *regexp.Regexp
    16  }
    17  
    18  func NewAgent(code string, suffixes []string, reg *regexp.Regexp, keywords []string) *Agent {
    19  	return &Agent{
    20  		Code:     code,
    21  		suffixes: suffixes,
    22  		reg:      reg,
    23  		Keywords: keywords,
    24  	}
    25  }
    26  
    27  func (this *Agent) Match(ptr string) bool {
    28  	if len(this.suffixes) > 0 {
    29  		for _, suffix := range this.suffixes {
    30  			if strings.HasSuffix(ptr, suffix) {
    31  				return true
    32  			}
    33  		}
    34  	}
    35  	if this.reg != nil {
    36  		return this.reg.MatchString(ptr)
    37  	}
    38  	return false
    39  }