@@ -101,15 +101,26 @@ function incrementIP(ip) {
101
101
}
102
102
103
103
function isValidConfigFormat ( inputConfig ) {
104
- if ( inputConfig . startsWith ( 'vmess://' ) ) {
104
+ if ( inputConfig . startsWith ( 'vmess://' ) || inputConfig . startsWith ( 'vless://' ) ||
105
+ inputConfig . startsWith ( 'wireguard://' ) || inputConfig . startsWith ( 'trojan://' ) ) {
105
106
return true ;
106
- } else if ( inputConfig . startsWith ( 'vless://' ) ) {
107
- const regex = / v l e s s : \/ \/ [ ^ @ ] + @ [ ^ : ] + : .+ / ;
108
- return regex . test ( inputConfig ) ;
109
107
}
110
108
return false ;
111
109
}
112
110
111
+ function detectConfigType ( inputConfig ) {
112
+ if ( inputConfig . startsWith ( 'vmess://' ) ) {
113
+ return 'vmess' ;
114
+ } else if ( inputConfig . startsWith ( 'vless://' ) ) {
115
+ return 'vless' ;
116
+ } else if ( inputConfig . startsWith ( 'wireguard://' ) ) {
117
+ return 'wireguard' ;
118
+ } else if ( inputConfig . startsWith ( 'trojan://' ) ) {
119
+ return 'trojan' ;
120
+ }
121
+ return null ;
122
+ }
123
+
113
124
function generateConfigs ( ) {
114
125
const inputType = document . getElementById ( 'inputType' ) . value ;
115
126
const inputConfig = document . getElementById ( 'inputConfig' ) . value ;
@@ -223,39 +234,51 @@ function modifyConfigsFromConfigsList() {
223
234
}
224
235
225
236
function extractAddressFromConfig ( config ) {
226
- let isVmess = config . startsWith ( 'vmess://' ) ;
227
- let isVless = config . startsWith ( 'vless://' ) ;
237
+ let configType = detectConfigType ( config ) ;
228
238
229
- if ( isVmess ) {
239
+ if ( configType === 'vmess' ) {
230
240
const base64Str = config . substring ( 8 ) ;
231
241
const decodedStr = Base64 . decode ( base64Str ) ;
232
242
const vmessConfig = JSON . parse ( decodedStr ) ;
233
243
return vmessConfig . add ;
234
- } else if ( isVless ) {
244
+ } else if ( configType === 'vless' ) {
235
245
const regex = / v l e s s : \/ \/ ( [ ^ @ ] + ) @ ( [ ^ : ] + ) : ( \d + ) ( \? [ ^ # ] * ) ? ( # .* ) ? / ;
236
246
const match = config . match ( regex ) ;
237
247
const address = match [ 2 ] ;
238
248
return address ;
249
+ } else if ( configType === 'wireguard' ) {
250
+ const regex = / w i r e g u a r d : \/ \/ [ ^ @ ] + @ ( [ ^ : ] + ) : .+ / ;
251
+ const match = config . match ( regex ) ;
252
+ return match [ 1 ] ;
253
+ } else if ( configType === 'trojan' ) {
254
+ const regex = / t r o j a n : \/ \/ [ ^ @ ] + @ ( [ ^ : ] + ) : .+ / ;
255
+ const match = config . match ( regex ) ;
256
+ return match [ 1 ] ;
239
257
}
240
258
241
259
return null ;
242
260
}
243
261
244
262
function replaceIPInConfig ( inputConfig , ipOrAddress ) {
245
- let isVmess = inputConfig . startsWith ( 'vmess://' ) ;
246
- let isVless = inputConfig . startsWith ( 'vless://' ) ;
263
+ let configType = detectConfigType ( inputConfig ) ;
247
264
let addressStr = typeof ipOrAddress === 'string' ? ipOrAddress : ipOrAddress . toString ( ) ;
248
265
let result = '' ;
249
266
250
- if ( isVmess ) {
267
+ if ( configType === 'vmess' ) {
251
268
let vmessConfig = JSON . parse ( Base64 . decode ( inputConfig . replace ( 'vmess://' , '' ) ) ) ;
252
269
vmessConfig . add = addressStr ;
253
270
result = `vmess://${ Base64 . encode ( JSON . stringify ( vmessConfig ) ) } \n\n` ;
254
- } else if ( isVless ) {
271
+ } else if ( configType === 'vless' ) {
255
272
addressStr = addressStr . includes ( ':' ) && ! addressStr . startsWith ( '[' ) ? `[${ addressStr } ]` : addressStr ;
256
273
const match = inputConfig . match ( / ^ ( v l e s s : \/ \/ [ ^ @ ] + ) @ ( [ ^ : ] + ) ( : .+ ) $ / ) ;
257
274
const [ _ , start , domain , end ] = match ;
258
275
result = `${ start } @${ addressStr } ${ end } \n\n` ;
276
+ } else if ( configType === 'wireguard' ) {
277
+ const regex = / ^ ( w i r e g u a r d : \/ \/ [ ^ @ ] + @ ) [ ^ : ] + ( : .+ ) $ / ;
278
+ result = inputConfig . replace ( regex , `$1${ addressStr } $2\n\n` ) ;
279
+ } else if ( configType === 'trojan' ) {
280
+ const regex = / ^ ( t r o j a n : \/ \/ [ ^ @ ] + @ ) [ ^ : ] + ( : .+ ) $ / ;
281
+ result = inputConfig . replace ( regex , `$1${ addressStr } $2\n\n` ) ;
259
282
}
260
283
261
284
return result ;
0 commit comments