This repository has been archived by the owner on Dec 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
134 lines (115 loc) · 4.26 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const net = require('net');
const fs = require('fs');
var crypto = require('crypto')
const port = 10075;
const host = '0.0.0.0';
const server = net.createServer();
server.listen(port, host, () => {
console.log('Test TCP Server is running on port ' + port + '.');
});
let sockets = [];
let state = 0;
const JOIN_PACKET = [
"\x92\x40\x00\xf4\xf8\x7a\xf7\x34",
"\x25\xc8\x9a\x6d\xa9\xdd\xeb\xab",
"\xa8\x3c\xa6\xe6\xb4\x72\x6d\xef",
"\x51\x23\x00\xde\xea\x43\xd5\x8f",
"\x22\x50\x3f\xaf\x9c\x52\x96\x10",
"\x7c\xa4\xbe\xa9\x57\x8a\xae\x49",
"\x68\x06\x20\x73\xc6\x24\xa8\x07",
"\xad\x44\xd2\x54\x29\x8d\x58\xb6",
"\x3c\xda\x3b\xe4\x33\x8c\x57"
];
function toHexString(byteArray) {
return Array.from(byteArray, function(byte) {
return "" + ('0' + (byte & 0xFF).toString(16)).slice(-2);
}).join(' ');
}
server.on('connection', function(sock) {
console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
sockets.push(sock);
sock.on('data', function(data) {
console.log('RECV: ' + sock.remoteAddress + ':' + sock.remotePort + ' > ' + toHexString(data));
var packet = [];
if (state == 0) {
packet = [
"\x93\x40\x00\x84\x95\x1f\xe2\xfb\x08\xe7",
"\x8a\xa4\xc0\xbb\xf1\x23\xa5\xdd\xd9\xab\x9d\xd1\xf4\x65\x26\xa2",
"\xff\x66\xc6\xf5\x98\x1d\xb9\x93\x83\x95\xb4\x4b\x61\x4b\xc3\x1f",
"\xd3\x5e\xbc\x7a\x26\xd7\xdf\x58\xda\x05\xa4\x7b\x0c\x01\x0e\xfc",
"\xa7\x6b\x62\x5e\xfe\xe1\xa6\x49\x59\x78\x52\xf9\x22"
];
state++;
} else if (state == 1) {
console.log("==== STATE 1");
packet = [
"\x94\x40\x00\x15\xe7\x62\x72\xa2\xa5\x95",
"\xc9\x3b\x34\xa9\x68\xd6\xfe\xb7\x56\xa4\x2a\x6c\x95\xfb\x91\x2c",
"\x94\xed\x7a\x9a\xa1\x27\x5a\xa6\xd2\xf6\x70\x61\xc4\x0e\xc8\x15",
"\x44\xfe\x00\xb6\x16\x6a\x42\xa9\x40\xdb\x50\x38\x31\x02\xf8\x05",
"\x94\xbd\xd9\x64\x93\xf7\xd3\xf1\x3b\x2c\xfa\x32\xe2"
];
state++;
} else if (state == 2) {
console.log("==== STATE 2");
packet = [
"\x8a\x32\x00\xbf\xeb\x09\x38\x9f\x54\x67",
"\x22\x6d\x90\xaa\xb5\xd8\xe6\x6e\x1b\xf9\x5c\xd7\x96\x10\xc3\xae",
"\x6a\xb4\x60\xde\x1a\xbc\x77\x02\xa4\x1c\x81\x0c\xd7\xf8\x90\xb1",
"\xdb\xc7\xc3\x08\x52\x38\x34\xe3\x50\xd0\x74\xcb\xd9\xc7\x06"
];
state++;
} else if (state == 3) {
console.log("==== STATE 3");
packet = [
"\x8a\x1e\x00\xad\xef\xb4\x3a\x89\x2c\x74",
"\x06\xf6\xe8\x9f\x51\x15\xba\x5d\x5f\xdc\xd7\x2d\x3d\x93\xb6\x12",
"\x83\xe4\xa6\x9b\xff\xaa\x69\x49\x44\xa4\x33"
];
state++;
} else if (state == 4) {
console.log("==== STATE 4");
packet = [
"\x8a\xc6\x00\xfa\x10\x9a\x26\x19\x9f\x8c",
"\x93\x3c\x35\x5f\xbd\x90\x21\xea\x65\x94\xd2\x4f\xa1\x19\xe2\x1f",
"\x09\xb9\x13\x8c\xf6\x67\xa7\xb1\x57\xcd\xc4\xa0\xc1\xc4\x05\xf2",
"\xed\xf0\x5b\x7e\x91\x70\x6b\xdc\x85\x70\xda\xe6\x26\xad\xe0\x9e",
"\xbd\xcf\x9e\x31\xfd\x8f\x37\xeb\xe9\xc5\xc7\x54\x2a\x77\x27\x90",
"\x73\xab\x37\xc8\x7b\x52\x7c\xda\xb5\x7f\x7c\xb4\xb3\x8c\xcd\x87",
"\xd5\x8e\x57\x34\x6a\x34\x7b\x98\x8d\x48\xe6\x3e\x1a\xc2\x2c\x0c",
"\x79\x74\x13\x7a\x35\x84\x56\x9e\xdc\x30\xbc\xa7\x13\xee\xdb\x5d",
"\xc8\xf8\x74\x52\x7b\xe3\xf6\xcd\xe3\x1a\x19\xfd\x39\xc7\xc9\xd8",
"\xbf\x89\xf5\x11\xaa\x75\x47\x00\x7f\x48\xc0\x13\x9f\x4b\x7d\xf9",
"\x10\x00\x13\xb0\xa4\x6f\xee\xf9\xd2\x04\x35\xb4\xe3\x29\xf2\x54",
"\x3d\x22\x5f\x4e\x62\x70\xc9\x90\x1c\xd5\x95\x6c\x45\x5e\xb4\xe1",
"\x3d\x31\xcb\xee\x69\x8e\xa8\x59\x6d\x95\x55\xed\x1d\xb9\x6e\xb9",
"\x4e\x6b\x08"
];
state++;
}
// Handle disconnect
if (data == "\x01\x00\x00") {
sock.end();
}
if (packet != null && packet.length > 0) {
var packetStr = packet.join('');
sock.write(packetStr);
var myBuffer = [];
var buffer = new Buffer(packetStr, 'binary');
for (var i=0; i<buffer.length; i++) {
myBuffer.push(buffer[i]);
}
console.log('SENT: ' + sock.remoteAddress + ':' + sock.remotePort + ' > ' + toHexString(myBuffer));
}
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
let index = sockets.findIndex(function(o) {
return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
})
if (index !== -1) {
sockets.splice(index, 1);
}
console.log('DISCONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
});
});