Got a strange issue, that i have no idea how to deal with. Trying to make auth server for World of Warcraft client application.
And here is what happening:
const net = require('net')
const server = net.createServer((socket) => {
console.log('Client connected');
socket.removeAllListeners('data');
socket.on('end', () => console.log('Client disconnected!'));
socket.on('close', () => console.log('Client closed connection to server.'));
socket.on('drain', () => console.log('Buffer drain!'));
socket.on('data', function(data) {
console.log(data)
onData(socket, data)
});
socket.pipe(socket);
}).on('error', (err) => {
console.log('Server error:' + err)
});
function onData(socket, data) {
console.log(`data length`, data.length)
if (data.length === 44) {
socket.write(Buffer.from('0000007cdc3e6e9cabc0cb57332ee5086df1b1ccc491d685996af531b4ec807ed2c667010720b79b3e2a87823cab8f5ebfbf8eb10108535006298b5badbd5b53e1895e644b891f97f4daa96fca449ea98bd3c8da6963012888a89b8e572abff4294e05c4fc98baa31e99a00b2157fc373fb369cdd2f100', 'hex'),
(err) => {
if (err)
console.log(err);
console.log(`Write data end`);
socket.end(); //<-- also tried without FIN, since response packet is 0x0 terminated
})
}
console.log('onData::End');
}
server.listen(3724, '127.0.0.1', function() {
console.log('Server is listening');
});
Output from this is:
Server is listening
Client connected
<Buffer 00 08 28 00 57 6f 57 00 03 03 05 34 30 36 38 78 00 6e 69 57 00 55 52 75 72 b4 00 00 00 7f 00 00 01 0a 53 49 4c 56 45 52 4c 45 58 58>
data length 44
onData::End
Write data end
Thing is that in Wireshark i see next package from client, but JS code doesn't receive it and seems like stuck on first packet.

On image you can see first request with protocol WoW coming from client with 84 bytes lenght, then my response with 159 bytes, then one more client request with 115 bytes witch i'm not getting. (And according to Wireshark, structure of packet is good, since it's parsed)
Just to test things out i created small golang app with that same behavior.
package main
import (
"fmt"
"net"
"io"
"encoding/hex"
)
func main() {
listener, _ := net.Listen("tcp", "127.0.0.1:3724")
for {
conn, err := listener.Accept()
if err != nil {
continue
}
fmt.Println("New socket")
go handleClient(conn)
}
}
func handleClient(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 0, 4096)
tmp := make([]byte, 32)
for {
fmt.Println("Start read")
n, err := conn.Read(tmp)
fmt.Println("Stop read")
if err != nil {
if err != io.EOF {
fmt.Println("read error:", err)
}
fmt.Println("EOF")
break
}
buf = append(buf, tmp[:n]...)
fmt.Println("total size:", len(buf))
s := "0000007cdc3e6e9cabc0cb57332ee5086df1b1ccc491d685996af531b4ec807ed2c667010720b79b3e2a87823cab8f5ebfbf8eb10108535006298b5badbd5b53e1895e644b891f97f4daa96fca449ea98bd3c8da6963012888a89b8e572abff4294e05c4fc98baa31e99a00b2157fc373fb369cdd2f100"
if len(buf) == 44 {
data, err := hex.DecodeString(s)
if err != nil {
panic(err)
}
conn.Write(data)
}
}
fmt.Println("total size:", len(buf))
}
And it works as supposed to, here is output:
New socket
Start read
Stop read
total size: 32
Start read
Stop read
total size: 44 //<-- first packet end, after this point we send response to client
Start read
Stop read
total size: 76 //<-- start reading second packet (so we got the next one)
Start read
Stop read
total size: 108
Start read
Stop read
total size: 119
Start read
So my question is where i messed up in NodeJS, it should be simple with almost no code, but i just have no idea.
EDIT: Just in case if it would help:
- First client packet: 45000054aaed4000800600007f0000017f000001e5920e8c52e913174311bd4b501820fa346e000000082800576f57000303053430363878006e69570055527572b40000007f0000010a53494c5645524c455858
- Second client packet: 45000073ab094000800600007f0000017f000001e5920e8c52e913434311bdc2501820fa5ece00000191280a5b88b457d77d8df421b06e3d5c77a26b61b35c7fad12cdad9b71959e45395db29a611163d1d446e4a9973bf4f714afb6b532d1b21bdf3347de5aa780d270c01b1a43f15d510000
UPDATE: Did a small client on NodeJS sockets, that simulates client behavior and it seems like working fine with server provided.
Server is listening
Client connected
<Buffer 00 08 28 00 57 6f 57 00 03 03 05 34 30 36 38 78 00 6e 69 57 00 55 52 75 72 b4 00 00 00 7f 00 00 01 0a 53 49 4c 56 45 52 4c 45 58 58>
data length 44
onData::End
Write data end
<Buffer 00 00 00 7c dc 3e 6e 9c ab c0 cb 57 33 2e e5 08 6d f1 b1 cc c4 91 d6 85 99 6a f5 31 b4 ec 80 7e d2 c6 67 01 07 20 b7 9b 3e 2a 87 82 3c ab 8f 5e bf bf ... 69 more bytes>
data length 119
onData::End
So that might be something specific with WoW client and NodeJS socket implementation. Still have no idea what's wrong.
Is it possible that your server is invoking the close event after the connection with client terminates? (I haven't seen your client side code yet)
Because if the server invokes the close event then it will not be able to conenct / receive from any other clients. Some of the websocket events are reserved and their logs might not reflect here even if you've explicitly defined the event.
Perhaps what you can also try is removing the
socket.close()line and check what happens.