[GH-ISSUE #116] 为 Gemini CLI OAuth 提供者添加原生 HTTPS 代理支持 #94

Closed
opened 2026-02-27 07:17:57 +03:00 by kerem · 4 comments
Owner

Originally created by @chchuj on GitHub (Dec 19, 2025).
Original GitHub issue: https://github.com/justlovemaki/AIClient-2-API/issues/116

功能请求:为 Gemini CLI OAuth 提供者添加原生 HTTPS 代理支持

问题描述

在需要通过代理才能访问 Google 服务的网络环境中(例如中国大陆用户)使用 gemini-cli-oauth 提供者时,API 请求会失败并报以下错误:

[API] Error calling generateContent: undefined request to https://cloudcode-pa.googleapis.com/v1internal:generateContent failed, reason:

根本原因src/gemini/gemini-core.js 中的 GeminiApiService 类使用了自定义的 https.Agent,该 Agent 不会自动读取 HTTPS_PROXYHTTP_PROXY 环境变量。

期望行为

服务应该能够:

  1. 自动检测并使用 HTTPS_PROXY / HTTP_PROXY 环境变量。
  2. 可选:提供命令行参数(如 --proxy <url>)来显式配置代理。

当前解决方法(手动打补丁)

1. 安装 https-proxy-agent

npm install https-proxy-agent

2. 修改 src/gemini/gemini-core.js

import { HttpsProxyAgent } from 'https-proxy-agent';

// 在 GeminiApiService 构造函数内部:
constructor(config) {
    const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
    let authAgent = httpsAgent; // 默认 agent

    if (proxyUrl) {
        console.log(`[Gemini] Detected proxy configuration: ${proxyUrl}`);
        authAgent = new HttpsProxyAgent(proxyUrl, {
            keepAlive: true,
            timeout: 120000
        });
    }

    this.authClient = new OAuth2Client({
        // ...
        transporterOptions: {
            agent: authAgent, // 使用支持代理的 agent
        },
    });
    // ...
}

3. 在项目根目录创建 .env 文件:

HTTPS_PROXY=http://127.0.0.1:7891

建议实现

  • https-proxy-agent 添加为项目依赖。
  • 从环境变量(HTTPS_PROXYHTTP_PROXY)或新增的 CLI 参数(--proxy)中读取代理设置。
  • 将代理 agent 应用于 gemini-core.jsOAuth2ClienttransporterOptions
  • 建议同时考虑为 claude-core.jsopenai-core.jsqwen-core.js 等其他核心模块添加类似支持,因为它们可能也存在相同的问题。

环境信息

  • 操作系统:Windows 11
  • Node.js 版本:v20.x
  • AIClient-2-API 版本:最新版(2025-12-19 克隆)
Originally created by @chchuj on GitHub (Dec 19, 2025). Original GitHub issue: https://github.com/justlovemaki/AIClient-2-API/issues/116 # 功能请求:为 Gemini CLI OAuth 提供者添加原生 HTTPS 代理支持 ## 问题描述 在需要通过代理才能访问 Google 服务的网络环境中(例如中国大陆用户)使用 `gemini-cli-oauth` 提供者时,API 请求会失败并报以下错误: ``` [API] Error calling generateContent: undefined request to https://cloudcode-pa.googleapis.com/v1internal:generateContent failed, reason: ``` **根本原因**:`src/gemini/gemini-core.js` 中的 `GeminiApiService` 类使用了自定义的 `https.Agent`,该 Agent **不会自动读取** `HTTPS_PROXY` 或 `HTTP_PROXY` 环境变量。 ## 期望行为 服务应该能够: 1. **自动检测并使用** `HTTPS_PROXY` / `HTTP_PROXY` 环境变量。 2. **可选**:提供命令行参数(如 `--proxy <url>`)来显式配置代理。 ## 当前解决方法(手动打补丁) ### 1. 安装 `https-proxy-agent`: ```bash npm install https-proxy-agent ``` ### 2. 修改 `src/gemini/gemini-core.js`: ```javascript import { HttpsProxyAgent } from 'https-proxy-agent'; // 在 GeminiApiService 构造函数内部: constructor(config) { const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY; let authAgent = httpsAgent; // 默认 agent if (proxyUrl) { console.log(`[Gemini] Detected proxy configuration: ${proxyUrl}`); authAgent = new HttpsProxyAgent(proxyUrl, { keepAlive: true, timeout: 120000 }); } this.authClient = new OAuth2Client({ // ... transporterOptions: { agent: authAgent, // 使用支持代理的 agent }, }); // ... } ``` ### 3. 在项目根目录创建 `.env` 文件: ``` HTTPS_PROXY=http://127.0.0.1:7891 ``` ## 建议实现 - 将 `https-proxy-agent` 添加为项目依赖。 - 从环境变量(`HTTPS_PROXY`、`HTTP_PROXY`)或新增的 CLI 参数(`--proxy`)中读取代理设置。 - 将代理 agent 应用于 `gemini-core.js` 中 `OAuth2Client` 的 `transporterOptions`。 - 建议同时考虑为 `claude-core.js`、`openai-core.js`、`qwen-core.js` 等其他核心模块添加类似支持,因为它们可能也存在相同的问题。 ## 环境信息 - 操作系统:Windows 11 - Node.js 版本:v20.x - AIClient-2-API 版本:最新版(2025-12-19 克隆)
kerem closed this issue 2026-02-27 07:17:58 +03:00
Author
Owner

@justlovemaki commented on GitHub (Dec 19, 2025):

不考虑这种实现方式了,之前测过有些小问题,会影响其他不用代理的提供商访问网络。
最新版已支持配置提供商自定义URL,替换为代理URL即可。

<!-- gh-comment-id:3674430122 --> @justlovemaki commented on GitHub (Dec 19, 2025): 不考虑这种实现方式了,之前测过有些小问题,会影响其他不用代理的提供商访问网络。 最新版已支持配置提供商自定义URL,替换为代理URL即可。
Author
Owner

@lgc0313 commented on GitHub (Dec 21, 2025):

@justlovemaki 提供商自定义URL是在哪里配置?页面上只看到 Gemini Base URL (选填)

<!-- gh-comment-id:3678842257 --> @lgc0313 commented on GitHub (Dec 21, 2025): @justlovemaki 提供商自定义URL是在哪里配置?页面上只看到 `Gemini Base URL (选填)`
Author
Owner

@justlovemaki commented on GitHub (Dec 21, 2025):

@justlovemaki 提供商自定义URL是在哪里配置?页面上只看到 Gemini Base URL (选填)

就是这个,填自定义的

<!-- gh-comment-id:3678879823 --> @justlovemaki commented on GitHub (Dec 21, 2025): > [@justlovemaki](https://github.com/justlovemaki) 提供商自定义URL是在哪里配置?页面上只看到 `Gemini Base URL (选填)` 就是这个,填自定义的
Author
Owner

@lgc0313 commented on GitHub (Dec 23, 2025):

在Gemini Base URL中写http proxy代理不好用,用了 @chchuj 的方法,解决了我的问题

<!-- gh-comment-id:3685194425 --> @lgc0313 commented on GitHub (Dec 23, 2025): 在Gemini Base URL中写http proxy代理不好用,用了 @chchuj 的方法,解决了我的问题
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/AIClient-2-API-justlovemaki#94
No description provided.