ForcePilot/backend/package/yuxi/channels/adapters/nextcloudtalk/client.py
Kris 1f78c44b03 refactor: 整理并清理项目中的冗余代码与格式问题
这是一个批量整理提交,包含以下主要改动:
1.  删除多处冗余的空行和未使用的导入
2.  修复文件末尾缺少换行符的问题
3.  调整部分模块的导入顺序与代码排版
4.  修复部分配置默认值与策略逻辑
5.  新增多个功能模块与辅助工具
6.  完善异常处理与日志记录
7.  修复速率限制、消息缓存、权限校验等逻辑bug
8.  废弃部分旧有API与配置项并添加警告提示
2026-05-12 14:51:53 +08:00

146 lines
5.8 KiB
Python

from __future__ import annotations
import base64
import hashlib
import hmac
import json
import logging
import os
from typing import Any
from urllib.parse import urlparse
import aiohttp
from yuxi.channels.exceptions import ChannelNotConnectedError
logger = logging.getLogger(__name__)
def _compute_bot_signature(body: bytes, random_hex: str, secret: str) -> str:
payload = random_hex.encode() + body
return base64.b64encode(hmac.digest(secret.encode(), payload, hashlib.sha256)).decode()
def verify_hmac_signature(body: bytes, random_header: str, signature_header: str, secret: str) -> bool:
if not random_header or not signature_header or not secret:
return False
expected = _compute_bot_signature(body, random_header, secret)
return hmac.compare_digest(expected, signature_header)
class NextcloudTalkClient:
def __init__(
self,
server_url: str,
bot_user: str,
app_password: str,
proxy: str | None = None,
bot_secret: str | None = None,
):
self.server_url = server_url.rstrip("/")
self.bot_user = bot_user
self.basic_auth = base64.b64encode(f"{bot_user}:{app_password}".encode()).decode()
self.proxy = proxy
self.bot_secret = bot_secret
self._session: aiohttp.ClientSession | None = None
async def start(self) -> None:
if self._session is None:
connector_kwargs: dict[str, Any] = {"limit": 10, "ttl_dns_cache": 300}
timeout = aiohttp.ClientTimeout(total=60)
session_kwargs: dict[str, Any] = {
"connector": aiohttp.TCPConnector(**connector_kwargs),
"timeout": timeout,
"headers": {"User-Agent": "ForcePilot-NextcloudTalk-Adapter/1.0"},
}
if self.proxy:
session_kwargs["proxy"] = self.proxy
self._session = aiohttp.ClientSession(**session_kwargs)
async def stop(self) -> None:
if self._session:
await self._session.close()
self._session = None
@property
def session(self) -> aiohttp.ClientSession:
if self._session is None:
raise ChannelNotConnectedError()
return self._session
def auth_headers(self) -> dict[str, str]:
return {"Authorization": f"Basic {self.basic_auth}"}
def api_url(self, path: str) -> str:
return f"{self.server_url}{path}"
def _sign_body(self, body_bytes: bytes) -> dict[str, str]:
if not self.bot_secret:
return {}
random_hex = os.urandom(32).hex()
signature = _compute_bot_signature(body_bytes, random_hex, self.bot_secret)
return {
"X-Nextcloud-Talk-Bot-Random": random_hex,
"X-Nextcloud-Talk-Bot-Signature": signature,
}
def _validate_url(self, url: str) -> None:
parsed = urlparse(url)
server_parsed = urlparse(self.server_url)
if parsed.scheme not in ("https", "http"):
raise ValueError(f"Invalid URL scheme: {parsed.scheme}")
if parsed.hostname != server_parsed.hostname:
raise ValueError(f"SSRF blocked: external host {parsed.hostname} != {server_parsed.hostname}")
async def get(self, path: str, **kwargs) -> dict[str, Any]:
url = self.api_url(path)
self._validate_url(url)
headers = {**self.auth_headers(), "OCS-APIRequest": "true", "Accept": "application/json"}
headers.update(self._sign_body(b""))
headers.update(kwargs.pop("headers", {}))
async with self.session.get(url, headers=headers, **kwargs) as resp:
resp.raise_for_status()
return await resp.json()
async def post(self, path: str, json_data: dict[str, Any] | None = None, **kwargs) -> dict[str, Any]:
url = self.api_url(path)
self._validate_url(url)
headers = {**self.auth_headers(), "OCS-APIRequest": "true", "Accept": "application/json"}
body_bytes = json.dumps(json_data).encode() if json_data else b""
headers.update(self._sign_body(body_bytes))
headers.update(kwargs.pop("headers", {}))
async with self.session.post(url, headers=headers, json=json_data, **kwargs) as resp:
resp.raise_for_status()
return await resp.json()
async def post_form(self, path: str, form: aiohttp.FormData, **kwargs) -> dict[str, Any]:
url = self.api_url(path)
self._validate_url(url)
headers = {**self.auth_headers(), "OCS-APIRequest": "true", "Accept": "application/json"}
headers.update(self._sign_body(b""))
headers.update(kwargs.pop("headers", {}))
async with self.session.post(url, headers=headers, data=form, **kwargs) as resp:
resp.raise_for_status()
return await resp.json()
async def put(self, path: str, json_data: dict[str, Any] | None = None, **kwargs) -> dict[str, Any]:
url = self.api_url(path)
self._validate_url(url)
headers = {**self.auth_headers(), "OCS-APIRequest": "true", "Accept": "application/json"}
body_bytes = json.dumps(json_data).encode() if json_data else b""
headers.update(self._sign_body(body_bytes))
headers.update(kwargs.pop("headers", {}))
async with self.session.put(url, headers=headers, json=json_data, **kwargs) as resp:
resp.raise_for_status()
return await resp.json()
async def delete(self, path: str, **kwargs) -> dict[str, Any]:
url = self.api_url(path)
self._validate_url(url)
headers = {**self.auth_headers(), "OCS-APIRequest": "true", "Accept": "application/json"}
headers.update(self._sign_body(b""))
headers.update(kwargs.pop("headers", {}))
async with self.session.delete(url, headers=headers, **kwargs) as resp:
resp.raise_for_status()
return await resp.json()