feat: sync deployed v1.0.0-rc.38-custom.42

Sync production source including routing, billing, Astra detection and public uptime. Update custom build instructions and license packaging.
This commit is contained in:
cneicy committed 2026-09-26 04:49:11 +08:00
1 parent aa6c3f50f4
commit ceddfd3d6b
828 files changed
+122253 -13702

No files matched your search

+80 -22
View File
@@ -1,6 +1,7 @@
package model
import (
"encoding/json"
"errors"
"fmt"
"strings"
@@ -12,34 +13,78 @@ import (
)
type Token struct {
Id int `json:"id"`
UserId int `json:"user_id" gorm:"index"`
Key string `json:"key" gorm:"type:varchar(128);uniqueIndex"`
Status int `json:"status" gorm:"default:1"`
Name string `json:"name" gorm:"index" `
CreatedTime int64 `json:"created_time" gorm:"bigint"`
AccessedTime int64 `json:"accessed_time" gorm:"bigint"`
ExpiredTime int64 `json:"expired_time" gorm:"bigint;default:-1"` // -1 means never expired
RemainQuota int `json:"remain_quota" gorm:"default:0"`
UnlimitedQuota bool `json:"unlimited_quota"`
ModelLimitsEnabled bool `json:"model_limits_enabled"`
ModelLimits string `json:"model_limits" gorm:"type:text"`
AllowIps *string `json:"allow_ips" gorm:"default:''"`
UsedQuota int `json:"used_quota" gorm:"default:0"` // used quota
Group string `json:"group" gorm:"default:''"`
CrossGroupRetry bool `json:"cross_group_retry"` // 跨分组重试,仅auto分组有效
AutoGroups string `json:"-" gorm:"type:text"`
DeletedAt gorm.DeletedAt `gorm:"index"`
Id int `json:"id"`
UserId int `json:"user_id" gorm:"index"`
Key string `json:"key" gorm:"type:varchar(128);uniqueIndex"`
Status int `json:"status" gorm:"default:1"`
Name string `json:"name" gorm:"index" `
CreatedTime int64 `json:"created_time" gorm:"bigint"`
AccessedTime int64 `json:"accessed_time" gorm:"bigint"`
ExpiredTime int64 `json:"expired_time" gorm:"bigint;default:-1"` // -1 means never expired
RemainQuota int `json:"remain_quota" gorm:"default:0"`
UnlimitedQuota bool `json:"unlimited_quota"`
ModelLimitsEnabled bool `json:"model_limits_enabled"`
ModelLimits string `json:"model_limits" gorm:"type:text"`
AllowIps *string `json:"allow_ips" gorm:"default:''"`
UsedQuota int `json:"used_quota" gorm:"default:0"` // used quota
Group string `json:"group" gorm:"default:''"`
CrossGroupRetry bool `json:"cross_group_retry"` // 跨分组重试,仅auto分组有效
AstraDetectionEnabled bool `json:"astra_detection_enabled" gorm:"not null;default:false"`
AstraAutoSwitch bool `json:"astra_auto_switch" gorm:"not null;default:false"`
AutoGroups string `json:"-" gorm:"type:text"`
RoutingPolicy string `json:"-" gorm:"type:text"`
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (token *Token) GetAutoGroups() ([]string, error) {
// TokenGroupEntry is one group of a token's routing pool. Lower Priority values
// are preferred, and entries sharing a Priority are load-balanced together.
type TokenGroupEntry struct {
Group string `json:"group"`
Priority int `json:"priority"`
}
// GetGroupPool returns the token's routing pool in stored order. Stored entries
// may be plain group names, in which case the array position is the priority.
func (token *Token) GetGroupPool() ([]TokenGroupEntry, error) {
if token.AutoGroups == "" {
return nil, nil
}
var groups []string
if err := common.UnmarshalJsonStr(token.AutoGroups, &groups); err != nil {
var raw []json.RawMessage
if err := common.UnmarshalJsonStr(token.AutoGroups, &raw); err != nil {
return nil, err
}
entries := make([]TokenGroupEntry, 0, len(raw))
for index, item := range raw {
var name string
if err := json.Unmarshal(item, &name); err == nil {
if name != "" {
entries = append(entries, TokenGroupEntry{Group: name, Priority: index})
}
continue
}
var entry TokenGroupEntry
if err := json.Unmarshal(item, &entry); err != nil {
return nil, err
}
if entry.Group != "" {
entries = append(entries, entry)
}
}
return entries, nil
}
func (token *Token) GetAutoGroups() ([]string, error) {
entries, err := token.GetGroupPool()
if err != nil {
return nil, err
}
if len(entries) == 0 {
return nil, nil
}
groups := make([]string, 0, len(entries))
for _, entry := range entries {
groups = append(groups, entry.Group)
}
return groups, nil
}
@@ -56,6 +101,19 @@ func (token *Token) SetAutoGroups(groups []string) error {
return nil
}
func (token *Token) SetGroupPool(entries []TokenGroupEntry) error {
if len(entries) == 0 {
token.AutoGroups = ""
return nil
}
data, err := common.Marshal(entries)
if err != nil {
return err
}
token.AutoGroups = string(data)
return nil
}
func (token *Token) Clean() {
token.Key = ""
}
@@ -313,7 +371,7 @@ func (token *Token) Update() (err error) {
common.SysLog("failed to invalidate token cache before update: " + cacheErr.Error())
}
return DB.Model(token).Select("name", "status", "expired_time", "remain_quota", "unlimited_quota",
"model_limits_enabled", "model_limits", "allow_ips", "group", "cross_group_retry", "auto_groups").Updates(token).Error
"model_limits_enabled", "model_limits", "allow_ips", "group", "cross_group_retry", "astra_auto_switch", "astra_detection_enabled", "auto_groups", "routing_policy").Updates(token).Error
}
func (token *Token) SelectUpdate() (err error) {