feat: publish sanitized custom.97 source

This commit is contained in:
cneicy committed 2026-10-27 20:43:39 +08:00
1 parent 8e6ed0317b
commit 86c0276156
1022 files changed
+69903 -19207

No files matched your search

+28 -31
View File
@@ -1,7 +1,6 @@
package controller
import (
"fmt"
"net/http"
"strconv"
"strings"
@@ -9,6 +8,7 @@ import (
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/constant"
"github.com/QuantumNous/new-api/i18n"
"github.com/QuantumNous/new-api/logger"
"github.com/QuantumNous/new-api/model"
"github.com/QuantumNous/new-api/service"
"github.com/QuantumNous/new-api/setting"
@@ -80,7 +80,7 @@ func buildMaskedTokenResponse(token *model.Token) *tokenResponse {
maskedToken.Key = token.GetMaskedKey()
groupPool, err := token.GetGroupPool()
if err != nil {
common.SysError(fmt.Sprintf("failed to parse auto groups for token %d: %v", token.Id, err))
common.SysError(common.LogText("failed to parse auto groups for token %d: %v", token.Id, err))
groupPool = nil
}
autoGroups := make([]string, 0, len(groupPool))
@@ -123,7 +123,7 @@ func setTokenAutoGroups(c *gin.Context, token *model.Token, groups []string) boo
maxCount := setting.GetMaxTokenAutoGroups()
if len(groups) > maxCount {
common.ApiErrorI18n(c, i18n.MsgTokenAutoGroupsTooMany, map[string]any{"Max": maxCount})
common.ApiErrorT(c, "A token can select at most {{max}} Auto groups", map[string]any{"max": maxCount})
return false
}
@@ -135,12 +135,12 @@ func setTokenAutoGroups(c *gin.Context, token *model.Token, groups []string) boo
seen := make(map[string]struct{}, len(groups))
for _, group := range groups {
if _, ok := seen[group]; ok {
common.ApiErrorI18n(c, i18n.MsgTokenAutoGroupsDuplicate, map[string]any{"Group": group})
common.ApiErrorT(c, "Auto group {{group}} is duplicated", map[string]any{"group": group})
return false
}
seen[group] = struct{}{}
if !service.IsUserSelectableGroup(userGroup, group) {
common.ApiErrorI18n(c, i18n.MsgTokenAutoGroupsInvalid, map[string]any{"Group": group})
common.ApiErrorT(c, "Auto group {{group}} is unavailable or unauthorized", map[string]any{"group": group})
return false
}
}
@@ -155,12 +155,12 @@ func setTokenAutoGroups(c *gin.Context, token *model.Token, groups []string) boo
func setTokenBackupGroup(c *gin.Context, token *model.Token, groups []string) bool {
maxCount := setting.GetMaxTokenAutoGroups()
if len(groups) > maxCount {
common.ApiErrorI18n(c, i18n.MsgTokenAutoGroupsTooMany, map[string]any{"Max": maxCount})
common.ApiErrorT(c, "A token can select at most {{max}} Auto groups", map[string]any{"max": maxCount})
return false
}
for _, group := range groups {
if group == token.Group {
common.ApiErrorI18n(c, i18n.MsgTokenAutoGroupsDuplicate, map[string]any{"Group": group})
common.ApiErrorT(c, "Auto group {{group}} is duplicated", map[string]any{"group": group})
return false
}
}
@@ -174,7 +174,7 @@ func setTokenBackupGroup(c *gin.Context, token *model.Token, groups []string) bo
func setTokenGroupPool(c *gin.Context, token *model.Token, entries []model.TokenGroupEntry) bool {
maxCount := setting.GetMaxTokenAutoGroups()
if len(entries) > maxCount {
common.ApiErrorI18n(c, i18n.MsgTokenAutoGroupsTooMany, map[string]any{"Max": maxCount})
common.ApiErrorT(c, "A token can select at most {{max}} Auto groups", map[string]any{"max": maxCount})
return false
}
userGroup, err := getTokenRequestUserGroup(c)
@@ -185,16 +185,16 @@ func setTokenGroupPool(c *gin.Context, token *model.Token, entries []model.Token
seen := make(map[string]struct{}, len(entries))
for _, entry := range entries {
if entry.Group == token.Group {
common.ApiErrorI18n(c, i18n.MsgTokenAutoGroupsDuplicate, map[string]any{"Group": entry.Group})
common.ApiErrorT(c, "Auto group {{group}} is duplicated", map[string]any{"group": entry.Group})
return false
}
if _, ok := seen[entry.Group]; ok {
common.ApiErrorI18n(c, i18n.MsgTokenAutoGroupsDuplicate, map[string]any{"Group": entry.Group})
common.ApiErrorT(c, "Auto group {{group}} is duplicated", map[string]any{"group": entry.Group})
return false
}
seen[entry.Group] = struct{}{}
if !service.IsUserSelectableGroup(userGroup, entry.Group) {
common.ApiErrorI18n(c, i18n.MsgTokenAutoGroupsInvalid, map[string]any{"Group": entry.Group})
common.ApiErrorT(c, "Auto group {{group}} is unavailable or unauthorized", map[string]any{"group": entry.Group})
return false
}
}
@@ -327,7 +327,7 @@ func GetTokenUsage(c *gin.Context) {
token, err := model.GetTokenByKey(strings.TrimPrefix(tokenKey, "sk-"), false)
if err != nil {
common.SysError("failed to get token by key: " + err.Error())
common.SysError(common.LogText("failed to get token by key: %s", err.Error()))
common.ApiErrorI18n(c, i18n.MsgTokenGetInfoFailed)
return
}
@@ -373,7 +373,7 @@ func AddToken(c *gin.Context) {
token.AstraAutoSwitch = false
}
if len(token.Name) > 50 {
common.ApiErrorI18n(c, i18n.MsgTokenNameTooLong)
common.ApiErrorT(c, "Token name is too long")
return
}
params := tokenAuditParams(c)
@@ -381,12 +381,12 @@ func AddToken(c *gin.Context) {
// 非无限额度时,检查额度值是否超出有效范围
if !token.UnlimitedQuota {
if token.RemainQuota < 0 {
common.ApiErrorI18n(c, i18n.MsgTokenQuotaNegative)
common.ApiErrorT(c, "Quota value cannot be negative")
return
}
maxQuotaValue := maxTokenQuota()
if token.RemainQuota > maxQuotaValue {
common.ApiErrorI18n(c, i18n.MsgTokenQuotaExceedMax, map[string]any{"Max": maxQuotaValue})
common.ApiErrorT(c, "Quota value exceeds valid range, maximum is {{max}}", map[string]any{"max": logger.FormatQuota(maxQuotaValue)})
return
}
}
@@ -398,10 +398,7 @@ func AddToken(c *gin.Context) {
return
}
if int(count) >= maxTokens {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": fmt.Sprintf("已达到最大令牌数量限制 (%d)", maxTokens),
})
common.ApiErrorT(c, "Maximum number of tokens reached ({{max}})", map[string]any{"max": maxTokens})
return
}
if request.RoutingPolicy != nil {
@@ -436,8 +433,8 @@ func AddToken(c *gin.Context) {
}
key, err := common.GenerateKey()
if err != nil {
common.ApiErrorI18n(c, i18n.MsgTokenGenerateFailed)
common.SysLog("failed to generate token key: " + err.Error())
common.ApiErrorT(c, "Failed to generate token")
common.SysLog(common.LogText("failed to generate token key: %s", err.Error()))
return
}
cleanToken := model.Token{
@@ -509,17 +506,17 @@ func UpdateToken(c *gin.Context) {
params["id"] = token.Id
}
if len(token.Name) > 50 {
common.ApiErrorI18n(c, i18n.MsgTokenNameTooLong)
common.ApiErrorT(c, "Token name is too long")
return
}
if !token.UnlimitedQuota {
if token.RemainQuota < 0 {
common.ApiErrorI18n(c, i18n.MsgTokenQuotaNegative)
common.ApiErrorT(c, "Quota value cannot be negative")
return
}
maxQuotaValue := maxTokenQuota()
if token.RemainQuota > maxQuotaValue {
common.ApiErrorI18n(c, i18n.MsgTokenQuotaExceedMax, map[string]any{"Max": maxQuotaValue})
common.ApiErrorT(c, "Quota value exceeds valid range, maximum is {{max}}", map[string]any{"max": logger.FormatQuota(maxQuotaValue)})
return
}
}
@@ -532,11 +529,11 @@ func UpdateToken(c *gin.Context) {
previous := *cleanToken
if token.Status == common.TokenStatusEnabled {
if cleanToken.Status == common.TokenStatusExpired && cleanToken.ExpiredTime <= common.GetTimestamp() && cleanToken.ExpiredTime != -1 {
common.ApiErrorI18n(c, i18n.MsgTokenExpiredCannotEnable)
common.ApiErrorT(c, "Token has expired and cannot be enabled. Please modify the expiration time or set it to never expire")
return
}
if cleanToken.Status == common.TokenStatusExhausted && cleanToken.RemainQuota <= 0 && !cleanToken.UnlimitedQuota {
common.ApiErrorI18n(c, i18n.MsgTokenExhaustedCannotEable)
common.ApiErrorT(c, "Token quota is exhausted and cannot be enabled. Please modify the remaining quota or set it to unlimited")
return
}
}
@@ -632,12 +629,12 @@ type TokenBatch struct {
func DeleteTokenBatch(c *gin.Context) {
tokenBatch := TokenBatch{}
if err := c.ShouldBindJSON(&tokenBatch); err != nil {
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
common.ApiErrorT(c, "Invalid parameters")
return
}
params := tokenBatchAuditParams(c, tokenBatch.Ids)
if len(tokenBatch.Ids) == 0 {
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
common.ApiErrorT(c, "Invalid parameters")
return
}
userId := c.GetInt("id")
@@ -658,16 +655,16 @@ func DeleteTokenBatch(c *gin.Context) {
func GetTokenKeysBatch(c *gin.Context) {
tokenBatch := TokenBatch{}
if err := c.ShouldBindJSON(&tokenBatch); err != nil {
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
common.ApiErrorT(c, "Invalid parameters")
return
}
params := tokenBatchAuditParams(c, tokenBatch.Ids)
if len(tokenBatch.Ids) == 0 {
common.ApiErrorI18n(c, i18n.MsgInvalidParams)
common.ApiErrorT(c, "Invalid parameters")
return
}
if len(tokenBatch.Ids) > 100 {
common.ApiErrorI18n(c, i18n.MsgBatchTooMany, map[string]any{"Max": 100})
common.ApiErrorT(c, "Too many items in batch request, maximum is {{max}}", map[string]any{"max": 100})
return
}
userId := c.GetInt("id")