Commit 446f0f1f authored by Aryan Noshirvnai's avatar Aryan Noshirvnai

Route Pyrogram's MTProto connection through the SOCKS5 proxy too

HTTP_PROXY/HTTPS_PROXY only affect HTTP client libraries - Pyrogram talks
to Telegram's datacenters over raw MTProto/TCP, which never respected
those env vars. On a network that can't reach Telegram directly, every
Client() call hung in an infinite "Connection timed out" retry loop -
exactly what made Telegram linking "get stuck" on prod. Added a
TELEGRAM_PROXY config (opt-in via TELEGRAM_PROXY_HOST, unset by default so
dev's direct access needs no change) and passed it into all four
pyrogram.Client() construction sites.
Co-Authored-By: 's avatarClaude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019i3NAUkLkHNbsPg5vKfzF9
parent c9fbde98
Pipeline #1256 failed with stage
...@@ -34,6 +34,18 @@ CORS_ORIGINS = [o.strip() for o in os.getenv( ...@@ -34,6 +34,18 @@ CORS_ORIGINS = [o.strip() for o in os.getenv(
# at startup rather than silently falling back to something guessable. # at startup rather than silently falling back to something guessable.
JWT_SECRET = os.environ["JWT_SECRET"] JWT_SECRET = os.environ["JWT_SECRET"]
# Pyrogram's connection to Telegram's datacenters is raw MTProto over TCP, not HTTP - it
# never goes through HTTP_PROXY/HTTPS_PROXY (those only affect HTTP client libraries).
# Networks that can't reach Telegram's servers directly (see deploy's stax_proxy.service,
# an SSH SOCKS5 tunnel) need this passed explicitly into every pyrogram.Client(). None by
# default - only prod sets TELEGRAM_PROXY_HOST; dev has direct access and needs nothing here.
TELEGRAM_PROXY_HOST = os.getenv("TELEGRAM_PROXY_HOST")
TELEGRAM_PROXY = (
{"scheme": "socks5", "hostname": TELEGRAM_PROXY_HOST, "port": int(os.getenv("TELEGRAM_PROXY_PORT", "1080"))}
if TELEGRAM_PROXY_HOST
else None
)
# Setup centralized logging # Setup centralized logging
logging.basicConfig( logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
......
...@@ -3,6 +3,8 @@ from pyrogram.errors import SessionPasswordNeeded ...@@ -3,6 +3,8 @@ from pyrogram.errors import SessionPasswordNeeded
from typing import Dict from typing import Dict
import os import os
from src.core.config import TELEGRAM_PROXY
API_ID = int(os.getenv("TELEGRAM_API_ID", "2040")) # Fallback to official android app dummy API_ID = int(os.getenv("TELEGRAM_API_ID", "2040")) # Fallback to official android app dummy
API_HASH = os.getenv("TELEGRAM_API_HASH", "b18441a1ff607e10a989891a5462e627") API_HASH = os.getenv("TELEGRAM_API_HASH", "b18441a1ff607e10a989891a5462e627")
...@@ -19,7 +21,7 @@ class PasswordRequired(Exception): ...@@ -19,7 +21,7 @@ class PasswordRequired(Exception):
async def request_code(phone_number: str): async def request_code(phone_number: str):
client = Client(f"session_{phone_number.replace('+', '')}", api_id=API_ID, api_hash=API_HASH, in_memory=True) client = Client(f"session_{phone_number.replace('+', '')}", api_id=API_ID, api_hash=API_HASH, in_memory=True, proxy=TELEGRAM_PROXY)
await client.connect() await client.connect()
try: try:
sent_code = await client.send_code(phone_number) sent_code = await client.send_code(phone_number)
......
...@@ -2,11 +2,13 @@ from pyrogram import Client ...@@ -2,11 +2,13 @@ from pyrogram import Client
from pyrogram.enums import ChatType, MessagesFilter from pyrogram.enums import ChatType, MessagesFilter
import os import os
from src.core.config import TELEGRAM_PROXY
API_ID = int(os.getenv("TELEGRAM_API_ID", "2040")) API_ID = int(os.getenv("TELEGRAM_API_ID", "2040"))
API_HASH = os.getenv("TELEGRAM_API_HASH", "b18441a1ff607e10a989891a5462e627") API_HASH = os.getenv("TELEGRAM_API_HASH", "b18441a1ff607e10a989891a5462e627")
async def get_chats(session_string: str): async def get_chats(session_string: str):
client = Client("temp_session", session_string=session_string, api_id=API_ID, api_hash=API_HASH, in_memory=True) client = Client("temp_session", session_string=session_string, api_id=API_ID, api_hash=API_HASH, in_memory=True, proxy=TELEGRAM_PROXY)
await client.connect() await client.connect()
chats = [] chats = []
...@@ -26,7 +28,7 @@ async def download_chat_audio(session_string: str, chat_id: int, message_id: int ...@@ -26,7 +28,7 @@ async def download_chat_audio(session_string: str, chat_id: int, message_id: int
"""Downloads a chat message's audio attachment to dest_path. Returns the path pyrogram """Downloads a chat message's audio attachment to dest_path. Returns the path pyrogram
actually wrote to (download_media picks the extension if dest_path is a directory-less actually wrote to (download_media picks the extension if dest_path is a directory-less
filename without one, so callers should treat the return value as the real path).""" filename without one, so callers should treat the return value as the real path)."""
client = Client("temp_session", session_string=session_string, api_id=API_ID, api_hash=API_HASH, in_memory=True) client = Client("temp_session", session_string=session_string, api_id=API_ID, api_hash=API_HASH, in_memory=True, proxy=TELEGRAM_PROXY)
await client.connect() await client.connect()
try: try:
...@@ -40,7 +42,7 @@ async def download_chat_audio(session_string: str, chat_id: int, message_id: int ...@@ -40,7 +42,7 @@ async def download_chat_audio(session_string: str, chat_id: int, message_id: int
async def get_chat_music(session_string: str, chat_id: int, limit: int = 50): async def get_chat_music(session_string: str, chat_id: int, limit: int = 50):
client = Client("temp_session", session_string=session_string, api_id=API_ID, api_hash=API_HASH, in_memory=True) client = Client("temp_session", session_string=session_string, api_id=API_ID, api_hash=API_HASH, in_memory=True, proxy=TELEGRAM_PROXY)
await client.connect() await client.connect()
music = [] music = []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment