feat: 添加 socks5 代理认证 (#999)

* support socks5 proxy auth

* Update build_docker.yml

* Update README.md

* perf: 增加判断

* fix: lint

---------

Co-authored-by: ChenZhaoYu <790348264@qq.com>
main
yimiaoxiehou 2 years ago committed by GitHub
parent 142759ac50
commit 85543deca3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -259,6 +259,8 @@ services:
| `API_REVERSE_PROXY` | 可选,`Web API` 时可用 | `Web API` 反向代理地址 [详情](https://github.com/transitive-bullshit/chatgpt-api#reverse-proxy) | | `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_HOST` | 可选,和 `SOCKS_PROXY_PORT` 一起时生效 | Socks代理 |
| `SOCKS_PROXY_PORT` | 可选,和 `SOCKS_PROXY_HOST` 一起时生效 | 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 代理,支持 httphttps, socks5 | | `HTTPS_PROXY` | 可选 | HTTPS 代理,支持 httphttps, socks5 |
| `ALL_PROXY` | 可选 | 所有代理 代理,支持 httphttps, socks5 | | `ALL_PROXY` | 可选 | 所有代理 代理,支持 httphttps, socks5 |

@ -31,6 +31,12 @@ SOCKS_PROXY_HOST=
# Socks Proxy Port # Socks Proxy Port
SOCKS_PROXY_PORT= SOCKS_PROXY_PORT=
# Socks Proxy Username
SOCKS_PROXY_USERNAME=
# Socks Proxy Password
SOCKS_PROXY_PASSWORD=
# HTTPS PROXY # HTTPS PROXY
HTTPS_PROXY= HTTPS_PROXY=

@ -159,17 +159,19 @@ async function chatConfig() {
} }
function setupProxy(options: ChatGPTAPIOptions | ChatGPTUnofficialProxyAPIOptions) { 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({ const agent = new SocksProxyAgent({
hostname: process.env.SOCKS_PROXY_HOST, hostname: process.env.SOCKS_PROXY_HOST,
port: process.env.SOCKS_PROXY_PORT, 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) => { options.fetch = (url, options) => {
return fetch(url, { agent, ...options }) return fetch(url, { agent, ...options })
} }
} }
else { 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 const httpsProxy = process.env.HTTPS_PROXY || process.env.ALL_PROXY
if (httpsProxy) { if (httpsProxy) {
const agent = new HttpsProxyAgent(httpsProxy) const agent = new HttpsProxyAgent(httpsProxy)

Loading…
Cancel
Save