日志优化;拦截配置优化。
parent
6e88d434c8
commit
d2ae69a062
|
@ -283,13 +283,6 @@ module.exports = {
|
||||||
abort: true,
|
abort: true,
|
||||||
desc: '广告拦截'
|
desc: '广告拦截'
|
||||||
}
|
}
|
||||||
},
|
|
||||||
'*': {
|
|
||||||
'^.*\\?DS_DOWNLOAD$': {
|
|
||||||
requestReplace: { doDownload: true },
|
|
||||||
responseReplace: { doDownload: true },
|
|
||||||
desc: '下载请求拦截:移除请求地址中的 `?DS_DOWNLOAD`,并设置响应头 `Content-Disposition: attachment; filename=xxxx`,使浏览器强制执行下载逻辑,而不是在浏览器中浏览。'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// 预设置IP列表
|
// 预设置IP列表
|
||||||
|
|
|
@ -8,6 +8,7 @@ const dnsLookup = require('./dnsLookup')
|
||||||
function isSslConnect (sslConnectInterceptors, req, cltSocket, head) {
|
function isSslConnect (sslConnectInterceptors, req, cltSocket, head) {
|
||||||
for (const intercept of sslConnectInterceptors) {
|
for (const intercept of sslConnectInterceptors) {
|
||||||
const ret = intercept(req, cltSocket, head)
|
const ret = intercept(req, cltSocket, head)
|
||||||
|
log.debug(`拦截判断结果:${ret}, url: ${req.url}, intercept:`, intercept)
|
||||||
if (ret === false || ret === true) {
|
if (ret === false || ret === true) {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,21 +82,26 @@ module.exports = {
|
||||||
)
|
)
|
||||||
|
|
||||||
// 创建监听方法,用于监听 http 和 https 两个端口
|
// 创建监听方法,用于监听 http 和 https 两个端口
|
||||||
|
const printDebugLog = false && process.env.NODE_ENV === 'development' // 开发过程中,如有需要可以将此参数临时改为true,打印所有事件的日志
|
||||||
const serverListen = (server, ssl, port, host) => {
|
const serverListen = (server, ssl, port, host) => {
|
||||||
server.listen(port, host, () => {
|
server.listen(port, host, () => {
|
||||||
log.info(`dev-sidecar启动 ${ssl ? 'https' : 'http'} 端口: ${host}:${port}`)
|
log.info(`dev-sidecar启动 ${ssl ? 'https' : 'http'} 端口: ${host}:${port}`)
|
||||||
server.on('request', (req, res) => {
|
server.on('request', (req, res) => {
|
||||||
log.debug(`【server request, ssl: ${ssl}】\r\n----- req -----\r\n`, req, '\r\n----- res -----\r\n', res)
|
if (printDebugLog) log.debug(`【server request, ssl: ${ssl}】\r\n----- req -----\r\n`, req, '\r\n----- res -----\r\n', res)
|
||||||
requestHandler(req, res, ssl)
|
requestHandler(req, res, ssl)
|
||||||
})
|
})
|
||||||
// tunneling for https
|
// tunneling for https
|
||||||
server.on('connect', (req, cltSocket, head) => {
|
server.on('connect', (req, cltSocket, head) => {
|
||||||
log.debug(`【server connect, ssl: ${ssl}】\r\n----- req -----\r\n`, req, '\r\n----- cltSocket -----\r\n', cltSocket, '\r\n----- head -----\r\n', head)
|
if (printDebugLog) log.debug(`【server connect, ssl: ${ssl}】\r\n----- req -----\r\n`, req, '\r\n----- cltSocket -----\r\n', cltSocket, '\r\n----- head -----\r\n', head)
|
||||||
connectHandler(req, cltSocket, head, ssl)
|
connectHandler(req, cltSocket, head, ssl)
|
||||||
})
|
})
|
||||||
// TODO: handler WebSocket
|
// TODO: handler WebSocket
|
||||||
server.on('upgrade', function (req, cltSocket, head) {
|
server.on('upgrade', function (req, cltSocket, head) {
|
||||||
log.debug(`【server upgrade, ssl: ${ssl}】\r\n----- req -----\r\n`, req)
|
if (printDebugLog) {
|
||||||
|
log.debug(`【server upgrade, ssl: ${ssl}】\r\n----- req -----\r\n`, req)
|
||||||
|
} else {
|
||||||
|
log.info(`【server upgrade, ssl: ${ssl}】`, req.url)
|
||||||
|
}
|
||||||
upgradeHandler(req, cltSocket, head, ssl)
|
upgradeHandler(req, cltSocket, head, ssl)
|
||||||
})
|
})
|
||||||
server.on('error', (err) => {
|
server.on('error', (err) => {
|
||||||
|
@ -109,7 +114,7 @@ module.exports = {
|
||||||
})
|
})
|
||||||
|
|
||||||
// 其他事件:仅记录debug日志
|
// 其他事件:仅记录debug日志
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (printDebugLog) {
|
||||||
server.on('close', () => {
|
server.on('close', () => {
|
||||||
log.debug(`【server close, ssl: ${ssl}】no arguments...`)
|
log.debug(`【server close, ssl: ${ssl}】no arguments...`)
|
||||||
})
|
})
|
||||||
|
|
|
@ -115,17 +115,18 @@ module.exports = class FakeServersCenter {
|
||||||
}
|
}
|
||||||
serverPromiseObj.serverObj = serverObj
|
serverPromiseObj.serverObj = serverObj
|
||||||
|
|
||||||
|
const printDebugLog = false && process.env.NODE_ENV === 'development' // 开发过程中,如有需要可以将此参数临时改为true,打印所有事件的日志
|
||||||
fakeServer.listen(0, () => {
|
fakeServer.listen(0, () => {
|
||||||
const address = fakeServer.address()
|
const address = fakeServer.address()
|
||||||
serverObj.port = address.port
|
serverObj.port = address.port
|
||||||
})
|
})
|
||||||
fakeServer.on('request', (req, res) => {
|
fakeServer.on('request', (req, res) => {
|
||||||
log.debug(`【fakeServer request - ${hostname}:${port}】\r\n----- req -----\r\n`, req, '\r\n----- res -----\r\n', res)
|
if (printDebugLog) log.debug(`【fakeServer request - ${hostname}:${port}】\r\n----- req -----\r\n`, req, '\r\n----- res -----\r\n', res)
|
||||||
this.requestHandler(req, res, ssl)
|
this.requestHandler(req, res, ssl)
|
||||||
})
|
})
|
||||||
let once = true
|
let once = true
|
||||||
fakeServer.on('listening', () => {
|
fakeServer.on('listening', () => {
|
||||||
log.debug(`【fakeServer listening - ${hostname}:${port}】no arguments...`)
|
if (printDebugLog) log.debug(`【fakeServer listening - ${hostname}:${port}】no arguments...`)
|
||||||
if (cert && once) {
|
if (cert && once) {
|
||||||
once = false
|
once = false
|
||||||
let newMappingHostNames = tlsUtils.getMappingHostNamesFromCert(cert)
|
let newMappingHostNames = tlsUtils.getMappingHostNamesFromCert(cert)
|
||||||
|
@ -138,7 +139,7 @@ module.exports = class FakeServersCenter {
|
||||||
resolve(serverObj)
|
resolve(serverObj)
|
||||||
})
|
})
|
||||||
fakeServer.on('upgrade', (req, socket, head) => {
|
fakeServer.on('upgrade', (req, socket, head) => {
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (printDebugLog) {
|
||||||
log.debug(`【fakeServer upgrade - ${hostname}:${port}】\r\n----- req -----\r\n`, req, '\r\n----- socket -----\r\n', socket, '\r\n----- head -----\r\n', head)
|
log.debug(`【fakeServer upgrade - ${hostname}:${port}】\r\n----- req -----\r\n`, req, '\r\n----- socket -----\r\n', socket, '\r\n----- head -----\r\n', head)
|
||||||
} else {
|
} else {
|
||||||
log.info(`【fakeServer upgrade - ${hostname}:${port}】`, req.url)
|
log.info(`【fakeServer upgrade - ${hostname}:${port}】`, req.url)
|
||||||
|
@ -176,7 +177,7 @@ module.exports = class FakeServersCenter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 其他监听事件,只打印debug日志
|
// 其他监听事件,只打印debug日志
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (printDebugLog) {
|
||||||
if (ssl) {
|
if (ssl) {
|
||||||
fakeServer.on('keylog', (line, tlsSocket) => {
|
fakeServer.on('keylog', (line, tlsSocket) => {
|
||||||
log.debug(`【fakeServer keylog - ${hostname}:${port}】\r\n----- line -----\r\n`, line, '\r\n----- tlsSocket -----\r\n', tlsSocket)
|
log.debug(`【fakeServer keylog - ${hostname}:${port}】\r\n----- line -----\r\n`, line, '\r\n----- tlsSocket -----\r\n', tlsSocket)
|
||||||
|
|
|
@ -106,16 +106,21 @@ module.exports = (serverConfig) => {
|
||||||
middlewares,
|
middlewares,
|
||||||
sslConnectInterceptor: (req, cltSocket, head) => {
|
sslConnectInterceptor: (req, cltSocket, head) => {
|
||||||
const hostname = req.url.split(':')[0]
|
const hostname = req.url.split(':')[0]
|
||||||
|
|
||||||
|
// 配置了白名单的域名,将跳过代理
|
||||||
const inWhiteList = matchUtil.matchHostname(whiteList, hostname, 'in whiteList') != null
|
const inWhiteList = matchUtil.matchHostname(whiteList, hostname, 'in whiteList') != null
|
||||||
if (inWhiteList) {
|
if (inWhiteList) {
|
||||||
log.info(`为白名单域名,不拦截: ${hostname}, headers:`, req.headers)
|
log.info(`为白名单域名,不拦截: ${hostname}, headers:`, req.headers)
|
||||||
return false // 所有都不拦截
|
return false // 不拦截
|
||||||
}
|
}
|
||||||
|
|
||||||
// 配置了拦截的域名,将会被代理
|
// 配置了拦截的域名,将会被代理
|
||||||
const matched = !!matchUtil.matchHostname(intercepts, hostname, 'matched intercepts')
|
const matched = matchUtil.matchHostname(intercepts, hostname, 'matched intercepts')
|
||||||
if (matched === true) {
|
if ((!!matched) === true) {
|
||||||
return matched // 拦截
|
log.debug(`拦截器拦截:${req.url}, matched:`, matched)
|
||||||
|
return true // 拦截
|
||||||
}
|
}
|
||||||
|
|
||||||
return null // 未匹配到任何拦截配置,由下一个拦截器判断
|
return null // 未匹配到任何拦截配置,由下一个拦截器判断
|
||||||
},
|
},
|
||||||
createIntercepts: (context) => {
|
createIntercepts: (context) => {
|
||||||
|
|
|
@ -77,25 +77,15 @@ function matchHostname (hostMap, hostname, action) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 通配符匹配 或 正则表达式匹配
|
// 通配符匹配 或 正则表达式匹配
|
||||||
for (const target in hostMap) {
|
for (const regexp in hostMap) {
|
||||||
if (target === 'origin') {
|
if (regexp === 'origin') {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (target.indexOf('*') < 0 && target[0] !== '^') {
|
|
||||||
// continue // 不是通配符匹配串,也不是正则表达式,跳过
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 如果是通配符匹配串,转换为正则表达式
|
|
||||||
let regexp = target
|
|
||||||
// if (target[0] !== '^') {
|
|
||||||
// regexp = domainRegexply(regexp)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// 正则表达式匹配
|
// 正则表达式匹配
|
||||||
if (hostname.match(regexp)) {
|
if (hostname.match(regexp)) {
|
||||||
value = hostMap[target]
|
value = hostMap[regexp]
|
||||||
log.info(`matchHostname: ${action}: '${hostname}' -> '${target}': ${JSON.stringify(value)}`)
|
log.info(`matchHostname: ${action}: '${hostname}' -> { "${regexp}": ${JSON.stringify(value)} }`)
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue