sing-box/route/rule/rule_item_protocol.go

38 lines
799 B
Go
Raw Normal View History

2024-10-21 15:38:34 +00:00
package rule
2022-07-02 14:55:10 +00:00
import (
"strings"
2022-07-06 07:01:09 +00:00
"github.com/sagernet/sing-box/adapter"
2022-07-08 15:03:57 +00:00
F "github.com/sagernet/sing/common/format"
2022-07-02 14:55:10 +00:00
)
var _ RuleItem = (*ProtocolItem)(nil)
type ProtocolItem struct {
protocols []string
protocolMap map[string]bool
}
func NewProtocolItem(protocols []string) *ProtocolItem {
protocolMap := make(map[string]bool)
for _, protocol := range protocols {
protocolMap[protocol] = true
}
return &ProtocolItem{
protocols: protocols,
protocolMap: protocolMap,
}
}
func (r *ProtocolItem) Match(metadata *adapter.InboundContext) bool {
return r.protocolMap[metadata.Protocol]
}
func (r *ProtocolItem) String() string {
if len(r.protocols) == 1 {
return F.ToString("protocol=", r.protocols[0])
}
return F.ToString("protocol=[", strings.Join(r.protocols, " "), "]")
}