diff --git a/core/proxy.py b/core/proxy.py new file mode 100644 index 0000000..27f6486 --- /dev/null +++ b/core/proxy.py @@ -0,0 +1,26 @@ +import os +from typing import Optional, Dict, Any + +def get_telegram_proxy() -> Optional[Dict[str, Any]]: + # Only enable proxy if TELEGRAM_PROXY_ENABLE is 'true' or if local port 10809/10808 is requested + enable = os.getenv("TELEGRAM_PROXY_ENABLE", "true").lower() in ("true", "1", "yes") + if not enable: + return None + + proxy_host = os.getenv("TELEGRAM_PROXY_HOST", "host.docker.internal" if os.path.exists("/app") else "127.0.0.1") + proxy_port = os.getenv("TELEGRAM_PROXY_PORT", "10809") + proxy_type = os.getenv("TELEGRAM_PROXY_TYPE", "http").lower() + + if not proxy_host or not proxy_port: + return None + + try: + import python_socks + ptype = python_socks.ProxyType.HTTP if proxy_type == "http" else python_socks.ProxyType.SOCKS5 + return { + "proxy_type": ptype, + "addr": proxy_host, + "port": int(proxy_port) + } + except Exception: + return None diff --git a/docker-compose.yml b/docker-compose.yml index 662a0fe..197541e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -31,6 +31,8 @@ services: - "8000:8000" env_file: - .env + extra_hosts: + - "host.docker.internal:host-gateway" volumes: - copykar_data:/app/data - copykar_sessions:/app/sessions diff --git a/requirements.txt b/requirements.txt index 7ee60cb..46d2cf8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ python-dotenv==1.2.3 asyncpg==0.31.0 prometheus-client==0.26.0 httpx==0.28.1 +python-socks==3.0.0 diff --git a/services/admin_bot.py b/services/admin_bot.py index cc7f2c7..4895143 100644 --- a/services/admin_bot.py +++ b/services/admin_bot.py @@ -6,6 +6,7 @@ from db.models import Post, TargetChannel from db.repository import Repository from bot.keyboards import get_review_keyboard from core.metrics import ADMIN_ACTIONS_TOTAL +from core.proxy import get_telegram_proxy logger = logging.getLogger(__name__) @@ -31,7 +32,7 @@ class AdminBotService: self.admin_user_ids = admin_user_ids or [int(x.strip()) for x in raw_admins.split(",") if x.strip()] self.session_name = session_name or os.path.join(SESSION_DIR, "admin_bot.session") os.makedirs(os.path.dirname(self.session_name), exist_ok=True) - self.client = TelegramClient(self.session_name, self.api_id, self.api_hash) + self.client = TelegramClient(self.session_name, self.api_id, self.api_hash, proxy=get_telegram_proxy()) def is_admin(self, user_id: int) -> bool: return not self.admin_user_ids or user_id in self.admin_user_ids diff --git a/services/collector.py b/services/collector.py index 098728d..35db427 100644 --- a/services/collector.py +++ b/services/collector.py @@ -7,6 +7,7 @@ from db.repository import Repository from core.dedup import compute_content_hash, compute_file_hash from services.ai_processor import AIProcessor from core.metrics import COLLECTED_POSTS_TOTAL +from core.proxy import get_telegram_proxy logger = logging.getLogger(__name__) @@ -31,7 +32,7 @@ class CollectorService: self.session_name = session_name or os.path.join(SESSION_DIR, "collector.session") os.makedirs(os.path.dirname(self.session_name), exist_ok=True) os.makedirs(MEDIA_DIR, exist_ok=True) - self.client = TelegramClient(self.session_name, self.api_id, self.api_hash) + self.client = TelegramClient(self.session_name, self.api_id, self.api_hash, proxy=get_telegram_proxy()) async def start(self): logger.info("Starting Collector Userbot...") diff --git a/services/publisher.py b/services/publisher.py index 59255dc..f390190 100644 --- a/services/publisher.py +++ b/services/publisher.py @@ -6,6 +6,7 @@ from typing import Optional from telethon import TelegramClient from db.repository import Repository from core.metrics import POSTS_PUBLISHED_TOTAL, QUEUE_POSTS_GAUGE +from core.proxy import get_telegram_proxy logger = logging.getLogger(__name__) @@ -26,7 +27,7 @@ class PublisherService: self.bot_token = bot_token or os.getenv("BOT_TOKEN") self.session_name = session_name or os.path.join(SESSION_DIR, "publisher.session") os.makedirs(os.path.dirname(self.session_name), exist_ok=True) - self.client = TelegramClient(self.session_name, self.api_id, self.api_hash) + self.client = TelegramClient(self.session_name, self.api_id, self.api_hash, proxy=get_telegram_proxy()) self._running = False self._task: Optional[asyncio.Task] = None