* * @param { ArrayBuffer} vlessBuffer * @param {string} userID * @returns
(
vlessBuffer,
userID
)
| 277 | * @returns |
| 278 | */ |
| 279 | function processVlessHeader( |
| 280 | vlessBuffer, |
| 281 | userID |
| 282 | ) { |
| 283 | if (vlessBuffer.byteLength < 24) { |
| 284 | return { |
| 285 | hasError: true, |
| 286 | message: 'invalid data', |
| 287 | }; |
| 288 | } |
| 289 | const version = new Uint8Array(vlessBuffer.slice(0, 1)); |
| 290 | let isValidUser = false; |
| 291 | let isUDP = false; |
| 292 | if (stringify(new Uint8Array(vlessBuffer.slice(1, 17))) === userID) { |
| 293 | isValidUser = true; |
| 294 | } |
| 295 | if (!isValidUser) { |
| 296 | return { |
| 297 | hasError: true, |
| 298 | message: 'invalid user', |
| 299 | }; |
| 300 | } |
| 301 | |
| 302 | const optLength = new Uint8Array(vlessBuffer.slice(17, 18))[0]; |
| 303 | //skip opt for now |
| 304 | |
| 305 | const command = new Uint8Array( |
| 306 | vlessBuffer.slice(18 + optLength, 18 + optLength + 1) |
| 307 | )[0]; |
| 308 | |
| 309 | // 0x01 TCP |
| 310 | // 0x02 UDP |
| 311 | // 0x03 MUX |
| 312 | if (command === 1) { |
| 313 | } else if (command === 2) { |
| 314 | isUDP = true; |
| 315 | } else { |
| 316 | return { |
| 317 | hasError: true, |
| 318 | message: `command ${command} is not support, command 01-tcp,02-udp,03-mux`, |
| 319 | }; |
| 320 | } |
| 321 | const portIndex = 18 + optLength + 1; |
| 322 | const portBuffer = vlessBuffer.slice(portIndex, portIndex + 2); |
| 323 | // port is big-Endian in raw data etc 80 == 0x005d |
| 324 | const portRemote = new DataView(portBuffer).getUint16(0); |
| 325 | |
| 326 | let addressIndex = portIndex + 2; |
| 327 | const addressBuffer = new Uint8Array( |
| 328 | vlessBuffer.slice(addressIndex, addressIndex + 1) |
| 329 | ); |
| 330 | |
| 331 | // 1--> ipv4 addressLength =4 |
| 332 | // 2--> domain name addressLength=addressBuffer[1] |
| 333 | // 3--> ipv6 addressLength =16 |
| 334 | const addressType = addressBuffer[0]; |
| 335 | let addressLength = 0; |
| 336 | let addressValueIndex = addressIndex + 1; |