Spaces:
Paused
Paused
Upload config.go
Browse files- common/config/config.go +111 -0
common/config/config.go
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
package config
|
| 2 |
|
| 3 |
import (
|
|
|
|
| 4 |
"genspark2api/common/env"
|
|
|
|
| 5 |
"os"
|
| 6 |
"strings"
|
|
|
|
| 7 |
"time"
|
| 8 |
)
|
| 9 |
|
|
@@ -14,7 +17,9 @@ var GSCookies = strings.Split(os.Getenv("GS_COOKIE"), ",")
|
|
| 14 |
var AutoDelChat = env.Int("AUTO_DEL_CHAT", 0)
|
| 15 |
var ProxyUrl = env.String("PROXY_URL", "")
|
| 16 |
var ModelChatMapStr = env.String("MODEL_CHAT_MAP", "")
|
|
|
|
| 17 |
var ModelChatMap = make(map[string]string)
|
|
|
|
| 18 |
|
| 19 |
var AllDialogRecordEnable = os.Getenv("ALL_DIALOG_RECORD_ENABLE")
|
| 20 |
var RequestOutTime = os.Getenv("REQUEST_OUT_TIME")
|
|
@@ -32,3 +37,109 @@ var (
|
|
| 32 |
RequestRateLimitNum = env.Int("REQUEST_RATE_LIMIT", 60)
|
| 33 |
RequestRateLimitDuration int64 = 1 * 60
|
| 34 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
package config
|
| 2 |
|
| 3 |
import (
|
| 4 |
+
"errors"
|
| 5 |
"genspark2api/common/env"
|
| 6 |
+
"math/rand"
|
| 7 |
"os"
|
| 8 |
"strings"
|
| 9 |
+
"sync"
|
| 10 |
"time"
|
| 11 |
)
|
| 12 |
|
|
|
|
| 17 |
var AutoDelChat = env.Int("AUTO_DEL_CHAT", 0)
|
| 18 |
var ProxyUrl = env.String("PROXY_URL", "")
|
| 19 |
var ModelChatMapStr = env.String("MODEL_CHAT_MAP", "")
|
| 20 |
+
var AutoModelChatMapType = env.Int("AUTO_MODEL_CHAT_MAP_TYPE", 1)
|
| 21 |
var ModelChatMap = make(map[string]string)
|
| 22 |
+
var GlobalSessionManager *SessionManager
|
| 23 |
|
| 24 |
var AllDialogRecordEnable = os.Getenv("ALL_DIALOG_RECORD_ENABLE")
|
| 25 |
var RequestOutTime = os.Getenv("REQUEST_OUT_TIME")
|
|
|
|
| 37 |
RequestRateLimitNum = env.Int("REQUEST_RATE_LIMIT", 60)
|
| 38 |
RequestRateLimitDuration int64 = 1 * 60
|
| 39 |
)
|
| 40 |
+
|
| 41 |
+
type CookieManager struct {
|
| 42 |
+
Cookies []string
|
| 43 |
+
currentIndex int
|
| 44 |
+
mu sync.Mutex
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
func NewCookieManager() *CookieManager {
|
| 48 |
+
cookies := strings.Split(os.Getenv("GS_COOKIE"), ",")
|
| 49 |
+
// 过滤空字符串
|
| 50 |
+
var validCookies []string
|
| 51 |
+
for _, cookie := range cookies {
|
| 52 |
+
if strings.TrimSpace(cookie) != "" {
|
| 53 |
+
validCookies = append(validCookies, cookie)
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
return &CookieManager{
|
| 58 |
+
Cookies: validCookies,
|
| 59 |
+
currentIndex: 0,
|
| 60 |
+
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
func (cm *CookieManager) GetNextCookie() (string, error) {
|
| 64 |
+
cm.mu.Lock()
|
| 65 |
+
defer cm.mu.Unlock()
|
| 66 |
+
|
| 67 |
+
if len(cm.Cookies) == 0 {
|
| 68 |
+
return "", errors.New("no cookies available")
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
cm.currentIndex = (cm.currentIndex + 1) % len(cm.Cookies)
|
| 72 |
+
return cm.Cookies[cm.currentIndex], nil
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
func (cm *CookieManager) GetRandomCookie() (string, error) {
|
| 76 |
+
cm.mu.Lock()
|
| 77 |
+
defer cm.mu.Unlock()
|
| 78 |
+
|
| 79 |
+
if len(cm.Cookies) == 0 {
|
| 80 |
+
return "", errors.New("no cookies available")
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
// 生成随机索引
|
| 84 |
+
randomIndex := rand.Intn(len(cm.Cookies))
|
| 85 |
+
// 更新当前索引
|
| 86 |
+
cm.currentIndex = randomIndex
|
| 87 |
+
|
| 88 |
+
return cm.Cookies[randomIndex], nil
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
// SessionKey 定义复合键结构
|
| 92 |
+
type SessionKey struct {
|
| 93 |
+
Cookie string
|
| 94 |
+
Model string
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
// SessionManager 会话管理器
|
| 98 |
+
type SessionManager struct {
|
| 99 |
+
sessions map[SessionKey]string
|
| 100 |
+
mutex sync.RWMutex
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
// NewSessionManager 创建新的会话管理器
|
| 104 |
+
func NewSessionManager() *SessionManager {
|
| 105 |
+
return &SessionManager{
|
| 106 |
+
sessions: make(map[SessionKey]string),
|
| 107 |
+
}
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
// AddSession 添加会话记录(写操作,需要写锁)
|
| 111 |
+
func (sm *SessionManager) AddSession(cookie string, model string, chatID string) {
|
| 112 |
+
sm.mutex.Lock()
|
| 113 |
+
defer sm.mutex.Unlock()
|
| 114 |
+
|
| 115 |
+
key := SessionKey{
|
| 116 |
+
Cookie: cookie,
|
| 117 |
+
Model: model,
|
| 118 |
+
}
|
| 119 |
+
sm.sessions[key] = chatID
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// GetChatID 获取会话ID(读操作,使用读锁)
|
| 123 |
+
func (sm *SessionManager) GetChatID(cookie string, model string) (string, bool) {
|
| 124 |
+
sm.mutex.RLock()
|
| 125 |
+
defer sm.mutex.RUnlock()
|
| 126 |
+
|
| 127 |
+
key := SessionKey{
|
| 128 |
+
Cookie: cookie,
|
| 129 |
+
Model: model,
|
| 130 |
+
}
|
| 131 |
+
chatID, exists := sm.sessions[key]
|
| 132 |
+
return chatID, exists
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
// DeleteSession 删除会话记录(写操作,需要写锁)
|
| 136 |
+
func (sm *SessionManager) DeleteSession(cookie string, model string) {
|
| 137 |
+
sm.mutex.Lock()
|
| 138 |
+
defer sm.mutex.Unlock()
|
| 139 |
+
|
| 140 |
+
key := SessionKey{
|
| 141 |
+
Cookie: cookie,
|
| 142 |
+
Model: model,
|
| 143 |
+
}
|
| 144 |
+
delete(sm.sessions, key)
|
| 145 |
+
}
|