From 076a25ea421770128d651cbbacc3c2bfe53c69a9 Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Sat, 19 Aug 2023 14:09:07 +0330 Subject: [PATCH] Add config override --- shared/override.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 shared/override.go diff --git a/shared/override.go b/shared/override.go new file mode 100644 index 0000000..ea2cfb1 --- /dev/null +++ b/shared/override.go @@ -0,0 +1,98 @@ +package shared + +import ( + "fmt" + + C "github.com/sagernet/sing-box/constant" + "github.com/sagernet/sing-box/option" +) + +type ConfigOverrides struct { + ExcludeTunInbound bool + IncludeMixedInbound bool + IncludeLogOutput bool + IncludeLogTimestamp bool + LogLevel string + ClashApiPort int +} + +func ApplyOverrides(options option.Options, overrides ConfigOverrides) option.Options { + options.Log = &option.LogOptions{ + Disabled: false, + Timestamp: overrides.IncludeLogTimestamp, + DisableColor: true, + } + if overrides.LogLevel != "" { + options.Log.Level = overrides.LogLevel + } + if overrides.IncludeLogOutput { + options.Log.Output = "box.log" + } + + options.Experimental.ClashAPI = &option.ClashAPIOptions{ + ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", overrides.ClashApiPort), + StoreSelected: true, + Secret: "", + } + + var inbounds []option.Inbound + for _, inb := range options.Inbounds { + if overrides.ExcludeTunInbound && inb.Type == C.TypeTun { + continue + } + if overrides.IncludeMixedInbound && inb.Type == C.TypeMixed { + inb.MixedOptions.SetSystemProxy = true + inbounds = append(inbounds, inb) + continue + } + inbounds = append(inbounds, inb) + } + options.Inbounds = inbounds + + hasSelector := false + hasUrlTest := false + var selectable []option.Outbound + var urlTests []option.Outbound + for _, out := range options.Outbounds { + if out.Type == C.TypeSelector { + hasSelector = true + } else if out.Type == C.TypeURLTest { + hasUrlTest = true + urlTests = append(urlTests, out) + } + switch out.Type { + case C.TypeDirect, C.TypeBlock, C.TypeDNS: + continue + } + selectable = append(selectable, out) + } + var generatedUrlTest *option.Outbound + if !hasUrlTest { + var urlSelectOuts []string + for _, out := range selectable { + urlSelectOuts = append(urlSelectOuts, out.Tag) + } + generatedUrlTest = &option.Outbound{Type: C.TypeURLTest, Tag: "urltest", URLTestOptions: option.URLTestOutboundOptions{Outbounds: urlSelectOuts}} + urlTests = append(urlTests, *generatedUrlTest) + } + if !hasSelector { + var selectorOuts []string + for _, out := range selectable { + selectorOuts = append(selectorOuts, out.Tag) + } + for _, out := range urlTests { + selectorOuts = append(selectorOuts, out.Tag) + } + + defaultSelector := option.Outbound{Type: C.TypeSelector, Tag: "select", SelectorOptions: option.SelectorOutboundOptions{Outbounds: selectorOuts}} + if generatedUrlTest != nil { + defaultSelector.SelectorOptions.Default = generatedUrlTest.Tag + } + options.Outbounds = append([]option.Outbound{defaultSelector}, options.Outbounds...) + } + if generatedUrlTest != nil { + options.Outbounds = append(options.Outbounds, *generatedUrlTest) + } + + return options +}