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
11 changed files with 55 additions and 0 deletions

View File

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

View File

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

12
service/pnpm-lock.yaml generated
View File

@@ -9,6 +9,7 @@ specifiers:
eslint: ^8.35.0
esno: ^0.16.3
express: ^4.18.2
https-proxy-agent: ^5.0.1
isomorphic-fetch: ^3.0.0
node-fetch: ^3.3.0
rimraf: ^4.3.0
@@ -21,6 +22,7 @@ dependencies:
dotenv: 16.0.3
esno: 0.16.3
express: 4.18.2
https-proxy-agent: 5.0.1
isomorphic-fetch: 3.0.0
node-fetch: 3.3.0
socks-proxy-agent: 7.0.0
@@ -2079,6 +2081,16 @@ packages:
toidentifier: 1.0.1
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:
resolution: {integrity: sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==}
engines: {node: '>=10.17.0'}

View File

@@ -3,6 +3,7 @@ import 'isomorphic-fetch'
import type { ChatGPTAPIOptions, ChatMessage, SendMessageOptions } from 'chatgpt'
import { ChatGPTAPI, ChatGPTUnofficialProxyAPI } from 'chatgpt'
import { SocksProxyAgent } from 'socks-proxy-agent'
import { HttpsProxyAgent } from 'https-proxy-agent'
import fetch from 'node-fetch'
import { sendResponse } from '../utils'
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 })
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)
options.apiReverseProxyUrl = process.env.API_REVERSE_PROXY
@@ -119,6 +136,8 @@ async function chatReplyProcess(
}
async function chatConfig() {
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy
return sendResponse({
type: 'Success',
data: {
@@ -126,6 +145,7 @@ async function chatConfig() {
reverseProxy: process.env.API_REVERSE_PROXY,
timeoutMs,
socksProxy: (process.env.SOCKS_PROXY_HOST && process.env.SOCKS_PROXY_PORT) ? (`${process.env.SOCKS_PROXY_HOST}:${process.env.SOCKS_PROXY_PORT}`) : '-',
httpsProxy,
} as ModelConfig,
})
}

View File

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