2024-10-21 15:38:34 +00:00
|
|
|
package rule
|
2022-07-07 13:47:21 +00:00
|
|
|
|
|
|
|
import (
|
2025-01-12 04:45:27 +00:00
|
|
|
"context"
|
2022-07-07 13:47:21 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2025-01-12 04:45:27 +00:00
|
|
|
"github.com/sagernet/sing-box/experimental/deprecated"
|
2022-07-08 15:03:57 +00:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
2022-07-07 13:47:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ RuleItem = (*OutboundItem)(nil)
|
|
|
|
|
|
|
|
type OutboundItem struct {
|
|
|
|
outbounds []string
|
|
|
|
outboundMap map[string]bool
|
2023-03-29 02:30:31 +00:00
|
|
|
matchAny bool
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
|
2025-01-12 04:45:27 +00:00
|
|
|
func NewOutboundRule(ctx context.Context, outbounds []string) *OutboundItem {
|
|
|
|
deprecated.Report(ctx, deprecated.OptionOutboundDNSRuleItem)
|
2023-03-29 02:30:31 +00:00
|
|
|
rule := &OutboundItem{outbounds: outbounds, outboundMap: make(map[string]bool)}
|
2022-07-07 13:47:21 +00:00
|
|
|
for _, outbound := range outbounds {
|
2023-03-29 02:30:31 +00:00
|
|
|
if outbound == "any" {
|
|
|
|
rule.matchAny = true
|
|
|
|
} else {
|
|
|
|
rule.outboundMap[outbound] = true
|
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
return rule
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OutboundItem) Match(metadata *adapter.InboundContext) bool {
|
2025-01-12 04:45:27 +00:00
|
|
|
if r.matchAny {
|
|
|
|
return metadata.Outbound != ""
|
2023-03-29 02:30:31 +00:00
|
|
|
}
|
2022-07-07 13:47:21 +00:00
|
|
|
return r.outboundMap[metadata.Outbound]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *OutboundItem) String() string {
|
|
|
|
if len(r.outbounds) == 1 {
|
|
|
|
return F.ToString("outbound=", r.outbounds[0])
|
|
|
|
} else {
|
|
|
|
return F.ToString("outbound=[", strings.Join(r.outbounds, " "), "]")
|
|
|
|
}
|
|
|
|
}
|