github.com/kelleygo/clashcore@v1.0.2/rules/common/in_name.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	C "github.com/kelleygo/clashcore/constant"
     6  	"strings"
     7  )
     8  
     9  type InName struct {
    10  	*Base
    11  	names   []string
    12  	adapter string
    13  	payload string
    14  }
    15  
    16  func (u *InName) Match(metadata *C.Metadata) (bool, string) {
    17  	for _, name := range u.names {
    18  		if metadata.InName == name {
    19  			return true, u.adapter
    20  		}
    21  	}
    22  	return false, ""
    23  }
    24  
    25  func (u *InName) RuleType() C.RuleType {
    26  	return C.InName
    27  }
    28  
    29  func (u *InName) Adapter() string {
    30  	return u.adapter
    31  }
    32  
    33  func (u *InName) Payload() string {
    34  	return u.payload
    35  }
    36  
    37  func NewInName(iNames, adapter string) (*InName, error) {
    38  	names := strings.Split(iNames, "/")
    39  	if len(names) == 0 {
    40  		return nil, fmt.Errorf("in name couldn't be empty")
    41  	}
    42  
    43  	return &InName{
    44  		Base:    &Base{},
    45  		names:   names,
    46  		adapter: adapter,
    47  		payload: iNames,
    48  	}, nil
    49  }