optimize: 在匹配拦截配置时,可匹配到多个 ‘域名匹配符’ 下的拦截配置。

pull/286/head
王良 2024-03-29 22:14:09 +08:00
parent faf029827b
commit 0f7a258d2a
6 changed files with 41 additions and 33 deletions

View File

@ -65,7 +65,7 @@ function doDiff (oldObj, newObj) {
function deleteNullItems (target) {
lodash.forEach(target, (item, key) => {
if (item == null) {
if (item == null || item === '[delete]') {
delete target[key]
}
if (lodash.isObject(item)) {

View File

@ -17,7 +17,7 @@
:style="{ height: '100%' }"
>
<a-tab-pane tab="拦截设置" key="1" >
<vue-json-editor style="height:100%;" ref="editor" v-model="targetConfig.intercepts" mode="code" :show-btns="false" :expandedOnStart="true" @json-change="onJsonChange" ></vue-json-editor>
<vue-json-editor style="height:100%;" ref="editor" v-model="targetConfig.intercepts" mode="code" :show-btns="false" :expandedOnStart="true" @json-change="onJsonChange"></vue-json-editor>
</a-tab-pane>
<a-tab-pane tab="DNS设置" key="2">
<div>

View File

@ -138,7 +138,6 @@
<!-- <a-button type="danger" icon="minus" @click="deleteSniList(item,index)"/>-->
<!-- </a-col>-->
<!-- </a-row>-->
<!-- </a-tab-pane>-->
<a-tab-pane tab="IP测速" key="6">
<div>
@ -201,7 +200,6 @@
</a-card>
</a-col>
</a-row>
</div>
</a-tab-pane>
</a-tabs>

View File

@ -18,7 +18,7 @@ function matched (hostname, overWallTargetMap) {
log.info(`matchHostname: matched overwall: '${hostname}' -> '${ret}' in pac.txt`)
return true
} else {
// log.debug(`matchHostname: matched overwall: Not-Matched '${hostname}' -> '${ret}' in pac.txt`)
log.debug(`matchHostname: matched overwall: Not-Matched '${hostname}' -> '${ret}' in pac.txt`)
return false
}
}

View File

@ -70,7 +70,7 @@ module.exports = (config) => {
const matchInterceptsOpts = {}
for (const regexp in interceptOpts) { // 遍历拦截配置
const interceptOpt = interceptOpts[regexp]
interceptOpt.key = regexp
// interceptOpt.key = regexp
if (regexp !== true && regexp !== 'true') {
if (!matchUtil.isMatched(rOptions.path, regexp)) {
continue

View File

@ -94,6 +94,16 @@ function merge (oldObj, newObj) {
}
})
}
function deleteNullItems (target) {
lodash.forEach(target, (item, key) => {
if (item == null || item === '[delete]') {
delete target[key]
}
if (lodash.isObject(item)) {
deleteNullItems(item)
}
})
}
function matchHostnameAll (hostMap, hostname, action) {
// log.debug('matchHostnameAll:', action, hostMap)
@ -108,29 +118,9 @@ function matchHostnameAll (hostMap, hostname, action) {
}
let values = {}
let hasValue = false
let value
// 域名快速匹配:直接匹配 或者 两种前缀通配符匹配
let value = hostMap.origin[hostname]
if (value) {
log.info(`matchHostnameAll: ${action}: '${hostname}' -> '${hostname}': ${JSON.stringify(value)}`)
values = merge(values, value)
hasValue = true
}
value = hostMap.origin['*' + hostname]
if (value) {
log.info(`matchHostnameAll: ${action}: '${hostname}' -> '*${hostname}': ${JSON.stringify(value)}`)
values = merge(values, value)
hasValue = true
}
value = hostMap.origin['*.' + hostname]
if (value) {
log.info(`matchHostnameAll: ${action}: '${hostname}' -> '*.${hostname}': ${JSON.stringify(value)}`)
values = merge(values, value)
hasValue = true
}
// 通配符匹配 或 正则表达式匹配
// 通配符匹配 或 正则表达式匹配优先级1最低
for (const target in hostMap) {
if (target === 'origin') {
continue
@ -149,17 +139,37 @@ function matchHostnameAll (hostMap, hostname, action) {
// 正则表达式匹配
if (hostname.match(regexp)) {
value = hostMap[target]
// log.info(`matchHostname: ${action}: '${hostname}' -> '${target}': ${JSON.stringify(value)}`)
log.info(`matchHostnameOne: ${action}: '${hostname}' -> '${target}': ${JSON.stringify(value)}`)
values = merge(values, value)
hasValue = true
}
}
if (hasValue) {
log.info(`*matchHostnameAll*: ${action}: '${hostname}':`, JSON.stringify(values))
// 域名快速匹配:直接匹配 或者 两种前缀通配符匹配
// 优先级2
value = hostMap.origin['*' + hostname]
if (value) {
log.info(`matchHostnameOne: ${action}: '${hostname}' -> '*${hostname}': ${JSON.stringify(value)}`)
values = merge(values, value)
}
// 优先级3
value = hostMap.origin['*.' + hostname]
if (value) {
log.info(`matchHostnameOne: ${action}: '${hostname}' -> '*.${hostname}': ${JSON.stringify(value)}`)
values = merge(values, value)
}
// 优先级4最高优先级高的配置可以覆盖优先级低的配置甚至有空配置时可以移除已有配置
value = hostMap.origin[hostname]
if (value) {
log.info(`matchHostnameOne: ${action}: '${hostname}' -> '${hostname}': ${JSON.stringify(value)}`)
values = merge(values, value)
}
if (!lodash.isEmpty(values)) {
deleteNullItems(values)
log.info(`matchHostnameAll: ${action}: '${hostname}':`, JSON.stringify(values))
return values
} else {
log.debug(`*matchHostnameAll*: ${action}: '${hostname}' Not-Matched`)
log.debug(`matchHostnameAll: ${action}: '${hostname}' Not-Matched`)
}
}