Skip to content

Commit

Permalink
feat: add new clash directives
Browse files Browse the repository at this point in the history
Close #148
  • Loading branch information
geekdada committed Jun 20, 2021
1 parent 54de8a8 commit 5a9f4dd
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 238 deletions.
146 changes: 103 additions & 43 deletions lib/generator/__tests__/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { join } from 'path';
import {
convertNewSurgeScriptRuleToQuantumultXRewriteRule,
convertSurgeScriptRuleToQuantumultXRewriteRule,
getEngine, loadLocalSnippet,
getEngine,
loadLocalSnippet,
} from '../template';

const templateEngine = getEngine(process.cwd());
const assetDir = join(__dirname, '../../../test/asset/');

test('clash #1', t => {
const body = `{{ str | patchYamlArray }}`;
const body = `{{ str | clash }}`;
const str = `IP-CIDR,67.198.55.0/24,Proxy,no-resolve`;
const result = templateEngine.renderString(body, {
str,
Expand All @@ -23,7 +24,7 @@ test('clash #1', t => {
});

test('clash #2', t => {
const body = `{{ str | patchYamlArray }}`;
const body = `{{ str | clash }}`;
const str = `IP-CIDR,67.198.55.0/24,Proxy,no-resolve // test rule`;
const result = templateEngine.renderString(body, {
str,
Expand All @@ -33,7 +34,7 @@ test('clash #2', t => {
});

test('clash #3', t => {
const body = `{{ str | patchYamlArray }}`;
const body = `{{ str | clash }}`;
const str = `PROCESS-NAME,Telegram,Proxy,no-resolve // test rule`;
const result = templateEngine.renderString(body, {
str,
Expand All @@ -43,7 +44,7 @@ test('clash #3', t => {
});

test('clash #4', t => {
const body = `{{ str | patchYamlArray }}`;
const body = `{{ str | clash }}`;
const str = `# Comment`;
const result = templateEngine.renderString(body, {
str,
Expand All @@ -53,7 +54,7 @@ test('clash #4', t => {
});

test('clash #5', t => {
const body = `{{ str | patchYamlArray }}`;
const body = `{{ str | clash }}`;
const str = `# Comment`;
const result = templateEngine.renderString(body, {
str,
Expand All @@ -63,7 +64,7 @@ test('clash #5', t => {
});

test('clash #6', t => {
const body = `{{ str | patchYamlArray }}`;
const body = `{{ str | clash }}`;
const str = `URL-REGEX,xxxxxxxxxxxx`;
const result = templateEngine.renderString(body, {
str,
Expand All @@ -79,6 +80,34 @@ test('clash #7', t => {
t.is(templateEngine.renderString(body, { str: ' ' }), ' ');
});

test('clash #8', t => {
const body = `{{ str | clash }}`;

t.is(
templateEngine.renderString(body, {
str: 'IP-CIDR6,2011:dab8:3456:82a0::/50,DIRECT',
}),
`- IP-CIDR6,2011:dab8:3456:82a0::/50,DIRECT`,
);
t.is(
templateEngine.renderString(body, {
str: 'IP-CIDR6,2011:dab8:3456:82a0::/50,DIRECT,no-resolve',
}),
`- IP-CIDR6,2011:dab8:3456:82a0::/50,DIRECT,no-resolve`,
);
});

test('clash #9', t => {
const body = `{{ str | clash }}`;

t.is(
templateEngine.renderString(body, {
str: 'GEOIP,HK,DIRECT,no-resolve',
}),
`- GEOIP,HK,DIRECT,no-resolve`,
);
});

test('base64', t => {
const body = `{{ str | base64 }}`;
const str = `testtesttesttest`;
Expand All @@ -97,19 +126,21 @@ test('quantumultx filter 1', t => {
templateEngine.renderString(body, {
str: `PROCESS-NAME,Telegram,Proxy,no-resolve // test rule`,
}),
''
'',
);
t.is(
templateEngine.renderString(body, {
str: 'IP-CIDR6, 2001:4860:4860::8888/32, DIRECT',
}),
'IP6-CIDR, 2001:4860:4860::8888/32, DIRECT'
'IP6-CIDR, 2001:4860:4860::8888/32, DIRECT',
);
});

test('quantumultx filter 2', t => {
const body = `{{ str | quantumultx }}`;
const str = fs.readFileSync(join(assetDir, 'surge-script-list.txt'), { encoding: 'utf8' });
const str = fs.readFileSync(join(assetDir, 'surge-script-list.txt'), {
encoding: 'utf8',
});
const result = templateEngine.renderString(body, {
str,
});
Expand Down Expand Up @@ -155,13 +186,13 @@ test('loon filter 1', t => {
});

t.is(result, '# Comment');
})
});

test('loon filter 2', t => {
const body = `{{ str | loon }}`;
const str = [
'IP-CIDR,67.198.55.0/24,Proxy,no-resolve // test rule',
'DOMAIN,example.com,Proxy,force-remote-dns'
'DOMAIN,example.com,Proxy,force-remote-dns',
].join('\n');
const result = templateEngine.renderString(body, {
str,
Expand All @@ -172,7 +203,7 @@ test('loon filter 2', t => {
[
'IP-CIDR,67.198.55.0/24,Proxy,no-resolve',
'DOMAIN,example.com,Proxy,force-remote-dns',
].join('\n')
].join('\n'),
);
});

Expand All @@ -191,66 +222,95 @@ test('spaces in string', t => {

t.is(templateEngine.renderString(`{{ str | mellow }}`, { str }), ' ');
t.is(templateEngine.renderString(`{{ str | quantumultx }}`, { str }), ' ');
t.is(templateEngine.renderString(`{{ str | patchYamlArray }}`, { str }), ' ');
t.is(templateEngine.renderString(`{{ str | clash }}`, { str }), ' ');
});

test('ForeignMedia', t => {
const str = fs.readFileSync(join(assetDir, 'ForeignMedia.list'), { encoding: 'utf8' });
const str = fs.readFileSync(join(assetDir, 'ForeignMedia.list'), {
encoding: 'utf8',
});

t.snapshot(templateEngine.renderString(`{{ str | quantumultx }}`, {
str,
}));
t.snapshot(templateEngine.renderString(`{{ str | clash }}`, {
str,
}));
t.snapshot(templateEngine.renderString(`{{ str | mellow }}`, {
str,
}));
t.snapshot(
templateEngine.renderString(`{{ str | quantumultx }}`, {
str,
}),
);
t.snapshot(
templateEngine.renderString(`{{ str | clash }}`, {
str,
}),
);
t.snapshot(
templateEngine.renderString(`{{ str | mellow }}`, {
str,
}),
);
});

test('stringify', t => {
const obj = {
foo: 'bar',
};

t.snapshot(templateEngine.renderString(`{{ obj | yaml }}`, {
obj,
}));
t.snapshot(templateEngine.renderString(`{{ obj | json }}`, {
obj,
}));
t.snapshot(
templateEngine.renderString(`{{ obj | yaml }}`, {
obj,
}),
);
t.snapshot(
templateEngine.renderString(`{{ obj | json }}`, {
obj,
}),
);
});

test('convertSurgeScriptRuleToQuantumultXRewriteRule', t => {
t.is(convertSurgeScriptRuleToQuantumultXRewriteRule(''), '');
t.is(convertSurgeScriptRuleToQuantumultXRewriteRule('unknown-type https://api.zhihu.com/people/ script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'), '');
t.is(
convertSurgeScriptRuleToQuantumultXRewriteRule(
'unknown-type https://api.zhihu.com/people/ script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js',
),
'',
);
});

test('convertNewSurgeScriptRuleToQuantumultXRewriteRule', t => {
t.is(convertNewSurgeScriptRuleToQuantumultXRewriteRule(''), '');
t.is(
convertNewSurgeScriptRuleToQuantumultXRewriteRule('zhihu people = type=http-response,requires-body=1,max-size=0,pattern=https://api.zhihu.com/people/,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'),
'https://api.zhihu.com/people/ url script-response-body https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'
convertNewSurgeScriptRuleToQuantumultXRewriteRule(
'zhihu people = type=http-response,requires-body=1,max-size=0,pattern=https://api.zhihu.com/people/,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js',
),
'https://api.zhihu.com/people/ url script-response-body https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js',
);
t.is(
convertNewSurgeScriptRuleToQuantumultXRewriteRule('zhihu people = type=http-request,requires-body=1,max-size=0,pattern=https://api.zhihu.com/people/,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'),
'https://api.zhihu.com/people/ url script-request-body https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'
convertNewSurgeScriptRuleToQuantumultXRewriteRule(
'zhihu people = type=http-request,requires-body=1,max-size=0,pattern=https://api.zhihu.com/people/,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js',
),
'https://api.zhihu.com/people/ url script-request-body https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js',
);
t.is(
convertNewSurgeScriptRuleToQuantumultXRewriteRule('zhihu people = type=http-response,pattern=https://api.zhihu.com/people/,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'),
'https://api.zhihu.com/people/ url script-response-header https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'
convertNewSurgeScriptRuleToQuantumultXRewriteRule(
'zhihu people = type=http-response,pattern=https://api.zhihu.com/people/,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js',
),
'https://api.zhihu.com/people/ url script-response-header https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js',
);
t.is(
convertNewSurgeScriptRuleToQuantumultXRewriteRule('zhihu people = type=http-request,pattern=https://api.zhihu.com/people/,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'),
'https://api.zhihu.com/people/ url script-request-header https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js'
convertNewSurgeScriptRuleToQuantumultXRewriteRule(
'zhihu people = type=http-request,pattern=https://api.zhihu.com/people/,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js',
),
'https://api.zhihu.com/people/ url script-request-header https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js',
);
t.is(
convertNewSurgeScriptRuleToQuantumultXRewriteRule('JD = requires-body=1,max-size=0,script-path= https://raw.githubusercontent.com/NobyDa/Script/master/JD-DailyBonus/JD_DailyBonus.js,type=http-response,pattern=^https?://api\.m\.jd\.com/client\.action\?functionId=(start|signBean)'),
'^https?://api\.m\.jd\.com/client\.action\?functionId=(start|signBean) url script-response-body https://raw.githubusercontent.com/NobyDa/Script/master/JD-DailyBonus/JD_DailyBonus.js'
convertNewSurgeScriptRuleToQuantumultXRewriteRule(
'JD = requires-body=1,max-size=0,script-path= https://raw.githubusercontent.com/NobyDa/Script/master/JD-DailyBonus/JD_DailyBonus.js,type=http-response,pattern=^https?://api.m.jd.com/client.action?functionId=(start|signBean)',
),
'^https?://api.m.jd.com/client.action?functionId=(start|signBean) url script-response-body https://raw.githubusercontent.com/NobyDa/Script/master/JD-DailyBonus/JD_DailyBonus.js',
);
t.is(
convertNewSurgeScriptRuleToQuantumultXRewriteRule('zhihu people = type=unknown-type'),
''
convertNewSurgeScriptRuleToQuantumultXRewriteRule(
'zhihu people = type=unknown-type',
),
'',
);
});

Expand Down
Loading

1 comment on commit 5a9f4dd

@vercel
Copy link

@vercel vercel bot commented on 5a9f4dd Jun 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.