11import { Result , isPresent } from './utils' ;
2- import { isNotBlank } from '@/utils' ;
2+ import { isNotBlank , getIfNotBlank } from '@/utils' ;
33import $ from '@/core/app' ;
44
55const targetPlatform = 'Surge' ;
@@ -13,7 +13,8 @@ const ipVersions = {
1313} ;
1414
1515export default function Surge_Producer ( ) {
16- const produce = ( proxy ) => {
16+ const produce = ( proxy , type , opts = { } ) => {
17+ console . log ( opts ) ;
1718 switch ( proxy . type ) {
1819 case 'ss' :
1920 return shadowsocks ( proxy ) ;
@@ -30,10 +31,14 @@ export default function Surge_Producer() {
3031 case 'tuic' :
3132 return tuic ( proxy ) ;
3233 case 'wireguard-surge' :
33- return wireguard ( proxy ) ;
34+ return wireguard_surge ( proxy ) ;
3435 case 'hysteria2' :
3536 return hysteria2 ( proxy ) ;
3637 }
38+
39+ if ( opts [ 'include-unsupported-proxy' ] && proxy . type === 'wireguard' ) {
40+ return wireguard ( proxy ) ;
41+ }
3742 throw new Error (
3843 `Platform ${ targetPlatform } does not support proxy type: ${ proxy . type } ` ,
3944 ) ;
@@ -82,10 +87,8 @@ function shadowsocks(proxy) {
8287 result . append ( `,encrypt-method=${ proxy . cipher } ` ) ;
8388 result . appendIfPresent ( `,password=${ proxy . password } ` , 'password' ) ;
8489
85- result . appendIfPresent (
86- `,ip-version=${ ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] } ` ,
87- 'ip-version' ,
88- ) ;
90+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
91+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
8992
9093 result . appendIfPresent (
9194 `,no-error-alert=${ proxy [ 'no-error-alert' ] } ` ,
@@ -167,10 +170,8 @@ function trojan(proxy) {
167170 result . append ( `${ proxy . name } =${ proxy . type } ,${ proxy . server } ,${ proxy . port } ` ) ;
168171 result . appendIfPresent ( `,password=${ proxy . password } ` , 'password' ) ;
169172
170- result . appendIfPresent (
171- `,ip-version=${ ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] } ` ,
172- 'ip-version' ,
173- ) ;
173+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
174+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
174175
175176 result . appendIfPresent (
176177 `,no-error-alert=${ proxy [ 'no-error-alert' ] } ` ,
@@ -236,10 +237,8 @@ function vmess(proxy) {
236237 result . append ( `${ proxy . name } =${ proxy . type } ,${ proxy . server } ,${ proxy . port } ` ) ;
237238 result . appendIfPresent ( `,username=${ proxy . uuid } ` , 'uuid' ) ;
238239
239- result . appendIfPresent (
240- `,ip-version=${ ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] } ` ,
241- 'ip-version' ,
242- ) ;
240+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
241+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
243242
244243 result . appendIfPresent (
245244 `,no-error-alert=${ proxy [ 'no-error-alert' ] } ` ,
@@ -314,10 +313,8 @@ function http(proxy) {
314313 result . appendIfPresent ( `,${ proxy . username } ` , 'username' ) ;
315314 result . appendIfPresent ( `,${ proxy . password } ` , 'password' ) ;
316315
317- result . appendIfPresent (
318- `,ip-version=${ ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] } ` ,
319- 'ip-version' ,
320- ) ;
316+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
317+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
321318
322319 result . appendIfPresent (
323320 `,no-error-alert=${ proxy [ 'no-error-alert' ] } ` ,
@@ -379,10 +376,8 @@ function socks5(proxy) {
379376 result . appendIfPresent ( `,${ proxy . username } ` , 'username' ) ;
380377 result . appendIfPresent ( `,${ proxy . password } ` , 'password' ) ;
381378
382- result . appendIfPresent (
383- `,ip-version=${ ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] } ` ,
384- 'ip-version' ,
385- ) ;
379+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
380+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
386381
387382 result . appendIfPresent (
388383 `,no-error-alert=${ proxy [ 'no-error-alert' ] } ` ,
@@ -445,10 +440,8 @@ function snell(proxy) {
445440 result . appendIfPresent ( `,version=${ proxy . version } ` , 'version' ) ;
446441 result . appendIfPresent ( `,psk=${ proxy . psk } ` , 'psk' ) ;
447442
448- result . appendIfPresent (
449- `,ip-version=${ ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] } ` ,
450- 'ip-version' ,
451- ) ;
443+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
444+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
452445
453446 result . appendIfPresent (
454447 `,no-error-alert=${ proxy [ 'no-error-alert' ] } ` ,
@@ -525,10 +518,8 @@ function tuic(proxy) {
525518 'alpn' ,
526519 ) ;
527520
528- result . appendIfPresent (
529- `,ip-version=${ ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] } ` ,
530- 'ip-version' ,
531- ) ;
521+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
522+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
532523
533524 result . appendIfPresent (
534525 `,no-error-alert=${ proxy [ 'no-error-alert' ] } ` ,
@@ -587,9 +578,23 @@ function tuic(proxy) {
587578}
588579
589580function wireguard ( proxy ) {
581+ if ( Array . isArray ( proxy . peers ) && proxy . peers . length > 0 ) {
582+ proxy . server = proxy . peers [ 0 ] . server ;
583+ proxy . port = proxy . peers [ 0 ] . port ;
584+ proxy . ip = proxy . peers [ 0 ] . ip ;
585+ proxy . ipv6 = proxy . peers [ 0 ] . ipv6 ;
586+ proxy [ 'public-key' ] = proxy . peers [ 0 ] [ 'public-key' ] ;
587+ proxy [ 'preshared-key' ] = proxy . peers [ 0 ] [ 'pre-shared-key' ] ;
588+ // https://github.com/MetaCubeX/mihomo/blob/0404e35be8736b695eae018a08debb175c1f96e6/docs/config.yaml#L717
589+ proxy [ 'allowed-ips' ] = proxy . peers [ 0 ] [ 'allowed-ips' ] ;
590+ proxy . reserved = proxy . peers [ 0 ] . reserved ;
591+ }
590592 const result = new Result ( proxy ) ;
591593
592- result . append ( `${ proxy . name } =wireguard` ) ;
594+ result . append ( `# WireGuard Proxy ${ proxy . name }
595+ ${ proxy . name } =wireguard`) ;
596+
597+ proxy [ 'section-name' ] = getIfNotBlank ( proxy [ 'section-name' ] , proxy . name ) ;
593598
594599 result . appendIfPresent (
595600 `,section-name=${ proxy [ 'section-name' ] } ` ,
@@ -600,11 +605,96 @@ function wireguard(proxy) {
600605 'no-error-alert' ,
601606 ) ;
602607
608+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
609+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
610+
611+ // test-url
612+ result . appendIfPresent ( `,test-url=${ proxy [ 'test-url' ] } ` , 'test-url' ) ;
613+
614+ // shadow-tls
615+ if ( isPresent ( proxy , 'shadow-tls-password' ) ) {
616+ result . append ( `,shadow-tls-password=${ proxy [ 'shadow-tls-password' ] } ` ) ;
617+
618+ result . appendIfPresent (
619+ `,shadow-tls-version=${ proxy [ 'shadow-tls-version' ] } ` ,
620+ 'shadow-tls-version' ,
621+ ) ;
622+ result . appendIfPresent (
623+ `,shadow-tls-sni=${ proxy [ 'shadow-tls-sni' ] } ` ,
624+ 'shadow-tls-sni' ,
625+ ) ;
626+ }
627+
628+ // block-quic
629+ result . appendIfPresent ( `,block-quic=${ proxy [ 'block-quic' ] } ` , 'block-quic' ) ;
630+
631+ // underlying-proxy
632+ result . appendIfPresent (
633+ `,underlying-proxy=${ proxy [ 'underlying-proxy' ] } ` ,
634+ 'underlying-proxy' ,
635+ ) ;
636+
637+ result . append ( `
638+ # WireGuard Section ${ proxy . name }
639+ [WireGuard ${ proxy [ 'section-name' ] } ]
640+ private-key = ${ proxy [ 'private-key' ] } ` ) ;
641+
642+ result . appendIfPresent ( `\nself-ip = ${ proxy . ip } ` , 'ip' ) ;
643+ result . appendIfPresent ( `\nself-ip-v6 = ${ proxy . ipv6 } ` , 'ipv6' ) ;
644+ if ( proxy . dns ) {
645+ if ( Array . isArray ( proxy . dns ) ) {
646+ proxy . dns = proxy . dns . join ( ', ' ) ;
647+ }
648+ result . append ( `\ndns-server = ${ proxy . dns } ` ) ;
649+ }
650+ result . appendIfPresent ( `\nmtu = ${ proxy . mtu } ` , 'mtu' ) ;
651+
652+ if ( ip_version === 'prefer-v6' ) {
653+ result . append ( `\nprefer-ipv6 = true` ) ;
654+ }
655+ const allowedIps = Array . isArray ( proxy [ 'allowed-ips' ] )
656+ ? proxy [ 'allowed-ips' ] . join ( ',' )
657+ : proxy [ 'allowed-ips' ] ;
658+ let reserved = Array . isArray ( proxy . reserved )
659+ ? proxy . reserved . join ( '/' )
660+ : proxy . reserved ;
661+ let presharedKey = proxy [ 'preshared-key' ] ?? proxy [ 'pre-shared-key' ] ;
662+ if ( presharedKey ) {
663+ presharedKey = `,preshared-key="${ presharedKey } "` ;
664+ }
665+ const peer = {
666+ 'public-key' : proxy [ 'public-key' ] ,
667+ 'allowed-ips' : allowedIps ,
668+ endpoint : `${ proxy . server } :${ proxy . port } ` ,
669+ keepalive : proxy [ 'persistent-keepalive' ] || proxy . keepalive ,
670+ 'client-id' : reserved ,
671+ 'preshared-key' : presharedKey ,
672+ } ;
673+ result . append (
674+ `\npeer = (${ Object . keys ( peer )
675+ . filter ( ( k ) => peer [ k ] != null )
676+ . map ( ( k ) => `${ k } = ${ peer [ k ] } ` )
677+ . join ( ', ' ) } )`,
678+ ) ;
679+ return result . toString ( ) ;
680+ }
681+ function wireguard_surge ( proxy ) {
682+ const result = new Result ( proxy ) ;
683+
684+ result . append ( `${ proxy . name } =wireguard` ) ;
685+
603686 result . appendIfPresent (
604- `,ip-version=${ ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] } ` ,
605- 'ip-version' ,
687+ `,section-name=${ proxy [ 'section-name' ] } ` ,
688+ 'section-name' ,
689+ ) ;
690+ result . appendIfPresent (
691+ `,no-error-alert=${ proxy [ 'no-error-alert' ] } ` ,
692+ 'no-error-alert' ,
606693 ) ;
607694
695+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
696+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
697+
608698 // test-url
609699 result . appendIfPresent ( `,test-url=${ proxy [ 'test-url' ] } ` , 'test-url' ) ;
610700
@@ -643,10 +733,8 @@ function hysteria2(proxy) {
643733
644734 result . appendIfPresent ( `,password=${ proxy . password } ` , 'password' ) ;
645735
646- result . appendIfPresent (
647- `,ip-version=${ ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] } ` ,
648- 'ip-version' ,
649- ) ;
736+ const ip_version = ipVersions [ proxy [ 'ip-version' ] ] || proxy [ 'ip-version' ] ;
737+ result . appendIfPresent ( `,ip-version=${ ip_version } ` , 'ip-version' ) ;
650738
651739 result . appendIfPresent (
652740 `,no-error-alert=${ proxy [ 'no-error-alert' ] } ` ,
0 commit comments