forked from PuerNya/sing-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_item_port.go
52 lines (45 loc) · 997 Bytes
/
rule_item_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
package route
import (
"strings"
"github.com/sagernet/sing-box/adapter"
F "github.com/sagernet/sing/common/format"
)
var _ RuleItem = (*PortItem)(nil)
type PortItem struct {
ports []uint16
portMap map[uint16]bool
isSource bool
}
func NewPortItem(isSource bool, ports []uint16) *PortItem {
portMap := make(map[uint16]bool)
for _, port := range ports {
portMap[port] = true
}
return &PortItem{
ports: ports,
portMap: portMap,
isSource: isSource,
}
}
func (r *PortItem) Match(metadata *adapter.InboundContext) bool {
if r.isSource {
return r.portMap[metadata.Source.Port]
} else {
return r.portMap[metadata.Destination.Port]
}
}
func (r *PortItem) String() string {
var description string
if r.isSource {
description = "source_port="
} else {
description = "port="
}
pLen := len(r.ports)
if pLen == 1 {
description += F.ToString(r.ports[0])
} else {
description += "[" + strings.Join(F.MapToString(r.ports), " ") + "]"
}
return description
}