Skip to content

Commit 85543de

Browse files
feat: 添加 socks5 代理认证 (Chanzhaoyu#999)
* support socks5 proxy auth * Update build_docker.yml * Update README.md * perf: 增加判断 * fix: lint --------- Co-authored-by: ChenZhaoYu <[email protected]>
1 parent 142759a commit 85543de

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ services:
259259
| `API_REVERSE_PROXY` | 可选,`Web API` 时可用 | `Web API` 反向代理地址 [详情](https://github.com/transitive-bullshit/chatgpt-api#reverse-proxy) |
260260
| `SOCKS_PROXY_HOST` | 可选,和 `SOCKS_PROXY_PORT` 一起时生效 | Socks代理 |
261261
| `SOCKS_PROXY_PORT` | 可选,和 `SOCKS_PROXY_HOST` 一起时生效 | Socks代理端口 |
262+
| `SOCKS_PROXY_USERNAME` | 可选,和 `SOCKS_PROXY_HOST` 一起时生效 | Socks代理用户名 |
263+
| `SOCKS_PROXY_PASSWORD` | 可选,和 `SOCKS_PROXY_HOST` 一起时生效 | Socks代理密码 |
262264
| `HTTPS_PROXY` | 可选 | HTTPS 代理,支持 http,https, socks5 |
263265
| `ALL_PROXY` | 可选 | 所有代理 代理,支持 http,https, socks5 |
264266

service/.env.example

+6
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ SOCKS_PROXY_HOST=
3131
# Socks Proxy Port
3232
SOCKS_PROXY_PORT=
3333

34+
# Socks Proxy Username
35+
SOCKS_PROXY_USERNAME=
36+
37+
# Socks Proxy Password
38+
SOCKS_PROXY_PASSWORD=
39+
3440
# HTTPS PROXY
3541
HTTPS_PROXY=
3642

service/src/chatgpt/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -159,17 +159,19 @@ async function chatConfig() {
159159
}
160160

161161
function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOptions) {
162-
if (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) {
162+
if (isNotEmptyString(process.env.SOCKS_PROXY_HOST) && isNotEmptyString(process.env.SOCKS_PROXY_PORT)) {
163163
const agent = new SocksProxyAgent({
164164
hostname: process.env.SOCKS_PROXY_HOST,
165165
port: process.env.SOCKS_PROXY_PORT,
166+
userId: isNotEmptyString(process.env.SOCKS_PROXY_USERNAME) ? process.env.SOCKS_PROXY_USERNAME : undefined,
167+
password: isNotEmptyString(process.env.SOCKS_PROXY_PASSWORD) ? process.env.SOCKS_PROXY_PASSWORD : undefined,
166168
})
167169
options.fetch = (url, options) => {
168170
return fetch(url, { agent, ...options })
169171
}
170172
}
171173
else {
172-
if (process.env.HTTPS_PROXY || process.env.ALL_PROXY) {
174+
if (isNotEmptyString(process.env.HTTPS_PROXY) || isNotEmptyString(process.env.ALL_PROXY)) {
173175
const httpsProxy = process.env.HTTPS_PROXY || process.env.ALL_PROXY
174176
if (httpsProxy) {
175177
const agent = new HttpsProxyAgent(httpsProxy)

0 commit comments

Comments
 (0)