Skip to content

Commit

Permalink
conn.on('error')
Browse files Browse the repository at this point in the history
  • Loading branch information
mrluanma committed Mar 14, 2024
1 parent a026833 commit 0c7aa6b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
23 changes: 16 additions & 7 deletions local.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,29 @@ var server = net.createServer(async (conn) => {
let addrToSend = '';
const aServer = getServer();

await new Promise((resolve, reject) => {
conn.once('readable', resolve);
});
conn.on('error', (err) => console.error(`local: ${err}`));

// handshake
let data = await conn.read();
while (!data) {
await new Promise((resolve, reject) => {
conn.once('readable', resolve);
});
data = await conn.read();
}
// handshake
conn.write(Buffer.from([5, 0]));

const nextCmd = data.indexOf(5, 1);
if (nextCmd !== -1) {
data = data.subarray(nextCmd);
} else {
await new Promise((resolve, reject) => {
conn.once('readable', resolve);
});
data = await conn.read();
while (!data) {
await new Promise((resolve, reject) => {
conn.once('readable', resolve);
});
data = await conn.read();
}
}
// +----+-----+-------+------+----------+----------+
// |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT |
Expand Down Expand Up @@ -191,6 +198,8 @@ var server = net.createServer(async (conn) => {
});
}

ws.on('error', (err) => console.error(`local: ${err}`));

const wss = createWebSocketStream(ws);
console.log(`connecting ${remoteAddr} via ${aServer}`);

Expand Down
5 changes: 5 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ wsserver.on('connection', async (ws) => {
let remoteAddr;
let remotePort;

ws.on('error', (err) => console.error(`server: ${err}`));

const conn = createWebSocketStream(ws);
const readable = conn.pipe(
createTransform(encryptor.decrypt.bind(encryptor)),
Expand Down Expand Up @@ -120,6 +122,9 @@ wsserver.on('connection', async (ws) => {
}

const remote = net.connect(remotePort, remoteAddr);

remote.on('error', (err) => console.error(`server: ${err}`));

console.log('connecting', remoteAddr);
if (data.length > headerLength) {
remote.write(data.subarray(headerLength));
Expand Down

0 comments on commit 0c7aa6b

Please sign in to comment.