feat(network): add proxy support and docker host-gateway configuration

This commit is contained in:
mamad
2026-08-27 20:43:22 +03:30
parent 76a19131a6
commit 62af84c82e
6 changed files with 35 additions and 3 deletions
+26
View File
@@ -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
+2
View File
@@ -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
+1
View File
@@ -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
+2 -1
View File
@@ -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
+2 -1
View File
@@ -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...")
+2 -1
View File
@@ -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