feature: 请求超时时间和连接超时时间可配置化。

pull/354/head
王良 2024-09-11 18:27:48 +08:00
parent 9b2c5f1f25
commit 9d12aa4516
4 changed files with 55 additions and 9 deletions

View File

@ -44,6 +44,8 @@ module.exports = {
setting: { setting: {
NODE_TLS_REJECT_UNAUTHORIZED: true, NODE_TLS_REJECT_UNAUTHORIZED: true,
verifySsl: true, verifySsl: true,
timeout: 20000, // 代理请求超时时间
keepAliveTimeout: 30000, // socket连接的超时时间
script: { script: {
enabled: true, enabled: true,
defaultDir: './extra/scripts/' defaultDir: './extra/scripts/'

View File

@ -36,6 +36,14 @@
<a-input-number v-model="config.server.port" :min="0" :max="65535"/> <a-input-number v-model="config.server.port" :min="0" :max="65535"/>
<div class="form-help">修改后需要重启应用</div> <div class="form-help">修改后需要重启应用</div>
</a-form-item> </a-form-item>
<hr/>
<a-form-item label="请求超时时间" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input-number v-model="config.server.setting.timeout" :step="1000" :min="1000"/> ms对应 timeout 属性
</a-form-item>
<a-form-item label="连接超时时间" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input-number v-model="config.server.setting.keepAliveTimeout" :step="1000" :min="1000"/> ms对应 keepAliveTimeout 属性
</a-form-item>
<hr/>
<a-form-item label="全局校验SSL" :label-col="labelCol" :wrapper-col="wrapperCol"> <a-form-item label="全局校验SSL" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-checkbox v-model="config.server.setting.NODE_TLS_REJECT_UNAUTHORIZED"> <a-checkbox v-model="config.server.setting.NODE_TLS_REJECT_UNAUTHORIZED">
NODE_TLS_REJECT_UNAUTHORIZED NODE_TLS_REJECT_UNAUTHORIZED
@ -54,6 +62,7 @@
<a-input-search addon-before="Key" enter-button="" @search="onKeySelect" <a-input-search addon-before="Key" enter-button="" @search="onKeySelect"
v-model="config.server.setting.rootCaFile.keyPath"/> v-model="config.server.setting.rootCaFile.keyPath"/>
</a-form-item> </a-form-item>
<hr/>
<a-form-item label="启用拦截" :label-col="labelCol" :wrapper-col="wrapperCol"> <a-form-item label="启用拦截" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-checkbox v-model="config.server.intercept.enabled"> <a-checkbox v-model="config.server.intercept.enabled">
启用拦截 启用拦截
@ -136,8 +145,7 @@
</a-checkbox> </a-checkbox>
</a-form-item> </a-form-item>
<a-form-item label="自动测试间隔" :label-col="labelCol" :wrapper-col="wrapperCol"> <a-form-item label="自动测试间隔" :label-col="labelCol" :wrapper-col="wrapperCol">
<a-input-number id="inputNumber" v-model="getSpeedTestConfig().interval" :step="1000" :min="1"/> <a-input-number id="inputNumber" v-model="getSpeedTestConfig().interval" :step="1000" :min="1"/> ms
ms
</a-form-item> </a-form-item>
<div>使用以下dns获取ip进行测速</div> <div>使用以下dns获取ip进行测速</div>
<a-row style="margin-top:10px"> <a-row style="margin-top:10px">

View File

@ -15,10 +15,50 @@ const httpAgent = new Agent({
timeout: 20000, timeout: 20000,
keepAliveTimeout: 30000 // free socket keepalive for 30 seconds keepAliveTimeout: 30000 // free socket keepalive for 30 seconds
}) })
const httpsAgentCache = {}
const httpAgentCache = {}
let socketId = 0 let socketId = 0
let httpsOverHttpAgent, httpOverHttpsAgent, httpsOverHttpsAgent let httpsOverHttpAgent, httpOverHttpsAgent, httpsOverHttpsAgent
function createHttpsAgent (serverSetting) {
const key = (serverSetting.timeout || 20000) + '-' + (serverSetting.keepAliveTimeout || 30000)
if (!httpsAgentCache[key]) {
httpsAgentCache[key] = new HttpsAgent({
keepAlive: true,
timeout: serverSetting.timeout || 20000,
keepAliveTimeout: serverSetting.keepAliveTimeout || 30000,
rejectUnauthorized: false
})
}
return httpsAgentCache[key]
}
function createHttpAgent (serverSetting) {
const key = (serverSetting.timeout || 20000) + '-' + (serverSetting.keepAliveTimeout || 30000)
if (!httpAgentCache[key]) {
httpAgentCache[key] = new Agent({
keepAlive: true,
timeout: serverSetting.timeout || 20000,
keepAliveTimeout: serverSetting.keepAliveTimeout || 30000
})
}
return httpAgentCache[key]
}
function createAgent (protocol, serverSetting) {
if (protocol === 'https:') {
return !serverSetting || (serverSetting.timeout === 20000 && serverSetting.keepAliveTimeout === 30000)
? httpsAgent
: createHttpsAgent(serverSetting)
} else {
return !serverSetting || (serverSetting.timeout === 20000 && serverSetting.keepAliveTimeout === 30000)
? httpAgent
: createHttpAgent(serverSetting)
}
}
util.parseHostnameAndPort = (host, defaultPort) => { util.parseHostnameAndPort = (host, defaultPort) => {
let arr = host.match(/^(\[[^\]]+\])(?::(\d+))?$/) // 尝试解析IPv6 let arr = host.match(/^(\[[^\]]+\])(?::(\d+))?$/) // 尝试解析IPv6
if (arr) { if (arr) {
@ -42,7 +82,7 @@ util.parseHostnameAndPort = (host, defaultPort) => {
return arr return arr
} }
util.getOptionsFromRequest = (req, ssl, externalProxy = null) => { util.getOptionsFromRequest = (req, ssl, externalProxy = null, serverSetting) => {
// eslint-disable-next-line node/no-deprecated-api // eslint-disable-next-line node/no-deprecated-api
const urlObject = url.parse(req.url) const urlObject = url.parse(req.url)
const defaultPort = ssl ? 443 : 80 const defaultPort = ssl ? 443 : 80
@ -67,11 +107,7 @@ util.getOptionsFromRequest = (req, ssl, externalProxy = null) => {
if (!externalProxyUrl) { if (!externalProxyUrl) {
// keepAlive // keepAlive
if (headers.connection !== 'close') { if (headers.connection !== 'close') {
if (protocol === 'https:') { agent = createAgent(protocol, serverSetting)
agent = httpsAgent
} else {
agent = httpAgent
}
headers.connection = 'keep-alive' headers.connection = 'keep-alive'
} else { } else {
agent = false agent = false

View File

@ -15,7 +15,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
return function requestHandler (req, res, ssl) { return function requestHandler (req, res, ssl) {
let proxyReq let proxyReq
const rOptions = commonUtil.getOptionsFromRequest(req, ssl, externalProxy) const rOptions = commonUtil.getOptionsFromRequest(req, ssl, externalProxy, setting)
if (rOptions.agent) { if (rOptions.agent) {
rOptions.agent.options.rejectUnauthorized = setting.verifySsl rOptions.agent.options.rejectUnauthorized = setting.verifySsl