const https = require('https'); const http = require('http'); const dns = require('dns'); const { Agent, setGlobalDispatcher } = require('undici'); /** * Professional Network Resilience Engine * Implements a stealth failover system for restricted environments. */ // Use official Discord domains only (explicitly excludes legacy discordapp.com). const FALLBACK_DOMAINS = ['discord.com', 'canary.discord.com', 'ptb.discord.com']; let active_index = 0; function applyAdvancedFixes() { const target = FALLBACK_DOMAINS[active_index]; console.log(`[NETWORK] Stealth Failover Active: Using ${target} bridge.`); // 1. Force IPv4 Priority (Standard for HF Stability) if (dns.setDefaultResultOrder) dns.setDefaultResultOrder('ipv4first'); // 2. Low-Level DNS Hijacking // This ensures that even if a library hardcodes 'discord.com', it gets the IP of our bridge. const originalLookup = dns.lookup; dns.lookup = function(hostname, options, callback) { if (typeof options === 'function') { callback = options; options = {}; } let h = hostname; if (hostname.includes('discord.com') && !hostname.includes('gateway') && !hostname.includes(target)) { h = target; } return originalLookup(h, options, callback); }; // 3. Optimized Connection Pooling const agent = new Agent({ connect: { timeout: 30000, family: 4, servername: target // Critical for TLS Handshake consistency }, pipelining: 1, keepAliveTimeout: 60000 }); setGlobalDispatcher(agent); // 4. Legacy Module Support https.globalAgent.options.family = 4; } function rotateEndpoint() { active_index = (active_index + 1) % FALLBACK_DOMAINS.length; applyAdvancedFixes(); } function getTargetDomain() { return FALLBACK_DOMAINS[active_index]; } module.exports = { applyAdvancedFixes, rotateEndpoint, getTargetDomain };