fix: use certifi CA bundle for runtime SSL requests

This commit is contained in:
邹永赫
2026-02-10 01:46:28 +09:00
parent f1ad258949
commit b57312ac4b
3 changed files with 23 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
import ssl
from typing import Literal, TypedDict
import aiohttp
import certifi
from astrbot.core import logger
@@ -32,7 +34,11 @@ LLM_METADATAS: dict[str, LLMMetadata] = {}
async def update_llm_metadata() -> None:
url = "https://models.dev/api.json"
try:
async with aiohttp.ClientSession() as session:
ssl_context = ssl.create_default_context(cafile=certifi.where())
connector = aiohttp.TCPConnector(ssl=ssl_context)
async with aiohttp.ClientSession(
trust_env=True, connector=connector
) as session:
async with session.get(url) as response:
data = await response.json()
global LLM_METADATAS

View File

@@ -39,7 +39,12 @@ class NetworkRenderStrategy(RenderStrategy):
async def get_official_endpoints(self) -> None:
"""获取官方的 t2i 端点列表。"""
try:
async with aiohttp.ClientSession() as session:
ssl_context = ssl.create_default_context(cafile=certifi.where())
connector = aiohttp.TCPConnector(ssl=ssl_context)
async with aiohttp.ClientSession(
trust_env=True,
connector=connector,
) as session:
async with session.get(
"https://api.soulter.top/astrbot/t2i-endpoints",
) as resp:

10
main.py
View File

@@ -50,6 +50,16 @@ 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")