From 7a477d1752238535a83572f89e9b19c497762e38 Mon Sep 17 00:00:00 2001 From: Hank CP Date: Sun, 26 Mar 2023 01:23:08 +0800 Subject: [PATCH] feat: add socks username/password config --- README.en.md | 36 +++++++++++++++++------------ README.md | 38 +++++++++++++++++++------------ docker-compose/docker-compose.yml | 4 ++++ service/.env.example | 6 +++++ service/src/chatgpt/index.ts | 2 ++ 5 files changed, 57 insertions(+), 29 deletions(-) diff --git a/README.en.md b/README.en.md index 555ec7d113..f36843dabe 100644 --- a/README.en.md +++ b/README.en.md @@ -174,6 +174,8 @@ pnpm dev - `TIMEOUT_MS` timeout, in milliseconds, optional - `SOCKS_PROXY_HOST` optional, effective with SOCKS_PROXY_PORT - `SOCKS_PROXY_PORT` optional, effective with SOCKS_PROXY_HOST +- `SOCKS_PROXY_USERNAME` optional, effective with SOCKS_PROXY_HOST and SOCKS_PROXY_PORT +- `SOCKS_PROXY_PASSWORD` optional, effective with SOCKS_PROXY_HOST and SOCKS_PROXY_PORT - `HTTPS_PROXY` optional, support http,https, socks5 - `ALL_PROXY` optional, support http,https, socks5 @@ -225,6 +227,10 @@ services: SOCKS_PROXY_HOST: xxxx # socks proxy port, optional, effective with SOCKS_PROXY_HOST SOCKS_PROXY_PORT: xxxx + # socks proxy, optional, effective with SOCKS_PROXY_HOST and SOCKS_PROXY_PORT + SOCKS_PROXY_USERNAME: xxxx + # socks proxy port, optional, effective with SOCKS_PROXY_HOST and SOCKS_PROXY_PORT + SOCKS_PROXY_PASSWORD: xxxx # HTTPS Proxy,optional, support http, https, socks5 HTTPS_PROXY: http://xxx:7890 ``` @@ -237,20 +243,22 @@ The `OPENAI_API_MODEL` is optional and only used when setting the `OPENAI_API_KE #### Railway Environment Variables -| Environment Variable | Required | Description | -| -------------------- | -------- | ------------------------------------------------------------------------------------------------- | -| `PORT` | Required | Default: `3002` | -| `AUTH_SECRET_KEY` | Optional | access password | -| `TIMEOUT_MS` | Optional | Timeout in milliseconds | -| `OPENAI_API_KEY` | Optional | Required for `OpenAI API`. `apiKey` can be obtained from [here](https://platform.openai.com/overview). | -| `OPENAI_ACCESS_TOKEN`| Optional | Required for `Web API`. `accessToken` can be obtained from [here](https://chat.openai.com/api/auth/session).| -| `OPENAI_API_BASE_URL` | Optional, only for `OpenAI API` | API endpoint. | -| `OPENAI_API_MODEL` | Optional, only for `OpenAI API` | API model. | -| `API_REVERSE_PROXY` | Optional, only for `Web API` | Reverse proxy address for `Web API`. [Details](https://github.com/transitive-bullshit/chatgpt-api#reverse-proxy) | -| `SOCKS_PROXY_HOST` | Optional, effective with `SOCKS_PROXY_PORT` | Socks proxy. | -| `SOCKS_PROXY_PORT` | Optional, effective with `SOCKS_PROXY_HOST` | Socks proxy port. | -| `HTTPS_PROXY` | Optional | HTTPS Proxy. | -| `ALL_PROXY` | Optional | ALL Proxy. | +| Environment Variable | Required | Description | +|------------------------|-------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------| +| `PORT` | Required | Default: `3002` | +| `AUTH_SECRET_KEY` | Optional | access password | +| `TIMEOUT_MS` | Optional | Timeout in milliseconds | +| `OPENAI_API_KEY` | Optional | Required for `OpenAI API`. `apiKey` can be obtained from [here](https://platform.openai.com/overview). | +| `OPENAI_ACCESS_TOKEN` | Optional | Required for `Web API`. `accessToken` can be obtained from [here](https://chat.openai.com/api/auth/session). | +| `OPENAI_API_BASE_URL` | Optional, only for `OpenAI API` | API endpoint. | +| `OPENAI_API_MODEL` | Optional, only for `OpenAI API` | API model. | +| `API_REVERSE_PROXY` | Optional, only for `Web API` | Reverse proxy address for `Web API`. [Details](https://github.com/transitive-bullshit/chatgpt-api#reverse-proxy) | +| `SOCKS_PROXY_HOST` | Optional, effective with `SOCKS_PROXY_PORT` | Socks proxy. | +| `SOCKS_PROXY_PORT` | Optional, effective with `SOCKS_PROXY_HOST` | Socks proxy port. | +| `SOCKS_PROXY_USERNAME` | Optional, effective with `SOCKS_PROXY_HOST` & `SOCKS_PROXY_PORT` | Socks proxy username. | +| `SOCKS_PROXY_PASSWORD` | Optional, effective with `SOCKS_PROXY_HOST` & `SOCKS_PROXY_PORT` | Socks proxy password. | +| `HTTPS_PROXY` | Optional | HTTPS Proxy. | +| `ALL_PROXY` | Optional | ALL Proxy. | > Note: Changing environment variables in Railway will cause re-deployment. diff --git a/README.md b/README.md index f0cdc6b7fd..f2a92a8d10 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,8 @@ pnpm dev - `TIMEOUT_MS` 超时,单位毫秒,可选 - `SOCKS_PROXY_HOST` 和 `SOCKS_PROXY_PORT` 一起时生效,可选 - `SOCKS_PROXY_PORT` 和 `SOCKS_PROXY_HOST` 一起时生效,可选 +- `SOCKS_PROXY_USERNAME` 和 `SOCKS_PROXY_PORT` 一起时生效,可选 +- `SOCKS_PROXY_PASSWORD` 和 `SOCKS_PROXY_HOST` 一起时生效,可选 - `HTTPS_PROXY` 支持 `http`,`https`, `socks5`,可选 - `ALL_PROXY` 支持 `http`,`https`, `socks5`,可选 @@ -233,6 +235,10 @@ services: SOCKS_PROXY_HOST: xxx # Socks代理端口,可选,和 SOCKS_PROXY_HOST 一起时生效 SOCKS_PROXY_PORT: xxx + # Socks代理用户名,可选,和 SOCKS_PROXY_HOST & SOCKS_PROXY_PORT 一起时生效 + SOCKS_PROXY_USERNAME: xxxx + # Socks代理密码,可选,和 SOCKS_PROXY_HOST & SOCKS_PROXY_PORT 一起时生效 + SOCKS_PROXY_PASSWORD: xxxx # HTTPS 代理,可选,支持 http,https,socks5 HTTPS_PROXY: http://xxx:7890 ``` @@ -244,21 +250,23 @@ services: #### Railway 环境变量 -| 环境变量名称 | 必填 | 备注 | -| --------------------- | ---------------------- | -------------------------------------------------------------------------------------------------- | -| `PORT` | 必填 | 默认 `3002` -| `AUTH_SECRET_KEY` | 可选 | 访问权限密钥 | -| `MAX_REQUEST_PER_HOUR` | 可选 | 每小时最大请求次数,可选,默认无限 | -| `TIMEOUT_MS` | 可选 | 超时时间,单位毫秒 | -| `OPENAI_API_KEY` | `OpenAI API` 二选一 | 使用 `OpenAI API` 所需的 `apiKey` [(获取 apiKey)](https://platform.openai.com/overview) | -| `OPENAI_ACCESS_TOKEN` | `Web API` 二选一 | 使用 `Web API` 所需的 `accessToken` [(获取 accessToken)](https://chat.openai.com/api/auth/session) | -| `OPENAI_API_BASE_URL` | 可选,`OpenAI API` 时可用 | `API`接口地址 | -| `OPENAI_API_MODEL` | 可选,`OpenAI API` 时可用 | `API`模型 | -| `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代理端口 | -| `HTTPS_PROXY` | 可选 | HTTPS 代理,支持 http,https, socks5 | -| `ALL_PROXY` | 可选 | 所有代理 代理,支持 http,https, socks5 | +| 环境变量名称 | 必填 | 备注 | +|------------------------|----------------------------------| -------------------------------------------------------------------------------------------------- | +| `PORT` | 必填 | 默认 `3002` +| `AUTH_SECRET_KEY` | 可选 | 访问权限密钥 | +| `MAX_REQUEST_PER_HOUR` | 可选 | 每小时最大请求次数,可选,默认无限 | +| `TIMEOUT_MS` | 可选 | 超时时间,单位毫秒 | +| `OPENAI_API_KEY` | `OpenAI API` 二选一 | 使用 `OpenAI API` 所需的 `apiKey` [(获取 apiKey)](https://platform.openai.com/overview) | +| `OPENAI_ACCESS_TOKEN` | `Web API` 二选一 | 使用 `Web API` 所需的 `accessToken` [(获取 accessToken)](https://chat.openai.com/api/auth/session) | +| `OPENAI_API_BASE_URL` | 可选,`OpenAI API` 时可用 | `API`接口地址 | +| `OPENAI_API_MODEL` | 可选,`OpenAI API` 时可用 | `API`模型 | +| `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_PROXY_PORT` 一起时生效 | Socks代理 | +| `SOCKS_PROXY_PASSWORD` | 可选,和 `SOCKS_PROXY_HOST` & `SOCKS_PROXY_PORT` 一起时生效 | Socks代理端口 | +| `HTTPS_PROXY` | 可选 | HTTPS 代理,支持 http,https, socks5 | +| `ALL_PROXY` | 可选 | 所有代理 代理,支持 http,https, socks5 | > 注意: `Railway` 修改环境变量会重新 `Deploy` diff --git a/docker-compose/docker-compose.yml b/docker-compose/docker-compose.yml index dcf99b63cb..b596aceb16 100644 --- a/docker-compose/docker-compose.yml +++ b/docker-compose/docker-compose.yml @@ -26,6 +26,10 @@ services: SOCKS_PROXY_HOST: xxx # Socks代理端口,可选,和 SOCKS_PROXY_HOST 一起时生效 SOCKS_PROXY_PORT: xxx + # Socks代理用户名,可选,和 SOCKS_PROXY_HOST & SOCKS_PROXY_PORT 一起时生效 + SOCKS_PROXY_USERNAME: xxx + # Socks代理密码,可选,和 SOCKS_PROXY_HOST & SOCKS_PROXY_PORT 一起时生效 + SOCKS_PROXY_PASSWORD: xxx # HTTPS_PROXY 代理,可选 HTTPS_PROXY: http://xxx:7890 nginx: diff --git a/service/.env.example b/service/.env.example index ac8e9d6467..280fcb613d 100644 --- a/service/.env.example +++ b/service/.env.example @@ -28,6 +28,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 c702e6ac81..b7fd1af483 100644 --- a/service/src/chatgpt/index.ts +++ b/service/src/chatgpt/index.ts @@ -162,6 +162,8 @@ function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOption const agent = new SocksProxyAgent({ hostname: process.env.SOCKS_PROXY_HOST, port: process.env.SOCKS_PROXY_PORT, + userId: process.env.SOCKS_PROXY_USERNAME, + password: process.env.SOCKS_PROXY_PASSWORD, }) options.fetch = (url, options) => { return fetch(url, { agent, ...options })