Spaces:
Paused
Paused
Update bot.js
Browse files
bot.js
CHANGED
|
@@ -16,7 +16,7 @@ const https = require('https');
|
|
| 16 |
// --- AYARLAR ---
|
| 17 |
const HOME_ARRIVAL_DELAY_MS = 2000;
|
| 18 |
const POST_ACTION_DELAY_MS = 4000;
|
| 19 |
-
const WINDOW_OPEN_TIMEOUT_MS = 6000;
|
| 20 |
const PERIODIC_CHECK_INTERVAL_MS = 10 * 60 * 1000;
|
| 21 |
const FULL_FEED_COOLDOWN_MS = 50 * 60 * 1000;
|
| 22 |
const GO_HOME_COMMAND = "/home";
|
|
@@ -38,6 +38,7 @@ let reconnectTimeout = null;
|
|
| 38 |
let currentSshClient = null;
|
| 39 |
let currentStream = null;
|
| 40 |
let currentSocket = null;
|
|
|
|
| 41 |
|
| 42 |
// --- PANEL METRİKLERİ ---
|
| 43 |
let botUptimeStart = null;
|
|
@@ -107,8 +108,13 @@ async function createBot() {
|
|
| 107 |
|
| 108 |
logger.warn(`[Bağlantı Yedekleme] Doğrudan IP moduna geçiliyor (Sebep: ${reason}). Kendi (Yurt dışı) IP'miz üzerinden GERÇEK TCP bağlantısı kuruluyor...`);
|
| 109 |
|
|
|
|
| 110 |
if (sshClient) {
|
| 111 |
-
try {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
}
|
| 113 |
|
| 114 |
const socket = net.connect({
|
|
@@ -117,10 +123,8 @@ async function createBot() {
|
|
| 117 |
});
|
| 118 |
currentSocket = socket;
|
| 119 |
|
| 120 |
-
// KESİN ÇÖZÜM KİLİDİ: 1 dakikalık süreçte hem sunucu hatasının hem de timeout'un aynı anda çift tetiklenmesini engeller
|
| 121 |
let isDirectHandled = false;
|
| 122 |
|
| 123 |
-
// İstediğin gibi: Kendi IP'si için zaman aşımı toleransını tam 1 dakikaya (60.000 ms) çıkardık
|
| 124 |
const directTimeout = setTimeout(() => {
|
| 125 |
if (isDirectHandled) return;
|
| 126 |
isDirectHandled = true;
|
|
@@ -277,7 +281,6 @@ async function createBot() {
|
|
| 277 |
bot.loadPlugin(pathfinder);
|
| 278 |
|
| 279 |
let lastFullFeedAt = 0;
|
| 280 |
-
let windowOpenResolvers = [];
|
| 281 |
let periodicHandle = null;
|
| 282 |
let activeTimeouts = [];
|
| 283 |
|
|
@@ -470,16 +473,50 @@ async function createBot() {
|
|
| 470 |
|
| 471 |
try {
|
| 472 |
if (bot.lookAt) await bot.lookAt(best.entity.position.offset(0, 1.0, 0), true);
|
| 473 |
-
await new Promise(r => setTimeout(r,
|
| 474 |
-
|
| 475 |
-
logger.info(`activateEntityAt çağrıldı id:${best.entity.id}`);
|
| 476 |
-
if (typeof bot.activateEntityAt === "function") bot.activateEntityAt(best.entity, best.entity.position);
|
| 477 |
-
else if (typeof bot.activateEntity === "function") bot.activateEntity(best.entity);
|
| 478 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
await new Promise((resolve) => {
|
| 480 |
-
const
|
| 481 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 482 |
});
|
|
|
|
| 483 |
} catch (err) {}
|
| 484 |
}
|
| 485 |
|
|
@@ -589,6 +626,9 @@ function safeDestroyAllConnections() {
|
|
| 589 |
isDestroying = true;
|
| 590 |
isConnecting = false;
|
| 591 |
|
|
|
|
|
|
|
|
|
|
| 592 |
if (viewerInstance && typeof viewerInstance.close === 'function') {
|
| 593 |
try {
|
| 594 |
if (viewerInstance.ws && typeof viewerInstance.ws.close === 'function') {
|
|
@@ -600,14 +640,17 @@ function safeDestroyAllConnections() {
|
|
| 600 |
viewerInstance = null;
|
| 601 |
if (bot) { bot.viewer = null; }
|
| 602 |
|
|
|
|
| 603 |
if (currentStream && typeof currentStream.destroy === 'function') {
|
| 604 |
try { currentStream.destroy(); } catch(e){}
|
| 605 |
}
|
| 606 |
-
|
| 607 |
-
try { currentSocket.destroy(); } catch(e){}
|
| 608 |
-
}
|
| 609 |
if (currentSshClient && typeof currentSshClient.destroy === 'function') {
|
| 610 |
try { currentSshClient.destroy(); } catch(e){}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
}
|
| 612 |
|
| 613 |
currentStream = null;
|
|
@@ -615,8 +658,14 @@ function safeDestroyAllConnections() {
|
|
| 615 |
currentSshClient = null;
|
| 616 |
|
| 617 |
if (bot) {
|
| 618 |
-
try {
|
| 619 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 620 |
bot = null;
|
| 621 |
}
|
| 622 |
}
|
|
@@ -629,7 +678,7 @@ function triggerSafeReconnect() {
|
|
| 629 |
logger.info(`[Sistem] ${delay} ms sonra tekil bir hat üzerinden sistem sıfırdan başlatılacak...`);
|
| 630 |
|
| 631 |
reconnectTimeout = setTimeout(() => {
|
| 632 |
-
createBot();
|
| 633 |
}, delay);
|
| 634 |
}
|
| 635 |
|
|
@@ -758,18 +807,25 @@ app.use('/3dview', checkAndStartViewer, (req, res) => {
|
|
| 758 |
});
|
| 759 |
|
| 760 |
app.get("/restart", (req, res) => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 761 |
if (bot && botIsReady) {
|
| 762 |
-
logger.info("[Sistem] Web paneli üzerinden tam restart isteği geldi. Proje kapatılıyor...");
|
| 763 |
try { bot.chat("Bot web paneli uzerinden tum kodlariyla yeniden baslatiliyor..."); } catch(e){}
|
| 764 |
-
res.send({ success: true, message: "Tüm proje kapatıldı.
|
|
|
|
| 765 |
setTimeout(() => {
|
| 766 |
-
|
| 767 |
-
|
|
|
|
| 768 |
process.exit(0);
|
| 769 |
}, 1000);
|
| 770 |
} else {
|
| 771 |
-
|
| 772 |
-
res.send({ success: true, message: "Bot bağlı değildi ancak tüm proje kökten kapatıldı, yeniden başlatılıyor." });
|
| 773 |
setTimeout(() => {
|
| 774 |
process.exit(0);
|
| 775 |
}, 1000);
|
|
@@ -832,7 +888,7 @@ server.on('upgrade', (req, socket, head) => {
|
|
| 832 |
});
|
| 833 |
|
| 834 |
proxyReq.on('error', (err) => {
|
| 835 |
-
logger.error("[WS Tünel Hatası] Websocket
|
| 836 |
});
|
| 837 |
|
| 838 |
proxyReq.end();
|
|
|
|
| 16 |
// --- AYARLAR ---
|
| 17 |
const HOME_ARRIVAL_DELAY_MS = 2000;
|
| 18 |
const POST_ACTION_DELAY_MS = 4000;
|
| 19 |
+
const WINDOW_OPEN_TIMEOUT_MS = 6000; // Mutlak üst sınır (6 Saniye)
|
| 20 |
const PERIODIC_CHECK_INTERVAL_MS = 10 * 60 * 1000;
|
| 21 |
const FULL_FEED_COOLDOWN_MS = 50 * 60 * 1000;
|
| 22 |
const GO_HOME_COMMAND = "/home";
|
|
|
|
| 38 |
let currentSshClient = null;
|
| 39 |
let currentStream = null;
|
| 40 |
let currentSocket = null;
|
| 41 |
+
let windowOpenResolvers = []; // Havuzun global erişilebilirliği garanti altına alındı
|
| 42 |
|
| 43 |
// --- PANEL METRİKLERİ ---
|
| 44 |
let botUptimeStart = null;
|
|
|
|
| 108 |
|
| 109 |
logger.warn(`[Bağlantı Yedekleme] Doğrudan IP moduna geçiliyor (Sebep: ${reason}). Kendi (Yurt dışı) IP'miz üzerinden GERÇEK TCP bağlantısı kuruluyor...`);
|
| 110 |
|
| 111 |
+
// KESİN ÇÖZÜM 4: Eski SSH nesnesinin tüm listener'larını söküyoruz ki arkadan zombi tetikleme yapmasın
|
| 112 |
if (sshClient) {
|
| 113 |
+
try {
|
| 114 |
+
sshClient.removeAllListeners('ready');
|
| 115 |
+
sshClient.removeAllListeners('error');
|
| 116 |
+
sshClient.destroy();
|
| 117 |
+
} catch (e) {}
|
| 118 |
}
|
| 119 |
|
| 120 |
const socket = net.connect({
|
|
|
|
| 123 |
});
|
| 124 |
currentSocket = socket;
|
| 125 |
|
|
|
|
| 126 |
let isDirectHandled = false;
|
| 127 |
|
|
|
|
| 128 |
const directTimeout = setTimeout(() => {
|
| 129 |
if (isDirectHandled) return;
|
| 130 |
isDirectHandled = true;
|
|
|
|
| 281 |
bot.loadPlugin(pathfinder);
|
| 282 |
|
| 283 |
let lastFullFeedAt = 0;
|
|
|
|
| 284 |
let periodicHandle = null;
|
| 285 |
let activeTimeouts = [];
|
| 286 |
|
|
|
|
| 473 |
|
| 474 |
try {
|
| 475 |
if (bot.lookAt) await bot.lookAt(best.entity.position.offset(0, 1.0, 0), true);
|
| 476 |
+
await new Promise(r => setTimeout(r, 400));
|
|
|
|
|
|
|
|
|
|
|
|
|
| 477 |
|
| 478 |
+
const startTime = Date.now();
|
| 479 |
+
const endTime = startTime + WINDOW_OPEN_TIMEOUT_MS;
|
| 480 |
+
let clickCount = 0;
|
| 481 |
+
let isLoopActive = true;
|
| 482 |
+
|
| 483 |
await new Promise((resolve) => {
|
| 484 |
+
const cleanFinish = () => {
|
| 485 |
+
isLoopActive = false;
|
| 486 |
+
clearInterval(clickInterval);
|
| 487 |
+
windowOpenResolvers = []; // KESİN ÇÖZÜM 1: Havuz her durumda sıfırlanır
|
| 488 |
+
resolve();
|
| 489 |
+
};
|
| 490 |
+
|
| 491 |
+
windowOpenResolvers.push(() => {
|
| 492 |
+
cleanFinish();
|
| 493 |
+
});
|
| 494 |
+
|
| 495 |
+
const clickInterval = setInterval(() => {
|
| 496 |
+
if (!isLoopActive) return;
|
| 497 |
+
|
| 498 |
+
if (bot.currentWindow != null) {
|
| 499 |
+
cleanFinish();
|
| 500 |
+
return;
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
if (Date.now() > endTime) {
|
| 504 |
+
logger.warn(`[Tıklama Sistemi] ${WINDOW_OPEN_TIMEOUT_MS} ms doldu ancak menü sunucudan gelmedi. Pes ediliyor.`);
|
| 505 |
+
cleanFinish();
|
| 506 |
+
return;
|
| 507 |
+
}
|
| 508 |
+
|
| 509 |
+
clickCount++;
|
| 510 |
+
logger.info(`[Tıklama Sistemi] Menü tespiti başarısız. Deneme #${clickCount} gönderiliyor... (id:${best.entity.id})`);
|
| 511 |
+
|
| 512 |
+
try {
|
| 513 |
+
if (typeof bot.activateEntityAt === "function") bot.activateEntityAt(best.entity, best.entity.position);
|
| 514 |
+
else if (typeof bot.activateEntity === "function") bot.activateEntity(best.entity);
|
| 515 |
+
} catch (err) {}
|
| 516 |
+
|
| 517 |
+
}, 600);
|
| 518 |
});
|
| 519 |
+
|
| 520 |
} catch (err) {}
|
| 521 |
}
|
| 522 |
|
|
|
|
| 626 |
isDestroying = true;
|
| 627 |
isConnecting = false;
|
| 628 |
|
| 629 |
+
// KESİN ÇÖZÜM 2: Bağlantı koptuğu an resolvers dizisi kökten boşaltılır, bir sonraki girişe zombi taşımaz
|
| 630 |
+
windowOpenResolvers = [];
|
| 631 |
+
|
| 632 |
if (viewerInstance && typeof viewerInstance.close === 'function') {
|
| 633 |
try {
|
| 634 |
if (viewerInstance.ws && typeof viewerInstance.ws.close === 'function') {
|
|
|
|
| 640 |
viewerInstance = null;
|
| 641 |
if (bot) { bot.viewer = null; }
|
| 642 |
|
| 643 |
+
// KESİN ÇÖZÜM 1: Önce alt akış (stream) kapatılır, ardından hiyerarşik imha yapılır
|
| 644 |
if (currentStream && typeof currentStream.destroy === 'function') {
|
| 645 |
try { currentStream.destroy(); } catch(e){}
|
| 646 |
}
|
| 647 |
+
|
|
|
|
|
|
|
| 648 |
if (currentSshClient && typeof currentSshClient.destroy === 'function') {
|
| 649 |
try { currentSshClient.destroy(); } catch(e){}
|
| 650 |
+
} else {
|
| 651 |
+
if (currentSocket && typeof currentSocket.destroy === 'function') {
|
| 652 |
+
try { currentSocket.destroy(); } catch(e){}
|
| 653 |
+
}
|
| 654 |
}
|
| 655 |
|
| 656 |
currentStream = null;
|
|
|
|
| 658 |
currentSshClient = null;
|
| 659 |
|
| 660 |
if (bot) {
|
| 661 |
+
try {
|
| 662 |
+
// KESİN ÇÖZÜM 3: Event sızıntılarını önlemek için bot üstündeki tüm listenerlar zorla sökülür
|
| 663 |
+
bot.removeAllListeners('message');
|
| 664 |
+
bot.removeAllListeners('spawn');
|
| 665 |
+
bot.removeAllListeners('windowOpen');
|
| 666 |
+
bot.removeAllListeners('end');
|
| 667 |
+
bot.quit();
|
| 668 |
+
} catch(e){}
|
| 669 |
bot = null;
|
| 670 |
}
|
| 671 |
}
|
|
|
|
| 678 |
logger.info(`[Sistem] ${delay} ms sonra tekil bir hat üzerinden sistem sıfırdan başlatılacak...`);
|
| 679 |
|
| 680 |
reconnectTimeout = setTimeout(() => {
|
| 681 |
+
createBot();
|
| 682 |
}, delay);
|
| 683 |
}
|
| 684 |
|
|
|
|
| 807 |
});
|
| 808 |
|
| 809 |
app.get("/restart", (req, res) => {
|
| 810 |
+
logger.info("[Sistem] Web paneli üzerinden tam restart isteği geldi. Sistem felç ediliyor...");
|
| 811 |
+
|
| 812 |
+
if (config.utils) {
|
| 813 |
+
config.utils["auto-reconnect"] = false;
|
| 814 |
+
}
|
| 815 |
+
isDestroying = true;
|
| 816 |
+
|
| 817 |
if (bot && botIsReady) {
|
|
|
|
| 818 |
try { bot.chat("Bot web paneli uzerinden tum kodlariyla yeniden baslatiliyor..."); } catch(e){}
|
| 819 |
+
res.send({ success: true, message: "Tüm proje kapatıldı. Konteyner birkaç saniye içinde sıfırdan başlayacak." });
|
| 820 |
+
|
| 821 |
setTimeout(() => {
|
| 822 |
+
try { bot.quit(); } catch(e){}
|
| 823 |
+
safeDestroyAllConnections();
|
| 824 |
+
logger.info("[Sistem] process.exit(0) çağrılıyor.");
|
| 825 |
process.exit(0);
|
| 826 |
}, 1000);
|
| 827 |
} else {
|
| 828 |
+
res.send({ success: true, message: "Bot bağlı değildi ancak konteyner kökten kapatılıyor." });
|
|
|
|
| 829 |
setTimeout(() => {
|
| 830 |
process.exit(0);
|
| 831 |
}, 1000);
|
|
|
|
| 888 |
});
|
| 889 |
|
| 890 |
proxyReq.on('error', (err) => {
|
| 891 |
+
logger.error("[WS Tünel Hatası] Websocket aktarızedilemedi: " + err.message);
|
| 892 |
});
|
| 893 |
|
| 894 |
proxyReq.end();
|