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) { function deleteNullItems (target) {
lodash.forEach(target, (item, key) => { lodash.forEach(target, (item, key) => {
if (item == null) { if (item == null || item === '[delete]') {
delete target[key] delete target[key]
} }
if (lodash.isObject(item)) { if (lodash.isObject(item)) {

View File

@ -17,7 +17,7 @@
:style="{ height: '100%' }" :style="{ height: '100%' }"
> >
<a-tab-pane tab="拦截设置" key="1" > <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>
<a-tab-pane tab="DNS设置" key="2"> <a-tab-pane tab="DNS设置" key="2">
<div> <div>

View File

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

View File

@ -18,7 +18,7 @@ function matched (hostname, overWallTargetMap) {
log.info(`matchHostname: matched overwall: '${hostname}' -> '${ret}' in pac.txt`) log.info(`matchHostname: matched overwall: '${hostname}' -> '${ret}' in pac.txt`)
return true return true
} else { } 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 return false
} }
} }

View File

@ -70,7 +70,7 @@ module.exports = (config) => {
const matchInterceptsOpts = {} const matchInterceptsOpts = {}
for (const regexp in interceptOpts) { // 遍历拦截配置 for (const regexp in interceptOpts) { // 遍历拦截配置
const interceptOpt = interceptOpts[regexp] const interceptOpt = interceptOpts[regexp]
interceptOpt.key = regexp // interceptOpt.key = regexp
if (regexp !== true && regexp !== 'true') { if (regexp !== true && regexp !== 'true') {
if (!matchUtil.isMatched(rOptions.path, regexp)) { if (!matchUtil.isMatched(rOptions.path, regexp)) {
continue 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) { function matchHostnameAll (hostMap, hostname, action) {
// log.debug('matchHostnameAll:', action, hostMap) // log.debug('matchHostnameAll:', action, hostMap)
@ -108,29 +118,9 @@ function matchHostnameAll (hostMap, hostname, action) {
} }
let values = {} let values = {}
let hasValue = false let value
// 域名快速匹配:直接匹配 或者 两种前缀通配符匹配 // 通配符匹配 或 正则表达式匹配优先级1最低
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
}
// 通配符匹配 或 正则表达式匹配
for (const target in hostMap) { for (const target in hostMap) {
if (target === 'origin') { if (target === 'origin') {
continue continue
@ -149,17 +139,37 @@ function matchHostnameAll (hostMap, hostname, action) {
// 正则表达式匹配 // 正则表达式匹配
if (hostname.match(regexp)) { if (hostname.match(regexp)) {
value = hostMap[target] 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) 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 return values
} else { } else {
log.debug(`*matchHostnameAll*: ${action}: '${hostname}' Not-Matched`) log.debug(`matchHostnameAll: ${action}: '${hostname}' Not-Matched`)
} }
} }