@@ -674,6 +674,89 @@ function URI_TUIC() {
674674 } ;
675675 return { name, test, parse } ;
676676}
677+ function URI_WireGuard ( ) {
678+ const name = 'URI WireGuard Parser' ;
679+ const test = ( line ) => {
680+ return / ^ ( w i r e g u a r d | w g ) : \/ \/ / . test ( line ) ;
681+ } ;
682+ const parse = ( line ) => {
683+ line = line . split ( / ( w i r e g u a r d | w g ) : \/ \/ / ) [ 2 ] ;
684+ /* eslint-disable no-unused-vars */
685+ let [
686+ __ ,
687+ ___ ,
688+ privateKey ,
689+ server ,
690+ ____ ,
691+ port ,
692+ _____ ,
693+ addons = '' ,
694+ name ,
695+ ] = / ^ ( ( .* ?) @ ) ? ( .* ?) ( : ( \d + ) ) ? \/ ? ( \? ( .* ?) ) ? (?: # ( .* ?) ) ? $ / . exec ( line ) ;
696+ /* eslint-enable no-unused-vars */
697+
698+ port = parseInt ( `${ port } ` , 10 ) ;
699+ if ( isNaN ( port ) ) {
700+ port = 51820 ;
701+ }
702+ privateKey = decodeURIComponent ( privateKey ) ;
703+ if ( name != null ) {
704+ name = decodeURIComponent ( name ) ;
705+ }
706+ name = name ?? `WireGuard ${ server } :${ port } ` ;
707+ const proxy = {
708+ type : 'wireguard' ,
709+ name,
710+ server,
711+ port,
712+ 'private-key' : privateKey ,
713+ udp : true ,
714+ } ;
715+ for ( const addon of addons . split ( '&' ) ) {
716+ let [ key , value ] = addon . split ( '=' ) ;
717+ key = key . replace ( / _ / , '-' ) ;
718+ value = decodeURIComponent ( value ) ;
719+ if ( [ 'reserved' ] . includes ( key ) ) {
720+ const parsed = value
721+ . split ( ',' )
722+ . map ( ( i ) => parseInt ( i . trim ( ) , 10 ) )
723+ . filter ( ( i ) => Number . isInteger ( i ) ) ;
724+ if ( parsed . length === 3 ) {
725+ proxy [ key ] = parsed ;
726+ }
727+ } else if ( [ 'address' , 'ip' ] . includes ( key ) ) {
728+ value . split ( ',' ) . map ( ( i ) => {
729+ const ip = i
730+ . trim ( )
731+ . replace ( / \/ \d + $ / , '' )
732+ . replace ( / ^ \[ / , '' )
733+ . replace ( / \] $ / , '' ) ;
734+ if ( isIPv4 ( ip ) ) {
735+ proxy . ip = ip ;
736+ } else if ( isIPv6 ( ip ) ) {
737+ proxy . ipv6 = ip ;
738+ }
739+ } ) ;
740+ } else if ( [ 'mtu' ] . includes ( key ) ) {
741+ const parsed = parseInt ( value . trim ( ) , 10 ) ;
742+ if ( Number . isInteger ( parsed ) ) {
743+ proxy [ key ] = parsed ;
744+ }
745+ } else if ( / p u b l i c k e y / i. test ( key ) ) {
746+ proxy [ 'public-key' ] = value ;
747+ } else if ( / p r i v a t e k e y / i. test ( key ) ) {
748+ proxy [ 'private-key' ] = value ;
749+ } else if ( [ 'udp' ] . includes ( key ) ) {
750+ proxy [ key ] = / ( T R U E ) | 1 / i. test ( value ) ;
751+ } else if ( ! [ 'flag' ] . includes ( key ) ) {
752+ proxy [ key ] = value ;
753+ }
754+ }
755+
756+ return proxy ;
757+ } ;
758+ return { name, test, parse } ;
759+ }
677760
678761// Trojan URI format
679762function URI_Trojan ( ) {
@@ -1196,6 +1279,7 @@ export default [
11961279 URI_VMess ( ) ,
11971280 URI_VLESS ( ) ,
11981281 URI_TUIC ( ) ,
1282+ URI_WireGuard ( ) ,
11991283 URI_Hysteria ( ) ,
12001284 URI_Hysteria2 ( ) ,
12011285 URI_Trojan ( ) ,
0 commit comments