diff --git a/astrbot/core/utils/io.py b/astrbot/core/utils/io.py index ba09a9dd2..1cdb2a409 100644 --- a/astrbot/core/utils/io.py +++ b/astrbot/core/utils/io.py @@ -84,9 +84,13 @@ async def download_image_by_url( 下载图片, 返回 path """ try: - ssl_context = ssl.create_default_context(cafile=certifi.where()) # 使用 certifi 提供的 CA 证书 + ssl_context = ssl.create_default_context( + cafile=certifi.where() + ) # 使用 certifi 提供的 CA 证书 connector = aiohttp.TCPConnector(ssl=ssl_context) # 使用 certifi 的根证书 - async with aiohttp.ClientSession(trust_env=True, connector=connector) as session: + async with aiohttp.ClientSession( + trust_env=True, connector=connector + ) as session: if post: async with session.post(url, json=post_data) as resp: if not path: @@ -123,9 +127,13 @@ async def download_file(url: str, path: str, show_progress: bool = False): 从指定 url 下载文件到指定路径 path """ try: - ssl_context = ssl.create_default_context(cafile=certifi.where()) # 使用 certifi 提供的 CA 证书 + ssl_context = ssl.create_default_context( + cafile=certifi.where() + ) # 使用 certifi 提供的 CA 证书 connector = aiohttp.TCPConnector(ssl=ssl_context) - async with aiohttp.ClientSession(trust_env=True, connector=connector) as session: + async with aiohttp.ClientSession( + trust_env=True, connector=connector + ) as session: async with session.get(url, timeout=1800) as resp: if resp.status != 200: raise Exception(f"下载文件失败: {resp.status}") @@ -209,7 +217,9 @@ async def download_dashboard(): """下载管理面板文件""" dashboard_release_url = "https://astrbot-registry.soulter.top/download/astrbot-dashboard/latest/dist.zip" try: - ssl_context = ssl.create_default_context(cafile=certifi.where()) # 使用 certifi 提供的 CA 证书 + ssl_context = ssl.create_default_context( + cafile=certifi.where() + ) # 使用 certifi 提供的 CA 证书 await download_file( dashboard_release_url, "data/dashboard.zip", show_progress=True ) diff --git a/astrbot/core/zip_updator.py b/astrbot/core/zip_updator.py index cc951d257..4f17b0277 100644 --- a/astrbot/core/zip_updator.py +++ b/astrbot/core/zip_updator.py @@ -37,14 +37,22 @@ class RepoZipUpdator: 返回一个列表,每个元素是一个字典,包含版本号、发布时间、更新内容、commit hash等信息。 """ try: - ssl_context = ssl.create_default_context(cafile=certifi.where()) # 新增:创建基于 certifi 的 SSL 上下文 - connector = aiohttp.TCPConnector(ssl=ssl_context) # 新增:使用 TCPConnector 指定 SSL 上下文 - async with aiohttp.ClientSession(trust_env=True, connector=connector) as session: + ssl_context = ssl.create_default_context( + cafile=certifi.where() + ) # 新增:创建基于 certifi 的 SSL 上下文 + connector = aiohttp.TCPConnector( + ssl=ssl_context + ) # 新增:使用 TCPConnector 指定 SSL 上下文 + async with aiohttp.ClientSession( + trust_env=True, connector=connector + ) as session: async with session.get(url) as response: # 检查 HTTP 状态码 if response.status != 200: text = await response.text() - logger.error(f"请求 {url} 失败,状态码: {response.status}, 内容: {text}") + logger.error( + f"请求 {url} 失败,状态码: {response.status}, 内容: {text}" + ) raise Exception(f"请求失败,状态码: {response.status}") result = await response.json() if not result: diff --git a/astrbot/dashboard/routes/plugin.py b/astrbot/dashboard/routes/plugin.py index af4d0db31..32dc99176 100644 --- a/astrbot/dashboard/routes/plugin.py +++ b/astrbot/dashboard/routes/plugin.py @@ -74,7 +74,9 @@ class PluginRoute(Route): connector = aiohttp.TCPConnector(ssl=ssl_context) for url in urls: try: - async with aiohttp.ClientSession(trust_env=True, connector=connector) as session: + async with aiohttp.ClientSession( + trust_env=True, connector=connector + ) as session: async with session.get(url) as response: if response.status == 200: result = await response.json()