github.com/chwjbn/xclash@v0.2.0/rule/port.go (about)

     1  package rules
     2  
     3  import (
     4  	"strconv"
     5  
     6  	C "github.com/chwjbn/xclash/constant"
     7  )
     8  
     9  type Port struct {
    10  	adapter  string
    11  	port     string
    12  	isSource bool
    13  }
    14  
    15  func (p *Port) RuleType() C.RuleType {
    16  	if p.isSource {
    17  		return C.SrcPort
    18  	}
    19  	return C.DstPort
    20  }
    21  
    22  func (p *Port) Match(metadata *C.Metadata) bool {
    23  	if p.isSource {
    24  		return metadata.SrcPort == p.port
    25  	}
    26  	return metadata.DstPort == p.port
    27  }
    28  
    29  func (p *Port) Adapter() string {
    30  	return p.adapter
    31  }
    32  
    33  func (p *Port) Payload() string {
    34  	return p.port
    35  }
    36  
    37  func (p *Port) ShouldResolveIP() bool {
    38  	return false
    39  }
    40  
    41  func NewPort(port string, adapter string, isSource bool) (*Port, error) {
    42  	_, err := strconv.ParseUint(port, 10, 16)
    43  	if err != nil {
    44  		return nil, errPayload
    45  	}
    46  	return &Port{
    47  		adapter:  adapter,
    48  		port:     port,
    49  		isSource: isSource,
    50  	}, nil
    51  }