File size: 2,314 Bytes
43b6f45
 
e56617b
 
 
43b6f45
 
 
 
c2c9643
a102391
c2c9643
43b6f45
e56617b
 
 
af40bb9
 
43b6f45
af40bb9
 
 
43b6f45
 
e56617b
 
43b6f45
e56617b
43b6f45
e56617b
43b6f45
e56617b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import requests
import urllib3
# Suppress InsecureRequestWarning for our DNS bypass
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from dotenv import load_dotenv

load_dotenv()

# Sanitize URL: Remove whitespace and replace PTB/Canary domains with standard domain
WEBHOOK_URL = (os.getenv("WEBHOOK_URL") or "").strip()
WEBHOOK_URL = WEBHOOK_URL.replace("ptb.discord.com", "discord.com").replace("canary.discord.com", "discord.com")

# Hardcoded Discord IPs (Cloudflare) to bypass DNS blocking
DISCORD_IPS = ["162.159.135.232", "162.159.136.232", "162.159.137.232"]

def notify_discord(message: str) -> tuple[bool, str]:
    """Sends a notification to Discord. Returns (Success, status_message)."""
    if not WEBHOOK_URL:
        msg = "Warning: WEBHOOK_URL not set. Skipping notification."
        print(msg)
        return False, msg
    
    data = {"content": message}
    
    # 1. Try Standard Request
    try:
        response = requests.post(WEBHOOK_URL, json=data, timeout=5)
        response.raise_for_status()
        return True, "Notification sent successfully (Standard DNS)."
    except requests.exceptions.RequestException as e:
        print(f"Standard DNS failed: {e}")
        
        # 2. Try IP Bypass (DNS Blackhole Workaround)
        # Extract path from URL (e.g., /api/webhooks/...)
        from urllib.parse import urlparse
        parsed = urlparse(WEBHOOK_URL)
        path = parsed.path
        
        headers = {"Host": "discord.com"} 
        
        for ip in DISCORD_IPS:
            try:
                # Construct direct IP URL
                bypass_url = f"https://{ip}{path}"
                print(f"Trying DNS Bypass with IP: {ip}")
                
                # verify=False is needed because cert matches discord.com, not the IP. 
                # This is acceptable for a notification bot in a restricted env.
                response = requests.post(bypass_url, json=data, headers=headers, verify=False, timeout=5)
                response.raise_for_status()
                return True, f"Notification sent successfully (DNS Bypass: {ip})"
            except Exception as bypass_e:
                print(f"Bypass {ip} failed: {bypass_e}")

        # If all fail
        return False, f"All attempts failed. Standard Error: {e}"