Skip to content

The only downloader you need. 下载器的集大成者。

refactor(proxy): 重构系统代理获取逻辑

XiaoYouChRc2ae4b39e464
diff --git a/app/supports/utils.py b/app/supports/utils.py
index 1bef9539..8b06f99f 100644
--- a/app/supports/utils.py
+++ b/app/supports/utils.py
@@ -1,12 +1,11 @@
-import os
import sys
from datetime import datetime
from functools import wraps
from pathlib import Path
from time import sleep
from typing import Callable
-from urllib.parse import urlsplit, urlunsplit
+from niquests.utils import getproxies
from PySide6.QtCore import QUrl, Qt, QProcess, QStandardPaths
from PySide6.QtGui import QDesktopServices
from loguru import logger
@@ -42,147 +41,12 @@ def openAppLogFolder():
openFolder(f"{appLocalDataLocation}/GhostDownloader/GhostDownloader.log")
-def _getWindowsSystemProxies() -> dict[str, str]:
- import winreg
-
- with winreg.OpenKey(
- winreg.HKEY_CURRENT_USER,
- r"Software\Microsoft\Windows\CurrentVersion\Internet Settings",
- ) as key:
- proxyEnable, _ = winreg.QueryValueEx(key, "ProxyEnable")
- if not proxyEnable:
- return {}
-
- proxyServer, _ = winreg.QueryValueEx(key, "ProxyServer")
- rawProxies: dict[str, str] = {}
- defaultProxy = ""
- for entry in str(proxyServer or "").split(";"):
- entry = entry.strip()
- if not entry:
- continue
-
- if "=" not in entry:
- defaultProxy = entry
- continue
-
- protocol, value = entry.split("=", 1)
- protocol = protocol.strip().lower()
- value = value.strip()
- if protocol in _PROXY_PROTOCOLS and value:
- rawProxies[protocol] = value
-
- if defaultProxy:
- for protocol in _PROXY_PROTOCOLS:
- rawProxies.setdefault(protocol, defaultProxy)
-
- proxies = {
- protocol: value if "://" in value else f"http://{value}"
- for protocol, value in rawProxies.items()
- if value
- }
- if not proxies:
- return {}
-
- username = None
- password = None
- try:
- proxyUser, _ = winreg.QueryValueEx(key, "ProxyUser")
- if proxyUser:
- username = str(proxyUser)
-
- proxyPass, _ = winreg.QueryValueEx(key, "ProxyPass")
- if proxyPass:
- password = str(proxyPass)
- except (FileNotFoundError, OSError):
- pass
-
- if not username and not password:
- return proxies
-
- credentials = username or ""
- if password is not None:
- credentials = f"{credentials}:{password}"
-
- proxiesWithCredentials: dict[str, str] = {}
- for protocol, proxyUrl in proxies.items():
- parsed = urlsplit(proxyUrl)
- if parsed.username or parsed.password:
- proxiesWithCredentials[protocol] = proxyUrl
- continue
-
- proxiesWithCredentials[protocol] = urlunsplit(
- parsed._replace(netloc=f"{credentials}@{parsed.netloc}")
- )
-
- return proxiesWithCredentials
-
-
-def _getEnvSystemProxies() -> dict[str, str]:
- allProxy = str(os.environ.get("all_proxy") or os.environ.get("ALL_PROXY") or "").strip()
- proxies = {
- protocol: str(
- os.environ.get(f"{protocol}_proxy")
- or os.environ.get(f"{protocol.upper()}_PROXY")
- or allProxy
- or ""
- ).strip()
- for protocol in _PROXY_PROTOCOLS
- }
- fallbackProxy = proxies["https"] or proxies["http"]
- if fallbackProxy:
- for protocol in _PROXY_PROTOCOLS:
- if not proxies[protocol]:
- proxies[protocol] = fallbackProxy
-
- return {protocol: proxyUrl for protocol, proxyUrl in proxies.items() if proxyUrl}
-
-
-def _getLinuxSystemProxies() -> dict[str, str]:
- return _getEnvSystemProxies()
-
-
-def _getDarwinSystemProxies() -> dict[str, str]:
- try:
- import SystemConfiguration
- proxySettings = SystemConfiguration.SCDynamicStoreCopyProxies(None) # type: ignore
- except Exception:
- return _getEnvSystemProxies()
-
- if proxySettings.get("SOCKSEnable", 0):
- proxyUrl = f"socks5://{proxySettings.get('SOCKSProxy')}:{proxySettings.get('SOCKSPort')}"
- return {protocol: proxyUrl for protocol in _PROXY_PROTOCOLS}
-
- if proxySettings.get("HTTPEnable", 0):
- proxyUrl = f"http://{proxySettings.get('HTTPProxy')}:{proxySettings.get('HTTPPort')}"
- return {protocol: proxyUrl for protocol in _PROXY_PROTOCOLS}
-
- return _getEnvSystemProxies()
-
-
-def getSystemProxies() -> dict[str, str] | None:
- try:
- if sys.platform == "win32":
- proxies = _getWindowsSystemProxies()
- elif sys.platform == "linux":
- proxies = _getLinuxSystemProxies()
- elif sys.platform == "darwin":
- proxies = _getDarwinSystemProxies()
- else:
- proxies = {}
- except FileNotFoundError:
- return None
- except Exception as e:
- logger.opt(exception=e).error("无法获取系统代理服务器")
- return None
- return proxies or None
-
-
def getProxies():
if cfg.proxyServer.value == "Off":
return None
if cfg.proxyServer.value == "Auto":
- return getSystemProxies()
+ return getproxies() or None
proxyServer = str(cfg.proxyServer.value).strip()
if not proxyServer:
diff --git a/app/view/components/setting_cards.py b/app/view/components/setting_cards.py
index 3944423b..28113233 100644
--- a/app/view/components/setting_cards.py
+++ b/app/view/components/setting_cards.py
@@ -25,7 +25,7 @@ from qfluentwidgets import (
from urllib.parse import urlsplit
from app.supports.config import cfg
-from app.supports.utils import getSystemProxies
+from app.supports.utils import getProxies
class SpinBoxSettingCard(SettingCard):
@@ -238,16 +238,18 @@ class ProxySettingCard(ExpandGroupSettingCard):
self.customProxyWidget.setDisabled(True)
self.credentialsWidget.setDisabled(True)
- systemProxies = getSystemProxies()
- systemValue = None
- if systemProxies:
- for protocol in ("https", "http", "ftp"):
- systemValue = systemProxies.get(protocol)
- if systemValue:
- break
- self._applyProxyUrl(systemValue)
-
cfg.set(self.configItem, "Auto")
+ proxies = getProxies()
+ self._applyProxyUrl(
+ next(
+ (
+ proxies.get(protocol)
+ for protocol in ("https", "http", "ftp")
+ if proxies and proxies.get(protocol)
+ ),
+ None,
+ )
+ )
elif button is self.offRadioButton: # 关闭
# 禁用 Custom 编辑器