rairo commited on
Commit
9a66e38
·
verified ·
1 Parent(s): ad784ad

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +43 -19
main.py CHANGED
@@ -17,28 +17,51 @@ import random
17
  from datetime import datetime as dt_class, timezone
18
  from zoneinfo import ZoneInfo
19
  import pandas as pd
20
- import certifi
21
- import requests # <-- ADDED IMPORT
22
- import functools # <-- ADDED IMPORT
23
 
24
- # === MONKEY-PATCH FOR SSL CERTIFICATE FIX ===
25
- # This is the definitive solution to the SSL/TLS verification issue.
26
- # It intercepts any web request made by the `requests` library (which `resend` uses)
27
- # and forces it to use the modern certificate bundle from `certifi`.
 
28
 
29
- # 1. Store the original `request` method
30
- original_request = requests.request
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- # 2. Create a new patched method
33
- @functools.wraps(original_request)
34
- def patched_request(*args, **kwargs):
35
- # 3. If 'verify' is not already set, set it to the certifi bundle path
36
- kwargs.setdefault('verify', certifi.where())
37
- # 4. Call the original method with the potentially modified arguments
38
- return original_request(*args, **kwargs)
39
 
40
- # 5. Replace the original method in the `requests` library with our patched version
41
- requests.request = patched_request
42
 
43
  # === Logging Configuration ===
44
  if not os.path.exists('logs'):
@@ -60,7 +83,7 @@ root_logger.addHandler(console_handler)
60
 
61
  # === ENV Config & Admin Setup ===
62
  ADMIN_EMAILS = ["rairorr@gmail.com", "nharingosheperd@gmail.com"]
63
- SEND_TO_EMAILS = ["rairorr@gmail.com", "nharingosheperd+guard@gmail.com", "initiumzim@gmail.com"]
64
 
65
  RESEND_API_KEY = os.getenv("RESEND_API_KEY")
66
  FIREBASE_CRED_JSON = json.loads(os.getenv("FIREBASE"))
@@ -250,6 +273,7 @@ def log_response_info(response):
250
  def home():
251
  return jsonify({"message": "Rairo Guards API is running.", "status": "ok"}), 200
252
 
 
253
  @app.route("/upload_members", methods=["POST"])
254
  def upload_members():
255
  if not verify_token(request): return jsonify({"error": "Unauthorized"}), 401
 
17
  from datetime import datetime as dt_class, timezone
18
  from zoneinfo import ZoneInfo
19
  import pandas as pd
20
+ import dns.resolver # <-- ADDED IMPORT
21
+ import socket # <-- ADDED IMPORT
 
22
 
23
+ # === DNS RESOLUTION FIX ===
24
+ # This is the definitive solution for networking issues in restricted environments.
25
+ # It intercepts DNS lookups for 'api.resend.com' and forces them through a
26
+ # reliable public DNS resolver, bypassing any potential issues with the
27
+ # container's default DNS settings.
28
 
29
+ def setup_dns_override():
30
+ """Overrides the default socket address resolver for a specific domain."""
31
+ try:
32
+ # Use reliable public DNS servers
33
+ resolver = dns.resolver.Resolver()
34
+ resolver.nameservers = ['8.8.8.8', '8.8.4.4']
35
+
36
+ # Store the original resolver function
37
+ original_getaddrinfo = socket.getaddrinfo
38
+
39
+ def new_getaddrinfo(*args, **kwargs):
40
+ # Check if the domain we're looking up is the one we want to override
41
+ if args and isinstance(args[0], str) and 'api.resend.com' in args[0]:
42
+ try:
43
+ # Resolve the domain to an IP address using our custom resolver
44
+ answers = resolver.resolve(args[0], 'A')
45
+ ip_address = str(answers[0])
46
+ logger.info(f"DNS Override: Resolved {args[0]} to {ip_address}")
47
+ # Call the original function with the IP address instead of the domain
48
+ return original_getaddrinfo(ip_address, *args[1:], **kwargs)
49
+ except Exception as e:
50
+ logger.error(f"DNS Override Failed for {args[0]}: {e}. Falling back to default resolver.")
51
+
52
+ # For all other domains, use the original resolver
53
+ return original_getaddrinfo(*args, **kwargs)
54
+
55
+ # Replace the system's default resolver with our new one
56
+ socket.getaddrinfo = new_getaddrinfo
57
+ logger.info("Custom DNS resolver for 'api.resend.com' has been successfully set up.")
58
+ except Exception as e:
59
+ logger.error(f"Failed to set up custom DNS resolver: {e}")
60
 
61
+ # Apply the fix at startup
62
+ setup_dns_override()
63
+ # === END DNS FIX ===
 
 
 
 
64
 
 
 
65
 
66
  # === Logging Configuration ===
67
  if not os.path.exists('logs'):
 
83
 
84
  # === ENV Config & Admin Setup ===
85
  ADMIN_EMAILS = ["rairorr@gmail.com", "nharingosheperd@gmail.com"]
86
+ SEND_TO_EMAILS = ["rairorr@gmail.com", "nharingosheperd+guard@gmail.com"]
87
 
88
  RESEND_API_KEY = os.getenv("RESEND_API_KEY")
89
  FIREBASE_CRED_JSON = json.loads(os.getenv("FIREBASE"))
 
273
  def home():
274
  return jsonify({"message": "Rairo Guards API is running.", "status": "ok"}), 200
275
 
276
+ # (All other routes follow)
277
  @app.route("/upload_members", methods=["POST"])
278
  def upload_members():
279
  if not verify_token(request): return jsonify({"error": "Unauthorized"}), 401