From 01466fbf5709ae8827470068f1086ecdcb0849ca Mon Sep 17 00:00:00 2001 From: Roman Ryzhikov Date: Mon, 30 Mar 2026 14:12:46 +0300 Subject: [PATCH] fix: ensure proxy stable IDs are unique for distinct configs --- models/proxy_config.go | 42 +++++++++++++++++++++++++++++++++++++++++- xray/utils.go | 35 +++++++++++++++++++---------------- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/models/proxy_config.go b/models/proxy_config.go index a6c80bf2..9eb9f0ff 100644 --- a/models/proxy_config.go +++ b/models/proxy_config.go @@ -78,9 +78,9 @@ func (pc *ProxyConfig) GenerateStableID() string { var idComponents []string idComponents = append(idComponents, pc.Protocol) - idComponents = append(idComponents, pc.Server) idComponents = append(idComponents, fmt.Sprintf("%d", pc.Port)) + idComponents = append(idComponents, pc.Name) switch pc.Protocol { case "vless", "vmess": @@ -96,6 +96,14 @@ func (pc *ProxyConfig) GenerateStableID() string { } } + if pc.Flow != "" { + idComponents = append(idComponents, pc.Flow) + } + + if pc.Encryption != "" { + idComponents = append(idComponents, pc.Encryption) + } + if pc.SNI != "" { idComponents = append(idComponents, pc.SNI) } @@ -112,6 +120,38 @@ func (pc *ProxyConfig) GenerateStableID() string { idComponents = append(idComponents, pc.PublicKey) } + if pc.Fingerprint != "" { + idComponents = append(idComponents, pc.Fingerprint) + } + + if pc.ShortID != "" { + idComponents = append(idComponents, pc.ShortID) + } + + if pc.HeaderType != "" { + idComponents = append(idComponents, pc.HeaderType) + } + + if pc.Path != "" { + idComponents = append(idComponents, pc.Path) + } + + if pc.Host != "" { + idComponents = append(idComponents, pc.Host) + } + + if pc.Mode != "" { + idComponents = append(idComponents, pc.Mode) + } + + if pc.ServiceName != "" { + idComponents = append(idComponents, pc.ServiceName) + } + + if len(pc.ALPN) > 0 { + idComponents = append(idComponents, strings.Join(pc.ALPN, ",")) + } + idString := strings.Join(idComponents, "|") hash := sha256.Sum256([]byte(idString)) diff --git a/xray/utils.go b/xray/utils.go index 05e3c7e4..d25c1485 100644 --- a/xray/utils.go +++ b/xray/utils.go @@ -1,16 +1,25 @@ package xray import ( + "fmt" "xray-checker/models" ) func PrepareProxyConfigs(proxies []*models.ProxyConfig) { + seenIDs := make(map[string]int, len(proxies)) + for i := range proxies { proxies[i].Index = i - if proxies[i].StableID == "" { - proxies[i].StableID = proxies[i].GenerateStableID() + baseID := proxies[i].GenerateStableID() + seenIDs[baseID]++ + + if seenIDs[baseID] == 1 { + proxies[i].StableID = baseID + continue } + + proxies[i].StableID = fmt.Sprintf("%s-%d", baseID, seenIDs[baseID]) } } @@ -19,31 +28,25 @@ func IsConfigsEqual(old, new []*models.ProxyConfig) bool { return false } - oldMap := make(map[string]bool) - newMap := make(map[string]bool) + oldCounts := make(map[string]int, len(old)) + newCounts := make(map[string]int, len(new)) for _, cfg := range old { - if cfg.StableID == "" { - cfg.StableID = cfg.GenerateStableID() - } - oldMap[cfg.StableID] = true + oldCounts[cfg.GenerateStableID()]++ } for _, cfg := range new { - if cfg.StableID == "" { - cfg.StableID = cfg.GenerateStableID() - } - newMap[cfg.StableID] = true + newCounts[cfg.GenerateStableID()]++ } - for id := range oldMap { - if !newMap[id] { + for id, count := range oldCounts { + if newCounts[id] != count { return false } } - for id := range newMap { - if !oldMap[id] { + for id, count := range newCounts { + if oldCounts[id] != count { return false } }