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