该提交新增了完整的 Nostr 去中心化社交网络适配器实现,包含以下核心功能: 1. 基础加密与密钥处理:支持 nsec/npub/hex 格式密钥转换,实现 NIP-04/NIP-17 加解密 2. Relay 管理与健康监控:支持多 Relay 连接、自动重连、健康评分与自动选优 3. 事件与消息处理:实现事件校验、去重、速率限制、消息缓存与状态持久化 4. 个人资料管理:支持发布/导入/合并 Nostr Kind 0 元数据事件 5. 配对机制:实现安全的双向配对通信流程 6. Zap 功能:支持 NIP-57 打赏请求与收据解析 7. NIP-05 验证:实现域名身份验证 8. 配置系统:完整的配置校验与多账户支持 9. 监控与指标:提供连接监控、事件统计与健康快照 10. HTTP API:提供个人资料管理的 RESTful 接口 11. 安装向导:命令行配置向导与初始化流程 所有模块均遵循 Nostr 协议规范,支持多账户、多 Relay 部署,内置安全防护与流量控制机制。
51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
from dataclasses import dataclass
|
|
|
|
import websockets
|
|
|
|
from yuxi.utils.logging_config import logger
|
|
|
|
|
|
@dataclass
|
|
class ProbeResult:
|
|
url: str
|
|
connected: bool
|
|
latency_ms: float | None = None
|
|
error: str | None = None
|
|
|
|
|
|
async def probe_relay(url: str, timeout: float = 10.0) -> ProbeResult:
|
|
start = asyncio.get_event_loop().time()
|
|
try:
|
|
ws = await asyncio.wait_for(
|
|
websockets.connect(url, ping_interval=20, ping_timeout=10),
|
|
timeout=timeout,
|
|
)
|
|
latency_ms = (asyncio.get_event_loop().time() - start) * 1000
|
|
sub_id = "forcepilot_probe"
|
|
req = json.dumps(["REQ", sub_id, {"kinds": [1], "limit": 1}])
|
|
await asyncio.wait_for(ws.send(req), timeout=5)
|
|
eose_received = False
|
|
try:
|
|
while not eose_received:
|
|
raw = await asyncio.wait_for(ws.recv(), timeout=5)
|
|
data = json.loads(raw)
|
|
if isinstance(data, list) and len(data) >= 2 and data[0] == "EOSE":
|
|
eose_received = True
|
|
except TimeoutError:
|
|
pass
|
|
close_req = json.dumps(["CLOSE", sub_id])
|
|
try:
|
|
await ws.send(close_req)
|
|
except Exception:
|
|
pass
|
|
await ws.close()
|
|
return ProbeResult(url=url, connected=True, latency_ms=latency_ms)
|
|
except Exception as e:
|
|
latency_ms = (asyncio.get_event_loop().time() - start) * 1000
|
|
logger.debug(f"Relay 探测失败 {url}: {e}")
|
|
return ProbeResult(url=url, connected=False, latency_ms=latency_ms, error=str(e))
|