feat: support linux command line proxy standard (#308)

* feat: support linux command line proxy standard with env HTTPS_PROXY and ALL_PROXY

* chore: update docs for linux command linux proxy standard
This commit is contained in:
Eason 2023-03-12 19:38:59 +08:00 committed by GitHub
parent a4cfd0c380
commit bc5e835f78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 55 additions and 0 deletions

View File

@ -174,6 +174,8 @@ pnpm dev
- `TIMEOUT_MS` timeout, in milliseconds, optional - `TIMEOUT_MS` timeout, in milliseconds, optional
- `SOCKS_PROXY_HOST` optional, effective with SOCKS_PROXY_PORT - `SOCKS_PROXY_HOST` optional, effective with SOCKS_PROXY_PORT
- `SOCKS_PROXY_PORT` optional, effective with SOCKS_PROXY_HOST - `SOCKS_PROXY_PORT` optional, effective with SOCKS_PROXY_HOST
- `HTTPS_PROXY` optional, support httphttps, socks5
- `ALL_PROXY` optional, support httphttps, socks5
![docker](./docs/docker.png) ![docker](./docs/docker.png)
@ -223,6 +225,8 @@ services:
SOCKS_PROXY_HOST: xxxx SOCKS_PROXY_HOST: xxxx
# socks proxy port, optional, effective with SOCKS_PROXY_HOST # socks proxy port, optional, effective with SOCKS_PROXY_HOST
SOCKS_PROXY_PORT: xxxx SOCKS_PROXY_PORT: xxxx
# HTTPS Proxyoptional, support http, https, socks5
HTTPS_PROXY: http://xxx:7890
``` ```
The `OPENAI_API_BASE_URL` is optional and only used when setting the `OPENAI_API_KEY`. 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`. The `OPENAI_API_MODEL` is optional and only used when setting the `OPENAI_API_KEY`.
@ -245,6 +249,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) | | `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_HOST` | Optional, effective with `SOCKS_PROXY_PORT` | Socks proxy. |
| `SOCKS_PROXY_PORT` | Optional, effective with `SOCKS_PROXY_HOST` | Socks proxy port. | | `SOCKS_PROXY_PORT` | Optional, effective with `SOCKS_PROXY_HOST` | Socks proxy port. |
| `HTTPS_PROXY` | Optional | HTTPS Proxy. |
| `ALL_PROXY` | Optional | ALL Proxy. |
> Note: Changing environment variables in Railway will cause re-deployment. > Note: Changing environment variables in Railway will cause re-deployment.

View File

@ -178,6 +178,8 @@ pnpm dev
- `TIMEOUT_MS` 超时,单位毫秒,可选 - `TIMEOUT_MS` 超时,单位毫秒,可选
- `SOCKS_PROXY_HOST` 可选,和 SOCKS_PROXY_PORT 一起时生效 - `SOCKS_PROXY_HOST` 可选,和 SOCKS_PROXY_PORT 一起时生效
- `SOCKS_PROXY_PORT` 可选,和 SOCKS_PROXY_HOST 一起时生效 - `SOCKS_PROXY_PORT` 可选,和 SOCKS_PROXY_HOST 一起时生效
- `HTTPS_PROXY` 可选,支持 httphttps, socks5
- `ALL_PROXY` 可选,支持 httphttps, socks5
![docker](./docs/docker.png) ![docker](./docs/docker.png)
@ -227,6 +229,8 @@ services:
SOCKS_PROXY_HOST: xxxx SOCKS_PROXY_HOST: xxxx
# Socks代理端口可选和 SOCKS_PROXY_HOST 一起时生效 # Socks代理端口可选和 SOCKS_PROXY_HOST 一起时生效
SOCKS_PROXY_PORT: xxxx SOCKS_PROXY_PORT: xxxx
# HTTPS 代理,可选,支持 httphttpssocks5
HTTPS_PROXY: http://xxxx:7890
``` ```
- `OPENAI_API_BASE_URL` 可选,设置 `OPENAI_API_KEY` 时可用 - `OPENAI_API_BASE_URL` 可选,设置 `OPENAI_API_KEY` 时可用
- `OPENAI_API_MODEL` 可选,设置 `OPENAI_API_KEY` 时可用 - `OPENAI_API_MODEL` 可选,设置 `OPENAI_API_KEY` 时可用
@ -248,6 +252,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代理端口 |
| `HTTPS_PROXY` | 可选 | HTTPS 代理,支持 httphttps, socks5 |
| `ALL_PROXY` | 可选 | 所有代理 代理,支持 httphttps, socks5 |
> 注意: `Railway` 修改环境变量会重新 `Deploy` > 注意: `Railway` 修改环境变量会重新 `Deploy`

View File

@ -24,6 +24,8 @@ services:
SOCKS_PROXY_HOST: xxxx SOCKS_PROXY_HOST: xxxx
# Socks代理端口可选和 SOCKS_PROXY_HOST 一起时生效 # Socks代理端口可选和 SOCKS_PROXY_HOST 一起时生效
SOCKS_PROXY_PORT: xxxx SOCKS_PROXY_PORT: xxxx
# HTTPS_PROXY 代理,可选
HTTPS_PROXY: http://xxxx:7890
nginx: nginx:
image: nginx:alpine image: nginx:alpine
ports: ports:

View File

@ -24,3 +24,6 @@ SOCKS_PROXY_HOST=
# Socks Proxy Port # Socks Proxy Port
SOCKS_PROXY_PORT= SOCKS_PROXY_PORT=
# HTTPS PROXY
HTTPS_PROXY=

View File

@ -28,6 +28,7 @@
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"esno": "^0.16.3", "esno": "^0.16.3",
"express": "^4.18.2", "express": "^4.18.2",
"https-proxy-agent": "^5.0.1",
"isomorphic-fetch": "^3.0.0", "isomorphic-fetch": "^3.0.0",
"node-fetch": "^3.3.0", "node-fetch": "^3.3.0",
"socks-proxy-agent": "^7.0.0" "socks-proxy-agent": "^7.0.0"

View File

@ -9,6 +9,7 @@ specifiers:
eslint: ^8.35.0 eslint: ^8.35.0
esno: ^0.16.3 esno: ^0.16.3
express: ^4.18.2 express: ^4.18.2
https-proxy-agent: ^5.0.1
isomorphic-fetch: ^3.0.0 isomorphic-fetch: ^3.0.0
node-fetch: ^3.3.0 node-fetch: ^3.3.0
rimraf: ^4.3.0 rimraf: ^4.3.0
@ -21,6 +22,7 @@ dependencies:
dotenv: 16.0.3 dotenv: 16.0.3
esno: 0.16.3 esno: 0.16.3
express: 4.18.2 express: 4.18.2
https-proxy-agent: 5.0.1
isomorphic-fetch: 3.0.0 isomorphic-fetch: 3.0.0
node-fetch: 3.3.0 node-fetch: 3.3.0
socks-proxy-agent: 7.0.0 socks-proxy-agent: 7.0.0
@ -2079,6 +2081,16 @@ packages:
toidentifier: 1.0.1 toidentifier: 1.0.1
dev: false dev: false
/https-proxy-agent/5.0.1:
resolution: {integrity: sha512-dFcAjpTQFgoLMzC2VwU+C/CbS7uRL0lWmxDITmqm7C+7F0Odmj6s9l6alZc6AELXhrnggM2CeWSXHGOdX2YtwA==}
engines: {node: '>= 6'}
dependencies:
agent-base: 6.0.2
debug: 4.3.4
transitivePeerDependencies:
- supports-color
dev: false
/human-signals/2.1.0: /human-signals/2.1.0:
resolution: {integrity: sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==} resolution: {integrity: sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==}
engines: {node: '>=10.17.0'} engines: {node: '>=10.17.0'}

View File

@ -3,6 +3,7 @@ import 'isomorphic-fetch'
import type { ChatGPTAPIOptions, ChatMessage, SendMessageOptions } from 'chatgpt' import type { ChatGPTAPIOptions, ChatMessage, SendMessageOptions } from 'chatgpt'
import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt' import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt'
import { SocksProxyAgent } from 'socks-proxy-agent' import { SocksProxyAgent } from 'socks-proxy-agent'
import { HttpsProxyAgent } from 'https-proxy-agent'
import fetch from 'node-fetch' import fetch from 'node-fetch'
import { sendResponse } from '../utils' import { sendResponse } from '../utils'
import type { ApiModel, ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types' import type { ApiModel, ChatContext, ChatGPTUnofficialProxyAPIOptions, ModelConfig } from '../types'
@ -55,6 +56,14 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
} }
} }
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy
if (httpsProxy) {
const agent = new HttpsProxyAgent(httpsProxy)
options.fetch = (url, options) => {
return fetch(url, { agent, ...options })
}
}
api = new ChatGPTAPI({ ...options }) api = new ChatGPTAPI({ ...options })
apiModel = 'ChatGPTAPI' apiModel = 'ChatGPTAPI'
} }
@ -74,6 +83,14 @@ let api: ChatGPTAPI | ChatGPTUnofficialProxyAPI
} }
} }
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy
if (httpsProxy) {
const agent = new HttpsProxyAgent(httpsProxy)
options.fetch = (url, options) => {
return fetch(url, { agent, ...options })
}
}
if (process.env.API_REVERSE_PROXY) if (process.env.API_REVERSE_PROXY)
options.apiReverseProxyUrl = process.env.API_REVERSE_PROXY options.apiReverseProxyUrl = process.env.API_REVERSE_PROXY
@ -119,6 +136,8 @@ async function chatReplyProcess(
} }
async function chatConfig() { async function chatConfig() {
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy
return sendResponse({ return sendResponse({
type: 'Success', type: 'Success',
data: { data: {
@ -126,6 +145,7 @@ async function chatConfig() {
reverseProxy: process.env.API_REVERSE_PROXY, reverseProxy: process.env.API_REVERSE_PROXY,
timeoutMs, timeoutMs,
socksProxy: (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) ? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`) : '-', socksProxy: (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) ? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`) : '-',
httpsProxy,
} as ModelConfig, } as ModelConfig,
}) })
} }

View File

@ -19,6 +19,7 @@ export interface ModelConfig {
reverseProxy?: string reverseProxy?: string
timeoutMs?: number timeoutMs?: number
socksProxy?: string socksProxy?: string
httpsProxy?: string
} }
export type ApiModel = 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' | undefined export type ApiModel = 'ChatGPTAPI' | 'ChatGPTUnofficialProxyAPI' | undefined

View File

@ -9,6 +9,7 @@ interface ConfigState {
reverseProxy?: string reverseProxy?: string
apiModel?: string apiModel?: string
socksProxy?: string socksProxy?: string
httpsProxy?: string
} }
const loading = ref(false) const loading = ref(false)
@ -57,6 +58,7 @@ onMounted(() => {
<p>{{ $t("setting.reverseProxy") }}{{ config?.reverseProxy ?? '-' }}</p> <p>{{ $t("setting.reverseProxy") }}{{ config?.reverseProxy ?? '-' }}</p>
<p>{{ $t("setting.timeout") }}{{ config?.timeoutMs ?? '-' }}</p> <p>{{ $t("setting.timeout") }}{{ config?.timeoutMs ?? '-' }}</p>
<p>{{ $t("setting.socks") }}{{ config?.socksProxy ?? '-' }}</p> <p>{{ $t("setting.socks") }}{{ config?.socksProxy ?? '-' }}</p>
<p>{{ $t("setting.httpsProxy") }}{{ config?.httpsProxy ?? '-' }}</p>
</div> </div>
</NSpin> </NSpin>
</template> </template>

View File

@ -50,6 +50,7 @@ export default {
reverseProxy: 'Reverse Proxy', reverseProxy: 'Reverse Proxy',
timeout: 'Timeout', timeout: 'Timeout',
socks: 'Socks', socks: 'Socks',
httpsProxy: 'HTTPS Proxy',
}, },
} }

View File

@ -50,6 +50,7 @@ export default {
reverseProxy: '反向代理', reverseProxy: '反向代理',
timeout: '超时', timeout: '超时',
socks: 'Socks', socks: 'Socks',
httpsProxy: 'HTTPS Proxy',
}, },
} }