Skip to content

Commit 568f428

Browse files
author
Kerwin
committed
fix: fetch balance with proxy
1 parent c792148 commit 568f428

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

service/src/chatgpt/index.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,23 @@ async function fetchBalance() {
180180
'Authorization': `Bearer ${OPENAI_API_KEY}`,
181181
'Content-Type': 'application/json',
182182
}
183+
let socksAgent
184+
let httpsAgent
185+
if (isNotEmptyString(config.socksProxy)) {
186+
socksAgent = new SocksProxyAgent({
187+
hostname: config.socksProxy.split(':')[0],
188+
port: parseInt(config.socksProxy.split(':')[1]),
189+
userId: isNotEmptyString(config.socksAuth) ? config.socksAuth.split(':')[0] : undefined,
190+
password: isNotEmptyString(config.socksAuth) ? config.socksAuth.split(':')[1] : undefined,
191+
})
192+
}
193+
else if (isNotEmptyString(config.httpsProxy)) {
194+
httpsAgent = new HttpsProxyAgent(config.httpsProxy)
195+
}
183196

184197
try {
185198
// 获取API限额
186-
let response = await fetch(urlSubscription, { headers })
199+
let response = await fetch(urlSubscription, { agent: socksAgent === undefined ? httpsAgent : socksAgent, headers })
187200
if (!response.ok) {
188201
console.error('您的账户已被封禁,请登录OpenAI进行查看。')
189202
return
@@ -192,7 +205,7 @@ async function fetchBalance() {
192205
const totalAmount = subscriptionData.hard_limit_usd
193206

194207
// 获取已使用量
195-
response = await fetch(urlUsage, { headers })
208+
response = await fetch(urlUsage, { agent: socksAgent === undefined ? httpsAgent : socksAgent, headers })
196209
const usageData = await response.json()
197210
const totalUsage = usageData.total_usage / 100
198211

@@ -202,7 +215,8 @@ async function fetchBalance() {
202215

203216
return Promise.resolve(cachedBanlance.toFixed(3))
204217
}
205-
catch {
218+
catch (error) {
219+
global.console.error(error)
206220
return Promise.resolve('-')
207221
}
208222
}

0 commit comments

Comments
 (0)