Skip to content

Commit 4d01c26

Browse files
committed
Merge sites according to PSL by default (#97)
1 parent e90c0c7 commit 4d01c26

File tree

6 files changed

+47
-15
lines changed

6 files changed

+47
-15
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@types/generate-json-webpack-plugin": "^0.3.4",
2121
"@types/jest": "^27.5.0",
2222
"@types/node": "^17.0.31",
23+
"@types/psl": "^1.1.0",
2324
"@types/webpack": "^5.28.0",
2425
"@types/webpack-bundle-analyzer": "^4.4.1",
2526
"babel-loader": "^8.2.5",
@@ -49,7 +50,8 @@
4950
"clipboardy": "^3.0.0",
5051
"echarts": "^5.3.2",
5152
"element-plus": "1.2.0-beta.6",
53+
"psl": "^1.8.0",
5254
"vue": "^3.2.33",
5355
"vue-router": "^4.0.14"
5456
}
55-
}
57+
}

src/app/components/rule-merge/alert-info.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,38 @@
66
*/
77

88
import { ElAlert } from "element-plus"
9-
import { t } from "@app/locale"
9+
import { t, tN } from "@app/locale"
1010
import { defineComponent, h } from "vue"
1111
import { MergeRuleMessage } from "@app/locale/components/merge-rule"
12+
import { PSL_HOMEPAGE } from "@util/constant/url"
1213

13-
const liKeys: (keyof MergeRuleMessage)[] = ['infoAlert0', 'infoAlert1', 'infoAlert2', 'infoAlert3', 'infoAlert4', 'infoAlert5']
14+
const liKeys: (keyof MergeRuleMessage)[] = ['infoAlert0', 'infoAlert1', 'infoAlert2', 'infoAlert3', 'infoAlert4']
1415
const title = t(msg => msg.mergeRule.infoAlertTitle)
1516

17+
const pslStyle: Partial<CSSStyleDeclaration> = {
18+
fontSize: "var(--el-alert-description-font-size)",
19+
color: "var(--el-color-info)",
20+
marginLeft: "2px",
21+
marginRight: "2px",
22+
}
23+
24+
function renderPslLink() {
25+
return h('a', {
26+
href: PSL_HOMEPAGE,
27+
style: pslStyle,
28+
target: '_blank'
29+
}, 'Public Suffix List')
30+
}
31+
1632
const _default = defineComponent({
1733
name: "RuleMergeAlertInfo",
1834
render() {
1935
return h(ElAlert,
2036
{ type: 'info', title },
21-
() => liKeys.map(key => h('li', t(msg => msg.mergeRule[key])))
37+
() => [
38+
...liKeys.map(key => h('li', t(msg => msg.mergeRule[key]))),
39+
h('li', tN(msg => msg.mergeRule.infoAlert5, { psl: renderPslLink() }))
40+
]
2241
)
2342
}
2443
})

src/app/locale/components/merge-rule.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const _default: Messages<MergeRuleMessage> = {
4040
infoAlert2: '合并后域名可填具体的域名,或者填数字,或者不填',
4141
infoAlert3: '如果填数字,则表示合并后域名的级数。比如存在规则【 *.*.edu.cn >>> 3 】,那么 www.hust.edu.cn 将被合并至 hust.edu.cn',
4242
infoAlert4: '如果不填,则表示原域名不会被合并',
43-
infoAlert5: '如果没有匹配任何规则,则默认合并至 2 级域名'
43+
infoAlert5: '如果没有匹配任何规则,则默认会合并至 {psl} 的前一级'
4444
},
4545
zh_TW: {
4646
resultOfOrigin: '不合並',
@@ -57,7 +57,7 @@ const _default: Messages<MergeRuleMessage> = {
5757
infoAlert2: '合並後網域可填具體的網域,或者填數字,或者不填',
5858
infoAlert3: '如果填數字,則表示合並後網域的級數。比如存在規則【 *.*.edu.cn >>> 3 】,那麼 www.hust.edu.cn 將被合並至 hust.edu.cn',
5959
infoAlert4: '如果不填,則表示原網域不會被合並',
60-
infoAlert5: '如果沒有匹配任何規則,則默認合並至 2 級網域'
60+
infoAlert5: '如果沒有匹配任何規則,則默認會合並至 {psl} 的前一級',
6161
},
6262
en: {
6363
resultOfOrigin: 'Not Merge',
@@ -74,7 +74,7 @@ const _default: Messages<MergeRuleMessage> = {
7474
infoAlert2: 'The merged site can be filled with a specific site, number, or blank',
7575
infoAlert3: 'Number means the level of merged site. For example, there is a rule "*.*.edu.cn >>> 3", then "www.hust.edu.cn" will be merged to "hust.edu.cn"',
7676
infoAlert4: 'Blank means the origin site will not be merged',
77-
infoAlert5: 'If no rule are matched, sites will be merged into the second-level domain name by default'
77+
infoAlert5: 'If no rule is matched, it will default to the level before {psl}'
7878
},
7979
ja: {
8080
resultOfOrigin: '不合并',
@@ -91,7 +91,7 @@ const _default: Messages<MergeRuleMessage> = {
9191
infoAlert2: '統合されたドメイン名の後、特定のドメイン名を入力するか、番号を入力するか、空白のままにすることができます',
9292
infoAlert3: '数字を記入する場合は、ドメイン名のレベルが予約されていることを意味します。 たとえば、ルール [*.*.edu.cn >>> 3 ] がある場合、www.hust.edu.cn は hust.edu.cn にマージされます。',
9393
infoAlert4: '記入しない場合は、元のドメイン名が統合されないことを意味します',
94-
infoAlert5: '一致するルールがない場合、デフォルトで第 2 レベル ドメイン名にマージされます'
94+
infoAlert5: '一致するルールがない場合、デフォルトで {psl} より前のレベルになります'
9595
}
9696
}
9797

src/service/components/host-merge-ruler.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import HostMergeRuleItem from "@entity/dto/host-merge-rule-item"
99
import { isIpAndPort } from "@util/pattern"
10+
import psl from "psl"
1011

1112
/**
1213
* Ruler to merge host
@@ -74,11 +75,16 @@ export default class CustomizedHostMergeRuler implements IHostMergeRuler {
7475
merge(origin: string): string {
7576
// First check the static rules
7677
let merged = this.noRegMergeRules[origin]
77-
// The check the regular rules
78+
// Then check the regular rules
7879
let matchResult: undefined | RegRuleItem = undefined
7980
merged === undefined && (matchResult = this.regulars.find(item => item.reg.test(origin)))
8081
matchResult && (merged = matchResult.result)
81-
return this.merge0(merged === undefined ? 1 : merged, origin)
82+
if (merged === undefined) {
83+
// No rule matched
84+
return isIpAndPort(origin) ? origin : psl.get(origin)
85+
} else {
86+
return this.merge0(merged, origin)
87+
}
8288
}
8389

8490
private merge0(merged: string | number, origin: string): string {

src/util/constant/url.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,9 @@ export function iconUrlOfBrowser(protocol: string, host: string): string {
8888
/**
8989
* @since 0.2.9
9090
*/
91-
export const MEAT_URL = 'https://dun.mianbaoduo.com/@yangyang'
91+
export const MEAT_URL = 'https://dun.mianbaoduo.com/@yangyang'
92+
93+
/**
94+
* @since 0.9.3
95+
*/
96+
export const PSL_HOMEPAGE = 'https://publicsuffix.org/'

test/service/host-merge-ruler.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ test('specify merged url', () => {
2121
test1('www.baidu.com', targetUrl, 'www.baidu.com', targetUrl)
2222
test1('www.*.com', targetUrl, 'www.baidu.com', targetUrl)
2323
test1('www.*.*', targetUrl, 'www.baidu.123', targetUrl)
24-
// Not match
24+
// Not match, so merged with the public suffix
2525
test1('www.*.*', targetUrl, 'www1.baidu.123', 'baidu.123')
26-
test1('www.baidu.com', targetUrl, 'www.baidu.com.hk', 'com.hk')
26+
test1('www.baidu.com', targetUrl, 'www.baidu.com.hk', 'baidu.com.hk')
2727
})
2828

2929
test('specify kept dots', () => {
@@ -38,6 +38,6 @@ test('specify kept dots', () => {
3838
test('specify nothing', () => {
3939
test1('*.*.edu.cn', '', 'www.hust.edu.cn', 'www.hust.edu.cn')
4040
test1('*.*.edu.cn', '', '123.hust.edu.cn', '123.hust.edu.cn')
41-
// Not match
42-
test1('*.*.edu.cn', '', 'hust.edu.cn', 'edu.cn')
41+
// Not match, so merged with the public suffix
42+
test1('*.*.edu.cn', '', 'xx.xx.hust.edu.cn', 'hust.edu.cn')
4343
})

0 commit comments

Comments
 (0)