forked from Enchoseon/2based2wait
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproxy.js
412 lines (370 loc) · 14.2 KB
/
proxy.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"use strict";
// =======
// Imports
// =======
const mcproxy = require("@rob9315/mcproxy");
const mc = require("minecraft-protocol");
const { config, status, updateStatus, updateCoordinatorStatus } = require("./util/config.js");
const logger = require("./util/logger.js");
const notifier = require("./util/notifier.js");
const chatty = require("./util/chatty.js");
const ngrok = require("./util/ngrok.js");
const mineflayer = require("./util/mineflayer.js");
const queue = require("./util/queue.js");
const downloader = require("./util/downloader.js");
const webserver = require("./util/webserver.js");
// ===========
// Global Vars
// ===========
let conn;
let client;
let server;
// ==============
// Initialization
// ==============
// Max out the threadpool (if enabled)
if (config.experimental.maxThreadpool.active) {
process.env.UV_THREADPOOL_SIZE = require("os").cpus().length;
}
// Start proxy
start();
// =================
// Packet Handler(s)
// =================
/**
* Handle incoming packets
* @param {object} packetData Packet object data
* @param {object} packetMeta Packet metadata
*/
function packetHandler(packetData, packetMeta) {
// Log packets
logger.packetHandler(packetData, packetMeta, "server");
// Assorted packet handlers
switch (packetMeta.name) {
case "chat": // Forward chat packets to chatty.js for livechat relay and reading server restart messages
chatty.chatPacketHandler(packetData);
break;
case "difficulty": // Difficulty packet handler, checks whether or not we're in queue (explanation: when rerouted by Velocity, the difficulty packet is always sent after the MC|Brand packet.)
queue.difficultyPacketHandler(packetData, conn);
break;
case "playerlist_header": // Playerlist packet handler, checks position in queue
queue.playerlistHeaderPacketHandler(packetData, server);
break;
case "map_chunk":
if (!config.experimental.worldDownloader.active) break;
downloader.mapChunkPacketHandler(packetData); // Don't proceed if world downloader isn't enabled
break;
default:
break;
}
// Reset uncleanDisconnectMonitor timer
refreshMonitor();
}
// =================
// Start Proxy Stack
// =================
/** Start proxy stack */
function start() {
logger.log("proxy", "Starting proxy stack.", "proxy");
// Delete any leftover coordinator.flag files
if (config.coordination.active) {
updateCoordinatorStatus();
}
// Create local server
createLocalServer();
// Create client (connects to server)
if (!config.waitForControllerBeforeConnect) { // ... but if waitForControllerBeforeConnect is true, delay the creation of the client until someone connects to the local server
createClient();
} else {
console.log("Waiting for a controller...");
if (config.ngrok.active) { // Create ngrok tunnel
ngrok.createTunnel(); // Note: May overwrite MSA instructions in console(?)
}
}
}
// ==========================
// Client (Connect to Server)
// ==========================
/**
* Create the client, initialize Mineflayer, and connect to the server
*/
function createClient() {
console.log("Creating client (connecting to server)");
logger.log("proxy", "Creating client.", "proxy");
// Connect to server
conn = new mcproxy.Conn({
"host": config.server.host,
"version": config.server.version,
"port": config.server.port,
"username": config.account.username,
"password": config.account.password,
"auth": config.account.auth
});
client = conn.bot._client;
mineflayer.initialize(conn.bot);
// Log connect and start Mineflayer
client.on("connect", function () {
logger.log("connected", "Client connected", "proxy");
startMineflayer();
// Create ngrok tunnel (unless waitForControllerBeforeConnect is true, in which case the tunnel already exists)
if (config.ngrok.active && !config.waitForControllerBeforeConnect) {
ngrok.createTunnel();
}
});
// Log disconnect
client.on("disconnect", function (packet) {
logger.log("disconnected", packet.reason, "proxy");
notifier.sendWebhook({
title: "Disconnected from Server: " + packet.reason,
category: "spam"
});
if (JSON.parse(packet.reason).text === "You are already connected to this proxy!") { // Send notifications when the proxy is unable to log on because the account is already in use
notifier.sendWebhook({
title: "Someone is already connected to the server using this proxy's account.",
category: "spam"
});
if (typeof server !== "undefined" && typeof server.clients[0] !== "undefined") { // Make sure client exists
server.clients[0].end("Someone is already connected to the server using this proxy's account."); // Disconnect client from the proxy with a helpful message
}
}
reconnect();
});
// Log kick
client.on("kick_disconnect", function (packet) {
logger.log("kick/disconnect", packet.reason, "proxy");
notifier.sendWebhook({
title: "Kicked from Server: " + packet.reason,
category: "spam"
});
reconnect();
});
// Packet handlers
client.on("packet", (packetData, packetMeta) => {
packetHandler(packetData, packetMeta);
});
}
// ============
// Local Server
// ============
/**
* Create the local server.
* Handles players connecting & bridges packets between from players to the bridgeClient
*/
function createLocalServer() {
console.log("Creating local server");
logger.log("proxy", "Creating local server.", "proxy");
// Create server
server = mc.createServer({
"online-mode": config.proxy.onlineMode,
"encryption": true,
"host": config.proxy.loopbackAddress,
"port": config.proxy.port,
"version": config.server.version,
"max-players": 1,
"beforePing": (function (response, client, answerToPing) {
if (!config.experimental.spoofPing.active) { // Don't proceed if noPing isn't enabled in config.json
return;
}
if (config.experimental.spoofPing.noResponse) { // Don't return a response
answerToPing(false);
} else { // Or return a fake response
answerToPing(null, config.experimental.spoofPing.fakeResponse);
}
})
});
// Handle logins
server.on("login", (bridgeClient) => {
// Block attempt if...
if (config.proxy.whitelist.findIndex(needle => bridgeClient.username.toLowerCase() === needle.toLowerCase()) === -1) { // ... player isn't in whitelist
bridgeClient.end("Your account (" + bridgeClient.username + ") is not whitelisted.\n\nIf you're getting this error in error the Microsoft account token may have expired.");
logSpam(bridgeClient.username + " (" + bridgeClient.uuid + ")" + " was denied connection to the proxy for not being whitelisted.");
return;
} else if (server.playerCount > 1) { // ... and another player isn't already connected
bridgeClient.end("This proxy is at max capacity.\n\nCurrent Controller: " + status.controller);
logSpam(bridgeClient.username + " (" + bridgeClient.uuid + ")" + " was denied connection to the proxy despite being whitelisted because " + status.controller + " was already in control.");
return;
}
// Finally, kick the player if the proxy is restarting
if (status.restart === ("Reconnecting in " + config.reconnectInterval + " seconds...")) {
if (config.ngrok.active) {
bridgeClient.end("This proxy is currently restarting.\n\nPlease wait at least " + config.reconnectInterval + " seconds and try again using the new tunnel.");
} else {
bridgeClient.end("This proxy is currently restarting.\n\nPlease wait at least " + config.reconnectInterval + " seconds and try again.");
}
logSpam(bridgeClient.username + " (" + bridgeClient.uuid + ")" + " was denied connection to the proxy despite being whitelisted because the proxy was restarting.");
return;
}
// Log successful connection attempt
logSpam(bridgeClient.username + " (" + bridgeClient.uuid + ")" + " has connected to the proxy.");
updateStatus("controller", bridgeClient.username);
if (config.notify.whenControlling) { // optional: send message to status webhook
notifier.sendWebhook({
title: bridgeClient.username + " is using the proxy.",
category: "status",
deleteOnRestart: true
});
}
// Create client if it hasn't been created yet (waitForControllerBeforeConnect)
if (config.waitForControllerBeforeConnect && typeof conn === "undefined") {
createClient();
client.on("packet", (packetData, packetMeta) => {
if (packetMeta.name === "success") {
createBridge();
}
});
} else {
createBridge();
}
/** Create bridge between client and local server */
function createBridge() {
console.log("Creating packet bridge");
logger.log("proxy", "Creating packet bridge.", "proxy");
webserver.updateWebStatus('updateController', status.controller);
// Start Mineflayer when disconnected
bridgeClient.on("end", () => {
// Log disconnect
logSpam(bridgeClient.username + " (" + bridgeClient.uuid + ")" + " has disconnected from the local server.");
updateStatus("controller", "None");
webserver.updateWebStatus('updateController', status.controller);
if (config.notify.whenControlling) { // optional: send message to status webhook
notifier.sendWebhook({
title: bridgeClient.username + " is no longer using the proxy.",
category: "status",
deleteOnRestart: true
});
}
// Disconnect if no controller after config.experimental.disconnectIfNoController.delay seconds
if (config.experimental.disconnectIfNoController.active && status.inQueue === "false") {
setTimeout(function () {
if (status.controller === "None") {
logger.log("proxy", "Restarting proxy because noone was in control " + config.experimental.disconnectIfNoController.delay + " seconds after someone DCed from proxy while it was on the server.", "proxy");
reconnect();
}
}, config.experimental.disconnectIfNoController.delay * 1000);
}
// Start Mineflayer
startMineflayer();
});
// Stop Mineflayer
stopMineflayer();
// Spoof player_info (skin fix)
if (config.experimental.spoofPlayerInfo.active) {
conn.bot.waitForTicks(1).then(() => {
bridgeClient.write("player_info", { // Add spoofed player to tablist
action: 0,
data: [{
UUID: bridgeClient.uuid,
name: conn.bot.player.username,
properties: [{
"name": "textures", // Remember to get skin info from https://sessionserver.mojang.com/session/minecraft/profile/<uuid>?unsigned=false!
"value": config.experimental.spoofPlayerInfo.texture.value,
"signature": config.experimental.spoofPlayerInfo.texture.signature
}]
}]
});
bridgeClient.write("player_info", { // Remove bot player from tablist
action: 4,
data: [{
UUID: conn.bot.player.uuid,
name: conn.bot.player.username,
properties: []
}]
});
});
}
// Log packets
bridgeClient.on("packet", (packetData, packetMeta) => {
logger.packetHandler(packetData, packetMeta, "bridgeClient");
});
// Bridge packets
bridgeClient.on("packet", (data, meta, rawData) => {
bridge(rawData, meta, client);
});
conn.sendPackets(bridgeClient);
conn.link(bridgeClient);
}
/**
* Send message to logger and spam webhook
* @param {string} logMsg Message to log
*/
function logSpam(logMsg) {
logger.log("bridgeclient", logMsg, "proxy");
notifier.sendWebhook({
title: logMsg,
category: "spam"
});
}
});
}
// ==========================
// Unclean Disconnect Monitor
// ==========================
let uncleanDisconnectMonitor;
/**
* If no packets are received for config.uncleanDisconnectInterval seconds, assume we were disconnected uncleanly and reconnect.
*/
function refreshMonitor() {
if (!uncleanDisconnectMonitor) { // Create timer on the first packet
uncleanDisconnectMonitor = setTimeout(function () {
logger.log("proxy", "No packets were received for " + config.uncleanDisconnectInterval + " seconds. Assuming unclean disconnect.", "proxy");
reconnect();
}, config.uncleanDisconnectInterval * 1000);
return;
}
uncleanDisconnectMonitor.refresh(); // Restart the timer
}
// =========
// Functions
// =========
/** Reconnect (Remember to read https://github.com/Enchoseon/2based2wait/wiki/How-to-Auto-Reconnect-with-Supervisor or this will just cause the script to shut down!) */
function reconnect() {
console.log("Reconnecting...");
logger.log("proxy", "Reconnecting...", "proxy");
if (typeof conn !== "undefined") { // Make sure connection exists
conn.disconnect(); // Disconnect proxy from the server
}
if (typeof server !== "undefined" && typeof server.clients[0] !== "undefined") { // Make sure client exists
server.clients[0].end("Proxy restarting..."); // Disconnect client from the proxy
}
notifier.sendWebhook({
title: "Reconnecting...",
category: "spam"
});
updateStatus("restart", "Reconnecting in " + config.reconnectInterval + " seconds...");
updateStatus("livechatRelay", "false");
notifier.deleteMarkedMessages();
setTimeout(function() {
updateStatus("restart", "Reconnecting now!");
notifier.sendToast("Reconnecting now!");
process.exit(1);
}, config.reconnectInterval * 1000);
}
/** Start Mineflayer */
function startMineflayer() {
logger.log("mineflayer", "Starting Mineflayer.", "proxy");
updateStatus("mineflayer", true);
if (config.mineflayer.active) {
conn.bot.autoEat.enable();
conn.bot.afk.start();
}
}
/** Stop Mineflayer */
function stopMineflayer() {
logger.log("mineflayer", "Stopping Mineflayer.", "proxy");
updateStatus("mineflayer", false);
if (config.mineflayer.active) {
conn.bot.autoEat.disable();
conn.bot.afk.stop();
}
}
/**
* Send packets from Point A to Point B
* @param {object} packetData Packet object data
* @param {object} packetMeta Packet metadata
* @param {object} dest McProxy client to write data to
*/
function bridge(packetData, packetMeta, dest) {
if (packetMeta.name !== "keep_alive" && packetMeta.name !== "update_time") { // Keep-alive packets are filtered bc the client already handles them. Sending double would kick us.
dest.writeRaw(packetData);
}
}