feat(network): add proxy support and docker host-gateway configuration
This commit is contained in:
@@ -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
|
||||
Reference in New Issue
Block a user