mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-16 22:57:53 +08:00
Merge pull request #7165 from sususu98/fix/credential-dedup-connection-pool-7163
fix(config): include connection-pool settings in credential deduplication keys
This commit is contained in:
@@ -50,7 +50,11 @@ func toggleConfigAPIKeyExcludedAll(cfg *config.Config, auth *coreauth.Auth, disa
|
||||
if key == "" && base == "" {
|
||||
continue
|
||||
}
|
||||
id, _ := idGen.Next("gemini:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("gemini:apikey", idParts...)
|
||||
if id == authID {
|
||||
entry.ExcludedModels = setConfigAPIKeyExcludedAll(entry.ExcludedModels, disable)
|
||||
return true, nil
|
||||
@@ -65,7 +69,11 @@ func toggleConfigAPIKeyExcludedAll(cfg *config.Config, auth *coreauth.Auth, disa
|
||||
if key == "" && base == "" {
|
||||
continue
|
||||
}
|
||||
id, _ := idGen.Next("gemini-interactions:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("gemini-interactions:apikey", idParts...)
|
||||
if id == authID {
|
||||
entry.ExcludedModels = setConfigAPIKeyExcludedAll(entry.ExcludedModels, disable)
|
||||
return true, nil
|
||||
@@ -80,7 +88,11 @@ func toggleConfigAPIKeyExcludedAll(cfg *config.Config, auth *coreauth.Auth, disa
|
||||
if key == "" && base == "" {
|
||||
continue
|
||||
}
|
||||
id, _ := idGen.Next("claude:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("claude:apikey", idParts...)
|
||||
if id == authID {
|
||||
entry.ExcludedModels = setConfigAPIKeyExcludedAll(entry.ExcludedModels, disable)
|
||||
return true, nil
|
||||
@@ -95,7 +107,11 @@ func toggleConfigAPIKeyExcludedAll(cfg *config.Config, auth *coreauth.Auth, disa
|
||||
if key == "" && base == "" {
|
||||
continue
|
||||
}
|
||||
id, _ := idGen.Next("codex:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("codex:apikey", idParts...)
|
||||
if id == authID {
|
||||
entry.ExcludedModels = setConfigAPIKeyExcludedAll(entry.ExcludedModels, disable)
|
||||
return true, nil
|
||||
@@ -110,7 +126,11 @@ func toggleConfigAPIKeyExcludedAll(cfg *config.Config, auth *coreauth.Auth, disa
|
||||
if key == "" && base == "" {
|
||||
continue
|
||||
}
|
||||
id, _ := idGen.Next("xai:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("xai:apikey", idParts...)
|
||||
if id == authID {
|
||||
entry.ExcludedModels = setConfigAPIKeyExcludedAll(entry.ExcludedModels, disable)
|
||||
return true, nil
|
||||
@@ -125,7 +145,11 @@ func toggleConfigAPIKeyExcludedAll(cfg *config.Config, auth *coreauth.Auth, disa
|
||||
if key == "" && base == "" {
|
||||
continue
|
||||
}
|
||||
id, _ := idGen.Next("meta:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("meta:apikey", idParts...)
|
||||
if id == authID {
|
||||
entry.ExcludedModels = setConfigAPIKeyExcludedAll(entry.ExcludedModels, disable)
|
||||
return true, nil
|
||||
@@ -136,7 +160,11 @@ func toggleConfigAPIKeyExcludedAll(cfg *config.Config, auth *coreauth.Auth, disa
|
||||
key := strings.TrimSpace(entry.APIKey)
|
||||
base := strings.TrimSpace(entry.BaseURL)
|
||||
proxy := strings.TrimSpace(entry.ProxyURL)
|
||||
id, _ := idGen.Next("vertex:apikey", key, base, proxy)
|
||||
idParts := []string{key, base, proxy}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("vertex:apikey", idParts...)
|
||||
if id == authID {
|
||||
entry.ExcludedModels = setConfigAPIKeyExcludedAll(entry.ExcludedModels, disable)
|
||||
return true, nil
|
||||
|
||||
@@ -264,6 +264,63 @@ func resolveInheritedHeaders(keyNode, groupNode *yaml.Node, field string) map[st
|
||||
return nil
|
||||
}
|
||||
|
||||
func yamlMapNodePresent(node *yaml.Node, key string) *yaml.Node {
|
||||
if node == nil || node.Kind != yaml.MappingNode {
|
||||
return nil
|
||||
}
|
||||
for i := 0; i+1 < len(node.Content); i += 2 {
|
||||
if node.Content[i].Value == key {
|
||||
valNode := node.Content[i+1]
|
||||
if valNode == nil || valNode.Tag == "!!null" {
|
||||
return nil
|
||||
}
|
||||
return valNode
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func resolveInheritedConnectionPool(keyNode, groupNode *yaml.Node) *config.ProxyConnectionPoolConfig {
|
||||
groupPoolNode := yamlMapNodePresent(groupNode, "connection-pool")
|
||||
keyPoolNode := yamlMapNodePresent(keyNode, "connection-pool")
|
||||
if groupPoolNode == nil && keyPoolNode == nil {
|
||||
return nil
|
||||
}
|
||||
mergedNode := &yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
|
||||
if groupPoolNode != nil && groupPoolNode.Kind == yaml.MappingNode {
|
||||
for i := 0; i+1 < len(groupPoolNode.Content); i += 2 {
|
||||
if groupPoolNode.Content[i+1].Tag == "!!null" {
|
||||
continue
|
||||
}
|
||||
mergedNode.Content = append(mergedNode.Content, groupPoolNode.Content[i], groupPoolNode.Content[i+1])
|
||||
}
|
||||
}
|
||||
if keyPoolNode != nil && keyPoolNode.Kind == yaml.MappingNode {
|
||||
for i := 0; i+1 < len(keyPoolNode.Content); i += 2 {
|
||||
if keyPoolNode.Content[i+1].Tag == "!!null" {
|
||||
continue
|
||||
}
|
||||
field := keyPoolNode.Content[i].Value
|
||||
replaced := false
|
||||
for j := 0; j+1 < len(mergedNode.Content); j += 2 {
|
||||
if mergedNode.Content[j].Value == field {
|
||||
mergedNode.Content[j+1] = keyPoolNode.Content[i+1]
|
||||
replaced = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !replaced {
|
||||
mergedNode.Content = append(mergedNode.Content, keyPoolNode.Content[i], keyPoolNode.Content[i+1])
|
||||
}
|
||||
}
|
||||
}
|
||||
var pool config.ProxyConnectionPoolConfig
|
||||
if errDecode := mergedNode.Decode(&pool); errDecode != nil {
|
||||
return nil
|
||||
}
|
||||
return &pool
|
||||
}
|
||||
|
||||
func normalizeModelPrefixHelper(prefix string) string {
|
||||
trimmed := strings.TrimSpace(prefix)
|
||||
trimmed = strings.Trim(trimmed, "/")
|
||||
@@ -273,7 +330,7 @@ func normalizeModelPrefixHelper(prefix string) string {
|
||||
return trimmed
|
||||
}
|
||||
|
||||
func formatCredentialDedupKey(key, base, proxyURL, prefix string, headers map[string]string) string {
|
||||
func formatCredentialDedupKey(key, base, proxyURL, prefix string, headers map[string]string, pool *config.ProxyConnectionPoolConfig) string {
|
||||
var b strings.Builder
|
||||
b.WriteString(strings.TrimSpace(key))
|
||||
b.WriteByte(0)
|
||||
@@ -284,6 +341,8 @@ func formatCredentialDedupKey(key, base, proxyURL, prefix string, headers map[st
|
||||
b.WriteString(normalizeModelPrefixHelper(prefix))
|
||||
b.WriteByte(0)
|
||||
b.WriteString(config.FormatSortedHeaders(config.NormalizeHeaders(headers)))
|
||||
b.WriteByte(0)
|
||||
b.WriteString(config.FormatConnectionPoolDedupID(pool))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
@@ -347,7 +406,7 @@ func (h *Handler) injectV8APIKeyAuthIndexesLocked(root *yaml.Node, data []byte)
|
||||
continue
|
||||
}
|
||||
e := cfg.GeminiKey[idx]
|
||||
key := formatCredentialDedupKey(e.APIKey, e.BaseURL, e.ProxyURL, e.Prefix, e.Headers)
|
||||
key := formatCredentialDedupKey(e.APIKey, e.BaseURL, e.ProxyURL, e.Prefix, e.Headers, e.ConnectionPool)
|
||||
providerAuthMap["gemini"][key] = resolveAuthIndex(a)
|
||||
}
|
||||
for _, a := range authsByProvider["gemini-interactions"] {
|
||||
@@ -359,7 +418,7 @@ func (h *Handler) injectV8APIKeyAuthIndexesLocked(root *yaml.Node, data []byte)
|
||||
continue
|
||||
}
|
||||
e := cfg.InteractionsKey[idx]
|
||||
key := formatCredentialDedupKey(e.APIKey, e.BaseURL, e.ProxyURL, e.Prefix, e.Headers)
|
||||
key := formatCredentialDedupKey(e.APIKey, e.BaseURL, e.ProxyURL, e.Prefix, e.Headers, e.ConnectionPool)
|
||||
providerAuthMap["interactions"][key] = resolveAuthIndex(a)
|
||||
}
|
||||
for _, a := range authsByProvider["vertex"] {
|
||||
@@ -371,7 +430,7 @@ func (h *Handler) injectV8APIKeyAuthIndexesLocked(root *yaml.Node, data []byte)
|
||||
continue
|
||||
}
|
||||
e := cfg.VertexCompatAPIKey[idx]
|
||||
key := strings.TrimSpace(e.APIKey) + "|" + strings.TrimSpace(e.BaseURL)
|
||||
key := strings.TrimSpace(e.APIKey) + "|" + strings.TrimSpace(e.BaseURL) + "\x00" + config.FormatConnectionPoolDedupID(e.ConnectionPool)
|
||||
providerAuthMap["vertex"][key] = resolveAuthIndex(a)
|
||||
}
|
||||
|
||||
@@ -554,8 +613,10 @@ func (h *Handler) injectV8APIKeyAuthIndexesLocked(root *yaml.Node, data []byte)
|
||||
continue
|
||||
}
|
||||
apiKey := strings.TrimSpace(yamlMapScalar(keyNode, "api-key"))
|
||||
pool := resolveInheritedConnectionPool(keyNode, group)
|
||||
if providerName == "vertex" {
|
||||
if idx, ok := providerAuthMap["vertex"][apiKey+"|"+groupBase]; ok {
|
||||
vertexKey := apiKey + "|" + groupBase + "\x00" + config.FormatConnectionPoolDedupID(pool)
|
||||
if idx, ok := providerAuthMap["vertex"][vertexKey]; ok {
|
||||
setMapScalar(keyNode, "auth_index", idx)
|
||||
}
|
||||
continue
|
||||
@@ -564,7 +625,7 @@ func (h *Handler) injectV8APIKeyAuthIndexesLocked(root *yaml.Node, data []byte)
|
||||
proxyURL := resolveInheritedScalar(keyNode, group, "proxy-url")
|
||||
prefix := resolveInheritedScalar(keyNode, group, "prefix")
|
||||
headers := resolveInheritedHeaders(keyNode, group, "headers")
|
||||
dedupKey := formatCredentialDedupKey(apiKey, groupBase, proxyURL, prefix, headers)
|
||||
dedupKey := formatCredentialDedupKey(apiKey, groupBase, proxyURL, prefix, headers, pool)
|
||||
if idx, ok := providerAuthMap[providerName][dedupKey]; ok {
|
||||
setMapScalar(keyNode, "auth_index", idx)
|
||||
}
|
||||
@@ -595,7 +656,11 @@ func (h *Handler) geminiKeysWithAuthIndex() []geminiKeyWithAuthIndex {
|
||||
proxyURL := strings.TrimSpace(entry.ProxyURL)
|
||||
prefix := strings.TrimSpace(entry.Prefix)
|
||||
if key != "" || base != "" {
|
||||
id, _ := idGen.Next("gemini:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("gemini:apikey", idParts...)
|
||||
authIndex = liveIndexByID[id]
|
||||
}
|
||||
out[i] = geminiKeyWithAuthIndex{
|
||||
@@ -628,7 +693,11 @@ func (h *Handler) interactionsKeysWithAuthIndex() []geminiKeyWithAuthIndex {
|
||||
proxyURL := strings.TrimSpace(entry.ProxyURL)
|
||||
prefix := strings.TrimSpace(entry.Prefix)
|
||||
if key != "" || base != "" {
|
||||
id, _ := idGen.Next("gemini-interactions:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("gemini-interactions:apikey", idParts...)
|
||||
authIndex = liveIndexByID[id]
|
||||
}
|
||||
out[i] = geminiKeyWithAuthIndex{
|
||||
@@ -661,7 +730,11 @@ func (h *Handler) claudeKeysWithAuthIndex() []claudeKeyWithAuthIndex {
|
||||
proxyURL := strings.TrimSpace(entry.ProxyURL)
|
||||
prefix := strings.TrimSpace(entry.Prefix)
|
||||
if key != "" || base != "" {
|
||||
id, _ := idGen.Next("claude:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("claude:apikey", idParts...)
|
||||
authIndex = liveIndexByID[id]
|
||||
}
|
||||
out[i] = claudeKeyWithAuthIndex{
|
||||
@@ -694,7 +767,11 @@ func (h *Handler) codexKeysWithAuthIndex() []codexKeyWithAuthIndex {
|
||||
proxyURL := strings.TrimSpace(entry.ProxyURL)
|
||||
prefix := strings.TrimSpace(entry.Prefix)
|
||||
if key != "" || base != "" {
|
||||
id, _ := idGen.Next("codex:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("codex:apikey", idParts...)
|
||||
authIndex = liveIndexByID[id]
|
||||
}
|
||||
out[i] = codexKeyWithAuthIndex{
|
||||
@@ -727,7 +804,11 @@ func (h *Handler) xaiKeysWithAuthIndex() []xaiKeyWithAuthIndex {
|
||||
proxyURL := strings.TrimSpace(entry.ProxyURL)
|
||||
prefix := strings.TrimSpace(entry.Prefix)
|
||||
if key != "" || base != "" {
|
||||
id, _ := idGen.Next("xai:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("xai:apikey", idParts...)
|
||||
authIndex = liveIndexByID[id]
|
||||
}
|
||||
out[i] = xaiKeyWithAuthIndex{
|
||||
@@ -760,7 +841,11 @@ func (h *Handler) metaKeysWithAuthIndex() []metaKeyWithAuthIndex {
|
||||
proxyURL := strings.TrimSpace(entry.ProxyURL)
|
||||
prefix := strings.TrimSpace(entry.Prefix)
|
||||
if key != "" || base != "" {
|
||||
id, _ := idGen.Next("meta:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("meta:apikey", idParts...)
|
||||
authIndex = liveIndexByID[id]
|
||||
}
|
||||
out[i] = metaKeyWithAuthIndex{
|
||||
@@ -787,7 +872,11 @@ func (h *Handler) vertexCompatKeysWithAuthIndex() []vertexCompatKeyWithAuthIndex
|
||||
out := make([]vertexCompatKeyWithAuthIndex, len(h.cfg.VertexCompatAPIKey))
|
||||
for i := range h.cfg.VertexCompatAPIKey {
|
||||
entry := h.cfg.VertexCompatAPIKey[i]
|
||||
id, _ := idGen.Next("vertex:apikey", entry.APIKey, entry.BaseURL, entry.ProxyURL)
|
||||
idParts := []string{entry.APIKey, entry.BaseURL, entry.ProxyURL}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next("vertex:apikey", idParts...)
|
||||
authIndex := liveIndexByID[id]
|
||||
out[i] = vertexCompatKeyWithAuthIndex{
|
||||
VertexCompatKey: entry,
|
||||
@@ -835,13 +924,21 @@ func (h *Handler) openAICompatibilityWithAuthIndex() []openAICompatibilityWithAu
|
||||
AuthIndex: "",
|
||||
}
|
||||
if len(entry.APIKeyEntries) == 0 {
|
||||
id, _ := idGen.Next(idKind, entry.BaseURL)
|
||||
idParts := []string{entry.BaseURL}
|
||||
if poolID := config.FormatConnectionPoolDedupID(synthesizer.OpenAICompatConnectionPool(&entry, nil)); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next(idKind, idParts...)
|
||||
response.AuthIndex = liveIndexByID[id]
|
||||
} else {
|
||||
response.APIKeyEntries = make([]openAICompatibilityAPIKeyWithAuthIndex, len(entry.APIKeyEntries))
|
||||
for j := range entry.APIKeyEntries {
|
||||
apiKeyEntry := entry.APIKeyEntries[j]
|
||||
id, _ := idGen.Next(idKind, apiKeyEntry.APIKey, entry.BaseURL, apiKeyEntry.ProxyURL)
|
||||
idParts := []string{apiKeyEntry.APIKey, entry.BaseURL, apiKeyEntry.ProxyURL}
|
||||
if poolID := config.FormatConnectionPoolDedupID(synthesizer.OpenAICompatConnectionPool(&entry, &apiKeyEntry)); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, _ := idGen.Next(idKind, idParts...)
|
||||
response.APIKeyEntries[j] = openAICompatibilityAPIKeyWithAuthIndex{
|
||||
OpenAICompatibilityAPIKey: apiKeyEntry,
|
||||
AuthIndex: liveIndexByID[id],
|
||||
|
||||
@@ -13,6 +13,8 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v8/internal/watcher/synthesizer"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/auth"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -892,3 +894,84 @@ api-keys:
|
||||
t.Fatalf("unexpected loaded headers: %+v", loaded.OpenAICompatibility[0].Headers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigV8InjectsDistinctAuthIndexesForDistinctConnectionPools(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
file := filepath.Join(t.TempDir(), "config.yaml")
|
||||
raw := `config-version: 8
|
||||
api-keys:
|
||||
gemini:
|
||||
- name: gemini-group
|
||||
base-url: "https://example.invalid"
|
||||
connection-pool:
|
||||
enabled: true
|
||||
idle-conn-timeout: "120s"
|
||||
max-idle-conns-per-host: 2
|
||||
keys:
|
||||
- api-key: "shared-gemini-key"
|
||||
- api-key: "shared-gemini-key"
|
||||
connection-pool:
|
||||
idle-conn-timeout: ""
|
||||
max-idle-conns-per-host: 5
|
||||
vertex:
|
||||
- name: vertex-group
|
||||
base-url: "https://vertex.example.invalid"
|
||||
connection-pool:
|
||||
enabled: true
|
||||
max-idle-conns-per-host: 2
|
||||
keys:
|
||||
- api-key: "shared-vertex-key"
|
||||
- api-key: "shared-vertex-key"
|
||||
connection-pool:
|
||||
max-idle-conns-per-host: 6
|
||||
`
|
||||
if errWrite := os.WriteFile(file, []byte(raw), 0o600); errWrite != nil {
|
||||
t.Fatal(errWrite)
|
||||
}
|
||||
cfg, errLoad := config.LoadConfig(file)
|
||||
if errLoad != nil {
|
||||
t.Fatal(errLoad)
|
||||
}
|
||||
manager := coreauth.NewManager(nil, nil, nil)
|
||||
synth := synthesizer.NewConfigSynthesizer()
|
||||
auths, errSynth := synth.Synthesize(&synthesizer.SynthesisContext{
|
||||
Config: cfg,
|
||||
Now: time.Now(),
|
||||
IDGenerator: synthesizer.NewStableIDGenerator(),
|
||||
})
|
||||
if errSynth != nil {
|
||||
t.Fatal(errSynth)
|
||||
}
|
||||
for _, a := range auths {
|
||||
if _, errReg := manager.Register(context.Background(), a); errReg != nil {
|
||||
t.Fatal(errReg)
|
||||
}
|
||||
}
|
||||
h := &Handler{cfg: cfg, configFilePath: file, authManager: manager}
|
||||
router := gin.New()
|
||||
router.GET("/v8/management/config/*path", h.ConfigV8)
|
||||
|
||||
for _, provider := range []string{"gemini", "vertex"} {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/v8/management/config/api-keys/"+provider, nil))
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("GET %s status=%d body=%s", provider, w.Code, w.Body.String())
|
||||
}
|
||||
var groups []struct {
|
||||
Keys []struct {
|
||||
AuthIndex string `yaml:"auth_index" json:"auth_index"`
|
||||
} `yaml:"keys" json:"keys"`
|
||||
}
|
||||
if err := yaml.Unmarshal(w.Body.Bytes(), &groups); err != nil {
|
||||
t.Fatalf("unmarshal %s response: %v", provider, err)
|
||||
}
|
||||
if len(groups) != 1 || len(groups[0].Keys) != 2 {
|
||||
t.Fatalf("%s groups = %+v, want 1 group with 2 keys", provider, groups)
|
||||
}
|
||||
first := groups[0].Keys[0].AuthIndex
|
||||
second := groups[0].Keys[1].AuthIndex
|
||||
if first == "" || second == "" || first == second {
|
||||
t.Fatalf("%s auth_index values = (%q, %q), want two distinct non-empty indexes", provider, first, second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
sdkpluginstore "github.com/router-for-me/CLIProxyAPI/v8/sdk/pluginstore"
|
||||
@@ -432,6 +433,28 @@ func formatGeminiKeyDedupID(entry GeminiKey) string {
|
||||
b.WriteString(entry.Prefix)
|
||||
b.WriteByte(0)
|
||||
b.WriteString(FormatSortedHeaders(entry.Headers))
|
||||
b.WriteByte(0)
|
||||
b.WriteString(FormatConnectionPoolDedupID(entry.ConnectionPool))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// FormatConnectionPoolDedupID serializes a per-credential connection-pool override
|
||||
// deterministically for credential deduplication and auth-index mapping.
|
||||
func FormatConnectionPoolDedupID(pool *ProxyConnectionPoolConfig) string {
|
||||
if pool == nil {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("pool:")
|
||||
if pool.Enabled != nil {
|
||||
b.WriteString(strconv.FormatBool(*pool.Enabled))
|
||||
}
|
||||
b.WriteByte(0)
|
||||
b.WriteString(strings.TrimSpace(pool.IdleConnTimeout))
|
||||
b.WriteByte(0)
|
||||
if pool.MaxIdleConnsPerHost != nil {
|
||||
b.WriteString(strconv.Itoa(*pool.MaxIdleConnsPerHost))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
|
||||
@@ -173,3 +173,61 @@ api-keys:
|
||||
t.Fatalf("null-subkey override max-idle-conns-per-host = %v, want 4", nullSubkey.MaxIdleConnsPerHost)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCredentialDeduplicationPreservesDistinctConnectionPools(t *testing.T) {
|
||||
raw := []byte(`gemini-api-key:
|
||||
- api-key: "shared-gemini-key"
|
||||
base-url: "https://example.invalid"
|
||||
connection-pool:
|
||||
enabled: true
|
||||
max-idle-conns-per-host: 2
|
||||
- api-key: "shared-gemini-key"
|
||||
base-url: "https://example.invalid"
|
||||
connection-pool:
|
||||
enabled: true
|
||||
max-idle-conns-per-host: 5
|
||||
- api-key: "shared-gemini-key"
|
||||
base-url: "https://example.invalid"
|
||||
connection-pool:
|
||||
enabled: true
|
||||
max-idle-conns-per-host: 5
|
||||
vertex-api-key:
|
||||
- api-key: "shared-vertex-key"
|
||||
base-url: "https://vertex.example.invalid"
|
||||
connection-pool:
|
||||
enabled: true
|
||||
max-idle-conns-per-host: 2
|
||||
- api-key: "shared-vertex-key"
|
||||
base-url: "https://vertex.example.invalid"
|
||||
connection-pool:
|
||||
enabled: true
|
||||
max-idle-conns-per-host: 6
|
||||
- api-key: "shared-vertex-key"
|
||||
base-url: "https://vertex.example.invalid"
|
||||
connection-pool:
|
||||
enabled: true
|
||||
max-idle-conns-per-host: 6
|
||||
`)
|
||||
cfg, err := config.ParseConfigBytes(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseConfigBytes: %v", err)
|
||||
}
|
||||
if len(cfg.GeminiKey) != 2 {
|
||||
t.Fatalf("len(GeminiKey) = %d, want 2", len(cfg.GeminiKey))
|
||||
}
|
||||
if cfg.GeminiKey[0].ConnectionPool == nil || cfg.GeminiKey[0].ConnectionPool.MaxIdleConnsPerHost == nil || *cfg.GeminiKey[0].ConnectionPool.MaxIdleConnsPerHost != 2 {
|
||||
t.Fatalf("GeminiKey[0].ConnectionPool = %+v, want max-idle-conns-per-host=2", cfg.GeminiKey[0].ConnectionPool)
|
||||
}
|
||||
if cfg.GeminiKey[1].ConnectionPool == nil || cfg.GeminiKey[1].ConnectionPool.MaxIdleConnsPerHost == nil || *cfg.GeminiKey[1].ConnectionPool.MaxIdleConnsPerHost != 5 {
|
||||
t.Fatalf("GeminiKey[1].ConnectionPool = %+v, want max-idle-conns-per-host=5", cfg.GeminiKey[1].ConnectionPool)
|
||||
}
|
||||
if len(cfg.VertexCompatAPIKey) != 2 {
|
||||
t.Fatalf("len(VertexCompatAPIKey) = %d, want 2", len(cfg.VertexCompatAPIKey))
|
||||
}
|
||||
if cfg.VertexCompatAPIKey[0].ConnectionPool == nil || cfg.VertexCompatAPIKey[0].ConnectionPool.MaxIdleConnsPerHost == nil || *cfg.VertexCompatAPIKey[0].ConnectionPool.MaxIdleConnsPerHost != 2 {
|
||||
t.Fatalf("VertexCompatAPIKey[0].ConnectionPool = %+v, want max-idle-conns-per-host=2", cfg.VertexCompatAPIKey[0].ConnectionPool)
|
||||
}
|
||||
if cfg.VertexCompatAPIKey[1].ConnectionPool == nil || cfg.VertexCompatAPIKey[1].ConnectionPool.MaxIdleConnsPerHost == nil || *cfg.VertexCompatAPIKey[1].ConnectionPool.MaxIdleConnsPerHost != 6 {
|
||||
t.Fatalf("VertexCompatAPIKey[1].ConnectionPool = %+v, want max-idle-conns-per-host=6", cfg.VertexCompatAPIKey[1].ConnectionPool)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,8 +141,8 @@ func (cfg *Config) SanitizeVertexCompatKeys() {
|
||||
}
|
||||
entry.Models = sanitizedModels
|
||||
|
||||
// Use API key + base URL as uniqueness key
|
||||
uniqueKey := entry.APIKey + "|" + entry.BaseURL
|
||||
// Use API key + base URL + connection-pool settings as uniqueness key
|
||||
uniqueKey := entry.APIKey + "|" + entry.BaseURL + "\x00" + FormatConnectionPoolDedupID(entry.ConnectionPool)
|
||||
if _, exists := seen[uniqueKey]; exists {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -46,6 +46,11 @@ func authProxyConnectionPool(pool *config.ProxyConnectionPoolConfig) *coreauth.P
|
||||
// openAICompatConnectionPool resolves the pool override for one OpenAI-compatible
|
||||
// key: the per-key block wins field by field, so a key that overrides only one value
|
||||
// keeps inheriting the provider-group default for the rest.
|
||||
// OpenAICompatConnectionPool resolves the effective connection-pool config for an OpenAI-compatible entry.
|
||||
func OpenAICompatConnectionPool(compat *config.OpenAICompatibility, entry *config.OpenAICompatibilityAPIKey) *config.ProxyConnectionPoolConfig {
|
||||
return openAICompatConnectionPool(compat, entry)
|
||||
}
|
||||
|
||||
func openAICompatConnectionPool(compat *config.OpenAICompatibility, entry *config.OpenAICompatibilityAPIKey) *config.ProxyConnectionPoolConfig {
|
||||
var group *config.ProxyConnectionPoolConfig
|
||||
if compat != nil {
|
||||
@@ -134,7 +139,11 @@ func (s *ConfigSynthesizer) synthesizeGeminiKeyEntries(ctx *SynthesisContext, en
|
||||
}
|
||||
prefix := strings.TrimSpace(entry.Prefix)
|
||||
proxyURL := strings.TrimSpace(entry.ProxyURL)
|
||||
id, token := idGen.Next(idKind, key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, token := idGen.Next(idKind, idParts...)
|
||||
attrs := map[string]string{
|
||||
"source": fmt.Sprintf("config:%s[%s]", sourceName, token),
|
||||
"config_index": strconv.Itoa(i),
|
||||
@@ -198,7 +207,11 @@ func (s *ConfigSynthesizer) synthesizeClaudeKeys(ctx *SynthesisContext) []*corea
|
||||
}
|
||||
prefix := strings.TrimSpace(ck.Prefix)
|
||||
proxyURL := strings.TrimSpace(ck.ProxyURL)
|
||||
id, token := idGen.Next("claude:apikey", key, base, proxyURL, prefix, config.FormatSortedHeaders(ck.Headers))
|
||||
idParts := []string{key, base, proxyURL, prefix, config.FormatSortedHeaders(ck.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(ck.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, token := idGen.Next("claude:apikey", idParts...)
|
||||
attrs := map[string]string{
|
||||
"source": fmt.Sprintf("config:claude[%s]", token),
|
||||
"config_index": strconv.Itoa(i),
|
||||
@@ -284,7 +297,11 @@ func (s *ConfigSynthesizer) synthesizeCodexStyleKeys(ctx *SynthesisContext, entr
|
||||
}
|
||||
prefix := strings.TrimSpace(entry.Prefix)
|
||||
proxyURL := strings.TrimSpace(entry.ProxyURL)
|
||||
id, token := idGen.Next(provider+":apikey", key, baseURL, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers))
|
||||
idParts := []string{key, baseURL, proxyURL, prefix, config.FormatSortedHeaders(entry.Headers)}
|
||||
if poolID := config.FormatConnectionPoolDedupID(entry.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, token := idGen.Next(provider+":apikey", idParts...)
|
||||
attrs := map[string]string{
|
||||
"source": fmt.Sprintf("config:%s[%s]", provider, token),
|
||||
"config_index": strconv.Itoa(i),
|
||||
@@ -367,8 +384,13 @@ func (s *ConfigSynthesizer) synthesizeOpenAICompat(ctx *SynthesisContext) []*cor
|
||||
entry := &compat.APIKeyEntries[j]
|
||||
key := strings.TrimSpace(entry.APIKey)
|
||||
proxyURL := strings.TrimSpace(entry.ProxyURL)
|
||||
effectivePool := openAICompatConnectionPool(compat, entry)
|
||||
idKind := fmt.Sprintf("openai-compatibility:%s", providerName)
|
||||
id, token := idGen.Next(idKind, key, base, proxyURL)
|
||||
idParts := []string{key, base, proxyURL}
|
||||
if poolID := config.FormatConnectionPoolDedupID(effectivePool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, token := idGen.Next(idKind, idParts...)
|
||||
attrs := map[string]string{
|
||||
"source": fmt.Sprintf("config:%s[%s]", providerName, token),
|
||||
"base_url": base,
|
||||
@@ -414,8 +436,13 @@ func (s *ConfigSynthesizer) synthesizeOpenAICompat(ctx *SynthesisContext) []*cor
|
||||
}
|
||||
// Fallback: create entry without API key if no APIKeyEntries
|
||||
if createdEntries == 0 {
|
||||
effectivePool := openAICompatConnectionPool(compat, nil)
|
||||
idKind := fmt.Sprintf("openai-compatibility:%s", providerName)
|
||||
id, token := idGen.Next(idKind, base)
|
||||
idParts := []string{base}
|
||||
if poolID := config.FormatConnectionPoolDedupID(effectivePool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, token := idGen.Next(idKind, idParts...)
|
||||
attrs := map[string]string{
|
||||
"source": fmt.Sprintf("config:%s[%s]", providerName, token),
|
||||
"base_url": base,
|
||||
@@ -473,7 +500,11 @@ func (s *ConfigSynthesizer) synthesizeVertexCompat(ctx *SynthesisContext) []*cor
|
||||
prefix := strings.TrimSpace(compat.Prefix)
|
||||
proxyURL := strings.TrimSpace(compat.ProxyURL)
|
||||
idKind := "vertex:apikey"
|
||||
id, token := idGen.Next(idKind, key, base, proxyURL)
|
||||
idParts := []string{key, base, proxyURL}
|
||||
if poolID := config.FormatConnectionPoolDedupID(compat.ConnectionPool); poolID != "" {
|
||||
idParts = append(idParts, poolID)
|
||||
}
|
||||
id, token := idGen.Next(idKind, idParts...)
|
||||
attrs := map[string]string{
|
||||
"source": fmt.Sprintf("config:vertex-apikey[%s]", token),
|
||||
"base_url": base,
|
||||
|
||||
@@ -519,6 +519,24 @@ type ProxyConnectionPoolOverride struct {
|
||||
}
|
||||
|
||||
// Clone deep copies the override so auth snapshots never share mutable state.
|
||||
func (o *ProxyConnectionPoolOverride) seedKey() string {
|
||||
if o == nil {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("pool:")
|
||||
if o.Enabled != nil {
|
||||
b.WriteString(strconv.FormatBool(*o.Enabled))
|
||||
}
|
||||
b.WriteByte(0)
|
||||
b.WriteString(strings.TrimSpace(o.IdleConnTimeout))
|
||||
b.WriteByte(0)
|
||||
if o.MaxIdleConnsPerHost != nil {
|
||||
b.WriteString(strconv.Itoa(*o.MaxIdleConnsPerHost))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func (o *ProxyConnectionPoolOverride) Clone() *ProxyConnectionPoolOverride {
|
||||
if o == nil {
|
||||
return nil
|
||||
@@ -677,7 +695,11 @@ func (a *Auth) indexSeed() string {
|
||||
}
|
||||
}
|
||||
if apiPrefix != "" {
|
||||
return apiPrefix + ":" + strings.TrimSpace(baseURL) + "+" + strings.TrimSpace(apiKey)
|
||||
seed := apiPrefix + ":" + strings.TrimSpace(baseURL) + "+" + strings.TrimSpace(apiKey)
|
||||
if poolSeed := a.ProxyConnectionPool.seedKey(); poolSeed != "" {
|
||||
seed += "\x00" + poolSeed
|
||||
}
|
||||
return seed
|
||||
}
|
||||
|
||||
if id := strings.TrimSpace(a.ID); id != "" {
|
||||
|
||||
Reference in New Issue
Block a user