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