refactor: review fixes and code improvements
- Add shared proxy module (bot/utils/proxy.py) to eliminate duplicate SOCKS5 parsing - Fix AI client: escape HTML before Markdown→HTML conversion, unify timeouts, make health check optional and disabled by default, handle 429 retries - Fix ai.py: correct forwarded message handling (aiogram 3.x forward_origin), pass relevant summaries via extra_system_content - Fix dialogue.py: only respond to Astra's messages, use system context instead of prompt injection, answer on the limit message before phase transition - Fix voice.py: load Whisper model in thread pool, safe WAV path generation - Improve database.py: composite indexes, Boolean is_active, upsert file_id cache, add context cleanup helper - Update weather.py and yadisk_download.py to use shared proxy connector - Update yadisk.py: validate URL before cache clear, add download size limit, wrap sync file ops in to_thread - Reuse S3 client via lru_cache - Update setup_commands with /aiclear and /aiuser - Update README, Dockerfile (Python 3.11), docker-compose (mount models) - Pin dependency versions, remove unused httpx[socks] - Add basic pytest tests for layout converter and voice normalization
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import logging
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from aiohttp_socks import ProxyConnector, ProxyType
|
||||
|
||||
from config import PROXY_ENABLED, PROXY_URL
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_proxy_connector() -> ProxyConnector | None:
|
||||
"""Create an aiohttp SOCKS5 proxy connector from PROXY_URL if enabled."""
|
||||
if not PROXY_ENABLED or not PROXY_URL:
|
||||
return None
|
||||
|
||||
parsed = urlparse(PROXY_URL)
|
||||
|
||||
if parsed.scheme not in ("socks5", "socks5h"):
|
||||
logger.warning("Unsupported proxy scheme: %s", parsed.scheme)
|
||||
return None
|
||||
|
||||
host = parsed.hostname
|
||||
port = parsed.port
|
||||
if not host or not port:
|
||||
logger.warning("Invalid proxy URL: missing host or port")
|
||||
return None
|
||||
|
||||
username = parsed.username
|
||||
password = parsed.password
|
||||
|
||||
try:
|
||||
return ProxyConnector(
|
||||
proxy_type=ProxyType.SOCKS5,
|
||||
host=host,
|
||||
port=port,
|
||||
username=username,
|
||||
password=password,
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.error("Failed to create proxy connector: %s", exc)
|
||||
return None
|
||||
Reference in New Issue
Block a user