Skip to content

Commit 64d5ae5

Browse files
bugfix: 修复 matchUtil.domainMapRegexply() 方法BUG导致部分配置丢失有问题
1 parent b6e3b5c commit 64d5ae5

File tree

3 files changed

+80
-28
lines changed

3 files changed

+80
-28
lines changed

packages/mitmproxy/src/utils/util.match.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ function domainMapRegexply (hostMap) {
3232
const regexpMap = {}
3333
const origin = {} // 用于快速匹配,见matchHostname、matchHostnameAll方法
3434
lodash.each(hostMap, (value, domain) => {
35-
if (lodash.isEmpty(value)) {
36-
return
37-
}
38-
3935
// 将域名匹配串格式如 `.xxx.com` 转换为 `*.xxx.com`
40-
if (domain[0] === '.' && lodash.isEmpty(hostMap[`*${domain}`])) {
36+
if (domain[0] === '.') {
37+
if (hostMap[`*${domain}`] != null) {
38+
return // 如果已经有匹配串 `*.xxx.com`,则忽略 `.xxx.com`
39+
}
4140
domain = `*${domain}`
4241
}
4342

packages/mitmproxy/test/lodashTest.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1+
const assert = require('node:assert')
12
const lodash = require('lodash')
23

4+
// test lodash.isEqual
35
const arr1 = [1, 2, 3]
4-
const arr2 = [3, 2, 1]
5-
console.log(lodash.isEqual(arr1.sort(), arr2.sort()))
6+
const arr2 = [1, 2, 3]
7+
const arr3 = [3, 2, 1]
8+
assert.strictEqual(lodash.isEqual(arr1, arr2), true)
9+
assert.strictEqual(lodash.isEqual(arr1.sort(), arr3.sort()), true)
10+
11+
// test lodash.isEmpty
12+
13+
function isEmpty (obj) {
14+
return obj == null || (lodash.isObject(obj) && lodash.isEmpty(obj))
15+
}
16+
17+
// true
18+
assert.strictEqual(isEmpty(null), true)
19+
assert.strictEqual(isEmpty({}), true)
20+
assert.strictEqual(isEmpty([]), true)
21+
// false
22+
assert.strictEqual(isEmpty(true), false)
23+
assert.strictEqual(isEmpty(false), false)
24+
assert.strictEqual(isEmpty(1), false)
25+
assert.strictEqual(isEmpty(0), false)
26+
assert.strictEqual(isEmpty(-1), false)
27+
assert.strictEqual(isEmpty(''), false)
28+
assert.strictEqual(isEmpty('1'), false)
+51-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const assert = require('node:assert')
12
const matchUtil = require('../src/utils/util.match')
23

34
const hostMap = matchUtil.domainMapRegexply({
@@ -6,48 +7,77 @@ const hostMap = matchUtil.domainMapRegexply({
67
'*.ccc.com': true,
78
'^.{1,3}ddd.com$': true,
89
'*.cn': true,
10+
'.github.com': true,
11+
12+
'*.eee.com': true,
13+
'.eee.com': false, // 此配置将被忽略,因为有 '*.eee.com' 了,优先级更高
914
})
1015

1116
console.log(hostMap)
17+
assert.strictEqual(hostMap['^.*bbb\\.com$'], true)
18+
assert.strictEqual(hostMap['^.*\\.ccc\\.com$'], true)
19+
assert.strictEqual(hostMap['^.{1,3}ddd.com$'], true)
20+
assert.strictEqual(hostMap['^.*\\.cn$'], true)
21+
assert.strictEqual(hostMap['^.*\\.github\\.com$'], true)
22+
assert.strictEqual(hostMap['^.*\\.github\\.com$'], true)
23+
assert.strictEqual(hostMap['^.*\\.eee\\.com$'], true)
24+
25+
const origin = hostMap.origin
26+
assert.strictEqual(origin['aaa.com'], true)
27+
assert.strictEqual(origin['*bbb.com'], true)
28+
assert.strictEqual(origin['*.ccc.com'], true)
29+
assert.strictEqual(origin['*.cn'], true)
30+
assert.strictEqual(origin['*.github.com'], true)
31+
assert.strictEqual(origin['.eee.com'], undefined)
1232

13-
console.log('test1: aaa.com')
1433
const value11 = matchUtil.matchHostname(hostMap, 'aaa.com', 'test1.1')
1534
const value12 = matchUtil.matchHostname(hostMap, 'aaaa.com', 'test1.2')
1635
const value13 = matchUtil.matchHostname(hostMap, 'aaaa.comx', 'test1.3')
17-
console.log(value11) // true
18-
console.log(value12) // undefined
19-
console.log(value13) // undefined
36+
console.log('test1: aaa.com')
37+
assert.strictEqual(value11, true)
38+
assert.strictEqual(value12, undefined)
39+
assert.strictEqual(value13, undefined)
2040

21-
console.log('test2: *bbb.com')
2241
const value21 = matchUtil.matchHostname(hostMap, 'bbb.com', 'test2.1')
2342
const value22 = matchUtil.matchHostname(hostMap, 'xbbb.com', 'test2.2')
2443
const value23 = matchUtil.matchHostname(hostMap, 'bbb.comx', 'test2.3')
2544
const value24 = matchUtil.matchHostname(hostMap, 'x.bbb.com', 'test2.4')
26-
console.log(value21) // true
27-
console.log(value22) // true
28-
console.log(value23) // undefined
29-
console.log(value24) // true
45+
console.log('test2: *bbb.com')
46+
assert.strictEqual(value21, true)
47+
assert.strictEqual(value22, true)
48+
assert.strictEqual(value23, undefined)
49+
assert.strictEqual(value24, true)
3050

31-
console.log('test3: *.ccc.com')
3251
const value31 = matchUtil.matchHostname(hostMap, 'ccc.com', 'test3.1')
3352
const value32 = matchUtil.matchHostname(hostMap, 'x.ccc.com', 'test3.2')
3453
const value33 = matchUtil.matchHostname(hostMap, 'xccc.com', 'test3.3')
35-
console.log(value31) // true
36-
console.log(value32) // true
37-
console.log(value33) // undefined
54+
console.log('test3: *.ccc.com')
55+
assert.strictEqual(value31, true)
56+
assert.strictEqual(value32, true)
57+
assert.strictEqual(value33, undefined)
3858

39-
console.log('test4: ^.{1,3}ddd.com$')
4059
const value41 = matchUtil.matchHostname(hostMap, 'ddd.com', 'test4.1')
4160
const value42 = matchUtil.matchHostname(hostMap, 'x.ddd.com', 'test4.2')
4261
const value43 = matchUtil.matchHostname(hostMap, 'xddd.com', 'test4.3')
43-
console.log(value41) // undefined
44-
console.log(value42) // true
45-
console.log(value43) // true
62+
console.log('test4: ^.{1,3}ddd.com$')
63+
assert.strictEqual(value41, undefined)
64+
assert.strictEqual(value42, true)
65+
assert.strictEqual(value43, true)
4666

47-
console.log('test5: *.cn')
4867
const value51 = matchUtil.matchHostname(hostMap, 'eee.cn', 'test5.1')
4968
const value52 = matchUtil.matchHostname(hostMap, 'x.eee.cn', 'test5.2')
5069
const value53 = matchUtil.matchHostname(hostMap, 'aaaa.cnet.com', 'test5.3')
51-
console.log(value51) // true
52-
console.log(value52) // true
53-
console.log(value53) // undefined
70+
console.log('test5: *.cn')
71+
assert.strictEqual(value51, true)
72+
assert.strictEqual(value52, true)
73+
assert.strictEqual(value53, undefined)
74+
75+
const value61 = matchUtil.matchHostname(hostMap, 'github.com', 'test6.1')
76+
const value62 = matchUtil.matchHostname(hostMap, 'api.github.com', 'test6.2')
77+
const value63 = matchUtil.matchHostname(hostMap, 'aa.bb.github.com', 'test6.3')
78+
const value64 = matchUtil.matchHostname(hostMap, 'aaagithub.com', 'test6.4')
79+
console.log('test6: .github.com')
80+
assert.strictEqual(value61, true)
81+
assert.strictEqual(value62, true)
82+
assert.strictEqual(value63, true)
83+
assert.strictEqual(value64, undefined)

0 commit comments

Comments
 (0)