forked from fossabot/clash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
port.go
79 lines (66 loc) · 1.35 KB
/
port.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package rules
import (
"fmt"
"strconv"
C "github.com/Dreamacro/clash/constant"
)
type PortType int
const (
PortTypeSrc PortType = iota
PortTypeDest
PortTypeInbound
)
// Implements C.Rule
var _ C.Rule = (*Port)(nil)
type Port struct {
adapter string
port C.Port
portType PortType
}
func (p *Port) RuleType() C.RuleType {
switch p.portType {
case PortTypeSrc:
return C.SrcPort
case PortTypeDest:
return C.DstPort
case PortTypeInbound:
return C.InboundPort
default:
panic(fmt.Errorf("unknown port type: %v", p.portType))
}
}
func (p *Port) Match(metadata *C.Metadata) bool {
switch p.portType {
case PortTypeSrc:
return metadata.SrcPort == p.port
case PortTypeDest:
return metadata.DstPort == p.port
case PortTypeInbound:
return metadata.OriginDst.Port() == uint16(p.port)
default:
panic(fmt.Errorf("unknown port type: %v", p.portType))
}
}
func (p *Port) Adapter() string {
return p.adapter
}
func (p *Port) Payload() string {
return p.port.String()
}
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)
if err != nil {
return nil, errPayload
}
return &Port{
adapter: adapter,
port: C.Port(p),
portType: portType,
}, nil
}