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) rule.allItems = append(rule.allItems, item)
} }
if options.ClashMode != "" { if options.ClashMode != "" {
clashServer := service.FromContext[adapter.ClashServer](ctx) item := NewClashModeItem(ctx, options.ClashMode)
item := NewClashModeItem(clashServer, options.ClashMode)
rule.items = append(rule.items, item) rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, 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) rule.allItems = append(rule.allItems, item)
} }
if options.ClashMode != "" { if options.ClashMode != "" {
clashServer := service.FromContext[adapter.ClashServer](ctx) item := NewClashModeItem(ctx, options.ClashMode)
item := NewClashModeItem(clashServer, options.ClashMode)
rule.items = append(rule.items, item) rule.items = append(rule.items, item)
rule.allItems = append(rule.allItems, item) rule.allItems = append(rule.allItems, item)
} }

View File

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