File size: 7,162 Bytes
6bc074c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | package bootstrap
import (
"os"
"strings"
"time"
"aurora/internal/accounts"
"aurora/internal/browserfp"
"aurora/internal/chatgpt"
"aurora/internal/config"
"aurora/internal/handler"
"aurora/internal/proxy"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/joho/godotenv"
)
// App 封装应用启动所需的所有依赖
type App struct {
Router *gin.Engine
Config *config.Config
AccountPool *accounts.Pool
Cleanup func() // 服务退出时调用,停止后台协程
}
// Init 完成所有初始化逻辑,返回 App 实例
func Init() (*App, error) {
gin.SetMode(gin.ReleaseMode)
_ = godotenv.Load(".env")
browserfp.Init()
cfg := config.Load()
proxies := loadProxyList()
proxyPool := proxy.NewPool(proxies)
// ─── 加载账号 ────────────────────────────────────────────────
profiles := accounts.DefaultProfiles
var accs []*accounts.Account
// 去重 key: chatgpt_account_id (for free/puid) 或 token (for noauth/refresh)
seen := make(map[string]bool)
// 1. access_tokens.txt — 纯 access token,不可续期
for _, t := range accounts.LoadTokensFromFile("access_tokens.txt") {
// 从 access_token JWT 解析 chatgpt_account_id 用于去重
chatGPTID := accounts.ExtractChatGPTAccountID(t.Token)
key := "access:" + chatGPTID
if seen[key] {
continue
}
seen[key] = true
acct := accounts.CreateAccount(t.Token, accounts.TypeFree, profiles)
acct.TeamUserID = t.TeamID
acct.ChatGPTAccountID = chatGPTID
acct.Status = accounts.StatusActive
acct.Proxy = proxyPool.Allocate()
accs = append(accs, acct)
}
// 2. refresh_tokens.txt — 带 refresh_token,可续期
for _, t := range accounts.LoadTokensFromFile("refresh_tokens.txt") {
// 立即交换一次获取 access_token 才能解析 chatgpt_account_id
// 先用 refresh_token 本身做去重 key,避免重复交换
key := "refresh:" + t.Token
if seen[key] {
continue
}
seen[key] = true
acct := accounts.CreateAccount("", accounts.TypeFree, profiles)
acct.RefreshToken = t.Token
acct.TeamUserID = t.TeamID
acct.Proxy = proxyPool.Allocate()
// 立即交换一次获取 access_token
if exchangeRefreshToken(acct) {
// exchange 成功后从 access_token 解析 chatgpt_account_id
acct.ChatGPTAccountID = accounts.ExtractChatGPTAccountID(acct.Token)
acct.Status = accounts.StatusActive
} else {
acct.Status = accounts.StatusExpired
}
accs = append(accs, acct)
}
// 3. session_tokens.txt — ChatGPT session token,用于免费账号续期
for _, t := range accounts.LoadTokensFromFile("session_tokens.txt") {
key := "session:" + t.Token
if seen[key] {
continue
}
seen[key] = true
acct := accounts.CreateAccount("", accounts.TypeFree, profiles)
acct.SessionToken = t.Token
acct.Proxy = proxyPool.Allocate()
// 立即交换一次获取 access_token
if exchangeSessionToken(acct) {
// exchange 成功后从 access_token 解析 chatgpt_account_id
acct.ChatGPTAccountID = accounts.ExtractChatGPTAccountID(acct.Token)
acct.Status = accounts.StatusActive
} else {
acct.Status = accounts.StatusExpired
}
accs = append(accs, acct)
}
// 4. free_tokens.txt — 设备 UUID
for _, t := range accounts.LoadTokensFromFile("free_tokens.txt") {
key := "free:" + t.Token
if seen[key] {
continue
}
seen[key] = true
acct := accounts.CreateAccount(t.Token, accounts.TypeNoAuth, profiles)
acct.Proxy = proxyPool.Allocate()
acct.Status = accounts.StatusActive
accs = append(accs, acct)
}
// 5. FREE_ACCOUNTS — 自动生成 UUID 账号
if cfg.FreeAccounts {
for i := 0; i < cfg.FreeAccountsNum; i++ {
uid := uuid.NewString()
// UUID 设备号天然唯一,不需要去重
acct := accounts.CreateAccount(uid, accounts.TypeNoAuth, profiles)
acct.Proxy = proxyPool.Allocate()
acct.Status = accounts.StatusActive
accs = append(accs, acct)
}
}
// 初始化 TLS Client
for _, acct := range accs {
_ = acct.InitClient()
}
accountPool := accounts.NewPool(accs)
// 启动健康检查(每 10 分钟续期过期 token)
renewFn := func(acct *accounts.Account) bool {
if acct.RefreshToken != "" {
return exchangeRefreshToken(acct)
}
if acct.SessionToken != "" {
return exchangeSessionToken(acct)
}
return false
}
stopHealthCheck := accountPool.StartHealthCheck(10*time.Minute, renewFn)
stopTempGC := accountPool.StartTempAccountGC(10*time.Minute, 1*time.Minute)
// 注册路由
router := handler.RegisterRouter(accountPool, &cfg)
return &App{
Router: router,
Config: &cfg,
AccountPool: accountPool,
Cleanup: func() {
stopHealthCheck()
stopTempGC()
},
}, nil
}
// exchangeRefreshToken 用 refresh_token 换 access_token,使用账号自身的 Client(已绑定代理)
func exchangeRefreshToken(acct *accounts.Account) bool {
if acct.RefreshToken == "" {
return false
}
if acct.Client == nil {
_ = acct.InitClient()
}
result, _, err := chatgpt.GETTokenForRefreshToken(acct.Client, acct.RefreshToken, "")
if err != nil {
return false
}
// 优先尝试 struct 形式 (*OpenAIAccessTokenWithSession 等)
if r, ok := result.(interface{ GetAccessToken() string }); ok {
if at := r.GetAccessToken(); at != "" {
acct.Token = at
return true
}
}
// fallback: map[string]interface{} (旧 API 直接返回 map)
if data, ok := result.(map[string]interface{}); ok {
if accessToken, ok := data["access_token"].(string); ok && accessToken != "" {
acct.Token = accessToken
return true
}
}
return false
}
// exchangeSessionToken 用 session_token 换 access_token,使用账号自身的 Client(已绑定代理)
func exchangeSessionToken(acct *accounts.Account) bool {
if acct.SessionToken == "" {
return false
}
if acct.Client == nil {
_ = acct.InitClient()
}
result, _, err := chatgpt.GETTokenForSessionToken(acct.Client, acct.SessionToken, "")
if err != nil {
return false
}
// 优先尝试 struct 形式 (*OpenAIAccessTokenWithSession)
if r, ok := result.(interface{ GetAccessToken() string }); ok {
if at := r.GetAccessToken(); at != "" {
acct.Token = at
return true
}
}
// fallback: map[string]interface{}
if data, ok := result.(map[string]interface{}); ok {
if accessToken, ok := data["accessToken"].(string); ok && accessToken != "" {
acct.Token = accessToken
return true
}
}
return false
}
// loadProxyList 从 proxies.txt / PROXY_URL / http_proxy 加载代理列表
func loadProxyList() []string {
var proxies []string
proxyUrl := os.Getenv("PROXY_URL")
if proxyUrl != "" {
proxies = append(proxies, proxyUrl)
}
if _, err := os.Stat("proxies.txt"); err == nil {
data, err := os.ReadFile("proxies.txt")
if err == nil {
lines := string(data)
for _, line := range strings.Split(lines, "\n") {
line = strings.TrimSpace(line)
if line != "" {
proxies = append(proxies, line)
}
}
}
}
if len(proxies) == 0 {
proxy := os.Getenv("http_proxy")
if proxy != "" {
proxies = append(proxies, proxy)
}
}
return proxies
}
|