-
-
Notifications
You must be signed in to change notification settings - Fork 65
fix: ensure proxy stable IDs are unique for distinct configs #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Comment on lines
120
to
+124
|
||
| } | ||
|
|
||
| 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, "|") | ||
|
|
||
|
Comment on lines
+151
to
156
|
||
| hash := sha256.Sum256([]byte(idString)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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()]++ | ||
|
Comment on lines
34
to
+39
|
||
| } | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GenerateStableID()now incorporatespc.Name. SinceNameis a user-facing label that can change without the underlying proxy transport/credentials changing (e.g., subscription providers often rename entries), this will change stable IDs and can trigger unnecessary config reloads / break any consumers that expect the ID to be stable across renames. Consider removingNamefrom the identity hash (or introducing a separate, explicitly "display" field) so identity is derived only from connection-defining fields.