File size: 16,988 Bytes
95287d5
 
 
 
 
 
 
 
 
 
 
 
f39bfe4
 
 
95287d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f39bfe4
95287d5
 
 
 
 
 
 
 
 
 
 
 
 
 
f39bfe4
 
 
 
 
 
95287d5
 
 
 
 
 
 
 
 
 
f39bfe4
 
 
 
 
95287d5
 
f39bfe4
95287d5
 
 
 
f39bfe4
 
 
 
 
 
95287d5
 
f39bfe4
 
 
 
 
 
95287d5
f39bfe4
 
95287d5
 
 
 
 
 
f39bfe4
 
 
 
 
 
95287d5
 
 
 
 
 
 
 
 
 
 
f39bfe4
 
 
95287d5
 
 
 
 
 
f39bfe4
95287d5
f39bfe4
95287d5
 
 
 
 
 
 
 
 
f39bfe4
95287d5
 
 
 
 
 
f39bfe4
 
 
 
 
 
 
 
 
95287d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f39bfe4
 
95287d5
f39bfe4
95287d5
 
f39bfe4
 
 
 
 
95287d5
 
 
f39bfe4
95287d5
 
 
 
f39bfe4
 
 
 
 
 
 
 
 
 
 
 
 
95287d5
 
 
f39bfe4
 
 
 
 
95287d5
 
 
 
f39bfe4
 
 
 
 
 
 
 
95287d5
 
f39bfe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95287d5
f39bfe4
95287d5
f39bfe4
 
 
 
95287d5
f39bfe4
95287d5
f39bfe4
 
 
 
 
95287d5
 
f39bfe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95287d5
 
 
 
 
f39bfe4
 
 
 
 
 
95287d5
 
 
 
 
f39bfe4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95287d5
 
 
 
 
f39bfe4
 
 
 
 
 
 
95287d5
f39bfe4
 
 
95287d5
 
 
 
f39bfe4
 
 
 
 
95287d5
 
 
 
 
 
 
 
 
 
 
 
f39bfe4
 
 
 
 
 
 
95287d5
f39bfe4
 
 
95287d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f39bfe4
 
 
 
 
 
 
 
 
 
 
 
 
95287d5
 
 
f39bfe4
95287d5
 
 
 
 
 
 
 
 
 
f39bfe4
 
 
 
 
 
 
 
 
 
 
 
 
95287d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const mineflayer = require('mineflayer');
const path = require('path');

const app = express();
const server = http.createServer(app);
const io = new Server(server, {
    cors: {
        origin: "*",
        methods: ["GET", "POST"]
    },
    pingTimeout: 60000,
    pingInterval: 25000
});

const PORT = process.env.PORT || 7860;

// Store active bots
const activeBots = new Map();

// Middleware
app.use(express.json());
app.use(express.static('public'));

// Health check endpoint
app.get('/health', (req, res) => {
    res.json({ status: true, uptime: process.uptime(), bots: activeBots.size });
});

// API Routes
app.get('/api/bots', (req, res) => {
    const bots = Array.from(activeBots.entries()).map(([id, data]) => ({
        id,
        username: data.bot?.username || 'unknown',
        host: data.host,
        port: data.port,
        connected: data.connected,
        players: data.players || []
    }));
    res.json(bots);
});

// Socket.IO Connection
io.on('connection', (socket) => {
    console.log('Client connected:', socket.id);

    let currentBot = null;
    let botId = null;
    let reconnectTimer = null;
    let reconnectAttempts = 0;
    const MAX_RECONNECT_ATTEMPTS = 5;
    const RECONNECT_DELAY = 5000;
    let botConfig = null;
    let isIntentionalDisconnect = false;

    // Connect bot to Minecraft server
    socket.on('connect-bot', (config) => {
        const { username, host, port = 25565, version } = config;

        if (!username || !host) {
            socket.emit('error', { message: 'Username and host are required' });
            return;
        }

        // Save config for reconnect
        botConfig = { username, host, port, version };
        isIntentionalDisconnect = false;
        reconnectAttempts = 0;

        // Disconnect existing bot for this socket
        if (currentBot) {
            isIntentionalDisconnect = true;
            currentBot.quit();
            activeBots.delete(botId);
        }

        // Clear any pending reconnect
        if (reconnectTimer) {
            clearTimeout(reconnectTimer);
            reconnectTimer = null;
        }

        botId = `${socket.id}_${Date.now()}`;

        createBotConnection(config, socket);
    });

    function createBotConnection(config, socket) {
        const { username, host, port = 25565, version } = config;

        try {
            console.log(`Creating bot ${username} for ${host}:${port}...`);

            const bot = mineflayer.createBot({
                host: host,
                port: parseInt(port),
                username: username,
                version: version || false,
                auth: 'offline',
                hideErrors: false,
                checkTimeoutInterval: 30000,
                closeTimeout: 10000,
                keepAlive: true,
                // Add some delay to avoid rate limiting
                connectTimeout: 30000
            });

            currentBot = bot;

            const botData = {
                bot: bot,
                socket: socket,
                host: host,
                port: port,
                connected: false,
                players: [],
                chatHistory: [],
                spawnTimeout: null,
                keepAliveInterval: null
            };

            activeBots.set(botId, botData);

            // Bot Events
            bot.on('login', () => {
                console.log(`Bot ${username} logged in to ${host}:${port}`);
                botData.connected = true;
                reconnectAttempts = 0;
                socket.emit('bot-status', {
                    status: 'online',
                    username: bot.username,
                    host: host,
                    port: port
                });
            });

            bot.on('spawn', () => {
                console.log(`Bot ${username} spawned`);
                socket.emit('bot-spawned', {
                    position: bot.entity?.position,
                    health: bot.health,
                    food: bot.food
                });
                updatePlayersList(bot, socket);

                // Start keep-alive mechanism
                startKeepAlive(bot, botData);

                // Clear spawn timeout
                if (botData.spawnTimeout) {
                    clearTimeout(botData.spawnTimeout);
                    botData.spawnTimeout = null;
                }
            });

            bot.on('chat', (username, message) => {
                if (username === bot.username) return;

                const chatData = {
                    username: username,
                    message: message,
                    timestamp: new Date().toISOString(),
                    type: 'chat'
                };

                botData.chatHistory.push(chatData);
                socket.emit('chat-message', chatData);
            });

            bot.on('message', (jsonMsg) => {
                const text = jsonMsg.toString();
                if (text && !text.includes(bot.username)) {
                    socket.emit('system-message', {
                        message: text,
                        timestamp: new Date().toISOString()
                    });
                }
            });

            bot.on('playerJoined', (player) => {
                socket.emit('player-joined', {
                    username: player.username,
                    timestamp: new Date().toISOString()
                });
                updatePlayersList(bot, socket);
            });

            bot.on('playerLeft', (player) => {
                socket.emit('player-left', {
                    username: player.username,
                    timestamp: new Date().toISOString()
                });
                updatePlayersList(bot, socket);
            });

            bot.on('health', () => {
                socket.emit('bot-health', {
                    health: bot.health,
                    food: bot.food
                });
            });

            bot.on('death', () => {
                socket.emit('bot-death', {
                    message: 'Bot died!',
                    timestamp: new Date().toISOString()
                });
            });

            bot.on('kicked', (reason) => {
                console.log(`Bot ${username} kicked:`, reason);
                const reasonStr = reason.toString ? reason.toString() : JSON.stringify(reason);
                socket.emit('bot-kicked', {
                    reason: reasonStr,
                    timestamp: new Date().toISOString()
                });

                // Try to reconnect if not intentional
                if (!isIntentionalDisconnect) {
                    attemptReconnect(socket, `Kicked: ${reasonStr}`);
                }
            });

            bot.on('error', (err) => {
                console.error(`Bot ${username} error:`, err.message);
                socket.emit('bot-error', {
                    message: err.message,
                    timestamp: new Date().toISOString()
                });

                // Only attempt reconnect for connection errors, not auth errors
                if (err.message && (
                    err.message.includes('ETIMEDOUT') ||
                    err.message.includes('ECONNRESET') ||
                    err.message.includes('ECONNREFUSED') ||
                    err.message.includes('timeout') ||
                    err.message.includes('disconnected')
                )) {
                    if (!isIntentionalDisconnect) {
                        attemptReconnect(socket, `Error: ${err.message}`);
                    }
                }
            });

            bot.on('end', (reason) => {
                console.log(`Bot ${username} disconnected. Reason:`, reason, 'Intentional:', isIntentionalDisconnect);

                // Stop keep-alive
                stopKeepAlive(botData);

                socket.emit('bot-disconnected', {
                    reason: reason || 'Connection closed',
                    timestamp: new Date().toISOString()
                });

                // Cleanup but keep the bot data for potential reconnect
                cleanupBotListeners(botId);

                // Attempt reconnect if not intentional disconnect
                if (!isIntentionalDisconnect) {
                    attemptReconnect(socket, reason || 'Connection closed');
                }
            });

            // Spawn timeout - if bot doesn't spawn within 30 seconds, reconnect
            botData.spawnTimeout = setTimeout(() => {
                if (!botData.connected) {
                    console.log(`Bot ${username} spawn timeout, reconnecting...`);
                    socket.emit('bot-error', {
                        message: 'Spawn timeout, reconnecting...',
                        timestamp: new Date().toISOString()
                    });
                    if (!isIntentionalDisconnect) {
                        bot.quit();
                        attemptReconnect(socket, 'Spawn timeout');
                    }
                }
            }, 30000);

        } catch (error) {
            console.error('Failed to create bot:', error);
            socket.emit('error', { message: error.message });

            if (!isIntentionalDisconnect) {
                attemptReconnect(socket, `Create error: ${error.message}`);
            }
        }
    }

    function attemptReconnect(socket, reason) {
        if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
            console.log(`Max reconnect attempts reached for ${botConfig?.username}`);
            socket.emit('bot-error', {
                message: `Max reconnect attempts reached. Please check server details or try again later.`,
                timestamp: new Date().toISOString()
            });
            return;
        }

        reconnectAttempts++;
        console.log(`Reconnect attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS} for ${botConfig?.username} in ${RECONNECT_DELAY}ms`);

        socket.emit('bot-reconnecting', {
            attempt: reconnectAttempts,
            maxAttempts: MAX_RECONNECT_ATTEMPTS,
            delay: RECONNECT_DELAY,
            reason: reason,
            timestamp: new Date().toISOString()
        });

        reconnectTimer = setTimeout(() => {
            if (botConfig && !isIntentionalDisconnect) {
                console.log(`Executing reconnect for ${botConfig.username}...`);
                createBotConnection(botConfig, socket);
            }
        }, RECONNECT_DELAY);
    }

    function startKeepAlive(bot, botData) {
        // Send periodic position updates to keep connection alive
        botData.keepAliveInterval = setInterval(() => {
            if (bot && bot.entity) {
                // Small random movement to prevent AFK kick
                try {
                    const random = Math.random();
                    if (random < 0.1) {
                        // 10% chance to look around
                        const yaw = Math.random() * Math.PI * 2;
                        bot.look(yaw, 0, false);
                    }
                } catch (e) {
                    // Ignore look errors
                }
            }
        }, 15000); // Every 15 seconds
    }

    function stopKeepAlive(botData) {
        if (botData.keepAliveInterval) {
            clearInterval(botData.keepAliveInterval);
            botData.keepAliveInterval = null;
        }
        if (botData.spawnTimeout) {
            clearTimeout(botData.spawnTimeout);
            botData.spawnTimeout = null;
        }
    }

    // Send chat message
    socket.on('send-chat', (message) => {
        if (currentBot && currentBot.chat && botConfig) {
            try {
                currentBot.chat(message);
                socket.emit('chat-sent', {
                    username: currentBot.username,
                    message: message,
                    timestamp: new Date().toISOString()
                });
            } catch (e) {
                socket.emit('error', { message: 'Failed to send chat: ' + e.message });
            }
        }
    });

    // Send command
    socket.on('send-command', (command) => {
        if (currentBot && currentBot.chat && botConfig) {
            try {
                currentBot.chat(command);
            } catch (e) {
                socket.emit('error', { message: 'Failed to send command: ' + e.message });
            }
        }
    });

    // Bot actions
    socket.on('bot-action', (action) => {
        if (!currentBot || !botConfig) return;

        try {
            switch(action.type) {
                case 'jump':
                    currentBot.setControlState('jump', true);
                    setTimeout(() => currentBot.setControlState('jump', false), 500);
                    break;
                case 'sneak':
                    currentBot.setControlState('sneak', true);
                    setTimeout(() => currentBot.setControlState('sneak', false), 1000);
                    break;
                case 'look':
                    if (action.x !== undefined && action.y !== undefined && action.z !== undefined) {
                        const Vec3 = require('vec3');
                        currentBot.lookAt(new Vec3(action.x, action.y, action.z));
                    }
                    break;
                case 'move':
                    if (action.direction) {
                        currentBot.setControlState(action.direction, true);
                        setTimeout(() => currentBot.setControlState(action.direction, false), 500);
                    }
                    break;
            }
        } catch (e) {
            socket.emit('error', { message: 'Action failed: ' + e.message });
        }
    });

    // Disconnect bot
    socket.on('disconnect-bot', () => {
        isIntentionalDisconnect = true;

        if (reconnectTimer) {
            clearTimeout(reconnectTimer);
            reconnectTimer = null;
        }

        if (currentBot) {
            try {
                currentBot.quit();
            } catch (e) {}
            cleanupBot(botId);
            currentBot = null;
            botId = null;
        }

        socket.emit('bot-disconnected', {
            reason: 'Disconnected by user',
            timestamp: new Date().toISOString()
        });
    });

    // Get players list
    socket.on('get-players', () => {
        if (currentBot) {
            updatePlayersList(currentBot, socket);
        }
    });

    // Disconnect handler
    socket.on('disconnect', () => {
        console.log('Client disconnected:', socket.id);
        isIntentionalDisconnect = true;

        if (reconnectTimer) {
            clearTimeout(reconnectTimer);
            reconnectTimer = null;
        }

        if (currentBot) {
            try {
                currentBot.quit();
            } catch (e) {}
            cleanupBot(botId);
        }
    });
});

function updatePlayersList(bot, socket) {
    if (!bot.players) return;

    const players = Object.values(bot.players).map(p => ({
        username: p.username,
        ping: p.ping,
        uuid: p.uuid
    }));

    socket.emit('players-list', players);
}

function cleanupBotListeners(id) {
    if (activeBots.has(id)) {
        const data = activeBots.get(id);
        stopKeepAlive(data);
        if (data.bot) {
            try {
                data.bot.removeAllListeners();
            } catch (e) {}
        }
        activeBots.delete(id);
    }
}

function cleanupBot(id) {
    if (activeBots.has(id)) {
        const data = activeBots.get(id);
        stopKeepAlive(data);
        if (data.bot) {
            try {
                data.bot.quit();
                data.bot.removeAllListeners();
            } catch (e) {}
        }
        activeBots.delete(id);
    }
}

function stopKeepAlive(botData) {
    if (botData) {
        if (botData.keepAliveInterval) {
            clearInterval(botData.keepAliveInterval);
            botData.keepAliveInterval = null;
        }
        if (botData.spawnTimeout) {
            clearTimeout(botData.spawnTimeout);
            botData.spawnTimeout = null;
        }
    }
}

// Start server
server.listen(PORT, () => {
    console.log(`🎮 Minecraft Bot Controller running on port ${PORT}`);
    console.log(`🌐 Access the web interface at http://localhost:${PORT}`);
});

// Graceful shutdown
process.on('SIGTERM', () => {
    console.log('SIGTERM received, shutting down gracefully');
    activeBots.forEach((data, id) => {
        cleanupBot(id);
    });
    server.close(() => {
        process.exit(0);
    });
});

process.on('SIGINT', () => {
    console.log('SIGINT received, shutting down gracefully');
    activeBots.forEach((data, id) => {
        cleanupBot(id);
    });
    server.close(() => {
        process.exit(0);
    });
});