Skip to content

Commit

Permalink
feat: Clash 规则处理优化
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Feb 10, 2020
1 parent b8bf280 commit bec9abe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 34 deletions.
29 changes: 19 additions & 10 deletions lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { URL } from 'url';

import { decodeStringList, toBase64 } from './utils';
import {
CLASH_UNSUPPORTED_RULE,
MELLOW_UNSUPPORTED_RULE,
QUANTUMULT_X_SUPPORTED_RULE
QUANTUMULT_X_SUPPORTED_RULE,
CLASH_SUPPORTED_RULE,
} from './utils/constant';

export default function getEngine(templateDir: string, publicUrl: string): nunjucks.Environment {
Expand All @@ -19,19 +19,28 @@ export default function getEngine(templateDir: string, publicUrl: string): nunju
const array = str.split('\n');

return array
.filter(item => {
.map(item => {
const testString: string = (!!item && item.trim() !== '') ? item.toUpperCase() : '';

return CLASH_UNSUPPORTED_RULE.every(s => !testString.startsWith(s));
})
.map((item: string) => {
if (item.startsWith('#') || item.trim() === '') {
if (testString.startsWith('#') || testString === '') {
return item;
}
return `- ${item}`
.replace(/\/\/.*$/, '')
.trim();

const matched = testString.match(/^([\w-]+),/);

if (
matched &&
CLASH_SUPPORTED_RULE.some(s => matched[1] === s)
) {
// 过滤出支持的规则类型
return `- ${item}`
.replace(/\/\/.*$/, '')
.trim();
}

return null;
})
.filter(item => !!item)
.join('\n');
};

Expand Down
16 changes: 12 additions & 4 deletions lib/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ export const PROXY_TEST_URL = 'http://www.qualcomm.cn/generate_204';

export const PROXY_TEST_INTERVAL = 1200;

export const CLASH_UNSUPPORTED_RULE: ReadonlyArray<string> = [
'USER-AGENT', 'PROCESS-NAME', 'URL-REGEX', 'DEST-PORT', 'IN-PORT', 'RULE-SET',
'SRC-IP'
export const CLASH_SUPPORTED_RULE: ReadonlyArray<string> = [
'DOMAIN-SUFFIX',
'DOMAIN-KEYWORD',
'DOMAIN',
'SRC-IP-CIDR',
'IP-CIDR',
'GEOIP',
'DST-PORT',
'SRC-PORT',
'MATCH',
'FINAL',
];

export const QUANTUMULT_X_SUPPORTED_RULE: ReadonlyArray<string> = [
Expand Down Expand Up @@ -52,4 +60,4 @@ export const CATEGORIES = {
QUANTUMULT_X_FILTER: 'Quantumult X Filter',
QUANTUMULT_X_REWRITE: 'Quantumult X Rewrite',
CLASH: 'Clash',
};
};
22 changes: 2 additions & 20 deletions test/snapshots/template.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

The actual snapshot is saved in `template.test.ts.snap`.

Generated by [AVA](https://ava.li).
Generated by [AVA](https://avajs.dev).

## ForeignMedia

Expand Down Expand Up @@ -219,39 +219,30 @@ Generated by [AVA](https://ava.li).
# > Deezer␊
- DOMAIN-SUFFIX,deezer.com␊
- DOMAIN-SUFFIX,dzcdn.net␊
# > KKBOX␊
- DOMAIN-SUFFIX,kkbox.com␊
- DOMAIN-SUFFIX,kkbox.com.tw␊
- DOMAIN-SUFFIX,kfs.io␊
# > JOOX␊
- DOMAIN-SUFFIX,joox.com␊
# > Pandora␊
- DOMAIN-SUFFIX,pandora.com␊
# > SoundCloud␊
- DOMAIN-SUFFIX,p-cdn.us␊
- DOMAIN-SUFFIX,sndcdn.com␊
- DOMAIN-SUFFIX,soundcloud.com␊
# > Spotify␊
- DOMAIN-SUFFIX,pscdn.co␊
- DOMAIN-SUFFIX,scdn.co␊
- DOMAIN-SUFFIX,spotify.com␊
- DOMAIN-SUFFIX,spoti.fi␊
# > TIDAL␊
- DOMAIN-SUFFIX,tidal.com␊
# > YouTubeMusic␊
# (Video)␊
# > All4␊
- DOMAIN-SUFFIX,c4assets.com␊
- DOMAIN-SUFFIX,channel4.com␊
# > AbemaTV␊
- DOMAIN-SUFFIX,abema.io␊
- DOMAIN-SUFFIX,ameba.jp␊
Expand All @@ -261,44 +252,37 @@ Generated by [AVA](https://ava.li).
- DOMAIN,ds-linear-abematv.akamaized.net␊
- DOMAIN,ds-vod-abematv.akamaized.net␊
- DOMAIN,linear-abematv.akamaized.net␊
# > Amazon Prime Video␊
- DOMAIN-SUFFIX,aiv-cdn.net␊
- DOMAIN-SUFFIX,aiv-delivery.net␊
- DOMAIN-SUFFIX,amazonvideo.com␊
- DOMAIN-SUFFIX,media-amazon.com␊
- DOMAIN-SUFFIX,primevideo.com␊
# > Bahamut␊
- DOMAIN-SUFFIX,bahamut.com.tw␊
- DOMAIN-SUFFIX,gamer.com.tw␊
- DOMAIN,gamer-cds.cdn.hinet.net␊
- DOMAIN,gamer2-cds.cdn.hinet.net␊
# > BBC iPlayer␊
- DOMAIN-SUFFIX,bbc.co.uk␊
- DOMAIN-SUFFIX,bbci.co.uk␊
- DOMAIN-KEYWORD,bbcfmt␊
- DOMAIN-KEYWORD,uk-live␊
# > DAZN␊
- DOMAIN-SUFFIX,dazn.com␊
- DOMAIN-SUFFIX,dazn-api.com␊
- DOMAIN,d151l6v8er5bdm.cloudfront.net␊
- DOMAIN-KEYWORD,voddazn␊
# > Disney+␊
- DOMAIN-SUFFIX,bamgrid.com␊
- DOMAIN-SUFFIX,disney-plus.net␊
- DOMAIN-SUFFIX,disneyplus.com␊
- DOMAIN-SUFFIX,dssott.com␊
- DOMAIN,cdn.registerdisney.go.com␊
# > encoreTVB␊
- DOMAIN-SUFFIX,encoretvb.com␊
- DOMAIN,edge.api.brightcove.com␊
- DOMAIN,bcbolt446c5271-a.akamaihd.net␊
# > Fox+ & Fox Now␊
- DOMAIN-SUFFIX,dashasiafox.akamaized.net␊
- DOMAIN-SUFFIX,fox.com␊
Expand All @@ -307,7 +291,6 @@ Generated by [AVA](https://ava.li).
- DOMAIN-SUFFIX,staticasiafox.akamaized.net␊
- DOMAIN-SUFFIX,theplatform.com␊
- DOMAIN-SUFFIX,uplynk.com␊
# > HBO Now & HBO GO␊
- DOMAIN-SUFFIX,hbo.com␊
- DOMAIN-SUFFIX,hbogo.com␊
Expand Down Expand Up @@ -395,8 +378,7 @@ Generated by [AVA](https://ava.li).
# > YouTube␊
- DOMAIN-SUFFIX,googlevideo.com␊
- DOMAIN-SUFFIX,youtube.com␊
- DOMAIN,youtubei.googleapis.com␊
`
- DOMAIN,youtubei.googleapis.com`

> Snapshot 3
Expand Down
Binary file modified test/snapshots/template.test.ts.snap
Binary file not shown.

0 comments on commit bec9abe

Please sign in to comment.