2024-10-21 15:38:34 +00:00
|
|
|
package rule
|
2022-09-10 06:09:47 +00:00
|
|
|
|
|
|
|
import (
|
2024-11-13 09:06:34 +00:00
|
|
|
"context"
|
2022-09-10 06:09:47 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2024-11-13 09:06:34 +00:00
|
|
|
"github.com/sagernet/sing/service"
|
2022-09-10 06:09:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ RuleItem = (*ClashModeItem)(nil)
|
|
|
|
|
|
|
|
type ClashModeItem struct {
|
2024-11-13 09:06:34 +00:00
|
|
|
ctx context.Context
|
2024-11-10 08:46:59 +00:00
|
|
|
clashServer adapter.ClashServer
|
|
|
|
mode string
|
2022-09-10 06:09:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 09:06:34 +00:00
|
|
|
func NewClashModeItem(ctx context.Context, mode string) *ClashModeItem {
|
2022-09-10 06:09:47 +00:00
|
|
|
return &ClashModeItem{
|
2024-11-13 09:06:34 +00:00
|
|
|
ctx: ctx,
|
|
|
|
mode: mode,
|
2022-09-10 06:09:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 09:06:34 +00:00
|
|
|
func (r *ClashModeItem) Start() error {
|
|
|
|
r.clashServer = service.FromContext[adapter.ClashServer](r.ctx)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-10 06:09:47 +00:00
|
|
|
func (r *ClashModeItem) Match(metadata *adapter.InboundContext) bool {
|
2024-11-10 08:46:59 +00:00
|
|
|
if r.clashServer == nil {
|
2022-09-10 06:09:47 +00:00
|
|
|
return false
|
|
|
|
}
|
2024-11-10 08:46:59 +00:00
|
|
|
return strings.EqualFold(r.clashServer.Mode(), r.mode)
|
2022-09-10 06:09:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ClashModeItem) String() string {
|
|
|
|
return "clash_mode=" + r.mode
|
|
|
|
}
|