Fix override logic

This commit is contained in:
problematicconsumer
2023-08-22 00:54:58 +03:30
parent 85efdbb5ff
commit 83d668866e
8 changed files with 351 additions and 198 deletions

View File

@@ -8,91 +8,95 @@ import (
)
type ConfigOverrides struct {
ExcludeTunInbound bool
IncludeMixedInbound bool
IncludeLogOutput bool
IncludeLogTimestamp bool
LogLevel string
ClashApiPort int
ClashApiPort *int `json:"clash-api-port"`
EnableTun *bool `json:"enable-tun"`
SetSystemProxy *bool `json:"set-system-proxy"`
LogLevel *string `json:"log-level"`
LogOutput *string `json:"log-output"`
DNSRemote *string `json:"dns-remote"`
MixedPort *int `json:"mixed-port"`
}
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"
func ApplyOverrides(base option.Options, options option.Options, overrides ConfigOverrides) option.Options {
clashApiPort := pointerOrDefaultInt(overrides.ClashApiPort, 9090)
base.Experimental = &option.ExperimentalOptions{
ClashAPI: &option.ClashAPIOptions{
ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", clashApiPort),
StoreSelected: true,
},
}
options.Experimental.ClashAPI = &option.ClashAPIOptions{
ExternalController: fmt.Sprintf("%s:%d", "127.0.0.1", overrides.ClashApiPort),
StoreSelected: true,
Secret: "",
base.Log = &option.LogOptions{
Level: pointerOrDefaultString(overrides.LogLevel, "info"),
Output: pointerOrDefaultString(overrides.LogOutput, ""),
Disabled: false,
Timestamp: false,
DisableColor: true,
}
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
for _, inb := range base.Inbounds {
switch inb.Type {
case C.TypeTun:
if pointerOrDefaultBool(overrides.EnableTun, true) {
inbounds = append(inbounds, inb)
}
default:
inbounds = append(inbounds, inb)
continue
}
inbounds = append(inbounds, inb)
}
options.Inbounds = inbounds
base.Inbounds = inbounds
hasSelector := false
hasUrlTest := false
var selectable []option.Outbound
var urlTests []option.Outbound
var outbounds []option.Outbound
var tags []string
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
case C.TypeSelector, C.TypeURLTest:
continue
default:
tags = append(tags, out.Tag)
}
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)
outbounds = append(outbounds, out)
}
return options
urlTest := option.Outbound{
Type: C.TypeURLTest,
Tag: "auto",
URLTestOptions: option.URLTestOutboundOptions{
Outbounds: tags,
},
}
selector := option.Outbound{
Type: C.TypeSelector,
Tag: "select",
SelectorOptions: option.SelectorOutboundOptions{
Outbounds: append([]string{urlTest.Tag}, tags...),
Default: urlTest.Tag,
},
}
outbounds = append([]option.Outbound{selector, urlTest}, outbounds...)
base.Outbounds = append(
outbounds,
[]option.Outbound{
{
Tag: "direct",
Type: C.TypeDirect,
},
{
Tag: "block",
Type: C.TypeBlock,
},
{
Tag: "dns-out",
Type: C.TypeDNS,
},
}...,
)
return base
}