diff --git a/docs/en/configuration/env-vars.md b/docs/en/configuration/env-vars.md index 29d708554..9bdb6fcdb 100644 --- a/docs/en/configuration/env-vars.md +++ b/docs/en/configuration/env-vars.md @@ -108,6 +108,33 @@ Overrides the provider's `api_key` field in the configuration file. export OPENAI_API_KEY="sk-xxx" ``` +## Network proxy + +The following environment variables are used to configure HTTP/HTTPS proxies for use in network-restricted environments: + +| Environment Variable | Description | +| --- | --- | +| `HTTPS_PROXY` / `https_proxy` | HTTPS proxy URL | +| `HTTP_PROXY` / `http_proxy` | HTTP proxy URL | + +### `HTTPS_PROXY` / `HTTP_PROXY` + +Sets the proxy server address for all HTTP/HTTPS requests. Supports proxy addresses with `http://` and `https://` protocols. + +```sh +export HTTPS_PROXY="http://proxy.example.com:8080" +export HTTP_PROXY="http://proxy.example.com:8080" +``` + +::: tip +- Environment variable names are case-insensitive; both `HTTPS_PROXY` and `https_proxy` are supported +- If both `HTTPS_PROXY` and `HTTP_PROXY` are set, `HTTPS_PROXY` takes precedence for HTTPS requests +::: + +::: info Added +Added in version 1.23. +::: + ## Other environment variables | Environment Variable | Description | diff --git a/docs/zh/configuration/env-vars.md b/docs/zh/configuration/env-vars.md index 93e56b514..7caaa4c31 100644 --- a/docs/zh/configuration/env-vars.md +++ b/docs/zh/configuration/env-vars.md @@ -108,6 +108,33 @@ export OPENAI_BASE_URL="https://api.openai.com/v1" export OPENAI_API_KEY="sk-xxx" ``` +## 网络代理 + +以下环境变量用于配置 HTTP/HTTPS 代理,在有网络限制的环境中使用: + +| 环境变量 | 说明 | +| --- | --- | +| `HTTPS_PROXY` / `https_proxy` | HTTPS 代理 URL | +| `HTTP_PROXY` / `http_proxy` | HTTP 代理 URL | + +### `HTTPS_PROXY` / `HTTP_PROXY` + +设置代理服务器地址,用于所有 HTTP/HTTPS 请求。支持 `http://` 和 `https://` 协议的代理地址。 + +```sh +export HTTPS_PROXY="http://proxy.example.com:8080" +export HTTP_PROXY="http://proxy.example.com:8080" +``` + +::: tip 提示 +- 环境变量名称不区分大小写,同时支持 `HTTPS_PROXY` 和 `https_proxy` +- 如果同时设置了 `HTTPS_PROXY` 和 `HTTP_PROXY`,`HTTPS_PROXY` 优先用于 HTTPS 请求 +::: + +::: info 新增 +新增于 1.23 版本。 +::: + ## 其他环境变量 | 环境变量 | 说明 | diff --git a/src/kimi_cli/utils/aiohttp.py b/src/kimi_cli/utils/aiohttp.py index 0fe91b235..d07ff09f1 100644 --- a/src/kimi_cli/utils/aiohttp.py +++ b/src/kimi_cli/utils/aiohttp.py @@ -9,4 +9,5 @@ def new_client_session() -> aiohttp.ClientSession: - return aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=_ssl_context)) + connector = aiohttp.TCPConnector(ssl=_ssl_context) + return aiohttp.ClientSession(connector=connector, trust_env=True) diff --git a/tests/utils/test_aiohttp.py b/tests/utils/test_aiohttp.py new file mode 100644 index 000000000..7dae03a1b --- /dev/null +++ b/tests/utils/test_aiohttp.py @@ -0,0 +1,58 @@ +"""Tests for aiohttp utility module.""" + +from __future__ import annotations + +import ssl +from unittest.mock import MagicMock, patch + +import aiohttp +import pytest + +from kimi_cli.utils.aiohttp import new_client_session + + +class TestNewClientSession: + """Tests for new_client_session function.""" + + @pytest.mark.asyncio + async def test_trust_env_enabled(self) -> None: + """Test that ClientSession is created with trust_env=True for proxy support.""" + with patch("aiohttp.ClientSession") as mock_session: + mock_instance = MagicMock() + mock_session.return_value = mock_instance + + result = new_client_session() + + call_kwargs = mock_session.call_args.kwargs + assert call_kwargs.get("trust_env") is True + assert "connector" in call_kwargs + assert result == mock_instance + + @pytest.mark.asyncio + async def test_ssl_context_configuration(self) -> None: + """Test that SSL context is properly configured with certifi certificates.""" + with patch("aiohttp.ClientSession") as mock_session: + mock_instance = MagicMock() + mock_session.return_value = mock_instance + + new_client_session() + + call_kwargs = mock_session.call_args.kwargs + assert "connector" in call_kwargs + connector = call_kwargs["connector"] + assert isinstance(connector, aiohttp.TCPConnector) + + @pytest.mark.asyncio + async def test_no_explicit_proxy_set(self) -> None: + """Test that no explicit proxy is set, allowing trust_env to handle it.""" + with patch("aiohttp.ClientSession") as mock_session: + mock_instance = MagicMock() + mock_session.return_value = mock_instance + + new_client_session() + + call_kwargs = mock_session.call_args.kwargs + # Should not have explicit proxy parameter + assert "proxy" not in call_kwargs + # trust_env should be True to enable env-based proxy detection + assert call_kwargs.get("trust_env") is True