refactor: enforce panel precedence for outbound tags

- Avoid duplicate 'block'/'direct' tag error in sing-box/Xray
- Let panel or local config override internal defaults
- Ref #1
This commit is contained in:
xboard
2026-03-16 01:56:52 +08:00
parent 8a5034f2c4
commit 08795d2e04
4 changed files with 134 additions and 23 deletions
+18 -5
View File
@@ -14,20 +14,33 @@ import (
type M = map[string]interface{}
func buildConfig(kcfg config.KernelConfig, nc *panel.NodeConfig, users []panel.User, certFile, keyFile string) M {
outbounds := []M{
{"protocol": "freedom", "tag": "direct"},
{"protocol": "blackhole", "tag": "block"},
}
var outbounds []M
tags := make(map[string]bool)
// Panel-defined custom outbounds (structured, converted to Xray native)
for _, co := range nc.CustomOutbounds {
outbounds = append(outbounds, outboundConfigToXray(co))
tags[strings.ToLower(co.Tag)] = true
}
// Static outbounds from local config file (already kernel-native format)
// Static outbounds from local config file
for _, co := range kcfg.CustomOutbound {
tag, _ := co["tag"].(string)
if tag != "" {
tags[strings.ToLower(tag)] = true
}
outbounds = append(outbounds, M(co))
}
// Add default outbounds only if not already defined (Issue #1: Panel priority)
if !tags["direct"] {
outbounds = append([]M{{"protocol": "freedom", "tag": "direct"}}, outbounds...)
}
if !tags["block"] {
// block is often added after direct but before others for safety
outbounds = append(outbounds, M{"protocol": "blackhole", "tag": "block"})
}
cfg := M{
"log": M{
"loglevel": xrayLogLevel(kcfg.LogLevel),
+49
View File
@@ -18,6 +18,55 @@ var testUsers = []panel.User{
{ID: 5, UUID: "4d5965c8-a60c-452a-a943-af83ec0bb0db"},
}
func TestBuildConfig_OutboundPriority(t *testing.T) {
kcfg := config.KernelConfig{
LogLevel: "info",
CustomOutbound: []map[string]any{
{"tag": "block", "protocol": "dns"}, // Local static override
},
}
nc := &panel.NodeConfig{
Protocol: "shadowsocks",
ServerPort: 111,
Cipher: "aes-128-gcm",
CustomOutbounds: []panel.OutboundConfig{
{Tag: "direct", Protocol: "socks", Settings: map[string]any{"address": "1.2.3.4"}}, // Panel override
},
}
cfg := buildConfig(kcfg, nc, testUsers, "", "")
outbounds := cfg["outbounds"].([]M)
// Since we overrode both 'direct' and 'block', the result should contain
// exactly these two custom outbounds, without auto-generated defaults.
if len(outbounds) != 2 {
t.Errorf("outbounds: got %d, want 2", len(outbounds))
}
foundDirect := false
foundBlock := false
for _, o := range outbounds {
tag := o["tag"].(string)
if tag == "direct" {
foundDirect = true
if o["protocol"] != "socks" {
t.Errorf("expected 'direct' protocol to be 'socks' (panel priority), got %v", o["protocol"])
}
}
if tag == "block" {
foundBlock = true
if o["protocol"] != "dns" {
t.Errorf("expected 'block' protocol to be 'dns' (static config priority), got %v", o["protocol"])
}
}
}
if !foundDirect || !foundBlock {
t.Errorf("missing outbounds: direct=%v, block=%v", foundDirect, foundBlock)
}
}
func TestBuildConfig_AllProtocols_ValidJSON(t *testing.T) {
protocols := []struct {
name string