From e4d508b725f22325cf60dc8d5b1a30387a464507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sun, 12 May 2024 00:20:16 +0800 Subject: [PATCH] Remove null outbounds in group references --- .../filter/filter_null_group_reference.go | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 template/filter/filter_null_group_reference.go diff --git a/template/filter/filter_null_group_reference.go b/template/filter/filter_null_group_reference.go new file mode 100644 index 0000000..bcfc38c --- /dev/null +++ b/template/filter/filter_null_group_reference.go @@ -0,0 +1,33 @@ +package filter + +import ( + M "github.com/sagernet/serenity/common/metadata" + C "github.com/sagernet/sing-box/constant" + "github.com/sagernet/sing-box/option" + "github.com/sagernet/sing/common" +) + +func init() { + filters = append(filters, filterNullGroupReference) +} + +func filterNullGroupReference(metadata M.Metadata, options *option.Options) { + outboundTags := common.Map(options.Outbounds, func(it option.Outbound) string { + return it.Tag + }) + for i, outbound := range options.Outbounds { + switch outbound.Type { + case C.TypeSelector: + outbound.SelectorOptions.Outbounds = common.Filter(outbound.SelectorOptions.Outbounds, func(outbound string) bool { + return common.Contains(outboundTags, outbound) + }) + case C.TypeURLTest: + outbound.URLTestOptions.Outbounds = common.Filter(outbound.URLTestOptions.Outbounds, func(outbound string) bool { + return common.Contains(outboundTags, outbound) + }) + default: + continue + } + options.Outbounds[i] = outbound + } +}