Update modules/HFCorrections.js
Browse files- modules/HFCorrections.js +133 -154
modules/HFCorrections.js
CHANGED
|
@@ -1,187 +1,166 @@
|
|
| 1 |
/**
|
| 2 |
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 3 |
-
* HF SPACES
|
| 4 |
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
-
*
|
| 6 |
-
*
|
| 7 |
-
*
|
| 8 |
-
*
|
|
|
|
|
|
|
| 9 |
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 10 |
*/
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
const dns = require('dns');
|
| 13 |
-
const https = require('https');
|
| 14 |
-
const http = require('http');
|
| 15 |
|
| 16 |
-
//
|
|
|
|
|
|
|
|
|
|
| 17 |
const WHATSAPP_IPS = [
|
| 18 |
-
'108.177.14.0',
|
| 19 |
-
'
|
| 20 |
-
'
|
| 21 |
-
'142.250.
|
| 22 |
-
'142.250.81.0',
|
| 23 |
-
'172.217.28.0',
|
| 24 |
-
'172.217.162.0',
|
| 25 |
-
'172.217.163.0'
|
| 26 |
];
|
| 27 |
|
| 28 |
-
//
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
/**
|
| 34 |
-
* ObtΓ©m IP direto do WhatsApp (com cache)
|
| 35 |
-
*/
|
| 36 |
-
function getWhatsAppDirectIP() {
|
| 37 |
-
const now = Date.now();
|
| 38 |
-
if (cachedWhatsAppIP && (now - lastDNSResolve) < DNS_CACHE_TTL) {
|
| 39 |
-
return cachedWhatsAppIP;
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
// Retorna IP aleatΓ³rio da lista
|
| 43 |
-
const ip = WHATSAPP_IPS[Math.floor(Math.random() * WHATSAPP_IPS.length)];
|
| 44 |
-
cachedWhatsAppIP = ip;
|
| 45 |
-
lastDNSResolve = now;
|
| 46 |
-
|
| 47 |
-
console.log(`π IP WhatsApp direto: ${ip}`);
|
| 48 |
-
return ip;
|
| 49 |
}
|
| 50 |
|
| 51 |
-
/
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
// 1. ForΓ§a IPv4 para todas as operaΓ§Γ΅es DNS
|
| 58 |
try {
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
console.warn('β οΈ NΓ£o foi possΓvel definir ordem DNS:', e.message);
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
// 2. Sobrescreve resolve4 para usar IP direto em caso de falha
|
| 66 |
-
const originalResolve4 = dns.resolve4.bind(dns);
|
| 67 |
-
|
| 68 |
-
dns.resolve4 = function(hostname, options, callback) {
|
| 69 |
-
if (typeof options === 'function') {
|
| 70 |
-
callback = options;
|
| 71 |
-
options = { timeout: 10000, family: 4 };
|
| 72 |
-
}
|
| 73 |
|
| 74 |
-
//
|
| 75 |
-
|
| 76 |
-
const ip = getWhatsAppDirectIP();
|
| 77 |
-
console.log(`π DNS redirect: ${hostname} -> ${ip}`);
|
| 78 |
-
|
| 79 |
-
// Retorna IP formatado para DNS
|
| 80 |
-
setTimeout(() => {
|
| 81 |
-
callback(null, [ip]);
|
| 82 |
-
}, 10);
|
| 83 |
-
return;
|
| 84 |
-
}
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
console.log(`π DNS fallback para ${hostname}, tentando novamente...`);
|
| 90 |
-
setTimeout(() => {
|
| 91 |
-
originalResolve4(hostname, options, callback);
|
| 92 |
-
}, 2000);
|
| 93 |
-
} else {
|
| 94 |
-
callback(err, addresses);
|
| 95 |
-
}
|
| 96 |
-
});
|
| 97 |
-
};
|
| 98 |
-
|
| 99 |
-
// 3. Sobrescreve resolve (genΓ©rico) tambΓ©m
|
| 100 |
-
const originalResolve = dns.resolve.bind(dns);
|
| 101 |
-
|
| 102 |
-
dns.resolve = function(hostname, rrtype, callback) {
|
| 103 |
-
if (typeof rrtype === 'function') {
|
| 104 |
-
callback = rrtype;
|
| 105 |
-
rrtype = 'A';
|
| 106 |
-
}
|
| 107 |
-
|
| 108 |
-
if (hostname === 'web.whatsapp.com' || hostname === 'whatsapp.com') {
|
| 109 |
-
const ip = getWhatsAppDirectIP();
|
| 110 |
-
setTimeout(() => {
|
| 111 |
-
callback(null, [ip]);
|
| 112 |
-
}, 10);
|
| 113 |
-
return;
|
| 114 |
-
}
|
| 115 |
-
|
| 116 |
-
originalResolve(hostname, rrtype, callback);
|
| 117 |
-
};
|
| 118 |
-
|
| 119 |
-
// 4. Sobrescreve lookup para IP direto
|
| 120 |
-
const originalLookup = dns.lookup.bind(dns);
|
| 121 |
-
|
| 122 |
-
dns.lookup = function(hostname, options, callback) {
|
| 123 |
-
if (typeof options === 'function') {
|
| 124 |
-
callback = options;
|
| 125 |
-
options = {};
|
| 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 |
timeout: 60000,
|
| 151 |
-
|
| 152 |
-
maxFreeSockets: 20,
|
| 153 |
-
rejectUnauthorized: false
|
| 154 |
-
});
|
| 155 |
}
|
| 156 |
|
| 157 |
-
/
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
| 161 |
return new Promise((resolve) => {
|
| 162 |
-
|
| 163 |
-
const
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
resolve(false);
|
| 171 |
-
});
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
});
|
| 178 |
}
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
module.exports = {
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
};
|
| 187 |
|
|
|
|
| 1 |
/**
|
| 2 |
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 3 |
+
* CORREΓΓES HF SPACES - DNS E CONEXΓO WHATSAPP
|
| 4 |
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 5 |
+
* Corrige erro: queryA ENODATA web.whatsapp.com
|
| 6 |
+
* SoluΓ§Γ΅es aplicadas:
|
| 7 |
+
* 1. DNS Resolver Google (8.8.8.8)
|
| 8 |
+
* 2. Socket Baileys com IP direto do WhatsApp
|
| 9 |
+
* 3. Host header correto para WebSocket
|
| 10 |
+
* 4. Agente HTTP otimizado para ambientes restritos
|
| 11 |
* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 12 |
*/
|
| 13 |
|
| 14 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 15 |
+
// 1. CONFIGURAΓΓO DE DNS GOOGLE (8.8.8.8) - CORREΓΓO PRINCIPAL
|
| 16 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
+
|
| 18 |
const dns = require('dns');
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
+
// 2. IP'S DIRECTOS DO WHATSAPP (FALLBACK PARA CASO DNS FALHE)
|
| 22 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 23 |
+
|
| 24 |
const WHATSAPP_IPS = [
|
| 25 |
+
'108.177.14.0', // web.whatsapp.com
|
| 26 |
+
'142.250.79.0', // Google IPs often used
|
| 27 |
+
'172.217.28.0', //
|
| 28 |
+
'142.250.0.0', //
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
];
|
| 30 |
|
| 31 |
+
// FunΓ§Γ£o para obter IP direto do WhatsApp
|
| 32 |
+
function getWhatsAppIP() {
|
| 33 |
+
const index = Math.floor(Math.random() * WHATSAPP_IPS.length);
|
| 34 |
+
return WHATSAPP_IPS[index];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 38 |
+
// 3. HELPER: CRIA AGENTE HTTP COM FALLBACK DE DNS
|
| 39 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 40 |
+
|
| 41 |
+
function createHFAgent() {
|
|
|
|
|
|
|
| 42 |
try {
|
| 43 |
+
const https = require('https');
|
| 44 |
+
const http = require('http');
|
| 45 |
+
const { HttpsProxyAgent } = require('https-proxy-agent');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
// Verifica se hΓ‘ proxy configurado
|
| 48 |
+
const proxy = process.env.HTTPS_PROXY || process.env.HTTP_PROXY || process.env.https_proxy;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
if (proxy) {
|
| 51 |
+
console.log('π Usando proxy configurado:', proxy.substring(0, 30) + '...');
|
| 52 |
+
return new HttpsProxyAgent(proxy);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
+
// Sem proxy - usa agente padrΓ£o otimizado
|
| 56 |
+
return new https.Agent({
|
| 57 |
+
keepAlive: true,
|
| 58 |
+
keepAliveMsecs: 30000,
|
| 59 |
+
timeout: 60000,
|
| 60 |
+
maxSockets: 100,
|
| 61 |
+
maxFreeSockets: 20,
|
| 62 |
+
// NΓO define rejectUnauthorized para evitar problemas em containers
|
| 63 |
+
rejectUnauthorized: false
|
| 64 |
+
});
|
| 65 |
+
} catch (error) {
|
| 66 |
+
console.warn('β οΈ Erro ao criar agente HTTP:', error.message);
|
| 67 |
+
return undefined;
|
| 68 |
+
}
|
| 69 |
}
|
| 70 |
|
| 71 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 72 |
+
// 4. HELPER: CRIA WEBSOCKET OPTIONS OTIMIZADO PARA HF SPACES
|
| 73 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 74 |
+
|
| 75 |
+
function createWebSocketOptions() {
|
| 76 |
+
return {
|
| 77 |
+
// Headers que fingem ser browser real
|
| 78 |
+
headers: {
|
| 79 |
+
'Origin': 'https://web.whatsapp.com',
|
| 80 |
+
'Host': 'web.whatsapp.com',
|
| 81 |
+
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 82 |
+
'Accept': '*/*',
|
| 83 |
+
'Accept-Language': 'pt-BR,pt;q=0.9,en;q=0.8',
|
| 84 |
+
'Accept-Encoding': 'gzip, deflate, br',
|
| 85 |
+
'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
|
| 86 |
+
'Sec-WebSocket-Version': '13',
|
| 87 |
+
},
|
| 88 |
+
// Timeout mais longo para containers lentos
|
| 89 |
+
handshakeTimeout: 60000,
|
| 90 |
+
// Timeout deagent
|
| 91 |
timeout: 60000,
|
| 92 |
+
};
|
|
|
|
|
|
|
|
|
|
| 93 |
}
|
| 94 |
|
| 95 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 96 |
+
// 5. VERIFICAΓΓO DE REDE ESPECΓFICA PARA HF SPACES
|
| 97 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 98 |
+
|
| 99 |
+
async function verifyHFNetwork() {
|
| 100 |
+
console.log('π Verificando conectividade de rede (HF Spaces)...');
|
| 101 |
+
|
| 102 |
return new Promise((resolve) => {
|
| 103 |
+
const net = require('net');
|
| 104 |
+
const testHosts = [
|
| 105 |
+
{ host: '8.8.8.8', port: 53, name: 'Google DNS' },
|
| 106 |
+
{ host: '1.1.1.1', port: 53, name: 'Cloudflare DNS' },
|
| 107 |
+
{ host: 'web.whatsapp.com', port: 443, name: 'WhatsApp Web' }
|
| 108 |
+
];
|
| 109 |
|
| 110 |
+
let checked = 0;
|
| 111 |
+
let results = {};
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
testHosts.forEach(test => {
|
| 114 |
+
const socket = new net.Socket();
|
| 115 |
+
socket.setTimeout(5000);
|
| 116 |
+
|
| 117 |
+
socket.on('connect', () => {
|
| 118 |
+
results[test.name] = true;
|
| 119 |
+
socket.destroy();
|
| 120 |
+
checkDone();
|
| 121 |
+
});
|
| 122 |
+
|
| 123 |
+
socket.on('timeout', () => {
|
| 124 |
+
results[test.name] = false;
|
| 125 |
+
socket.destroy();
|
| 126 |
+
checkDone();
|
| 127 |
+
});
|
| 128 |
+
|
| 129 |
+
socket.on('error', (err) => {
|
| 130 |
+
results[test.name] = false;
|
| 131 |
+
socket.destroy();
|
| 132 |
+
checkDone();
|
| 133 |
+
});
|
| 134 |
+
|
| 135 |
+
socket.connect(test.port, test.host);
|
| 136 |
});
|
| 137 |
+
|
| 138 |
+
function checkDone() {
|
| 139 |
+
checked++;
|
| 140 |
+
if (checked >= testHosts.length) {
|
| 141 |
+
console.log('π Resultado dos testes de rede:', results);
|
| 142 |
+
resolve(results['WhatsApp Web'] || results['Google DNS']);
|
| 143 |
+
}
|
| 144 |
+
}
|
| 145 |
});
|
| 146 |
}
|
| 147 |
|
| 148 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 149 |
+
// EXPORTAΓΓES
|
| 150 |
+
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 151 |
+
|
| 152 |
module.exports = {
|
| 153 |
+
configureDNS: () => {
|
| 154 |
+
// DNS jΓ‘ configurado no inΓcio do arquivo
|
| 155 |
+
console.log('β
DNS configurado para IPv4 first');
|
| 156 |
+
},
|
| 157 |
+
|
| 158 |
+
getWhatsAppIP,
|
| 159 |
+
|
| 160 |
+
createHFAgent,
|
| 161 |
+
|
| 162 |
+
createWebSocketOptions,
|
| 163 |
+
|
| 164 |
+
verifyHFNetwork
|
| 165 |
};
|
| 166 |
|