Skip to content

Commit 67c66fa

Browse files
authored
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
1 parent 71fee07 commit 67c66fa

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

infra/conf/xray.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -504,21 +504,23 @@ func (c *Config) Override(o *Config, fn string) {
504504

505505
// update the Outbound in slice if the only one in overide config has same tag
506506
if len(o.OutboundConfigs) > 0 {
507-
if len(c.OutboundConfigs) > 0 && len(o.OutboundConfigs) == 1 {
508-
if idx := c.findOutboundTag(o.OutboundConfigs[0].Tag); idx > -1 {
509-
c.OutboundConfigs[idx] = o.OutboundConfigs[0]
510-
ctllog.Println("[", fn, "] updated outbound with tag: ", o.OutboundConfigs[0].Tag)
507+
outboundPrepends := []OutboundDetourConfig{}
508+
for i := range o.OutboundConfigs {
509+
if idx := c.findOutboundTag(o.OutboundConfigs[i].Tag); idx > -1 {
510+
c.OutboundConfigs[idx] = o.OutboundConfigs[i]
511+
newError("[", fn, "] updated outbound with tag: ", o.OutboundConfigs[i].Tag).AtInfo().WriteToLog()
511512
} else {
512513
if strings.Contains(strings.ToLower(fn), "tail") {
513-
c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[0])
514-
ctllog.Println("[", fn, "] appended outbound with tag: ", o.OutboundConfigs[0].Tag)
514+
c.OutboundConfigs = append(c.OutboundConfigs, o.OutboundConfigs[i])
515+
newError("[", fn, "] appended outbound with tag: ", o.OutboundConfigs[i].Tag).AtInfo().WriteToLog()
515516
} else {
516-
c.OutboundConfigs = append(o.OutboundConfigs, c.OutboundConfigs...)
517-
ctllog.Println("[", fn, "] prepended outbound with tag: ", o.OutboundConfigs[0].Tag)
517+
outboundPrepends = append(outboundPrepends, o.OutboundConfigs[i])
518+
newError("[", fn, "] prepend outbound with tag: ", o.OutboundConfigs[i].Tag).AtInfo().WriteToLog()
518519
}
519520
}
520-
} else {
521-
c.OutboundConfigs = o.OutboundConfigs
521+
}
522+
if !strings.Contains(strings.ToLower(fn), "tail") && len(outboundPrepends) > 0 {
523+
c.OutboundConfigs = append(outboundPrepends, c.OutboundConfigs...)
522524
}
523525
}
524526
}

infra/conf/xray_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -445,10 +445,10 @@ func TestConfig_Override(t *testing.T) {
445445
},
446446
{
447447
"replace/outbounds-prepend",
448-
&Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}}},
449-
&Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
448+
&Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos0"}, {Protocol: "vmess", Tag: "pos1"}, {Tag: "pos3"}}},
449+
&Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}, {Tag: "pos4", Protocol: "kcp"}}},
450450
"config.json",
451-
&Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos1", Protocol: "kcp"}, {Tag: "pos2", Protocol: "kcp"}}},
451+
&Config{OutboundConfigs: []OutboundDetourConfig{{Tag: "pos2", Protocol: "kcp"}, {Tag: "pos4", Protocol: "kcp"}, {Tag: "pos0"}, {Tag: "pos1", Protocol: "kcp"}, {Tag: "pos3"}}},
452452
},
453453
{
454454
"replace/outbounds-append",

0 commit comments

Comments
 (0)