* 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, * message?:
(vlessBuffer, userID)
| 376 | * }} An object with the relevant information extracted from the VLESS header buffer. |
| 377 | */ |
| 378 | function processVlessHeader(vlessBuffer, userID) { |
| 379 | if (vlessBuffer.byteLength < 24) { |
| 380 | return { |
| 381 | hasError: true, |
| 382 | message: 'invalid data', |
| 383 | }; |
| 384 | } |
| 385 | |
| 386 | const version = new Uint8Array(vlessBuffer.slice(0, 1)); |
| 387 | let isValidUser = false; |
| 388 | let isUDP = false; |
| 389 | const slicedBuffer = new Uint8Array(vlessBuffer.slice(1, 17)); |
| 390 | const slicedBufferString = stringify(slicedBuffer); |
| 391 | // check if userID is valid uuid or uuids split by , and contains userID in it otherwise return error message to console |
| 392 | const uuids = userID.includes(',') ? userID.split(",") : [userID]; |
| 393 | // uuid_validator(hostName, slicedBufferString); |
| 394 | |
| 395 | |
| 396 | // isValidUser = uuids.some(userUuid => slicedBufferString === userUuid.trim()); |
| 397 | isValidUser = uuids.some(userUuid => slicedBufferString === userUuid.trim()) || uuids.length === 1 && slicedBufferString === uuids[0].trim(); |
| 398 | |
| 399 | console.log(`userID: ${slicedBufferString}`); |
| 400 | |
| 401 | if (!isValidUser) { |
| 402 | return { |
| 403 | hasError: true, |
| 404 | message: 'invalid user', |
| 405 | }; |
| 406 | } |
| 407 | |
| 408 | const optLength = new Uint8Array(vlessBuffer.slice(17, 18))[0]; |
| 409 | //skip opt for now |
| 410 | |
| 411 | const command = new Uint8Array( |
| 412 | vlessBuffer.slice(18 + optLength, 18 + optLength + 1) |
| 413 | )[0]; |
| 414 | |
| 415 | // 0x01 TCP |
| 416 | // 0x02 UDP |
| 417 | // 0x03 MUX |
| 418 | if (command === 1) { |
| 419 | isUDP = false; |
| 420 | } else if (command === 2) { |
| 421 | isUDP = true; |
| 422 | } else { |
| 423 | return { |
| 424 | hasError: true, |
| 425 | message: `command ${command} is not support, command 01-tcp,02-udp,03-mux`, |
| 426 | }; |
| 427 | } |
| 428 | const portIndex = 18 + optLength + 1; |
| 429 | const portBuffer = vlessBuffer.slice(portIndex, portIndex + 2); |
| 430 | // port is big-Endian in raw data etc 80 == 0x005d |
| 431 | const portRemote = new DataView(portBuffer).getUint16(0); |
| 432 | |
| 433 | let addressIndex = portIndex + 2; |
| 434 | const addressBuffer = new Uint8Array( |
| 435 | vlessBuffer.slice(addressIndex, addressIndex + 1) |