clash/rule/port.go

77 lines
1.3 KiB
Go
Raw Normal View History

2019-05-09 21:00:29 +08:00
package rules
import (
"fmt"
2019-05-09 21:00:29 +08:00
"strconv"
C "github.com/Dreamacro/clash/constant"
)
type PortType int
const (
PortTypeSrc PortType = iota
PortTypeDest
PortTypeInbound
)
2019-05-09 21:00:29 +08:00
type Port struct {
adapter string
port C.Port
portType PortType
2019-05-09 21:00:29 +08:00
}
func (p *Port) RuleType() C.RuleType {
switch p.portType {
case PortTypeSrc:
2019-05-09 21:00:29 +08:00
return C.SrcPort
case PortTypeDest:
return C.DstPort
case PortTypeInbound:
return C.InboundPort
default:
panic(fmt.Errorf("unknown port type: %v", p.portType))
2019-05-09 21:00:29 +08:00
}
}
func (p *Port) Match(metadata *C.Metadata) bool {
switch p.portType {
case PortTypeSrc:
2019-05-09 21:00:29 +08:00
return metadata.SrcPort == p.port
case PortTypeDest:
return metadata.DstPort == p.port
case PortTypeInbound:
return metadata.InboundPort == uint16(p.port)
default:
panic(fmt.Errorf("unknown port type: %v", p.portType))
2019-05-09 21:00:29 +08:00
}
}
func (p *Port) Adapter() string {
return p.adapter
}
func (p *Port) Payload() string {
return p.port.String()
2019-05-09 21:00:29 +08:00
}
func (p *Port) ShouldResolveIP() bool {
return false
}
func (p *Port) ShouldFindProcess() bool {
return false
}
func NewPort(port string, adapter string, portType PortType) (*Port, error) {
p, err := strconv.ParseUint(port, 10, 16)
2019-05-09 21:00:29 +08:00
if err != nil {
return nil, errPayload
2019-05-09 21:00:29 +08:00
}
return &Port{
adapter: adapter,
port: C.Port(p),
portType: portType,
}, nil
2019-05-09 21:00:29 +08:00
}