From 1702ef25e2dcdfce64b235624c9ed2fbb45bcd06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=B0=B8=E8=B5=AB?= <1259085392@qq.com> Date: Tue, 10 Feb 2026 08:04:38 +0900 Subject: [PATCH] fix: configure certifi CA before core imports --- main.py | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 26355f7a0..66e7fa7d2 100644 --- a/main.py +++ b/main.py @@ -5,10 +5,25 @@ import os import sys from pathlib import Path -from astrbot.core import LogBroker, LogManager, db_helper, logger -from astrbot.core.config.default import VERSION -from astrbot.core.initial_loader import InitialLoader -from astrbot.core.utils.astrbot_path import ( + +def _configure_runtime_ca_bundle() -> None: + try: + import certifi + + certifi_cafile = certifi.where() + if certifi_cafile and os.path.exists(certifi_cafile): + os.environ.setdefault("SSL_CERT_FILE", certifi_cafile) + os.environ.setdefault("REQUESTS_CA_BUNDLE", certifi_cafile) + except Exception: + return + + +_configure_runtime_ca_bundle() + +from astrbot.core import LogBroker, LogManager, db_helper, logger # noqa: E402, I001 +from astrbot.core.config.default import VERSION # noqa: E402 +from astrbot.core.initial_loader import InitialLoader # noqa: E402 +from astrbot.core.utils.astrbot_path import ( # noqa: E402 get_astrbot_config_path, get_astrbot_data_path, get_astrbot_plugin_path, @@ -16,7 +31,7 @@ from astrbot.core.utils.astrbot_path import ( get_astrbot_site_packages_path, get_astrbot_temp_path, ) -from astrbot.core.utils.io import download_dashboard, get_dashboard_version +from astrbot.core.utils.io import download_dashboard, get_dashboard_version # noqa: E402 # 将父目录添加到 sys.path sys.path.append(Path(__file__).parent.as_posix()) @@ -50,16 +65,6 @@ def check_env() -> None: os.makedirs(get_astrbot_temp_path(), exist_ok=True) os.makedirs(site_packages_path, exist_ok=True) - try: - import certifi - - certifi_cafile = certifi.where() - if certifi_cafile and os.path.exists(certifi_cafile): - os.environ.setdefault("SSL_CERT_FILE", certifi_cafile) - os.environ.setdefault("REQUESTS_CA_BUNDLE", certifi_cafile) - except Exception as e: - logger.warning(f"Failed to configure certifi CA bundle: {e}") - # 针对问题 #181 的临时解决方案 mimetypes.add_type("text/javascript", ".js") mimetypes.add_type("text/javascript", ".mjs")