mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
refactor: standardize constant naming and improve file-based auth handling
- Renamed constants from uppercase to CamelCase for consistency. - Replaced redundant file-based auth handling logic with the new `util.CountAuthFiles` helper. - Fixed various error-handling inconsistencies and enhanced robustness in file operations. - Streamlined auth client reload logic in server and watcher components. - Applied minor code readability improvements across multiple packages.
This commit is contained in:
@@ -73,7 +73,7 @@ func (s *FileStore) Save(ctx context.Context, auth *Auth) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("auth filestore: marshal metadata failed: %w", err)
|
||||
}
|
||||
if existing, err := os.ReadFile(path); err == nil {
|
||||
if existing, errReadFile := os.ReadFile(path); errReadFile == nil {
|
||||
if jsonEqual(existing, raw) {
|
||||
return nil
|
||||
}
|
||||
@@ -108,8 +108,8 @@ func deepEqualJSON(a, b any) bool {
|
||||
return false
|
||||
}
|
||||
for key, subA := range valA {
|
||||
subB, ok := valB[key]
|
||||
if !ok || !deepEqualJSON(subA, subB) {
|
||||
subB, ok1 := valB[key]
|
||||
if !ok1 || !deepEqualJSON(subA, subB) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -795,7 +795,7 @@ func authLastRefreshTimestamp(a *Auth) (time.Time, bool) {
|
||||
func lookupMetadataTime(meta map[string]any, keys ...string) (time.Time, bool) {
|
||||
for _, key := range keys {
|
||||
if val, ok := meta[key]; ok {
|
||||
if ts, ok := parseTimeValue(val); ok {
|
||||
if ts, ok1 := parseTimeValue(val); ok1 {
|
||||
return ts, true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,6 +84,24 @@ func (a *Auth) AccountInfo() (bool, string) {
|
||||
if a == nil {
|
||||
return false, ""
|
||||
}
|
||||
if strings.ToLower(a.Provider) == "gemini-web" {
|
||||
if a.Metadata != nil {
|
||||
if v, ok := a.Metadata["secure_1psid"].(string); ok && v != "" {
|
||||
return true, v
|
||||
}
|
||||
if v, ok := a.Metadata["__Secure-1PSID"].(string); ok && v != "" {
|
||||
return true, v
|
||||
}
|
||||
}
|
||||
if a.Attributes != nil {
|
||||
if v := a.Attributes["secure_1psid"]; v != "" {
|
||||
return true, v
|
||||
}
|
||||
if v := a.Attributes["api_key"]; v != "" {
|
||||
return true, v
|
||||
}
|
||||
}
|
||||
}
|
||||
if a.Metadata != nil {
|
||||
if v, ok := a.Metadata["email"].(string); ok {
|
||||
return false, v
|
||||
@@ -125,7 +143,7 @@ func expirationFromMap(meta map[string]any) (time.Time, bool) {
|
||||
}
|
||||
for _, key := range expireKeys {
|
||||
if v, ok := meta[key]; ok {
|
||||
if ts, ok := parseTimeValue(v); ok {
|
||||
if ts, ok1 := parseTimeValue(v); ok1 {
|
||||
return ts, true
|
||||
}
|
||||
}
|
||||
@@ -134,7 +152,7 @@ func expirationFromMap(meta map[string]any) (time.Time, bool) {
|
||||
if nested, ok := meta[nestedKey]; ok {
|
||||
switch val := nested.(type) {
|
||||
case map[string]any:
|
||||
if ts, ok := expirationFromMap(val); ok {
|
||||
if ts, ok1 := expirationFromMap(val); ok1 {
|
||||
return ts, true
|
||||
}
|
||||
case map[string]string:
|
||||
@@ -142,7 +160,7 @@ func expirationFromMap(meta map[string]any) (time.Time, bool) {
|
||||
for k, v := range val {
|
||||
temp[k] = v
|
||||
}
|
||||
if ts, ok := expirationFromMap(temp); ok {
|
||||
if ts, ok1 := expirationFromMap(temp); ok1 {
|
||||
return ts, true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/registry"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/runtime/executor"
|
||||
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
||||
sdkAuth "github.com/router-for-me/CLIProxyAPI/v6/sdk/auth"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/auth"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -172,10 +173,11 @@ func (s *Service) Run(ctx context.Context) error {
|
||||
log.Infof("core auth auto-refresh started (interval=%s)", interval)
|
||||
}
|
||||
|
||||
totalNewClients := tokenResult.SuccessfulAuthed + apiKeyResult.GeminiKeyCount + apiKeyResult.ClaudeKeyCount + apiKeyResult.CodexKeyCount + apiKeyResult.OpenAICompatCount
|
||||
authFileCount := util.CountAuthFiles(s.cfg.AuthDir)
|
||||
totalNewClients := authFileCount + apiKeyResult.GeminiKeyCount + apiKeyResult.ClaudeKeyCount + apiKeyResult.CodexKeyCount + apiKeyResult.OpenAICompatCount
|
||||
log.Infof("full client load complete - %d clients (%d auth files + %d GL API keys + %d Claude API keys + %d Codex keys + %d OpenAI-compat)",
|
||||
totalNewClients,
|
||||
tokenResult.SuccessfulAuthed,
|
||||
authFileCount,
|
||||
apiKeyResult.GeminiKeyCount,
|
||||
apiKeyResult.ClaudeKeyCount,
|
||||
apiKeyResult.CodexKeyCount,
|
||||
@@ -292,19 +294,19 @@ func (s *Service) syncCoreAuthFromAuths(ctx context.Context, auths []*coreauth.A
|
||||
// Ensure executors registered per provider: prefer stateless where available.
|
||||
switch strings.ToLower(a.Provider) {
|
||||
case "gemini":
|
||||
s.coreManager.RegisterExecutor(executor.NewGeminiExecutor())
|
||||
s.coreManager.RegisterExecutor(executor.NewGeminiExecutor(s.cfg))
|
||||
case "gemini-cli":
|
||||
s.coreManager.RegisterExecutor(executor.NewGeminiCLIExecutor())
|
||||
s.coreManager.RegisterExecutor(executor.NewGeminiCLIExecutor(s.cfg))
|
||||
case "gemini-web":
|
||||
s.coreManager.RegisterExecutor(executor.NewGeminiWebExecutor(s.cfg))
|
||||
case "claude":
|
||||
s.coreManager.RegisterExecutor(executor.NewClaudeExecutor())
|
||||
s.coreManager.RegisterExecutor(executor.NewClaudeExecutor(s.cfg))
|
||||
case "codex":
|
||||
s.coreManager.RegisterExecutor(executor.NewCodexExecutor())
|
||||
s.coreManager.RegisterExecutor(executor.NewCodexExecutor(s.cfg))
|
||||
case "qwen":
|
||||
s.coreManager.RegisterExecutor(executor.NewQwenExecutor())
|
||||
s.coreManager.RegisterExecutor(executor.NewQwenExecutor(s.cfg))
|
||||
default:
|
||||
s.coreManager.RegisterExecutor(executor.NewOpenAICompatExecutor("openai-compatibility"))
|
||||
s.coreManager.RegisterExecutor(executor.NewOpenAICompatExecutor("openai-compatibility", s.cfg))
|
||||
}
|
||||
|
||||
// Preserve existing temporal fields
|
||||
@@ -316,9 +318,9 @@ func (s *Service) syncCoreAuthFromAuths(ctx context.Context, auths []*coreauth.A
|
||||
// Ensure model registry reflects core auth identity
|
||||
s.registerModelsForAuth(a)
|
||||
if _, ok := s.coreManager.GetByID(a.ID); ok {
|
||||
s.coreManager.Update(ctx, a)
|
||||
_, _ = s.coreManager.Update(ctx, a)
|
||||
} else {
|
||||
s.coreManager.Register(ctx, a)
|
||||
_, _ = s.coreManager.Register(ctx, a)
|
||||
}
|
||||
}
|
||||
// Disable removed auths
|
||||
@@ -333,7 +335,7 @@ func (s *Service) syncCoreAuthFromAuths(ctx context.Context, auths []*coreauth.A
|
||||
stored.Status = coreauth.StatusDisabled
|
||||
// Unregister from model registry when disabled
|
||||
GlobalModelRegistry().UnregisterClient(stored.ID)
|
||||
s.coreManager.Update(ctx, stored)
|
||||
_, _ = s.coreManager.Update(ctx, stored)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user