Fix match clash mode

This commit is contained in:
世界 2024-11-13 17:06:34 +08:00
parent e18b527eaa
commit 2be7482e32
No known key found for this signature in database
GPG Key ID: CD109927C34A63C4
3 changed files with 13 additions and 7 deletions

View File

@ -219,8 +219,7 @@ func NewDefaultRule(ctx context.Context, logger log.ContextLogger, options optio
rule.allItems = append(rule.allItems, item)
}
if options.ClashMode != "" {
clashServer := service.FromContext[adapter.ClashServer](ctx)
item := NewClashModeItem(clashServer, options.ClashMode)
item := NewClashModeItem(ctx, options.ClashMode)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}

View File

@ -216,8 +216,7 @@ func NewDefaultDNSRule(ctx context.Context, logger log.ContextLogger, options op
rule.allItems = append(rule.allItems, item)
}
if options.ClashMode != "" {
clashServer := service.FromContext[adapter.ClashServer](ctx)
item := NewClashModeItem(clashServer, options.ClashMode)
item := NewClashModeItem(ctx, options.ClashMode)
rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item)
}

View File

@ -1,25 +1,33 @@
package rule
import (
"context"
"strings"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing/service"
)
var _ RuleItem = (*ClashModeItem)(nil)
type ClashModeItem struct {
ctx context.Context
clashServer adapter.ClashServer
mode string
}
func NewClashModeItem(clashServer adapter.ClashServer, mode string) *ClashModeItem {
func NewClashModeItem(ctx context.Context, mode string) *ClashModeItem {
return &ClashModeItem{
clashServer: clashServer,
mode: mode,
ctx: ctx,
mode: mode,
}
}
func (r *ClashModeItem) Start() error {
r.clashServer = service.FromContext[adapter.ClashServer](r.ctx)
return nil
}
func (r *ClashModeItem) Match(metadata *adapter.InboundContext) bool {
if r.clashServer == nil {
return false