From 2be7482e32f232f0c3c3d3f223366fc296d20b97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Wed, 13 Nov 2024 17:06:34 +0800 Subject: [PATCH] Fix match clash mode --- route/rule/rule_default.go | 3 +-- route/rule/rule_dns.go | 3 +-- route/rule/rule_item_clash_mode.go | 14 +++++++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/route/rule/rule_default.go b/route/rule/rule_default.go index 5c37b415..a5d47b44 100644 --- a/route/rule/rule_default.go +++ b/route/rule/rule_default.go @@ -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) } diff --git a/route/rule/rule_dns.go b/route/rule/rule_dns.go index ecb68ece..4ac08a5a 100644 --- a/route/rule/rule_dns.go +++ b/route/rule/rule_dns.go @@ -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) } diff --git a/route/rule/rule_item_clash_mode.go b/route/rule/rule_item_clash_mode.go index 4f9bc7ea..fe2347a0 100644 --- a/route/rule/rule_item_clash_mode.go +++ b/route/rule/rule_item_clash_mode.go @@ -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