|
|
@ -1,6 +1,7 @@
|
|
|
|
const http = require('http')
|
|
|
|
const http = require('http')
|
|
|
|
const https = require('https')
|
|
|
|
const https = require('https')
|
|
|
|
const commonUtil = require('../common/util')
|
|
|
|
const commonUtil = require('../common/util')
|
|
|
|
|
|
|
|
const jsonApi = require('../../../json')
|
|
|
|
// const upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i
|
|
|
|
// const upgradeHeader = /(^|,)\s*upgrade\s*($|,)/i
|
|
|
|
const DnsUtil = require('../../dns/index')
|
|
|
|
const DnsUtil = require('../../dns/index')
|
|
|
|
const log = require('../../../utils/util.log')
|
|
|
|
const log = require('../../../utils/util.log')
|
|
|
@ -105,7 +106,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
|
|
|
|
function onFree () {
|
|
|
|
function onFree () {
|
|
|
|
url = `${rOptions.method} ➜ ${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${rOptions.path}`
|
|
|
|
url = `${rOptions.method} ➜ ${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${rOptions.path}`
|
|
|
|
const start = new Date()
|
|
|
|
const start = new Date()
|
|
|
|
log.info('发起代理请求:', url, (rOptions.servername ? ', sni: ' + rOptions.servername : ''))
|
|
|
|
log.info('发起代理请求:', url, (rOptions.servername ? ', sni: ' + rOptions.servername : ''), ', headers:', rOptions.headers)
|
|
|
|
|
|
|
|
|
|
|
|
const isDnsIntercept = {}
|
|
|
|
const isDnsIntercept = {}
|
|
|
|
if (dnsConfig && dnsConfig.providers) {
|
|
|
|
if (dnsConfig && dnsConfig.providers) {
|
|
|
@ -149,7 +150,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
|
|
|
|
proxyReq.on('timeout', () => {
|
|
|
|
proxyReq.on('timeout', () => {
|
|
|
|
const cost = new Date() - start
|
|
|
|
const cost = new Date() - start
|
|
|
|
const errorMsg = `代理请求超时: ${url}, cost: ${cost} ms`
|
|
|
|
const errorMsg = `代理请求超时: ${url}, cost: ${cost} ms`
|
|
|
|
log.error(errorMsg)
|
|
|
|
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
|
|
|
|
countSlow(isDnsIntercept, `代理请求超时, cost: ${cost} ms`)
|
|
|
|
countSlow(isDnsIntercept, `代理请求超时, cost: ${cost} ms`)
|
|
|
|
proxyReq.end()
|
|
|
|
proxyReq.end()
|
|
|
|
proxyReq.destroy()
|
|
|
|
proxyReq.destroy()
|
|
|
@ -159,14 +160,14 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
|
|
|
|
})
|
|
|
|
})
|
|
|
|
proxyReq.on('error', (e) => {
|
|
|
|
proxyReq.on('error', (e) => {
|
|
|
|
const cost = new Date() - start
|
|
|
|
const cost = new Date() - start
|
|
|
|
log.error(`代理请求错误: ${url}, cost: ${cost} ms, error:`, e)
|
|
|
|
log.error(`代理请求错误: ${url}, cost: ${cost} ms, error:`, e, ', rOptions:', jsonApi.stringify2(rOptions))
|
|
|
|
countSlow(isDnsIntercept, '代理请求错误: ' + e.message)
|
|
|
|
countSlow(isDnsIntercept, '代理请求错误: ' + e.message)
|
|
|
|
reject(e)
|
|
|
|
reject(e)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
proxyReq.on('aborted', () => {
|
|
|
|
proxyReq.on('aborted', () => {
|
|
|
|
const cost = new Date() - start
|
|
|
|
const cost = new Date() - start
|
|
|
|
const errorMsg = `代理请求被取消: ${url}, cost: ${cost} ms`
|
|
|
|
const errorMsg = `代理请求被取消: ${url}, cost: ${cost} ms`
|
|
|
|
log.error(errorMsg)
|
|
|
|
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
|
|
|
|
|
|
|
|
|
|
|
|
if (cost > MAX_SLOW_TIME) {
|
|
|
|
if (cost > MAX_SLOW_TIME) {
|
|
|
|
countSlow(isDnsIntercept, `代理请求被取消,且请求太慢, cost: ${cost} ms > ${MAX_SLOW_TIME} ms`)
|
|
|
|
countSlow(isDnsIntercept, `代理请求被取消,且请求太慢, cost: ${cost} ms > ${MAX_SLOW_TIME} ms`)
|
|
|
@ -182,7 +183,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
|
|
|
|
req.on('aborted', function () {
|
|
|
|
req.on('aborted', function () {
|
|
|
|
const cost = new Date() - start
|
|
|
|
const cost = new Date() - start
|
|
|
|
const errorMsg = `请求被取消: ${url}, cost: ${cost} ms`
|
|
|
|
const errorMsg = `请求被取消: ${url}, cost: ${cost} ms`
|
|
|
|
log.error(errorMsg)
|
|
|
|
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
|
|
|
|
proxyReq.abort()
|
|
|
|
proxyReq.abort()
|
|
|
|
if (res.writableEnded) {
|
|
|
|
if (res.writableEnded) {
|
|
|
|
return
|
|
|
|
return
|
|
|
@ -191,13 +192,13 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
|
|
|
|
})
|
|
|
|
})
|
|
|
|
req.on('error', function (e, req, res) {
|
|
|
|
req.on('error', function (e, req, res) {
|
|
|
|
const cost = new Date() - start
|
|
|
|
const cost = new Date() - start
|
|
|
|
log.error(`请求错误: ${url}, cost: ${cost} ms, error:`, e)
|
|
|
|
log.error(`请求错误: ${url}, cost: ${cost} ms, error:`, e, ', rOptions:', jsonApi.stringify2(rOptions))
|
|
|
|
reject(e)
|
|
|
|
reject(e)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
req.on('timeout', () => {
|
|
|
|
req.on('timeout', () => {
|
|
|
|
const cost = new Date() - start
|
|
|
|
const cost = new Date() - start
|
|
|
|
const errorMsg = `请求超时: ${url}, cost: ${cost} ms`
|
|
|
|
const errorMsg = `请求超时: ${url}, cost: ${cost} ms`
|
|
|
|
log.error(errorMsg)
|
|
|
|
log.error(errorMsg, ', rOptions:', jsonApi.stringify2(rOptions))
|
|
|
|
reject(new Error(errorMsg))
|
|
|
|
reject(new Error(errorMsg))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
req.pipe(proxyReq)
|
|
|
|
req.pipe(proxyReq)
|
|
|
|