Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add socks proxy auth support #530

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_PASSWORD
- `SOCKS_PROXY_PASSWORD` optional, effective with SOCKS_PROXY_USERNAME

![docker](./docs/docker.png)

Expand Down Expand Up @@ -223,6 +225,10 @@ services:
SOCKS_PROXY_HOST: xxxx
# socks proxy port, optional, effective with SOCKS_PROXY_HOST
SOCKS_PROXY_PORT: xxxx
# optional, effective with SOCKS_PROXY_PASSWORD
SOCKS_PROXY_USERNAME: xxxx
# optional, effective with SOCKS_PROXY_USERNAME
SOCKS_PROXY_PASSWORD: xxxx
```
The `OPENAI_API_BASE_URL` is optional and only used when setting the `OPENAI_API_KEY`.
The `OPENAI_API_MODEL` is optional and only used when setting the `OPENAI_API_KEY`.
Expand All @@ -245,6 +251,8 @@ The `OPENAI_API_MODEL` is optional and only used when setting the `OPENAI_API_KE
| `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_PASSWORD` | Socks proxy username. |
| `SOCKS_PROXY_PASSWORD` | Optional, effective with `SOCKS_PROXY_USERNAME` | Socks proxy password. |

> Note: Changing environment variables in Railway will cause re-deployment.

Expand Down
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ pnpm dev
- `TIMEOUT_MS` 超时,单位毫秒,可选
- `SOCKS_PROXY_HOST` 可选,和 SOCKS_PROXY_PORT 一起时生效
- `SOCKS_PROXY_PORT` 可选,和 SOCKS_PROXY_HOST 一起时生效
- `SOCKS_PROXY_USERNAME` 可选,和 SOCKS_PROXY_PASSWORD 一起时生效
- `SOCKS_PROXY_PASSWORD` 可选,和 SOCKS_PROXY_USERNAME 一起时生效

![docker](./docs/docker.png)

Expand Down Expand Up @@ -223,10 +225,14 @@ services:
AUTH_SECRET_KEY: xxx
# 超时,单位毫秒,可选
TIMEOUT_MS: 60000
# Socks代理,可选,和 SOCKS_PROXY_PORT 一起时生效
# Socks 代理,可选,和 SOCKS_PROXY_PORT 一起时生效
SOCKS_PROXY_HOST: xxxx
# Socks代理端口,可选,和 SOCKS_PROXY_HOST 一起时生效
# Socks 代理端口,可选,和 SOCKS_PROXY_HOST 一起时生效
SOCKS_PROXY_PORT: xxxx
# Socks 代理用户名,可选,和 SOCKS_PROXY_PASSWORD 一起时生效
SOCKS_PROXY_USERNAME: xxxx
# Socks 代理密码,可选,和 SOCKS_PROXY_USERNAME 一起时生效
SOCKS_PROXY_PASSWORD: xxxx
```
- `OPENAI_API_BASE_URL` 可选,设置 `OPENAI_API_KEY` 时可用
- `OPENAI_API_MODEL` 可选,设置 `OPENAI_API_KEY` 时可用
Expand All @@ -246,8 +252,10 @@ services:
| `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_HOST` | 可选,和 `SOCKS_PROXY_PORT` 一起时生效 | Socks 代理 |
| `SOCKS_PROXY_PORT` | 可选,和 `SOCKS_PROXY_HOST` 一起时生效 | Socks 代理端口 |
| `SOCKS_PROXY_USERNAME` | 可选,和 `SOCKS_PROXY_PASSWORD` 一起时生效 | Socks 代理用户名 |
| `SOCKS_PROXY_PASSWORD` | 可选,和 `SOCKS_PROXY_USERNAME` 一起时生效 | Socks 代理密码 |

> 注意: `Railway` 修改环境变量会重新 `Deploy`

Expand Down
8 changes: 6 additions & 2 deletions docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ services:
AUTH_SECRET_KEY: xxx
# 超时,单位毫秒,可选
TIMEOUT_MS: 60000
# Socks代理,可选,和 SOCKS_PROXY_PORT 一起时生效
# Socks 代理,可选,和 SOCKS_PROXY_PORT 一起时生效
SOCKS_PROXY_HOST: xxxx
# Socks代理端口,可选,和 SOCKS_PROXY_HOST 一起时生效
# Socks 代理端口,可选,和 SOCKS_PROXY_HOST 一起时生效
SOCKS_PROXY_PORT: xxxx
# Socks 代理用户名,可选,和 SOCKS_PROXY_PASSWORD 一起时生效
SOCKS_PROXY_USERNAME: xxxx
# Socks 代理密码,可选,和 SOCKS_PROXY_USERNAME 一起时生效
SOCKS_PROXY_PASSWORD: xxxx
nginx:
image: nginx:alpine
ports:
Expand Down
6 changes: 6 additions & 0 deletions service/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ SOCKS_PROXY_HOST=

# Socks Proxy Port
SOCKS_PROXY_PORT=

# Socks Proxy Username
SOCKS_PROXY_USERNAME=

# Socks Proxy Password
SOCKS_PROXY_PASSWORD=
4 changes: 4 additions & 0 deletions service/src/chatgpt/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
const agent = new SocksProxyAgent({
hostname: process.env.SOCKS_PROXY_HOST,
port: process.env.SOCKS_PROXY_PORT,
username: process.env.SOCKS_PROXY_USERNAME || null,
password: process.env.SOCKS_PROXY_PASSWORD || null,
})
options.fetch = (url, options) => {
return fetch(url, { agent, ...options })
Expand All @@ -68,6 +70,8 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
const agent = new SocksProxyAgent({
hostname: process.env.SOCKS_PROXY_HOST,
port: process.env.SOCKS_PROXY_PORT,
username: process.env.SOCKS_PROXY_USERNAME || null,
password: process.env.SOCKS_PROXY_PASSWORD || null,
})
options.fetch = (url, options) => {
return fetch(url, { agent, ...options })
Expand Down