From 85543deca39eea0239d8de4317c74d1f78fc1275 Mon Sep 17 00:00:00 2001 From: yimiaoxiehou Date: Fri, 31 Mar 2023 12:23:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20socks5=20=E4=BB=A3?= =?UTF-8?q?=E7=90=86=E8=AE=A4=E8=AF=81=20(#999)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * support socks5 proxy auth * Update build_docker.yml * Update README.md * perf: 增加判断 * fix: lint --------- Co-authored-by: ChenZhaoYu <790348264@qq.com> --- README.md | 2 ++ service/.env.example | 6 ++++++ service/src/chatgpt/index.ts | 6 ++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1c25c48..6f9cbe4 100644 --- a/README.md +++ b/README.md @@ -259,6 +259,8 @@ services: | `API_REVERSE_PROXY` | 可选,`Web API` 时可用 | `Web API` 反向代理地址 [详情](https://github.com/transitive-bullshit/chatgpt-api#reverse-proxy) | | `SOCKS_PROXY_HOST` | 可选,和 `SOCKS_PROXY_PORT` 一起时生效 | Socks代理 | | `SOCKS_PROXY_PORT` | 可选,和 `SOCKS_PROXY_HOST` 一起时生效 | Socks代理端口 | +| `SOCKS_PROXY_USERNAME` | 可选,和 `SOCKS_PROXY_HOST` 一起时生效 | Socks代理用户名 | +| `SOCKS_PROXY_PASSWORD` | 可选,和 `SOCKS_PROXY_HOST` 一起时生效 | Socks代理密码 | | `HTTPS_PROXY` | 可选 | HTTPS 代理,支持 http,https, socks5 | | `ALL_PROXY` | 可选 | 所有代理 代理,支持 http,https, socks5 | diff --git a/service/.env.example b/service/.env.example index 5d2866b..d6f2ba5 100644 --- a/service/.env.example +++ b/service/.env.example @@ -31,6 +31,12 @@ SOCKS_PROXY_HOST= # Socks Proxy Port SOCKS_PROXY_PORT= +# Socks Proxy Username +SOCKS_PROXY_USERNAME= + +# Socks Proxy Password +SOCKS_PROXY_PASSWORD= + # HTTPS PROXY HTTPS_PROXY= diff --git a/service/src/chatgpt/index.ts b/service/src/chatgpt/index.ts index c397fca..c96467b 100644 --- a/service/src/chatgpt/index.ts +++ b/service/src/chatgpt/index.ts @@ -159,17 +159,19 @@ async function chatConfig() { } function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOptions) { - if (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) { + if (isNotEmptyString(process.env.SOCKS_PROXY_HOST) && isNotEmptyString(process.env.SOCKS_PROXY_PORT)) { const agent = new SocksProxyAgent({ hostname: process.env.SOCKS_PROXY_HOST, port: process.env.SOCKS_PROXY_PORT, + userId: isNotEmptyString(process.env.SOCKS_PROXY_USERNAME) ? process.env.SOCKS_PROXY_USERNAME : undefined, + password: isNotEmptyString(process.env.SOCKS_PROXY_PASSWORD) ? process.env.SOCKS_PROXY_PASSWORD : undefined, }) options.fetch = (url, options) => { return fetch(url, { agent, ...options }) } } else { - if (process.env.HTTPS_PROXY || process.env.ALL_PROXY) { + if (isNotEmptyString(process.env.HTTPS_PROXY) || isNotEmptyString(process.env.ALL_PROXY)) { const httpsProxy = process.env.HTTPS_PROXY || process.env.ALL_PROXY if (httpsProxy) { const agent = new HttpsProxyAgent(httpsProxy)