27 lines
907 B
Python
27 lines
907 B
Python
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'
|
|
enable = os.getenv("TELEGRAM_PROXY_ENABLE", "false").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
|