From 67c66faaed7203fc0020183f8ae024b55da35f60 Mon Sep 17 00:00:00 2001 From: Hossin Asaadi Date: Tue, 24 Oct 2023 05:04:58 +0330 Subject: [PATCH] Override outbounds in multi config files (#2659) * fix override outbounds in multi config * log outbounds override status * fix prepend bug * improve outbounds-prepend test * fix prepend outbound * fix outbounds-prepend test --- infra/conf/xray.go | 22 ++++++++++++---------- infra/conf/xray_test.go | 6 +++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/infra/conf/xray.go b/infra/conf/xray.go index e1f1f48c..558e2baa 100644 --- a/infra/conf/xray.go +++ b/infra/conf/xray.go @@ -504,21 +504,23 @@ func (c *Config) Override(o *Config, fn string) { // update the Outbound in slice if the only one in overide config has same tag if len(o.OutboundConfigs) > 0 { - if len(c.OutboundConfigs) > 0 && len(o.OutboundConfigs) == 1 { - if idx := c.findOutboundTag(o.OutboundConfigs[0].Tag); idx > -1 { - c.OutboundConfigs[idx] = o.OutboundConfigs[0] - ctllog.Println("[", fn, "] updated outbound with tag: ", o.OutboundConfigs[0].Tag) + outboundPrepends := []OutboundDetourConfig{} + for i := range o.OutboundConfigs { + if idx := c.findOutboundTag(o.OutboundConfigs[i].Tag); idx > -1 { + c.OutboundConfigs[idx] = o.OutboundConfigs[i] + newError("[", fn, "] updated outbound with tag: ", o.OutboundConfigs[i].Tag).AtInfo().WriteToLog() } else { if strings.Contains(strings.ToLower(fn), "tail") { - c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[0]) - ctllog.Println("[", fn, "] appended outbound with tag: ", o.OutboundConfigs[0].Tag) + c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[i]) + newError("[", fn, "] appended outbound with tag: ", o.OutboundConfigs[i].Tag).AtInfo().WriteToLog() } else { - c.OutboundConfigs = append(o.OutboundConfigs, c.OutboundConfigs...) - ctllog.Println("[", fn, "] prepended outbound with tag: ", o.OutboundConfigs[0].Tag) + outboundPrepends = append(outboundPrepends, o.OutboundConfigs[i]) + newError("[", fn, "] prepend outbound with tag: ", o.OutboundConfigs[i].Tag).AtInfo().WriteToLog() } } - } else { - c.OutboundConfigs = o.OutboundConfigs + } + if !strings.Contains(strings.ToLower(fn), "tail") && len(outboundPrepends) > 0 { + c.OutboundConfigs = append(outboundPrepends, c.OutboundConfigs...) } } } diff --git a/infra/conf/xray_test.go b/infra/conf/xray_test.go index fef9ab58..a53cd216 100644 --- a/infra/conf/xray_test.go +++ b/infra/conf/xray_test.go @@ -445,10 +445,10 @@ func TestConfig_Override(t *testing.T) { }, { "replace/outbounds-prepend", - &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}}, - &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}, {Tag: "pos3"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}, {Tag: "pos4", Protocol: "kcp"}}}, "config.json", - &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}}, + &Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos2", Protocol: "kcp"}, {Tag: "pos4", Protocol: "kcp"}, {Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}, {Tag: "pos3"}}}, }, { "replace/outbounds-append",