mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 13:00:52 +08:00
refactor(client): Improve auth file handling and client lifecycle
This commit is contained in:
@@ -172,6 +172,7 @@ func NewGeminiWebClient(cfg *config.Config, ts *gemini.GeminiWebTokenStorage, to
|
|||||||
go client.backgroundInitRetry()
|
go client.backgroundInitRetry()
|
||||||
} else {
|
} else {
|
||||||
client.cookieRotationStarted = true
|
client.cookieRotationStarted = true
|
||||||
|
client.registerModelsOnce()
|
||||||
// Persist immediately once after successful init to capture fresh cookies
|
// Persist immediately once after successful init to capture fresh cookies
|
||||||
_ = client.SaveTokenToFile()
|
_ = client.SaveTokenToFile()
|
||||||
client.startCookiePersist()
|
client.startCookiePersist()
|
||||||
|
|||||||
@@ -70,12 +70,12 @@ func NewQwenClient(cfg *config.Config, ts *qwen.QwenTokenStorage, tokenFilePath
|
|||||||
|
|
||||||
// If created with a known token file path, record it.
|
// If created with a known token file path, record it.
|
||||||
if len(tokenFilePath) > 0 && tokenFilePath[0] != "" {
|
if len(tokenFilePath) > 0 && tokenFilePath[0] != "" {
|
||||||
client.tokenFilePath = tokenFilePath[0]
|
client.tokenFilePath = filepath.Clean(tokenFilePath[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no explicit path provided but email exists, derive the canonical path.
|
// If no explicit path provided but email exists, derive the canonical path.
|
||||||
if client.tokenFilePath == "" && ts != nil && ts.Email != "" {
|
if client.tokenFilePath == "" && ts != nil && ts.Email != "" {
|
||||||
client.tokenFilePath = filepath.Join(cfg.AuthDir, fmt.Sprintf("qwen-%s.json", ts.Email))
|
client.tokenFilePath = filepath.Clean(filepath.Join(cfg.AuthDir, fmt.Sprintf("qwen-%s.json", ts.Email)))
|
||||||
}
|
}
|
||||||
|
|
||||||
if client.tokenFilePath != "" {
|
if client.tokenFilePath != "" {
|
||||||
@@ -102,8 +102,10 @@ func NewQwenClient(cfg *config.Config, ts *qwen.QwenTokenStorage, tokenFilePath
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if _, err := client.snapshotManager.Apply(); err != nil {
|
if applied, err := client.snapshotManager.Apply(); err != nil {
|
||||||
log.Warnf("Failed to apply Qwen cookie snapshot for %s: %v", filepath.Base(client.tokenFilePath), err)
|
log.Warnf("Failed to apply Qwen cookie snapshot for %s: %v", filepath.Base(client.tokenFilePath), err)
|
||||||
|
} else if applied {
|
||||||
|
log.Debugf("Loaded Qwen cookie snapshot: %s", filepath.Base(util.CookieSnapshotPath(client.tokenFilePath)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -515,27 +517,29 @@ func (c *QwenClient) UnregisterClientWithReason(reason interfaces.UnregisterReas
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *QwenClient) unregisterClient(reason interfaces.UnregisterReason) {
|
func (c *QwenClient) unregisterClient(reason interfaces.UnregisterReason) {
|
||||||
if c.snapshotManager == nil {
|
if c.snapshotManager != nil {
|
||||||
return
|
switch reason {
|
||||||
}
|
case interfaces.UnregisterReasonAuthFileRemoved:
|
||||||
switch reason {
|
if c.tokenFilePath != "" {
|
||||||
case interfaces.UnregisterReasonAuthFileRemoved:
|
log.Debugf("skipping Qwen snapshot flush because auth file is missing: %s", filepath.Base(c.tokenFilePath))
|
||||||
if c.tokenFilePath != "" {
|
util.RemoveCookieSnapshots(c.tokenFilePath)
|
||||||
log.Debugf("skipping Qwen snapshot flush because auth file is missing: %s", filepath.Base(c.tokenFilePath))
|
}
|
||||||
util.RemoveCookieSnapshots(c.tokenFilePath)
|
case interfaces.UnregisterReasonAuthFileUpdated:
|
||||||
}
|
if c.tokenFilePath != "" {
|
||||||
case interfaces.UnregisterReasonAuthFileUpdated:
|
log.Debugf("skipping Qwen snapshot flush because auth file was updated: %s", filepath.Base(c.tokenFilePath))
|
||||||
if c.tokenFilePath != "" {
|
util.RemoveCookieSnapshots(c.tokenFilePath)
|
||||||
log.Debugf("skipping Qwen snapshot flush because auth file was updated: %s", filepath.Base(c.tokenFilePath))
|
}
|
||||||
util.RemoveCookieSnapshots(c.tokenFilePath)
|
case interfaces.UnregisterReasonShutdown, interfaces.UnregisterReasonReload:
|
||||||
}
|
if err := c.snapshotManager.Flush(); err != nil {
|
||||||
case interfaces.UnregisterReasonShutdown, interfaces.UnregisterReasonReload:
|
log.Errorf("Failed to flush Qwen cookie snapshot to main for %s: %v", filepath.Base(c.tokenFilePath), err)
|
||||||
if err := c.snapshotManager.Flush(); err != nil {
|
}
|
||||||
log.Errorf("Failed to flush Qwen cookie snapshot to main for %s: %v", filepath.Base(c.tokenFilePath), err)
|
default:
|
||||||
}
|
if err := c.snapshotManager.Flush(); err != nil {
|
||||||
default:
|
log.Errorf("Failed to flush Qwen cookie snapshot to main for %s: %v", filepath.Base(c.tokenFilePath), err)
|
||||||
if err := c.snapshotManager.Flush(); err != nil {
|
}
|
||||||
log.Errorf("Failed to flush Qwen cookie snapshot to main for %s: %v", filepath.Base(c.tokenFilePath), err)
|
}
|
||||||
}
|
} else if c.tokenFilePath != "" && (reason == interfaces.UnregisterReasonAuthFileRemoved || reason == interfaces.UnregisterReasonAuthFileUpdated) {
|
||||||
|
util.RemoveCookieSnapshots(c.tokenFilePath)
|
||||||
}
|
}
|
||||||
|
c.ClientBase.UnregisterClient()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,7 +139,7 @@ func StartService(cfg *config.Config, configPath string) {
|
|||||||
if err = json.Unmarshal(data, &ts); err == nil {
|
if err = json.Unmarshal(data, &ts); err == nil {
|
||||||
// For each valid Qwen token, create an authenticated client.
|
// For each valid Qwen token, create an authenticated client.
|
||||||
log.Info("Initializing qwen authentication for token...")
|
log.Info("Initializing qwen authentication for token...")
|
||||||
qwenClient := client.NewQwenClient(cfg, &ts)
|
qwenClient := client.NewQwenClient(cfg, &ts, path)
|
||||||
log.Info("Authentication successful.")
|
log.Info("Authentication successful.")
|
||||||
cliClients[path] = qwenClient
|
cliClients[path] = qwenClient
|
||||||
successfulAuthCount++
|
successfulAuthCount++
|
||||||
|
|||||||
@@ -393,7 +393,7 @@ func (w *Watcher) createClientFromFile(path string, cfg *config.Config) (interfa
|
|||||||
} else if tokenType == "qwen" {
|
} else if tokenType == "qwen" {
|
||||||
var ts qwen.QwenTokenStorage
|
var ts qwen.QwenTokenStorage
|
||||||
if err = json.Unmarshal(data, &ts); err == nil {
|
if err = json.Unmarshal(data, &ts); err == nil {
|
||||||
return client.NewQwenClient(cfg, &ts), nil
|
return client.NewQwenClient(cfg, &ts, path), nil
|
||||||
}
|
}
|
||||||
} else if tokenType == "gemini-web" {
|
} else if tokenType == "gemini-web" {
|
||||||
var ts gemini.GeminiWebTokenStorage
|
var ts gemini.GeminiWebTokenStorage
|
||||||
|
|||||||
Reference in New Issue
Block a user