chore: 暂存本地修改
This commit is contained in:
+42
-2
@@ -24,6 +24,19 @@ def _normalize_bool(value, default: bool = True) -> bool:
|
||||
return text not in ("0", "false", "no", "off", "")
|
||||
|
||||
|
||||
def _env_proxy_override() -> Optional[bool]:
|
||||
"""
|
||||
环境变量覆盖代理开关:
|
||||
- PROXY_ENABLED 未设置:返回 None(不覆盖,仍读取 proxy_settings.json)
|
||||
- PROXY_ENABLED=0/false/off:强制关闭代理
|
||||
- PROXY_ENABLED=1/true/on:强制开启代理(前提是配置字段齐全)
|
||||
"""
|
||||
raw = os.getenv("PROXY_ENABLED")
|
||||
if raw is None:
|
||||
return None
|
||||
return _normalize_bool(raw, True)
|
||||
|
||||
|
||||
def _load_config() -> Dict[str, str]:
|
||||
if not os.path.exists(CONFIG_PATH):
|
||||
return dict(DEFAULT_CONFIG)
|
||||
@@ -48,7 +61,12 @@ def report_proxy_status() -> None:
|
||||
_PROXY_STATUS_REPORTED = True
|
||||
|
||||
config = _load_config()
|
||||
enabled = _normalize_bool(config.get("enabled"), True)
|
||||
override = _env_proxy_override()
|
||||
if override is False:
|
||||
print("[proxy] disabled by env (PROXY_ENABLED=0)")
|
||||
return
|
||||
|
||||
enabled = _normalize_bool(config.get("enabled"), True) if override is None else True
|
||||
if not enabled:
|
||||
print("[proxy] disabled by config")
|
||||
return
|
||||
@@ -66,7 +84,10 @@ def get_proxies() -> Optional[Dict[str, str]]:
|
||||
代理配置从 proxy_settings.json 读取,不依赖环境变量。
|
||||
"""
|
||||
config = _load_config()
|
||||
if not _normalize_bool(config.get("enabled"), True):
|
||||
override = _env_proxy_override()
|
||||
if override is False:
|
||||
return None
|
||||
if override is None and not _normalize_bool(config.get("enabled"), True):
|
||||
return None
|
||||
|
||||
tunnel = str(config.get("tunnel") or "").strip()
|
||||
@@ -95,3 +116,22 @@ def apply_proxy(session) -> Optional[Dict[str, str]]:
|
||||
|
||||
|
||||
__all__ = ["get_proxies", "apply_proxy", "report_proxy_status"]
|
||||
|
||||
|
||||
def is_proxy_enabled() -> bool:
|
||||
"""
|
||||
判断当前进程是否启用了代理。
|
||||
|
||||
优先遵循环境变量 PROXY_ENABLED;
|
||||
未设置时回退到 proxy_settings.json 的 enabled 配置。
|
||||
"""
|
||||
config = _load_config()
|
||||
override = _env_proxy_override()
|
||||
if override is False:
|
||||
return False
|
||||
if override is True:
|
||||
return True
|
||||
return _normalize_bool(config.get("enabled"), True)
|
||||
|
||||
|
||||
__all__ = ["get_proxies", "apply_proxy", "report_proxy_status", "is_proxy_enabled"]
|
||||
|
||||
Reference in New Issue
Block a user