Browse Source

日志优化;拦截配置优化。

pull/376/head
王良 1 month ago
parent
commit
d2ae69a062
  1. 7
      packages/core/src/config/index.js
  2. 1
      packages/mitmproxy/src/lib/proxy/mitmproxy/createConnectHandler.js
  3. 13
      packages/mitmproxy/src/lib/proxy/mitmproxy/index.js
  4. 9
      packages/mitmproxy/src/lib/proxy/tls/FakeServersCenter.js
  5. 13
      packages/mitmproxy/src/options.js
  6. 18
      packages/mitmproxy/src/utils/util.match.js

7
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列表

1
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
}

13
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...`)
})

9
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)

13
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) => {

18
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
}
}

Loading…
Cancel
Save