enhance node-vless udp...

This commit is contained in:
zizifn
2023-02-27 01:10:58 +08:00
committed by zizifn
parent a2d5716358
commit c4f3cf8390

View File

@@ -99,8 +99,14 @@ vlessWServer.on('connection', async function connection(ws) {
async write(chunk: Buffer, controller) { async write(chunk: Buffer, controller) {
if (udpClientStream) { if (udpClientStream) {
const writer = udpClientStream.writable.getWriter(); const writer = udpClientStream.writable.getWriter();
// nodejs buffer will fill some byte, need offset is // nodejs buffer to ArrayBuffer issue
writer.write(chunk.buffer.slice(chunk.byteOffset)); // https://nodejs.org/dist/latest-v18.x/docs/api/buffer.html#bufbuffer
writer.write(
chunk.buffer.slice(
chunk.byteOffset,
chunk.byteOffset + chunk.length
)
);
writer.releaseLock(); writer.releaseLock();
return; return;
} }
@@ -109,7 +115,10 @@ vlessWServer.on('connection', async function connection(ws) {
// remoteConnection.write(chunk); // remoteConnection.write(chunk);
return; return;
} }
const vlessBuffer = chunk.buffer.slice(chunk.byteOffset); const vlessBuffer = chunk.buffer.slice(
chunk.byteOffset,
chunk.byteOffset + chunk.length
);
const { const {
hasError, hasError,
message, message,
@@ -290,8 +299,8 @@ function makeUDPSocketStream(portRemote, address) {
}, },
transform(chunk: ArrayBuffer, controller) { transform(chunk: ArrayBuffer, controller) {
// seems v2ray will use same web socket for dns query.. //seems v2ray will use same web socket for dns query..
// And v2ray will combine A record and AAAA record into one ws message and use 2 btye for dns query length //And v2ray will combine A record and AAAA record into one ws message and use 2 btye for dns query length
for (let index = 0; index < chunk.byteLength; ) { for (let index = 0; index < chunk.byteLength; ) {
const lengthBuffer = chunk.slice(index, index + 2); const lengthBuffer = chunk.slice(index, index + 2);
const udpPakcetLength = new DataView(lengthBuffer).getInt16(0); const udpPakcetLength = new DataView(lengthBuffer).getInt16(0);
@@ -299,7 +308,6 @@ function makeUDPSocketStream(portRemote, address) {
chunk.slice(index + 2, index + 2 + udpPakcetLength) chunk.slice(index + 2, index + 2 + udpPakcetLength)
); );
index = index + 2 + udpPakcetLength; index = index + 2 + udpPakcetLength;
udpClient.send(udpData, portRemote, address, (err) => { udpClient.send(udpData, portRemote, address, (err) => {
if (err) { if (err) {
console.log(err); console.log(err);