11// Developed by Surfboardv2ray
22// https://github.com/Surfboardv2ray/v2ray-refiner
3- // Version 1.2
3+ // Version 1.2.1
44// Change 'url.port' and 'const workerport' value if your config uses another port. Only one port will work at a time.
55
66export default {
@@ -290,30 +290,26 @@ async function handleConfigRefinement(request) {
290290 }
291291}
292292
293- // Handle vmess:// config refinement
293+
294294function handleVmessConfig ( config , hostname , cleanIp , workerUrl , workerPort ) {
295295 console . log ( "Original config:" , config ) ;
296296
297- // Extract the base64 part after 'vmess://'
298297 const base64Part = config . split ( 'vmess://' ) [ 1 ] ;
299298 console . log ( "Base64 part:" , base64Part ) ;
300-
301- // Check if the base64 part is present
299+
302300 if ( ! base64Part ) {
303301 throw new Error ( "Invalid vmess config: No base64 part found." ) ;
304302 }
305303
306304 let decodedConfig ;
307305
308306 try {
309- // Decode the base64 part
310307 decodedConfig = atob ( base64Part ) ;
311308 console . log ( "Decoded config:" , decodedConfig ) ;
312309 } catch ( e ) {
313310 throw new Error ( "Invalid vmess config: Base64 decoding failed." ) ;
314311 }
315312
316- // Parse the decoded JSON config
317313 let jsonConfig ;
318314 try {
319315 jsonConfig = JSON . parse ( decodedConfig ) ;
@@ -322,20 +318,17 @@ function handleVmessConfig(config, hostname, cleanIp, workerUrl, workerPort) {
322318 throw new Error ( "Invalid vmess config: JSON parsing failed." ) ;
323319 }
324320
325- // Validate required fields
326321 if ( ! jsonConfig . port || ! jsonConfig . ps || ! jsonConfig . id ) {
327322 throw new Error ( "Invalid vmess config: Missing required fields (port, ps, id)." ) ;
328323 }
329324
330325 const port = jsonConfig . port . toString ( ) ;
331326 console . log ( "Config port:" , port ) ;
332327
333- // Check if the port matches the worker port
334328 if ( port !== workerPort ) {
335329 throw new Error ( `The config port must be ${ workerPort } ` ) ;
336330 }
337331
338- // Construct the refined config
339332 const refinedConfig = {
340333 v : "2" ,
341334 ps : jsonConfig . ps ,
@@ -346,17 +339,16 @@ function handleVmessConfig(config, hostname, cleanIp, workerUrl, workerPort) {
346339 scy : "auto" ,
347340 net : "ws" ,
348341 type : "none" ,
349- host : workerUrl , // Set host to the Worker URL
350- path : `/${ hostname } ${ jsonConfig . path || '' } ` , // Ensure path is appended correctly
342+ host : workerUrl ,
343+ path : `/${ hostname } ${ jsonConfig . path || '' } ` ,
351344 tls : "tls" ,
352- sni : workerUrl , // Set SNI to the Worker URL
345+ sni : workerUrl ,
353346 alpn : "h2,http/1.1" ,
354347 fp : "chrome"
355348 } ;
356349
357350 console . log ( "Refined config object:" , refinedConfig ) ;
358351
359- // Convert refined config to base64
360352 let refinedConfigBase64 ;
361353 try {
362354 refinedConfigBase64 = btoa ( JSON . stringify ( refinedConfig ) ) ;
@@ -365,14 +357,11 @@ function handleVmessConfig(config, hostname, cleanIp, workerUrl, workerPort) {
365357 throw new Error ( "Failed to encode the refined configuration to base64." ) ;
366358 }
367359
368- // Return the refined configuration as a JSON response
369360 return new Response ( JSON . stringify ( { refinedConfig : `vmess://${ refinedConfigBase64 } ` } ) , {
370361 headers : { 'Content-Type' : 'application/json' } ,
371362 } ) ;
372363}
373364
374-
375- // Handle vless:// config refinement
376365function handleVlessConfig ( config , hostname , cleanIp , workerUrl , workerPort ) {
377366 const parts = config . split ( 'vless://' ) [ 1 ] . split ( '@' ) ;
378367 const uuid = parts [ 0 ] ;
@@ -393,7 +382,6 @@ function handleVlessConfig(config, hostname, cleanIp, workerUrl, workerPort) {
393382 } ) ;
394383}
395384
396- // Handle trojan:// config refinement
397385function handleTrojanConfig ( config , hostname , cleanIp , workerUrl , workerPort ) {
398386 const parts = config . split ( 'trojan://' ) [ 1 ] . split ( '@' ) ;
399387 const uuid = parts [ 0 ] ;
0 commit comments