github.com/igoogolx/clash@v1.19.8/rule/port.go (about) 1 package rules 2 3 import ( 4 "fmt" 5 "strconv" 6 7 C "github.com/igoogolx/clash/constant" 8 ) 9 10 type PortType int 11 12 const ( 13 PortTypeSrc PortType = iota 14 PortTypeDest 15 PortTypeInbound 16 ) 17 18 // Implements C.Rule 19 var _ C.Rule = (*Port)(nil) 20 21 type Port struct { 22 adapter string 23 port C.Port 24 portType PortType 25 } 26 27 func (p *Port) RuleType() C.RuleType { 28 switch p.portType { 29 case PortTypeSrc: 30 return C.SrcPort 31 case PortTypeDest: 32 return C.DstPort 33 case PortTypeInbound: 34 return C.InboundPort 35 default: 36 panic(fmt.Errorf("unknown port type: %v", p.portType)) 37 } 38 } 39 40 func (p *Port) Match(metadata *C.Metadata) bool { 41 switch p.portType { 42 case PortTypeSrc: 43 return metadata.SrcPort == p.port 44 case PortTypeDest: 45 return metadata.DstPort == p.port 46 case PortTypeInbound: 47 return metadata.OriginDst.Port() == uint16(p.port) 48 default: 49 panic(fmt.Errorf("unknown port type: %v", p.portType)) 50 } 51 } 52 53 func (p *Port) Adapter() string { 54 return p.adapter 55 } 56 57 func (p *Port) Payload() string { 58 return p.port.String() 59 } 60 61 func (p *Port) ShouldResolveIP() bool { 62 return false 63 } 64 65 func (p *Port) ShouldFindProcess() bool { 66 return false 67 } 68 69 func NewPort(port string, adapter string, portType PortType) (*Port, error) { 70 p, err := strconv.ParseUint(port, 10, 16) 71 if err != nil { 72 return nil, errPayload 73 } 74 return &Port{ 75 adapter: adapter, 76 port: C.Port(p), 77 portType: portType, 78 }, nil 79 }