Skip to content

Commit 556d5f3

Browse files
committed
feat: 带参数 includeUnsupportedProxy 时, 支持输出 Surge WireGuard Section
1 parent a8c0520 commit 556d5f3

File tree

3 files changed

+131
-40
lines changed

3 files changed

+131
-40
lines changed

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.14.214",
3+
"version": "2.14.217",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/core/proxy-utils/producers/loon.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,11 @@ function wireguard(proxy) {
344344
if (proxy.dns) {
345345
if (Array.isArray(proxy.dns)) {
346346
proxy.dnsv6 = proxy.dns.find((i) => isIPv6(i));
347-
proxy.dns = proxy.dns.find((i) => isIPv4(i));
347+
let dns = proxy.dns.find((i) => isIPv4(i));
348+
if (!dns) {
349+
dns = proxy.dns.find((i) => !isIPv4(i) && !isIPv6(i));
350+
}
351+
proxy.dns = dns;
348352
}
349353
}
350354
result.appendIfPresent(`,dns=${proxy.dns}`, 'dns');

backend/src/core/proxy-utils/producers/surge.js

Lines changed: 125 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Result, isPresent } from './utils';
2-
import { isNotBlank } from '@/utils';
2+
import { isNotBlank, getIfNotBlank } from '@/utils';
33
import $ from '@/core/app';
44

55
const targetPlatform = 'Surge';
@@ -13,7 +13,7 @@ const ipVersions = {
1313
};
1414

1515
export default function Surge_Producer() {
16-
const produce = (proxy) => {
16+
const produce = (proxy, type, opts = {}) => {
1717
switch (proxy.type) {
1818
case 'ss':
1919
return shadowsocks(proxy);
@@ -30,10 +30,14 @@ export default function Surge_Producer() {
3030
case 'tuic':
3131
return tuic(proxy);
3232
case 'wireguard-surge':
33-
return wireguard(proxy);
33+
return wireguard_surge(proxy);
3434
case 'hysteria2':
3535
return hysteria2(proxy);
3636
}
37+
38+
if (opts['include-unsupported-proxy'] && proxy.type === 'wireguard') {
39+
return wireguard(proxy);
40+
}
3741
throw new Error(
3842
`Platform ${targetPlatform} does not support proxy type: ${proxy.type}`,
3943
);
@@ -82,10 +86,8 @@ function shadowsocks(proxy) {
8286
result.append(`,encrypt-method=${proxy.cipher}`);
8387
result.appendIfPresent(`,password=${proxy.password}`, 'password');
8488

85-
result.appendIfPresent(
86-
`,ip-version=${ipVersions[proxy['ip-version']] || proxy['ip-version']}`,
87-
'ip-version',
88-
);
89+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
90+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
8991

9092
result.appendIfPresent(
9193
`,no-error-alert=${proxy['no-error-alert']}`,
@@ -167,10 +169,8 @@ function trojan(proxy) {
167169
result.append(`${proxy.name}=${proxy.type},${proxy.server},${proxy.port}`);
168170
result.appendIfPresent(`,password=${proxy.password}`, 'password');
169171

170-
result.appendIfPresent(
171-
`,ip-version=${ipVersions[proxy['ip-version']] || proxy['ip-version']}`,
172-
'ip-version',
173-
);
172+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
173+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
174174

175175
result.appendIfPresent(
176176
`,no-error-alert=${proxy['no-error-alert']}`,
@@ -236,10 +236,8 @@ function vmess(proxy) {
236236
result.append(`${proxy.name}=${proxy.type},${proxy.server},${proxy.port}`);
237237
result.appendIfPresent(`,username=${proxy.uuid}`, 'uuid');
238238

239-
result.appendIfPresent(
240-
`,ip-version=${ipVersions[proxy['ip-version']] || proxy['ip-version']}`,
241-
'ip-version',
242-
);
239+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
240+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
243241

244242
result.appendIfPresent(
245243
`,no-error-alert=${proxy['no-error-alert']}`,
@@ -314,10 +312,8 @@ function http(proxy) {
314312
result.appendIfPresent(`,${proxy.username}`, 'username');
315313
result.appendIfPresent(`,${proxy.password}`, 'password');
316314

317-
result.appendIfPresent(
318-
`,ip-version=${ipVersions[proxy['ip-version']] || proxy['ip-version']}`,
319-
'ip-version',
320-
);
315+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
316+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
321317

322318
result.appendIfPresent(
323319
`,no-error-alert=${proxy['no-error-alert']}`,
@@ -379,10 +375,8 @@ function socks5(proxy) {
379375
result.appendIfPresent(`,${proxy.username}`, 'username');
380376
result.appendIfPresent(`,${proxy.password}`, 'password');
381377

382-
result.appendIfPresent(
383-
`,ip-version=${ipVersions[proxy['ip-version']] || proxy['ip-version']}`,
384-
'ip-version',
385-
);
378+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
379+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
386380

387381
result.appendIfPresent(
388382
`,no-error-alert=${proxy['no-error-alert']}`,
@@ -445,10 +439,8 @@ function snell(proxy) {
445439
result.appendIfPresent(`,version=${proxy.version}`, 'version');
446440
result.appendIfPresent(`,psk=${proxy.psk}`, 'psk');
447441

448-
result.appendIfPresent(
449-
`,ip-version=${ipVersions[proxy['ip-version']] || proxy['ip-version']}`,
450-
'ip-version',
451-
);
442+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
443+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
452444

453445
result.appendIfPresent(
454446
`,no-error-alert=${proxy['no-error-alert']}`,
@@ -525,10 +517,8 @@ function tuic(proxy) {
525517
'alpn',
526518
);
527519

528-
result.appendIfPresent(
529-
`,ip-version=${ipVersions[proxy['ip-version']] || proxy['ip-version']}`,
530-
'ip-version',
531-
);
520+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
521+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
532522

533523
result.appendIfPresent(
534524
`,no-error-alert=${proxy['no-error-alert']}`,
@@ -587,9 +577,23 @@ function tuic(proxy) {
587577
}
588578

589579
function wireguard(proxy) {
580+
if (Array.isArray(proxy.peers) && proxy.peers.length > 0) {
581+
proxy.server = proxy.peers[0].server;
582+
proxy.port = proxy.peers[0].port;
583+
proxy.ip = proxy.peers[0].ip;
584+
proxy.ipv6 = proxy.peers[0].ipv6;
585+
proxy['public-key'] = proxy.peers[0]['public-key'];
586+
proxy['preshared-key'] = proxy.peers[0]['pre-shared-key'];
587+
// https://github.com/MetaCubeX/mihomo/blob/0404e35be8736b695eae018a08debb175c1f96e6/docs/config.yaml#L717
588+
proxy['allowed-ips'] = proxy.peers[0]['allowed-ips'];
589+
proxy.reserved = proxy.peers[0].reserved;
590+
}
590591
const result = new Result(proxy);
591592

592-
result.append(`${proxy.name}=wireguard`);
593+
result.append(`# WireGuard Proxy ${proxy.name}
594+
${proxy.name}=wireguard`);
595+
596+
proxy['section-name'] = getIfNotBlank(proxy['section-name'], proxy.name);
593597

594598
result.appendIfPresent(
595599
`,section-name=${proxy['section-name']}`,
@@ -600,11 +604,96 @@ function wireguard(proxy) {
600604
'no-error-alert',
601605
);
602606

607+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
608+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
609+
610+
// test-url
611+
result.appendIfPresent(`,test-url=${proxy['test-url']}`, 'test-url');
612+
613+
// shadow-tls
614+
if (isPresent(proxy, 'shadow-tls-password')) {
615+
result.append(`,shadow-tls-password=${proxy['shadow-tls-password']}`);
616+
617+
result.appendIfPresent(
618+
`,shadow-tls-version=${proxy['shadow-tls-version']}`,
619+
'shadow-tls-version',
620+
);
621+
result.appendIfPresent(
622+
`,shadow-tls-sni=${proxy['shadow-tls-sni']}`,
623+
'shadow-tls-sni',
624+
);
625+
}
626+
627+
// block-quic
628+
result.appendIfPresent(`,block-quic=${proxy['block-quic']}`, 'block-quic');
629+
630+
// underlying-proxy
631+
result.appendIfPresent(
632+
`,underlying-proxy=${proxy['underlying-proxy']}`,
633+
'underlying-proxy',
634+
);
635+
636+
result.append(`
637+
# WireGuard Section ${proxy.name}
638+
[WireGuard ${proxy['section-name']}]
639+
private-key = ${proxy['private-key']}`);
640+
641+
result.appendIfPresent(`\nself-ip = ${proxy.ip}`, 'ip');
642+
result.appendIfPresent(`\nself-ip-v6 = ${proxy.ipv6}`, 'ipv6');
643+
if (proxy.dns) {
644+
if (Array.isArray(proxy.dns)) {
645+
proxy.dns = proxy.dns.join(', ');
646+
}
647+
result.append(`\ndns-server = ${proxy.dns}`);
648+
}
649+
result.appendIfPresent(`\nmtu = ${proxy.mtu}`, 'mtu');
650+
651+
if (ip_version === 'prefer-v6') {
652+
result.append(`\nprefer-ipv6 = true`);
653+
}
654+
const allowedIps = Array.isArray(proxy['allowed-ips'])
655+
? proxy['allowed-ips'].join(',')
656+
: proxy['allowed-ips'];
657+
let reserved = Array.isArray(proxy.reserved)
658+
? proxy.reserved.join('/')
659+
: proxy.reserved;
660+
let presharedKey = proxy['preshared-key'] ?? proxy['pre-shared-key'];
661+
if (presharedKey) {
662+
presharedKey = `,preshared-key="${presharedKey}"`;
663+
}
664+
const peer = {
665+
'public-key': proxy['public-key'],
666+
'allowed-ips': allowedIps,
667+
endpoint: `${proxy.server}:${proxy.port}`,
668+
keepalive: proxy['persistent-keepalive'] || proxy.keepalive,
669+
'client-id': reserved,
670+
'preshared-key': presharedKey,
671+
};
672+
result.append(
673+
`\npeer = (${Object.keys(peer)
674+
.filter((k) => peer[k] != null)
675+
.map((k) => `${k} = ${peer[k]}`)
676+
.join(', ')})`,
677+
);
678+
return result.toString();
679+
}
680+
function wireguard_surge(proxy) {
681+
const result = new Result(proxy);
682+
683+
result.append(`${proxy.name}=wireguard`);
684+
603685
result.appendIfPresent(
604-
`,ip-version=${ipVersions[proxy['ip-version']] || proxy['ip-version']}`,
605-
'ip-version',
686+
`,section-name=${proxy['section-name']}`,
687+
'section-name',
688+
);
689+
result.appendIfPresent(
690+
`,no-error-alert=${proxy['no-error-alert']}`,
691+
'no-error-alert',
606692
);
607693

694+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
695+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
696+
608697
// test-url
609698
result.appendIfPresent(`,test-url=${proxy['test-url']}`, 'test-url');
610699

@@ -643,10 +732,8 @@ function hysteria2(proxy) {
643732

644733
result.appendIfPresent(`,password=${proxy.password}`, 'password');
645734

646-
result.appendIfPresent(
647-
`,ip-version=${ipVersions[proxy['ip-version']] || proxy['ip-version']}`,
648-
'ip-version',
649-
);
735+
const ip_version = ipVersions[proxy['ip-version']] || proxy['ip-version'];
736+
result.appendIfPresent(`,ip-version=${ip_version}`, 'ip-version');
650737

651738
result.appendIfPresent(
652739
`,no-error-alert=${proxy['no-error-alert']}`,

0 commit comments

Comments
 (0)