rule_item_outbound.go 936 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 = (*OutboundItem)(nil)
  8. type OutboundItem struct {
  9. outbounds []string
  10. outboundMap map[string]bool
  11. matchAny bool
  12. }
  13. func NewOutboundRule(outbounds []string) *OutboundItem {
  14. rule := &OutboundItem{outbounds: outbounds, outboundMap: make(map[string]bool)}
  15. for _, outbound := range outbounds {
  16. if outbound == "any" {
  17. rule.matchAny = true
  18. } else {
  19. rule.outboundMap[outbound] = true
  20. }
  21. }
  22. return rule
  23. }
  24. func (r *OutboundItem) Match(metadata *adapter.InboundContext) bool {
  25. if r.matchAny && metadata.Outbound != "" {
  26. return true
  27. }
  28. return r.outboundMap[metadata.Outbound]
  29. }
  30. func (r *OutboundItem) String() string {
  31. if len(r.outbounds) == 1 {
  32. return F.ToString("outbound=", r.outbounds[0])
  33. } else {
  34. return F.ToString("outbound=[", strings.Join(r.outbounds, " "), "]")
  35. }
  36. }