File size: 1,964 Bytes
b3dbe30
cf478c6
 
 
451288b
4185446
cf478c6
 
4185446
cf478c6
cde81f6
 
cf478c6
 
b3dbe30
cf478c6
 
 
 
 
1752ef1
cf478c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3dbe30
cf478c6
 
 
 
 
 
451288b
cf478c6
 
590919a
 
cf478c6
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
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 };