rule_item_inbound.go 746 B

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