From 9bddd87aeb2c7da90cf77aa7c6ebf3ee6fea5f50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Sun, 15 Sep 2024 01:03:20 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=8B=A6=E6=88=AA=E5=99=A8=E7=9A=84?= =?UTF-8?q?=20`exclusions`=20=E9=85=8D=E7=BD=AE=EF=BC=8C=E4=B9=9F=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=AF=B9=E8=B1=A1=E9=85=8D=E7=BD=AE=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/mitmproxy/src/options.js | 37 ++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/mitmproxy/src/options.js b/packages/mitmproxy/src/options.js index 767cb2c..602873d 100644 --- a/packages/mitmproxy/src/options.js +++ b/packages/mitmproxy/src/options.js @@ -4,6 +4,7 @@ const log = require('./utils/util.log') const matchUtil = require('./utils/util.match') const path = require('path') const fs = require('fs') +const lodash = require('lodash') const scriptInterceptor = require('./lib/interceptor/impl/res/script') const { getTmpPacFilePath, downloadPacAsync, createOverwallMiddleware } = require('./lib/proxy/middleware/overwall') @@ -16,6 +17,25 @@ function buildIntercepts (intercepts) { return intercepts } +// 从拦截器配置中,获取exclusions字段,返回数组类型 +function getExclusionArray (exclusions) { + let ret = null + if (Array.isArray(exclusions)) { + if (exclusions.length > 0) { + ret = exclusions + } + } else if (lodash.isObject(exclusions)) { + ret = [] + for (const exclusion in exclusions) { + ret.push(exclusion) + } + if (ret.length === 0) { + return null + } + } + return ret +} + module.exports = (serverConfig) => { const intercepts = matchUtil.domainMapRegexply(buildIntercepts(serverConfig.intercepts)) const whiteList = matchUtil.domainMapRegexply(serverConfig.whiteList) @@ -118,13 +138,20 @@ module.exports = (serverConfig) => { // 添加exclusions字段,用于排除某些路径 // @since 1.8.5 - if (Array.isArray(interceptOpt.exclusions) && interceptOpt.exclusions.length > 0) { + if (interceptOpt.exclusions) { let isExcluded = false - for (const exclusion of interceptOpt.exclusions) { - if (matchUtil.isMatched(rOptions.path, exclusion)) { - log.debug(`拦截器配置排除了path:${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${rOptions.path}, exclusion: '${exclusion}', interceptOpt:`, interceptOpt) - isExcluded = true + try { + const exclusions = getExclusionArray(interceptOpt.exclusions) + if (exclusions) { + for (const exclusion of exclusions) { + if (matchUtil.isMatched(rOptions.path, exclusion)) { + log.debug(`拦截器配置排除了path:${rOptions.protocol}//${rOptions.hostname}:${rOptions.port}${rOptions.path}, exclusion: '${exclusion}', interceptOpt:`, interceptOpt) + isExcluded = true + } + } } + } catch (e) { + log.error(`判断拦截器是否排除当前path时出现异常, path: ${rOptions.path}, interceptOpt:`, interceptOpt, ', error:', e) } if (isExcluded) { continue