rule_item_outbound.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package rule
  2. import (
  3. "context"
  4. "strings"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing-box/experimental/deprecated"
  7. F "github.com/sagernet/sing/common/format"
  8. )
  9. var _ RuleItem = (*OutboundItem)(nil)
  10. type OutboundItem struct {
  11. outbounds []string
  12. outboundMap map[string]bool
  13. matchAny bool
  14. }
  15. func NewOutboundRule(ctx context.Context, outbounds []string) *OutboundItem {
  16. deprecated.Report(ctx, deprecated.OptionOutboundDNSRuleItem)
  17. rule := &OutboundItem{outbounds: outbounds, outboundMap: make(map[string]bool)}
  18. for _, outbound := range outbounds {
  19. if outbound == "any" {
  20. rule.matchAny = true
  21. } else {
  22. rule.outboundMap[outbound] = true
  23. }
  24. }
  25. return rule
  26. }
  27. func (r *OutboundItem) Match(metadata *adapter.InboundContext) bool {
  28. if r.matchAny {
  29. return metadata.Outbound != ""
  30. }
  31. return r.outboundMap[metadata.Outbound]
  32. }
  33. func (r *OutboundItem) String() string {
  34. if len(r.outbounds) == 1 {
  35. return F.ToString("outbound=", r.outbounds[0])
  36. } else {
  37. return F.ToString("outbound=[", strings.Join(r.outbounds, " "), "]")
  38. }
  39. }