2024-10-21 15:38:34 +00:00
|
|
|
package rule
|
2022-07-02 06:07:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-07-06 07:01:09 +00:00
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2022-07-08 15:03:57 +00:00
|
|
|
F "github.com/sagernet/sing/common/format"
|
2022-07-02 06:07:50 +00:00
|
|
|
)
|
|
|
|
|
2022-07-02 14:55:10 +00:00
|
|
|
var _ RuleItem = (*InboundItem)(nil)
|
2022-07-02 06:07:50 +00:00
|
|
|
|
2022-07-02 14:55:10 +00:00
|
|
|
type InboundItem struct {
|
2022-07-02 06:07:50 +00:00
|
|
|
inbounds []string
|
|
|
|
inboundMap map[string]bool
|
|
|
|
}
|
|
|
|
|
2022-07-02 14:55:10 +00:00
|
|
|
func NewInboundRule(inbounds []string) *InboundItem {
|
|
|
|
rule := &InboundItem{inbounds, make(map[string]bool)}
|
2022-07-02 06:07:50 +00:00
|
|
|
for _, inbound := range inbounds {
|
|
|
|
rule.inboundMap[inbound] = true
|
|
|
|
}
|
|
|
|
return rule
|
|
|
|
}
|
|
|
|
|
2022-07-02 14:55:10 +00:00
|
|
|
func (r *InboundItem) Match(metadata *adapter.InboundContext) bool {
|
2022-07-02 06:07:50 +00:00
|
|
|
return r.inboundMap[metadata.Inbound]
|
|
|
|
}
|
|
|
|
|
2022-07-02 14:55:10 +00:00
|
|
|
func (r *InboundItem) String() string {
|
2022-07-02 06:07:50 +00:00
|
|
|
if len(r.inbounds) == 1 {
|
|
|
|
return F.ToString("inbound=", r.inbounds[0])
|
|
|
|
} else {
|
|
|
|
return F.ToString("inbound=[", strings.Join(r.inbounds, " "), "]")
|
|
|
|
}
|
|
|
|
}
|