mirror of
https://github.com/Wei-Shaw/claude-relay-service.git
synced 2026-01-23 00:53:33 +00:00
fix: 修复gemini转发代理ip未使用的问题
This commit is contained in:
@@ -110,11 +110,32 @@ setInterval(
|
||||
10 * 60 * 1000
|
||||
)
|
||||
|
||||
// 创建 OAuth2 客户端
|
||||
function createOAuth2Client(redirectUri = null) {
|
||||
// 创建 OAuth2 客户端(支持代理配置)
|
||||
function createOAuth2Client(redirectUri = null, proxyConfig = null) {
|
||||
// 如果没有提供 redirectUri,使用默认值
|
||||
const uri = redirectUri || 'http://localhost:45462'
|
||||
return new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, uri)
|
||||
|
||||
// 准备客户端选项
|
||||
const clientOptions = {
|
||||
clientId: OAUTH_CLIENT_ID,
|
||||
clientSecret: OAUTH_CLIENT_SECRET,
|
||||
redirectUri: uri
|
||||
}
|
||||
|
||||
// 如果有代理配置,设置 transporterOptions
|
||||
if (proxyConfig) {
|
||||
const proxyAgent = ProxyHelper.createProxyAgent(proxyConfig)
|
||||
if (proxyAgent) {
|
||||
// 通过 transporterOptions 传递代理配置给底层的 Gaxios
|
||||
clientOptions.transporterOptions = {
|
||||
agent: proxyAgent,
|
||||
httpsAgent: proxyAgent
|
||||
}
|
||||
logger.debug('Created OAuth2Client with proxy configuration')
|
||||
}
|
||||
}
|
||||
|
||||
return new OAuth2Client(clientOptions)
|
||||
}
|
||||
|
||||
// 生成授权 URL (支持 PKCE)
|
||||
@@ -197,11 +218,25 @@ async function pollAuthorizationStatus(sessionId, maxAttempts = 60, interval = 2
|
||||
}
|
||||
}
|
||||
|
||||
// 交换授权码获取 tokens (支持 PKCE)
|
||||
async function exchangeCodeForTokens(code, redirectUri = null, codeVerifier = null) {
|
||||
const oAuth2Client = createOAuth2Client(redirectUri)
|
||||
|
||||
// 交换授权码获取 tokens (支持 PKCE 和代理)
|
||||
async function exchangeCodeForTokens(
|
||||
code,
|
||||
redirectUri = null,
|
||||
codeVerifier = null,
|
||||
proxyConfig = null
|
||||
) {
|
||||
try {
|
||||
// 创建带代理配置的 OAuth2Client
|
||||
const oAuth2Client = createOAuth2Client(redirectUri, proxyConfig)
|
||||
|
||||
if (proxyConfig) {
|
||||
logger.info(
|
||||
`🌐 Using proxy for Gemini token exchange: ${ProxyHelper.getProxyDescription(proxyConfig)}`
|
||||
)
|
||||
} else {
|
||||
logger.debug('🌐 No proxy configured for Gemini token exchange')
|
||||
}
|
||||
|
||||
const tokenParams = {
|
||||
code,
|
||||
redirect_uri: redirectUri
|
||||
@@ -230,7 +265,8 @@ async function exchangeCodeForTokens(code, redirectUri = null, codeVerifier = nu
|
||||
|
||||
// 刷新访问令牌
|
||||
async function refreshAccessToken(refreshToken, proxyConfig = null) {
|
||||
const oAuth2Client = createOAuth2Client()
|
||||
// 创建带代理配置的 OAuth2Client
|
||||
const oAuth2Client = createOAuth2Client(null, proxyConfig)
|
||||
|
||||
try {
|
||||
// 设置 refresh_token
|
||||
@@ -238,13 +274,7 @@ async function refreshAccessToken(refreshToken, proxyConfig = null) {
|
||||
refresh_token: refreshToken
|
||||
})
|
||||
|
||||
// 配置代理(如果提供)
|
||||
const proxyAgent = ProxyHelper.createProxyAgent(proxyConfig)
|
||||
if (proxyAgent) {
|
||||
// Google Auth Library 使用 Axios,可以通过 transportOptions 配置代理
|
||||
oAuth2Client.transportOptions = {
|
||||
httpsAgent: proxyAgent
|
||||
}
|
||||
if (proxyConfig) {
|
||||
logger.info(
|
||||
`🔄 Using proxy for Gemini token refresh: ${ProxyHelper.maskProxyInfo(proxyConfig)}`
|
||||
)
|
||||
@@ -1187,7 +1217,8 @@ async function generateContent(
|
||||
requestData,
|
||||
userPromptId,
|
||||
projectId = null,
|
||||
sessionId = null
|
||||
sessionId = null,
|
||||
proxyConfig = null
|
||||
) {
|
||||
const axios = require('axios')
|
||||
const CODE_ASSIST_ENDPOINT = 'https://cloudcode-pa.googleapis.com'
|
||||
@@ -1224,6 +1255,17 @@ async function generateContent(
|
||||
timeout: 60000 // 生成内容可能需要更长时间
|
||||
}
|
||||
|
||||
// 添加代理配置
|
||||
const proxyAgent = ProxyHelper.createProxyAgent(proxyConfig)
|
||||
if (proxyAgent) {
|
||||
axiosConfig.httpsAgent = proxyAgent
|
||||
logger.info(
|
||||
`🌐 Using proxy for Gemini generateContent: ${ProxyHelper.getProxyDescription(proxyConfig)}`
|
||||
)
|
||||
} else {
|
||||
logger.debug('🌐 No proxy configured for Gemini generateContent')
|
||||
}
|
||||
|
||||
const response = await axios(axiosConfig)
|
||||
|
||||
logger.info('✅ generateContent API调用成功')
|
||||
@@ -1237,7 +1279,8 @@ async function generateContentStream(
|
||||
userPromptId,
|
||||
projectId = null,
|
||||
sessionId = null,
|
||||
signal = null
|
||||
signal = null,
|
||||
proxyConfig = null
|
||||
) {
|
||||
const axios = require('axios')
|
||||
const CODE_ASSIST_ENDPOINT = 'https://cloudcode-pa.googleapis.com'
|
||||
@@ -1278,6 +1321,17 @@ async function generateContentStream(
|
||||
timeout: 60000
|
||||
}
|
||||
|
||||
// 添加代理配置
|
||||
const proxyAgent = ProxyHelper.createProxyAgent(proxyConfig)
|
||||
if (proxyAgent) {
|
||||
axiosConfig.httpsAgent = proxyAgent
|
||||
logger.info(
|
||||
`🌐 Using proxy for Gemini streamGenerateContent: ${ProxyHelper.getProxyDescription(proxyConfig)}`
|
||||
)
|
||||
} else {
|
||||
logger.debug('🌐 No proxy configured for Gemini streamGenerateContent')
|
||||
}
|
||||
|
||||
// 如果提供了中止信号,添加到配置中
|
||||
if (signal) {
|
||||
axiosConfig.signal = signal
|
||||
|
||||
Reference in New Issue
Block a user