-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (109 loc) · 3.46 KB
/
index.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
const fs = require('fs');
const net = require('net');
const varint = require('varint');
const config = require('./config.json');
const writeQueue = [];
function write() {
const data = writeQueue.splice(0).join('\n');
if (data.length) {
console.log(data);
fs.appendFileSync(config.logFile, data + '\n');
}
setTimeout(write, 0);
}
write();
const server = net.createServer((socket) => {
socket.connected = true;
setTimeout(() => { if (socket.connected) socket.end() }, 6000);
state = 0;
let leftover = Buffer.alloc(0);
socket.on('data', (data) => {
try {
data = Buffer.concat([leftover, data]);
let packetLength;
try {
packetLength = varint.decode(data);
} catch (err) {
leftover = Buffer.alloc(0);
return;
}
if (data.length < packetLength + varint.decode.bytes) {
leftover = data;
return;
}
leftover = data.subarray(packetLength + varint.decode.bytes);
data = data.subarray(varint.decode.bytes, packetLength + varint.decode.bytes);
switch (state) {
case 0: {
// handshake
state = data[data.length - 1];
if (data[0] != 0 || state < 1 || state > 2) socket.end();
break;
}
case 1: {
// status request
if (data[0] != 0) {
socket.end();
break;
}
writeQueue.push(`${new Date().getTime()} STATUS ${socket.remoteAddress}`);
for (const webhook of config.webhooks) {
fetch(webhook, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
content: `STATUS ${socket.remoteAddress}`
})
});
}
const response = fs.readFileSync('./status.json').toString();
const responseData = Buffer.concat([
Buffer.from([0x00]), // packet id
Buffer.from(varint.encode(response.length)),
Buffer.from(response, 'utf-8')
]);
const responseLength = Buffer.from(varint.encode(responseData.length));
socket.write(Buffer.concat([responseLength, responseData]));
socket.end();
break;
}
case 2: {
// login start
if (data[0] != 0) {
socket.end();
break;
}
writeQueue.push(`${new Date().getTime()} JOIN ${socket.remoteAddress}`);
for (const webhook of config.webhooks) {
fetch(webhook, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
content: `JOIN ${socket.remoteAddress}`
})
});
}
const response = fs.readFileSync('./disconnectMessage.json').toString();
const responseData = Buffer.concat([
Buffer.from([0x00]), // packet id
Buffer.from([response.length]),
Buffer.from(response, 'utf-8')
]);
const responseLength = Buffer.alloc(1);
responseLength.writeUInt8(responseData.length);
socket.write(Buffer.concat([responseLength, responseData]));
socket.end();
break;
}
}
socket.emit('data', Buffer.alloc(0))
} catch (err) {
socket.end();
}
});
socket.on('end', () => { socket.connected = false })
}).listen(25565);