rule_item_protocol.go 799 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package rule
  2. import (
  3. "strings"
  4. "github.com/sagernet/sing-box/adapter"
  5. F "github.com/sagernet/sing/common/format"
  6. )
  7. var _ RuleItem = (*ProtocolItem)(nil)
  8. type ProtocolItem struct {
  9. protocols []string
  10. protocolMap map[string]bool
  11. }
  12. func NewProtocolItem(protocols []string) *ProtocolItem {
  13. protocolMap := make(map[string]bool)
  14. for _, protocol := range protocols {
  15. protocolMap[protocol] = true
  16. }
  17. return &ProtocolItem{
  18. protocols: protocols,
  19. protocolMap: protocolMap,
  20. }
  21. }
  22. func (r *ProtocolItem) Match(metadata *adapter.InboundContext) bool {
  23. return r.protocolMap[metadata.Protocol]
  24. }
  25. func (r *ProtocolItem) String() string {
  26. if len(r.protocols) == 1 {
  27. return F.ToString("protocol=", r.protocols[0])
  28. }
  29. return F.ToString("protocol=[", strings.Join(r.protocols, " "), "]")
  30. }