MCPcopy Index your code
hub / github.com/Ptechgithub/pp-worker / processVlessHeader

Function processVlessHeader

_worker.js:456–572  ·  view source on GitHub ↗

* Processes the VLESS header buffer and returns an object with the relevant information. * @param {ArrayBuffer} vlessBuffer The VLESS header buffer to process. * @param {string} userID The user ID to validate against the UUID in the VLESS header. * @returns {{ * hasError: boolean, * mess

(vlessBuffer, userID)

Source from the content-addressed store, hash-verified

454 * }} An object with the relevant information extracted from the VLESS header buffer.
455 */
456function processVlessHeader(vlessBuffer, userID) {
457 if (vlessBuffer.byteLength < 24) {
458 return {
459 hasError: true,
460 message: 'invalid data',
461 };
462 }
463 const version = new Uint8Array(vlessBuffer.slice(0, 1));
464 let isValidUser = false;
465 let isUDP = false;
466 const slicedBuffer = new Uint8Array(vlessBuffer.slice(1, 17));
467 const slicedBufferString = stringify(slicedBuffer);
468 // check if userID is valid uuid or uuids split by , and contains userID in it otherwise return error message to console
469 const uuids = userID.includes(',') ? userID.split(",") : [userID];
470 console.log(slicedBufferString, uuids);
471
472 // isValidUser = uuids.some(userUuid => slicedBufferString === userUuid.trim());
473 isValidUser = uuids.some(userUuid => slicedBufferString === userUuid.trim()) || uuids.length === 1 && slicedBufferString === uuids[0].trim();
474
475 console.log(`userID: ${slicedBufferString}`);
476
477 if (!isValidUser) {
478 return {
479 hasError: true,
480 message: 'invalid user',
481 };
482 }
483
484 const optLength = new Uint8Array(vlessBuffer.slice(17, 18))[0];
485 //skip opt for now
486
487 const command = new Uint8Array(
488 vlessBuffer.slice(18 + optLength, 18 + optLength + 1)
489 )[0];
490
491 // 0x01 TCP
492 // 0x02 UDP
493 // 0x03 MUX
494 if (command === 1) {
495 isUDP = false;
496 } else if (command === 2) {
497 isUDP = true;
498 } else {
499 return {
500 hasError: true,
501 message: `command ${command} is not support, command 01-tcp,02-udp,03-mux`,
502 };
503 }
504 const portIndex = 18 + optLength + 1;
505 const portBuffer = vlessBuffer.slice(portIndex, portIndex + 2);
506 // port is big-Endian in raw data etc 80 == 0x005d
507 const portRemote = new DataView(portBuffer).getUint16(0);
508
509 let addressIndex = portIndex + 2;
510 const addressBuffer = new Uint8Array(
511 vlessBuffer.slice(addressIndex, addressIndex + 1)
512 );
513

Callers 1

writeFunction · 0.85

Calls 1

stringifyFunction · 0.85

Tested by

no test coverage detected