QChatGPT/pkg/utils/proxy.py

35 lines
942 B
Python
Raw Permalink Normal View History

2024-01-29 21:22:27 +08:00
from __future__ import annotations
2024-02-06 21:26:03 +08:00
import os
import sys
2024-01-29 21:22:27 +08:00
from ..core import app
class ProxyManager:
2024-03-03 16:34:59 +08:00
"""代理管理器
"""
2024-01-29 21:22:27 +08:00
ap: app.Application
forward_proxies: dict[str, str]
def __init__(self, ap: app.Application):
self.ap = ap
self.forward_proxies = {}
async def initialize(self):
2024-02-06 21:26:03 +08:00
self.forward_proxies = {
2024-02-07 23:58:22 +08:00
"http://": os.getenv("HTTP_PROXY") or os.getenv("http_proxy"),
"https://": os.getenv("HTTPS_PROXY") or os.getenv("https_proxy"),
2024-02-06 21:26:03 +08:00
}
if 'http' in self.ap.system_cfg.data['network-proxies']:
2024-02-07 23:58:22 +08:00
self.forward_proxies['http://'] = self.ap.system_cfg.data['network-proxies']['http']
2024-02-06 21:26:03 +08:00
if 'https' in self.ap.system_cfg.data['network-proxies']:
2024-02-07 23:58:22 +08:00
self.forward_proxies['https://'] = self.ap.system_cfg.data['network-proxies']['https']
2024-02-06 21:26:03 +08:00
def get_forward_proxies(self) -> dict:
2024-02-07 23:58:22 +08:00
return self.forward_proxies.copy()