Skip to content

Commit

Permalink
feat: 支持从 Surge 配置中解析转换 Quantumult X 的以下几种脚本
Browse files Browse the repository at this point in the history
- script-response-body
- script-response-header
- script-request-header
- script-request-body
  • Loading branch information
geekdada committed Apr 7, 2020
1 parent ece0b3a commit 066060d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/generator/__tests__/__snapshots__/template.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ Generated by [AVA](https://avajs.dev).
https://api.zhihu.com/.*/questions url script-response-body https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20answer.js␊
https://api.zhihu.com/market/header url script-response-body https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20market.js␊
https://api.zhihu.com/people/ url script-response-body https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js␊
https://api.zhihu.com/.*/questions url script-request-body https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20answer.js␊
https://api.zhihu.com/market/header url script-request-header https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20market.js␊
# RULE 3`

## stringify
Expand Down
Binary file modified lib/generator/__tests__/__snapshots__/template.test.ts.snap
Binary file not shown.
29 changes: 25 additions & 4 deletions lib/generator/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ export function getEngine(templateDir: string): nunjucks.Environment {
if (testString.startsWith('HTTP-RESPONSE')) {
return convertSurgeScriptRuleToQuantumultXRewriteRule(item);
}
if (testString.startsWith('HTTP-REQUEST')) {
return convertSurgeScriptRuleToQuantumultXRewriteRule(item);
}

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

Expand Down Expand Up @@ -117,17 +120,35 @@ export const convertSurgeScriptRuleToQuantumultXRewriteRule = (str: string): str
const result = [];

switch (parts[0]) {
case 'http-response':
case 'http-response': {
const params = decodeStringList(parts.splice(2).join('').split(','));
const scriptPath = params['script-path'];
const isRequireBody = 'requires-body' in params;

// parts[1] => Effective URL Rule
result.push(parts[1], 'url', 'script-response-body', scriptPath);
if (isRequireBody) {
// parts[1] => Effective URL Rule
result.push(parts[1], 'url', 'script-response-body', scriptPath);
} else {
result.push(parts[1], 'url', 'script-response-header', scriptPath);
}

return result.join(' ');
}
case 'http-request': {
const params = decodeStringList(parts.splice(2).join('').split(','));
const scriptPath = params['script-path'];
const isRequireBody = 'requires-body' in params;

if (isRequireBody) {
// parts[1] => Effective URL Rule
result.push(parts[1], 'url', 'script-request-body', scriptPath);
} else {
result.push(parts[1], 'url', 'script-request-header', scriptPath);
}

return result.join(' ');
}
default:
return '';
}

};
2 changes: 2 additions & 0 deletions test/asset/surge-script-list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ http-response https://api.zhihu.com/topstory/recommend requires-body=1,max-size=
http-response https://api.zhihu.com/.*/questions requires-body=1,max-size=0,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20answer.js,script-update-interval=-1
http-response https://api.zhihu.com/market/header requires-body=1,max-size=0,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20market.js,script-update-interval=-1
http-response https://api.zhihu.com/people/ requires-body=1,max-size=0,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20people.js,script-update-interval=-1
http-request https://api.zhihu.com/.*/questions requires-body=1,max-size=0,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20answer.js,script-update-interval=-1
http-request https://api.zhihu.com/market/header max-size=0,script-path=https://raw.githubusercontent.com/onewayticket255/Surge-Script/master/surge%20zhihu%20market.js,script-update-interval=-1

# RULE 3
dns dnspod script-path=http://example.com/dnspod.js
42 changes: 42 additions & 0 deletions test/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,33 @@ test('getQuantumultXNodes', t => {
t.is(schemeList[5], 'shadowsocks=us.example.com:443, method=chacha20-ietf-poly1305, password=password, obfs=tls, obfs-host=gateway-carry.icloud.com, udp-relay=true, fast-open=true, tag=🇺🇸US 1');
t.is(schemeList[6], 'vmess=1.1.1.1:443, method=chacha20-ietf-poly1305, password=1386f85e-657b-4d6e-9d56-78badb75e1fd, udp-relay=true, obfs=over-tls, tag=测试 4');

t.is(
utils.getQuantumultXNodes([{
type: NodeTypeEnum.Vmess,
alterId: '64',
hostname: '1.1.1.1',
method: 'auto',
network: 'tcp',
nodeName: '测试',
port: 443,
tls: true,
tls13: true,
uuid: '1386f85e-657b-4d6e-9d56-78badb75e1fd',
}]),
'vmess=1.1.1.1:443, method=chacha20-ietf-poly1305, password=1386f85e-657b-4d6e-9d56-78badb75e1fd, udp-relay=true, obfs=over-tls, tag=测试'
);
t.is(
utils.getQuantumultXNodes([{
type: NodeTypeEnum.HTTPS,
nodeName: 'test',
hostname: 'a.com',
port: 443,
tls13: true,
username: 'snsms',
password: 'nndndnd',
}]),
'http=a.com:443, username=snsms, password=nndndnd, over-tls=true, tls-verification=true, tls13=true, tag=test'
);
t.is(
utils.getQuantumultXNodes([{
type: NodeTypeEnum.Trojan,
Expand All @@ -1262,6 +1289,21 @@ test('getQuantumultXNodes', t => {
}]),
'trojan=example.com:443, password=password1, over-tls=true, tls-verification=false, fast-open=true, udp-relay=true, tag=trojan'
);
t.is(
utils.getQuantumultXNodes([{
type: NodeTypeEnum.Trojan,
nodeName: 'trojan',
hostname: 'example.com',
port: 443,
password: 'password1',
sni: 'sni.example.com',
'udp-relay': true,
skipCertVerify: true,
tfo: true,
tls13: true,
}]),
'trojan=example.com:443, password=password1, over-tls=true, tls-verification=false, tls-host=sni.example.com, fast-open=true, udp-relay=true, tls13=true, tag=trojan'
);
});

test('formatV2rayConfig', t => {
Expand Down

0 comments on commit 066060d

Please sign in to comment.