|
|
|
@ -15,10 +15,18 @@ function matched (hostname, regexpMap) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = function createOverWallIntercept (overWallConfig) {
|
|
|
|
|
if (!overWallConfig || overWallConfig.enabled !== true || !overWallConfig.server || Object.keys(overWallConfig.server).length === 0) {
|
|
|
|
|
if (!overWallConfig || overWallConfig.enabled !== true) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
let server = overWallConfig.server
|
|
|
|
|
let keys = Object.keys(server)
|
|
|
|
|
if (keys.length === 0) {
|
|
|
|
|
server = overWallConfig.serverDefault
|
|
|
|
|
keys = Object.keys(server)
|
|
|
|
|
}
|
|
|
|
|
if (keys.length === 0) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
const keys = Object.keys(overWallConfig.server)
|
|
|
|
|
const regexpMap = matchUtil.domainMapRegexply(overWallConfig.intercepts)
|
|
|
|
|
return {
|
|
|
|
|
sslConnectInterceptor: (req, cltSocket, head) => {
|
|
|
|
@ -34,10 +42,10 @@ module.exports = function createOverWallIntercept (overWallConfig) {
|
|
|
|
|
if (!matched(hostname, regexpMap)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
const key = '__over_wall_proxy__'
|
|
|
|
|
const cacheKey = '__over_wall_proxy__'
|
|
|
|
|
let proxyServer = keys[0]
|
|
|
|
|
if (RequestCounter && keys.length > 1) {
|
|
|
|
|
const count = RequestCounter.getOrCreate(key, keys)
|
|
|
|
|
const count = RequestCounter.getOrCreate(cacheKey, keys)
|
|
|
|
|
if (count.value == null) {
|
|
|
|
|
count.doRank()
|
|
|
|
|
}
|
|
|
|
@ -47,23 +55,29 @@ module.exports = function createOverWallIntercept (overWallConfig) {
|
|
|
|
|
count.doCount(count.value)
|
|
|
|
|
proxyServer = count.value
|
|
|
|
|
context.requestCount = {
|
|
|
|
|
key,
|
|
|
|
|
key: cacheKey,
|
|
|
|
|
value: count.value,
|
|
|
|
|
count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const proxyTarget = proxyServer + '/' + hostname + req.url
|
|
|
|
|
const domain = proxyServer
|
|
|
|
|
const path = server[domain].path
|
|
|
|
|
const password = server[domain].password
|
|
|
|
|
const proxyTarget = domain + '/' + path + '/' + hostname + req.url
|
|
|
|
|
|
|
|
|
|
// const backup = interceptOpt.backup
|
|
|
|
|
const proxy = proxyTarget.indexOf('http') === 0 ? proxyTarget : rOptions.protocol + '//' + proxyTarget
|
|
|
|
|
const proxy = proxyTarget.indexOf('http') === 0 ? proxyTarget : (rOptions.protocol + '//' + proxyTarget)
|
|
|
|
|
// eslint-disable-next-line node/no-deprecated-api
|
|
|
|
|
const URL = url.parse(proxy)
|
|
|
|
|
rOptions.protocol = URL.protocol
|
|
|
|
|
rOptions.hostname = URL.host
|
|
|
|
|
rOptions.host = URL.host
|
|
|
|
|
rOptions.headers.host = URL.host
|
|
|
|
|
if (password) {
|
|
|
|
|
rOptions.headers.dspassword = password
|
|
|
|
|
}
|
|
|
|
|
rOptions.path = URL.path
|
|
|
|
|
if (URL.port == null) {
|
|
|
|
|
rOptions.port = rOptions.protocol === 'https:' ? 443 : 80
|
|
|
|
|