feature: 各JSON配置中,域名匹配串支持格式如 `.xxx.com` 的配置,效果同 `*.xxx.com`
parent
cadef1a1f4
commit
3a0fda6483
|
@ -12,7 +12,7 @@ function isMatched (url, regexp) {
|
||||||
urlRegexp = `.${regexp}`
|
urlRegexp = `.${regexp}`
|
||||||
}
|
}
|
||||||
return url.match(urlRegexp)
|
return url.match(urlRegexp)
|
||||||
} catch (e) {
|
} catch {
|
||||||
log.error('匹配串有问题:', regexp)
|
log.error('匹配串有问题:', regexp)
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,11 @@ function domainMapRegexply (hostMap) {
|
||||||
const regexpMap = {}
|
const regexpMap = {}
|
||||||
const origin = {} // 用于快速匹配,见matchHostname、matchHostnameAll方法
|
const origin = {} // 用于快速匹配,见matchHostname、matchHostnameAll方法
|
||||||
lodash.each(hostMap, (value, domain) => {
|
lodash.each(hostMap, (value, domain) => {
|
||||||
|
// 将域名匹配串格式如 `.xxx.com` 转换为 `*.xxx.com`
|
||||||
|
if (domain[0] === '.' && lodash.isEmpty(hostMap[`*${domain}`])) {
|
||||||
|
domain = `*${domain}`
|
||||||
|
}
|
||||||
|
|
||||||
if (domain.includes('*') || domain[0] === '^') {
|
if (domain.includes('*') || domain[0] === '^') {
|
||||||
const regDomain = domain[0] !== '^' ? domainRegexply(domain) : domain
|
const regDomain = domain[0] !== '^' ? domainRegexply(domain) : domain
|
||||||
regexpMap[regDomain] = value
|
regexpMap[regDomain] = value
|
||||||
|
@ -59,22 +64,23 @@ function matchHostname (hostMap, hostname, action) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
// 域名快速匹配:直接匹配 或者 两种前缀通配符匹配
|
// 域名快速匹配:直接匹配(优先级最高)
|
||||||
let value = hostMap.origin[hostname]
|
let value = hostMap.origin[hostname]
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
log.info(`matchHostname: ${action}: '${hostname}' -> { "${hostname}": ${JSON.stringify(value)} }`)
|
log.info(`matchHostname: ${action}: '${hostname}' -> { "${hostname}": ${JSON.stringify(value)} }`)
|
||||||
return value // 快速匹配成功
|
return value // 快速匹配成功
|
||||||
}
|
}
|
||||||
value = hostMap.origin[`*${hostname}`]
|
// 域名快速匹配:三种前缀通配符匹配
|
||||||
if (value != null) {
|
|
||||||
log.info(`matchHostname: ${action}: '${hostname}' -> { "*${hostname}": ${JSON.stringify(value)} }`)
|
|
||||||
return value // 快速匹配成功
|
|
||||||
}
|
|
||||||
value = hostMap.origin[`*.${hostname}`]
|
value = hostMap.origin[`*.${hostname}`]
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
log.info(`matchHostname: ${action}: '${hostname}' -> { "*.${hostname}": ${JSON.stringify(value)} }`)
|
log.info(`matchHostname: ${action}: '${hostname}' -> { "*.${hostname}": ${JSON.stringify(value)} }`)
|
||||||
return value // 快速匹配成功
|
return value // 快速匹配成功
|
||||||
}
|
}
|
||||||
|
value = hostMap.origin[`*${hostname}`]
|
||||||
|
if (value != null) {
|
||||||
|
log.info(`matchHostname: ${action}: '${hostname}' -> { "*${hostname}": ${JSON.stringify(value)} }`)
|
||||||
|
return value // 快速匹配成功
|
||||||
|
}
|
||||||
|
|
||||||
// 通配符匹配 或 正则表达式匹配
|
// 通配符匹配 或 正则表达式匹配
|
||||||
for (const regexp in hostMap) {
|
for (const regexp in hostMap) {
|
||||||
|
|
Loading…
Reference in New Issue