Update modules/BotCore.js
Browse files- modules/BotCore.js +12 -123
modules/BotCore.js
CHANGED
|
@@ -10,50 +10,16 @@
|
|
| 10 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
// HF SPACES DNS CORRECTIONS - CORREΓΓO CRΓTICA PARA QR CODE
|
| 12 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
-
const
|
| 14 |
-
const https = require('https');
|
| 15 |
|
| 16 |
-
//
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
// 2. Sobrescreve resolve para usar DNS do Google como fallback
|
| 20 |
-
const originalResolve4 = dns.resolve4.bind(dns);
|
| 21 |
-
const originalResolve = dns.resolve.bind(dns);
|
| 22 |
-
|
| 23 |
-
dns.resolve4 = function(hostname, options, callback) {
|
| 24 |
-
if (typeof options === 'function') {
|
| 25 |
-
callback = options;
|
| 26 |
-
options = { timeout: 10000, family: 4 };
|
| 27 |
-
}
|
| 28 |
-
|
| 29 |
-
originalResolve4(hostname, options, (err, addresses) => {
|
| 30 |
-
if (err && (err.code === 'ENODATA' || err.code === 'ENOTFOUND' || err.code === 'EAI_AGAIN')) {
|
| 31 |
-
console.log(`π DNS fallback para ${hostname}, tentando novamente...`);
|
| 32 |
-
setTimeout(() => {
|
| 33 |
-
originalResolve4(hostname, options, callback);
|
| 34 |
-
}, 3000);
|
| 35 |
-
} else {
|
| 36 |
-
callback(err, addresses);
|
| 37 |
-
}
|
| 38 |
-
});
|
| 39 |
-
};
|
| 40 |
-
|
| 41 |
-
// 4. IP direto do WhatsApp como fallback
|
| 42 |
-
const WHATSAPP_DIRECT_IPS = [
|
| 43 |
-
'108.177.14.0',
|
| 44 |
-
'108.177.15.0',
|
| 45 |
-
'142.250.79.0',
|
| 46 |
-
'172.217.28.0'
|
| 47 |
-
];
|
| 48 |
|
|
|
|
| 49 |
function getWhatsAppFallbackIP() {
|
| 50 |
-
return
|
| 51 |
}
|
| 52 |
|
| 53 |
-
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 54 |
-
// FIM DAS CORREΓΓES HF SPACES
|
| 55 |
-
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 56 |
-
|
| 57 |
const {
|
| 58 |
default: makeWASocket,
|
| 59 |
useMultiFileAuthState,
|
|
@@ -265,65 +231,11 @@ class BotCore {
|
|
| 265 |
}
|
| 266 |
}
|
| 267 |
|
| 268 |
-
/**
|
| 269 |
-
* Verifica conectividade de rede ANTES de tentar conectar
|
| 270 |
-
* VersΓ£o otimizada para HF Spaces com fallback de DNS (SIMPLIFICADA)
|
| 271 |
-
*/
|
| 272 |
-
async _checkNetworkConnectivitySimple() {
|
| 273 |
-
try {
|
| 274 |
-
console.log('π Verificando conectividade de rede (simplificado)...');
|
| 275 |
-
|
| 276 |
-
// Teste DNS simples
|
| 277 |
-
const addresses = await new Promise((resolve, reject) => {
|
| 278 |
-
dns.resolve4('web.whatsapp.com', { timeout: 5000, family: 4 }, (err, addresses) => {
|
| 279 |
-
if (err) reject(err);
|
| 280 |
-
else resolve(addresses);
|
| 281 |
-
});
|
| 282 |
-
});
|
| 283 |
-
|
| 284 |
-
console.log(`β
DNS OK: ${addresses.join(', ')}`);
|
| 285 |
-
return true;
|
| 286 |
-
|
| 287 |
-
} catch (error) {
|
| 288 |
-
console.warn(`β οΈ DNS falhou: ${error.message}`);
|
| 289 |
-
// NΓ£o bloqueia, deixa tentar conectar mesmo assim
|
| 290 |
-
return true;
|
| 291 |
-
}
|
| 292 |
-
}
|
| 293 |
-
|
| 294 |
/**
|
| 295 |
* Cria agente HTTP personalizado para ambientes restritos (HF Spaces)
|
| 296 |
*/
|
| 297 |
_createCustomAgent() {
|
| 298 |
-
|
| 299 |
-
// Tenta usar proxy se configurado
|
| 300 |
-
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
|
| 301 |
-
|
| 302 |
-
if (proxy) {
|
| 303 |
-
console.log(`π Usando proxy: ${proxy.substring(0, 40)}...`);
|
| 304 |
-
try {
|
| 305 |
-
const { HttpsProxyAgent } = require('https-proxy-agent');
|
| 306 |
-
return new HttpsProxyAgent(proxy);
|
| 307 |
-
} catch (e) {
|
| 308 |
-
console.warn('β οΈ NΓ£o foi possΓvel carregar https-proxy-agent');
|
| 309 |
-
}
|
| 310 |
-
}
|
| 311 |
-
|
| 312 |
-
// Caso contrΓ‘rio, usa agente padrΓ£o com configuraΓ§Γ΅es otimizadas para HF Spaces
|
| 313 |
-
return new https.Agent({
|
| 314 |
-
keepAlive: true,
|
| 315 |
-
keepAliveMsecs: 30000,
|
| 316 |
-
timeout: 60000,
|
| 317 |
-
maxSockets: 100,
|
| 318 |
-
maxFreeSockets: 20,
|
| 319 |
-
// NΓO define rejectUnauthorized para evitar problemas em containers
|
| 320 |
-
rejectUnauthorized: false
|
| 321 |
-
});
|
| 322 |
-
|
| 323 |
-
} catch (error) {
|
| 324 |
-
console.warn(`β οΈ Erro ao criar agente customizado: ${error.message}`);
|
| 325 |
-
return undefined;
|
| 326 |
-
}
|
| 327 |
}
|
| 328 |
|
| 329 |
/**
|
|
@@ -406,28 +318,11 @@ class BotCore {
|
|
| 406 |
this.logger.info(`π Usando pasta de auth: ${this.config.AUTH_FOLDER}`);
|
| 407 |
|
| 408 |
// βββ VERIFICAΓΓO DE DNS OTIMIZADA PARA HF SPACES βββ
|
| 409 |
-
console.log('π Verificando resoluΓ§Γ£o DNS...');
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
const addresses = await new Promise((resolve, reject) => {
|
| 413 |
-
dns.resolve4('web.whatsapp.com', { timeout: 15000, family: 4 }, (err, addresses) => {
|
| 414 |
-
if (err) reject(err);
|
| 415 |
-
else resolve(addresses);
|
| 416 |
-
});
|
| 417 |
-
});
|
| 418 |
-
console.log(`β
DNS: web.whatsapp.com resolvido: ${addresses.join(', ')}`);
|
| 419 |
-
} catch (dnsError) {
|
| 420 |
-
console.warn(`β οΈ Erro DNS: ${dnsError.message}`);
|
| 421 |
-
console.log('π Tentando IP direto do WhatsApp como fallback...');
|
| 422 |
-
const fallbackIP = getWhatsAppFallbackIP();
|
| 423 |
-
console.log(`π IP fallback disponΓvel: ${fallbackIP}`);
|
| 424 |
-
// Continua mesmo com erro de DNS, o Baileys pode tentar resolver
|
| 425 |
-
}
|
| 426 |
-
|
| 427 |
-
// Verifica conectividade de rede antes de tentar conectar (simplificado para HF Spaces)
|
| 428 |
-
// NΓ£o bloqueia, tenta conectar mesmo se a verificaΓ§Γ£o falhar
|
| 429 |
try {
|
| 430 |
-
await
|
| 431 |
} catch (netError) {
|
| 432 |
console.log('β οΈ VerificaΓ§Γ£o de rede ignorada, continuando...');
|
| 433 |
}
|
|
@@ -461,13 +356,7 @@ class BotCore {
|
|
| 461 |
// ForΓ§a uso de IPv4
|
| 462 |
fetchAgent: this._createCustomAgent(),
|
| 463 |
// ConfiguraΓ§Γ΅es de WebSocket otimizadas
|
| 464 |
-
wsOptions:
|
| 465 |
-
headers: {
|
| 466 |
-
'Origin': 'https://web.whatsapp.com',
|
| 467 |
-
'Host': 'web.whatsapp.com',
|
| 468 |
-
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
|
| 469 |
-
}
|
| 470 |
-
},
|
| 471 |
getMessage: async (key) => {
|
| 472 |
if (!key) return undefined;
|
| 473 |
try {
|
|
@@ -1226,4 +1115,4 @@ class BotCore {
|
|
| 1226 |
}
|
| 1227 |
}
|
| 1228 |
|
| 1229 |
-
module.exports = BotCore;
|
|
|
|
| 10 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 11 |
// HF SPACES DNS CORRECTIONS - CORREΓΓO CRΓTICA PARA QR CODE
|
| 12 |
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 13 |
+
const HFCorrections = require('./HFCorrections');
|
|
|
|
| 14 |
|
| 15 |
+
// Inicializa correΓ§Γ΅es DNS para HF Spaces
|
| 16 |
+
HFCorrections.configureDNS();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
// FunΓ§Γ£o helper para obter IP direto do WhatsApp
|
| 19 |
function getWhatsAppFallbackIP() {
|
| 20 |
+
return HFCorrections.getWhatsAppIP();
|
| 21 |
}
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
const {
|
| 24 |
default: makeWASocket,
|
| 25 |
useMultiFileAuthState,
|
|
|
|
| 231 |
}
|
| 232 |
}
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
/**
|
| 235 |
* Cria agente HTTP personalizado para ambientes restritos (HF Spaces)
|
| 236 |
*/
|
| 237 |
_createCustomAgent() {
|
| 238 |
+
return HFCorrections.createHFAgent();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
}
|
| 240 |
|
| 241 |
/**
|
|
|
|
| 318 |
this.logger.info(`π Usando pasta de auth: ${this.config.AUTH_FOLDER}`);
|
| 319 |
|
| 320 |
// βββ VERIFICAΓΓO DE DNS OTIMIZADA PARA HF SPACES βββ
|
| 321 |
+
console.log('π Verificando resoluΓ§Γ£o DNS (via HFCorrections)...');
|
| 322 |
+
|
| 323 |
+
// Verifica conectividade de rede antes de tentar conectar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
try {
|
| 325 |
+
await HFCorrections.verifyHFNetwork();
|
| 326 |
} catch (netError) {
|
| 327 |
console.log('β οΈ VerificaΓ§Γ£o de rede ignorada, continuando...');
|
| 328 |
}
|
|
|
|
| 356 |
// ForΓ§a uso de IPv4
|
| 357 |
fetchAgent: this._createCustomAgent(),
|
| 358 |
// ConfiguraΓ§Γ΅es de WebSocket otimizadas
|
| 359 |
+
wsOptions: HFCorrections.createWebSocketOptions(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
getMessage: async (key) => {
|
| 361 |
if (!key) return undefined;
|
| 362 |
try {
|
|
|
|
| 1115 |
}
|
| 1116 |
}
|
| 1117 |
|
| 1118 |
+
module.exports = BotCore;
|