github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/route/rule_item_port.go (about) 1 package route 2 3 import ( 4 "strings" 5 6 "github.com/inazumav/sing-box/adapter" 7 F "github.com/sagernet/sing/common/format" 8 ) 9 10 var _ RuleItem = (*PortItem)(nil) 11 12 type PortItem struct { 13 ports []uint16 14 portMap map[uint16]bool 15 isSource bool 16 } 17 18 func NewPortItem(isSource bool, ports []uint16) *PortItem { 19 portMap := make(map[uint16]bool) 20 for _, port := range ports { 21 portMap[port] = true 22 } 23 return &PortItem{ 24 ports: ports, 25 portMap: portMap, 26 isSource: isSource, 27 } 28 } 29 30 func (r *PortItem) Match(metadata *adapter.InboundContext) bool { 31 if r.isSource { 32 return r.portMap[metadata.Source.Port] 33 } else { 34 return r.portMap[metadata.Destination.Port] 35 } 36 } 37 38 func (r *PortItem) String() string { 39 var description string 40 if r.isSource { 41 description = "source_port=" 42 } else { 43 description = "port=" 44 } 45 pLen := len(r.ports) 46 if pLen == 1 { 47 description += F.ToString(r.ports[0]) 48 } else { 49 description += "[" + strings.Join(F.MapToString(r.ports), " ") + "]" 50 } 51 return description 52 }