mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-09-06 08:02:22 +08:00
feat(auth): add support for credential weight parsing in auth file handling
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/auth/codex"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/credentialweight"
|
||||
"github.com/router-for-me/CLIProxyAPI/v7/internal/registry"
|
||||
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@@ -260,6 +261,20 @@ func (h *Handler) listAuthFilesFromDisk(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if wv := gjson.GetBytes(data, coreauth.AttributeWeight); wv.Exists() {
|
||||
var rawWeight string
|
||||
switch wv.Type {
|
||||
case gjson.Number:
|
||||
rawWeight = wv.Raw
|
||||
case gjson.String:
|
||||
rawWeight = wv.String()
|
||||
}
|
||||
if rawWeight != "" {
|
||||
if weight, errWeight := credentialweight.ParseString(rawWeight); errWeight == nil {
|
||||
fileData[coreauth.AttributeWeight] = weight
|
||||
}
|
||||
}
|
||||
}
|
||||
if nv := gjson.GetBytes(data, "note"); nv.Exists() && nv.Type == gjson.String {
|
||||
if trimmed := strings.TrimSpace(nv.String()); trimmed != "" {
|
||||
fileData["note"] = trimmed
|
||||
@@ -403,12 +418,34 @@ func (h *Handler) buildAuthFileEntryLocked(auth *coreauth.Auth) gin.H {
|
||||
}
|
||||
}
|
||||
}
|
||||
if weight, ok := authWeightValue(auth); ok {
|
||||
entry[coreauth.AttributeWeight] = weight
|
||||
}
|
||||
if websockets, ok := authWebsocketsValue(auth); ok {
|
||||
entry["websockets"] = websockets
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
func authWeightValue(auth *coreauth.Auth) (int64, bool) {
|
||||
if auth == nil {
|
||||
return 0, false
|
||||
}
|
||||
if rawWeight := strings.TrimSpace(authAttribute(auth, coreauth.AttributeWeight)); rawWeight != "" {
|
||||
weight, errWeight := credentialweight.ParseString(rawWeight)
|
||||
return weight, errWeight == nil
|
||||
}
|
||||
if auth.Metadata == nil {
|
||||
return 0, false
|
||||
}
|
||||
rawWeight, ok := auth.Metadata[coreauth.AttributeWeight]
|
||||
if !ok || rawWeight == nil {
|
||||
return 0, false
|
||||
}
|
||||
weight, errWeight := credentialweight.ParseValue(rawWeight)
|
||||
return weight, errWeight == nil
|
||||
}
|
||||
|
||||
func authWebsocketsValue(auth *coreauth.Auth) (bool, bool) {
|
||||
if auth == nil {
|
||||
return false, false
|
||||
|
||||
Reference in New Issue
Block a user