修复了一些错误 更新了AI.md(给ai看的)

This commit is contained in:
Falck
2026-05-02 19:21:50 +08:00
parent 0783428f80
commit 70c531860b
240 changed files with 5626 additions and 10790 deletions

View File

@@ -1,48 +1,15 @@
"""HTML 渲染服务 - 通过 config.json 配置,统一文件入口"""
import json
import sys
from pathlib import Path
from oss.plugin.types import Plugin, register_plugin_type, Response
class _Log:
_TTY = sys.stdout.isatty()
_C = {"reset": "\033[0m", "white": "\033[0;37m", "yellow": "\033[1;33m", "blue": "\033[1;34m", "red": "\033[1;31m"}
@classmethod
def _c(cls, t, c):
return f"{cls._C.get(c,'')}{t}{cls._C['reset']}" if cls._TTY else t
@classmethod
def info(cls, m): print(f"{cls._c('[html-render]', 'white')} {cls._c(m, 'white')}")
@classmethod
def warn(cls, m): print(f"{cls._c('[html-render]', 'yellow')} {cls._c('', 'yellow')} {cls._c(m, 'yellow')}")
@classmethod
def error(cls, m): print(f"{cls._c('[html-render]', 'red')} {cls._c('', 'red')} {cls._c(m, 'red')}")
class HtmlRenderPlugin(Plugin):
"""HTML 渲染插件 - 渲染服务由 html-render 提供"""
def __init__(self):
self.http_api = None
self.storage = None # plugin-storage 入口
self.config = {}
self.root_dir = None # 解析后的网站根目录
self.storage = None self.config = {}
self.root_dir = None
def init(self, deps: dict = None):
"""初始化 - 读取 config.json 并解析网站根目录"""
self._load_config()
_Log.info(f"配置加载完成: root_dir={self.root_dir}")
def start(self):
"""启动 - 注册路由到 http-api共享配置给 web-toolkit"""
# 注册首页路由
if self.http_api and hasattr(self.http_api, 'router'):
self.http_api.router.get("/", self._serve_html)
_Log.info("已注册路由到 http-api")
else:
_Log.warn("http-api 未加载")
# 将配置共享给 web-toolkit通过 plugin-storage 的 DCIM 共享存储)
if self.storage:
shared = self.storage.get_shared()
shared.set_shared("html-render-config", {
@@ -53,19 +20,9 @@ class HtmlRenderPlugin(Plugin):
_Log.info("配置已共享到 DCIM")
def stop(self):
"""停止"""
pass
def set_http_api(self, instance):
"""设置 http-api 实例"""
self.http_api = instance
def set_plugin_storage(self, instance):
"""设置 plugin-storage 实例(唯一文件读写入口)"""
self.storage = instance
def _load_config(self):
"""读取 config.json解析根目录"""
config_path = Path("./data/html-render/config.json")
if not config_path.exists():
_Log.warn("config.json 不存在,使用默认配置")
@@ -74,40 +31,13 @@ class HtmlRenderPlugin(Plugin):
with open(config_path, "r", encoding="utf-8") as f:
self.config = json.load(f)
# 解析根目录(相对于 config.json 的路径)
root_relative = self.config.get("root_dir", "../website")
self.root_dir = (config_path.parent / root_relative).resolve()
def _serve_html(self, request):
"""提供 HTML 页面 - 通过 plugin-storage 读取并注入静态资源路径"""
index_file = self.config.get("index_file", "index.html")
# 安全检查:防止路径穿越
if ".." in index_file or index_file.startswith("/"):
return Response(status=403, body="Forbidden")
if self.storage:
storage = self.storage.get_storage("html-render")
if storage.file_exists(index_file):
content = storage.read_file(index_file)
if content:
# 注入静态资源路径(相对路径 → /website/ 前缀)
content = self._inject_static_paths(content)
return Response(
status=200,
headers={"Content-Type": "text/html; charset=utf-8"},
body=content
)
return Response(status=404, body="Not Found")
def _inject_static_paths(self, html: str) -> str:
"""将相对静态资源路径替换为 /website/ 前缀"""
import re
# href="css/xxx" → href="/website/css/xxx"
html = re.sub(r'(href\s*=\s*["\'])css/', r'\1/website/css/', html)
# src="js/xxx" → src="/website/js/xxx"
html = re.sub(r'(src\s*=\s*["\'])js/', r'\1/website/js/', html)
# src="logo.svg" → src="/website/logo.svg"
html = re.sub(r'(src\s*=\s*["\'])(?!https?://|/)([\w.-]+\.(svg|png|jpg|gif|ico|webp))', r'\1/website/\2', html)
return html

View File

@@ -1,29 +1,3 @@
"""Web 工具包 - 路由注册、静态文件服务、前端事件(不负责渲染)"""
import json
import sys
from pathlib import Path
from oss.plugin.types import Plugin, register_plugin_type, Response
from .router import WebRouter
from .static import StaticFileHandler
from .template import TemplateEngine
class _Log:
_TTY = sys.stdout.isatty()
_C = {"reset": "\033[0m", "white": "\033[0;37m", "yellow": "\033[1;33m", "blue": "\033[1;34m", "red": "\033[1;31m"}
@classmethod
def _c(cls, t, c):
return f"{cls._C.get(c,'')}{t}{cls._C['reset']}" if cls._TTY else t
@classmethod
def info(cls, m): print(f"{cls._c('[web-toolkit]', 'white')} {cls._c(m, 'white')}")
@classmethod
def warn(cls, m): print(f"{cls._c('[web-toolkit]', 'yellow')} {cls._c('', 'yellow')} {cls._c(m, 'yellow')}")
@classmethod
def error(cls, m): print(f"{cls._c('[web-toolkit]', 'red')} {cls._c('', 'red')} {cls._c(m, 'red')}")
class WebToolkitPlugin(Plugin):
"""Web 工具包插件 - 提供网站前端所有服务"""
def __init__(self):
self.router = None
@@ -32,24 +6,12 @@ class WebToolkitPlugin(Plugin):
self.http_api = None
self.http_tcp = None
self.storage = None
self.config = {} # 从 config.json 读取
self.root_dir = None
self.config = {} self.root_dir = None
def init(self, deps: dict = None):
"""初始化 - 读取 config.json 配置"""
self.router = WebRouter()
self.template_engine = TemplateEngine()
self._load_config()
self.static_handler = StaticFileHandler(root=str(self.root_dir))
_Log.info(f"配置加载完成: root_dir={self.root_dir}")
def start(self):
"""启动"""
# 注册路由到 http-api
if self.http_api:
http_instance = self.http_api
if hasattr(http_instance, "router"):
# 精确路由先注册,参数化路由后注册
http_instance.router.get(
self.config.get("website_prefix", "/website") + "/",
self._serve_website_index
@@ -63,7 +25,6 @@ class WebToolkitPlugin(Plugin):
self._serve_static
)
# 注册路由到 http-tcp
if self.http_tcp:
tcp_instance = self.http_tcp
if hasattr(tcp_instance, "router"):
@@ -83,59 +44,17 @@ class WebToolkitPlugin(Plugin):
_Log.info("Web 工具包已启动")
def stop(self):
"""停止"""
pass
def set_http_api(self, instance):
"""设置 HTTP API 实例"""
self.http_api = instance
def set_http_tcp(self, instance):
"""设置 HTTP TCP 实例"""
self.http_tcp = instance
def set_plugin_storage(self, instance):
"""设置 plugin-storage 实例(唯一文件读写入口)"""
self.storage = instance
def set_static_dir(self, path: str):
"""设置静态文件目录"""
self.static_handler.set_root(path)
def set_template_dir(self, path: str):
"""设置模板目录"""
template_root = Path(path)
if template_root.exists():
self.template_engine.set_root(str(template_root))
def _load_config(self):
"""读取 config.json解析网站根目录"""
config_path = Path("./data/web-toolkit/config.json")
if not config_path.exists():
_Log.warn("config.json 不存在,使用默认配置")
self.config = {
"root_dir": "../website",
"index_file": "index.html",
"static_prefix": "/static",
"website_prefix": "/website",
}
else:
with open(config_path, "r", encoding="utf-8") as f:
self.config = json.load(f)
# 解析根目录(相对于 config.json 的路径)
root_relative = self.config.get("root_dir", "../website")
self.root_dir = (config_path.parent / root_relative).resolve()
# 初始化模板引擎
template_dir = self.config.get("template_dir", "")
if template_dir:
template_path = self.root_dir / template_dir
if template_path.exists():
self.template_engine.set_root(str(template_path))
def _serve_website_index(self, request):
"""提供 website 目录首页"""
index_file = self.config.get("index_file", "index.html")
if self.root_dir:
path = self.root_dir / index_file
@@ -149,29 +68,3 @@ class WebToolkitPlugin(Plugin):
return Response(status=404, body="Index file not found")
def _serve_static(self, request):
"""提供静态文件"""
path = request.path
website_prefix = self.config.get("website_prefix", "/website")
static_prefix = self.config.get("static_prefix", "/static")
if path.startswith(website_prefix + "/"):
filename = path[len(website_prefix) + 1:]
elif path.startswith(static_prefix + "/"):
filename = path[len(static_prefix) + 1:]
else:
filename = path.lstrip("/")
# 安全检查:防止路径穿越
if ".." in filename or filename.startswith("/"):
return Response(status=403, body="Forbidden")
if not filename:
return self._serve_website_index(request)
return self.static_handler.serve(filename)
register_plugin_type("WebToolkitPlugin", WebToolkitPlugin)
def New():
return WebToolkitPlugin()

View File

@@ -1,21 +1,2 @@
"""Web 路由器"""
from typing import Callable, Optional, Any
from oss.shared.router import BaseRouter, match_path
class WebRouter(BaseRouter):
"""Web 路由器"""
def handle(self, request: dict) -> Optional[Any]:
"""处理请求"""
method = request.get("method", "GET")
path = request.get("path", "/")
result = self.find_route(method, path)
if result:
route, params = result
# 将路径参数注入到请求中
request["path_params"] = params
return route.handler(request)
return None

View File

@@ -1,68 +1,13 @@
"""静态文件处理器"""
import os
import mimetypes
from pathlib import Path
from typing import Optional, Any
from oss.plugin.types import Response
class StaticFileHandler:
"""静态文件处理器"""
def __init__(self, root: str = "./static"):
self.root = root
self._ensure_root()
def _ensure_root(self):
"""确保静态目录存在"""
Path(self.root).mkdir(parents=True, exist_ok=True)
def set_root(self, path: str):
"""设置静态文件根目录"""
self.root = path
self._ensure_root()
def serve(self, filename: str) -> Optional[Response]:
"""提供静态文件"""
file_path = Path(self.root) / filename
# 安全检查:防止目录遍历
try:
file_path.resolve().relative_to(Path(self.root).resolve())
except ValueError:
return Response(status=403, body="Forbidden")
if not file_path.exists() or not file_path.is_file():
return Response(status=404, body="File not found")
# 检测 MIME 类型
content_type, _ = mimetypes.guess_type(str(file_path))
if not content_type:
content_type = "application/octet-stream"
# 读取文件内容
try:
if content_type.startswith("text/") or content_type in (
"application/json", "application/javascript", "application/xml"
):
content = file_path.read_text(encoding="utf-8")
else:
content = file_path.read_bytes()
return Response(
status=200,
headers={
"Content-Type": content_type,
"Cache-Control": "public, max-age=3600",
},
body=content,
)
except Exception as e:
return Response(status=500, body=f"Error reading file: {e}")
def list_files(self) -> list[str]:
"""列出静态文件"""
root_path = Path(self.root)
if not root_path.exists():
return []

View File

@@ -1,12 +1,3 @@
"""模板引擎"""
import re
import ast
from pathlib import Path
from typing import Any, Optional
class TemplateEngine:
"""简单模板引擎"""
def __init__(self, root: str = "./templates", max_depth: int = 10):
self.root = root
@@ -15,22 +6,11 @@ class TemplateEngine:
self._ensure_root()
def _ensure_root(self):
"""确保模板目录存在"""
Path(self.root).mkdir(parents=True, exist_ok=True)
def set_root(self, path: str):
"""设置模板根目录"""
self.root = path
self._ensure_root()
self._cache.clear()
def render(self, name: str, context: dict[str, Any]) -> str:
"""渲染模板"""
template = self._load_template(name)
return self._render_template(template, context, depth=0)
def _load_template(self, name: str) -> str:
"""加载模板"""
if name in self._cache:
return self._cache[name]
@@ -43,24 +23,6 @@ class TemplateEngine:
return content
def _safe_eval(self, expression: str, context: dict) -> Any:
"""安全评估表达式(使用 AST 验证,不使用 eval"""
try:
tree = ast.parse(expression, mode='eval')
except SyntaxError:
return False
# 验证 AST 节点
if not self._validate_ast(tree.body[0].value, set(context.keys())):
return False
# 使用安全的 AST 解释器,不使用 eval
try:
return self._eval_ast(tree.body[0].value, context)
except Exception:
return False
def _eval_ast(self, node: ast.AST, context: dict) -> Any:
"""安全地评估 AST 节点"""
if isinstance(node, ast.Constant):
return node.value
elif isinstance(node, ast.Name):
@@ -80,31 +42,6 @@ class TemplateEngine:
return False
def _eval_compare(self, node: ast.Compare, context: dict) -> bool:
"""评估比较表达式"""
left = self._eval_ast(node.left, context)
for op, comp in zip(node.ops, node.comparators):
right = self._eval_ast(comp, context)
if isinstance(op, ast.Eq):
if not (left == right): return False
elif isinstance(op, ast.NotEq):
if not (left != right): return False
elif isinstance(op, ast.Lt):
if not (left < right): return False
elif isinstance(op, ast.Gt):
if not (left > right): return False
elif isinstance(op, ast.LtE):
if not (left <= right): return False
elif isinstance(op, ast.GtE):
if not (left >= right): return False
elif isinstance(op, ast.In):
if not (left in right): return False
elif isinstance(op, ast.NotIn):
if not (left not in right): return False
left = right
return True
def _eval_subscript(self, node: ast.Subscript, context: dict) -> Any:
"""评估下标访问"""
value = self._eval_ast(node.value, context)
key = self._eval_ast(node.slice, context)
if isinstance(value, (dict, list, str)):
@@ -112,32 +49,6 @@ class TemplateEngine:
return None
def _validate_ast(self, node: ast.AST, allowed_names: set) -> bool:
"""验证 AST 只包含安全的操作"""
if isinstance(node, ast.Name):
return node.id in allowed_names or node.id in ('True', 'False', 'None')
elif isinstance(node, ast.Constant):
return True
elif isinstance(node, ast.BoolOp):
return all(self._validate_ast(v, allowed_names) for v in node.values)
elif isinstance(node, ast.Compare):
return (self._validate_ast(node.left, allowed_names) and
all(self._validate_ast(c, allowed_names) for c in node.comparators))
elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.Not):
return self._validate_ast(node.operand, allowed_names)
elif isinstance(node, ast.Attribute):
# 不允许属性访问(防止绕过安全限制)
return False
elif isinstance(node, ast.Call):
# 不允许函数调用
return False
elif isinstance(node, ast.Subscript):
# 允许简单的索引访问
return (self._validate_ast(node.value, allowed_names) and
self._validate_ast(node.slice, allowed_names))
return False
def _render_template(self, template: str, context: dict[str, Any], depth: int = 0) -> str:
"""渲染模板内容
Args:
template: 模板内容
@@ -146,13 +57,11 @@ class TemplateEngine:
Raises:
RecursionError: 当嵌套深度超过 max_depth
"""
if depth > self.max_depth:
raise RecursionError(
f"模板嵌套深度超过限制 ({self.max_depth}),可能存在无限递归"
)
# 替换 {{ variable }}
def replace_var(match):
var_name = match.group(1).strip()
value = context.get(var_name, "")
@@ -163,32 +72,13 @@ class TemplateEngine:
result = re.sub(r'\{\{(.*?)\}\}', replace_var, template)
# 处理 {% if condition %} ... {% endif %}
result = self._process_if(result, context, depth)
# 处理 {% for item in list %} ... {% endfor %}
result = self._process_for(result, context, depth)
return result
def _process_if(self, template: str, context: dict, depth: int = 0) -> str:
"""处理 if 条件"""
pattern = r'\{%\s*if\s+(.*?)\s*%\}(.*?){%\s*endif\s*%\}'
def replace_if(match):
condition = match.group(1).strip()
content = match.group(2)
# 安全条件评估
value = self._safe_eval(condition, context)
if value:
# 递归处理嵌套内容,深度+1
return self._render_template(content, context, depth + 1)
return ""
return re.sub(pattern, replace_if, template, flags=re.DOTALL)
def _process_for(self, template: str, context: dict, depth: int = 0) -> str:
"""处理 for 循环"""
pattern = r'\{%\s*for\s+(\w+)\s+in\s+(\w+)\s*%\}(.*?){%\s*endfor\s*%\}'
def replace_for(match):
@@ -203,7 +93,6 @@ class TemplateEngine:
result = ""
for item in items:
loop_context = {**context, item_name: item}
# 递归处理嵌套内容,深度+1
result += self._render_template(content, loop_context, depth + 1)
return result

View File

@@ -1,100 +0,0 @@
"""质量检查器"""
import ast
class QualityChecker:
"""质量检查器"""
def check(self, filepath: str, content: str) -> list:
"""执行质量检查"""
issues = []
# 检查函数长度
issues.extend(self._check_function_length(filepath, content))
# 检查参数数量
issues.extend(self._check_parameter_count(filepath, content))
# 检查复杂度
issues.extend(self._check_complexity(filepath, content))
return issues
def _check_function_length(self, filepath: str, content: str) -> list:
"""检查函数长度"""
issues = []
try:
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
lines = node.end_lineno - node.lineno
if lines > 100:
issues.append({
"file": filepath,
"line": node.lineno,
"severity": "warning",
"type": "long_function",
"message": f"函数 {node.name} 过长 ({lines} 行)"
})
except:
pass
return issues
def _check_parameter_count(self, filepath: str, content: str) -> list:
"""检查参数数量"""
issues = []
try:
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
args = node.args
count = len(args.args)
if count > 5:
issues.append({
"file": filepath,
"line": node.lineno,
"severity": "info",
"type": "too_many_params",
"message": f"函数 {node.name} 参数过多 ({count} 个)"
})
except:
pass
return issues
def _check_complexity(self, filepath: str, content: str) -> list:
"""检查圈复杂度"""
issues = []
try:
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
complexity = self._calculate_complexity(node)
if complexity > 10:
issues.append({
"file": filepath,
"line": node.lineno,
"severity": "warning",
"type": "high_complexity",
"message": f"函数 {node.name} 复杂度过高 (圈复杂度: {complexity})"
})
except:
pass
return issues
def _calculate_complexity(self, node: ast.AST) -> int:
"""计算圈复杂度"""
complexity = 1
for child in ast.walk(node):
if isinstance(child, (ast.If, ast.While, ast.For, ast.Try, ast.With)):
complexity += 1
elif isinstance(child, ast.BoolOp):
complexity += len(child.values) - 1
return complexity

View File

@@ -1,85 +0,0 @@
"""安全检查器"""
class SecurityChecker:
"""安全检查器"""
def check(self, filepath: str, content: str) -> list:
"""执行安全检查"""
issues = []
# 检查硬编码密钥
issues.extend(self._check_secrets(filepath, content))
# 检查危险函数
issues.extend(self._check_dangerous_functions(filepath, content))
# 检查路径穿越
issues.extend(self._check_path_traversal(filepath, content))
return issues
def _check_secrets(self, filepath: str, content: str) -> list:
"""检查硬编码密钥"""
issues = []
patterns = ['password', 'secret', 'token', 'api_key', 'access_token']
for i, line in enumerate(content.split('\n'), 1):
stripped = line.strip()
# 跳过注释和模式定义行
if stripped.startswith('#') or stripped.startswith('patterns') or "'" in stripped[:20]:
continue
for pattern in patterns:
if pattern + ' = "' in line.lower() or pattern + " = '" in line.lower():
issues.append({
"file": filepath,
"line": i,
"severity": "critical",
"type": "hardcoded_secret",
"message": f"发现硬编码密钥: {line.strip()[:50]}"
})
return issues
def _check_dangerous_functions(self, filepath: str, content: str) -> list:
"""检查危险函数"""
issues = []
dangerous = ['eval(', 'exec(', 'os.system(', 'subprocess.call(', 'subprocess.run(']
# 跳过检查安全检查器自身
if 'code-reviewer/checks/security.py' in filepath:
return []
for i, line in enumerate(content.split('\n'), 1):
# 跳过注释和模式定义行
stripped = line.strip()
if stripped.startswith('#') or 'dangerous' in stripped.lower() or "['" in stripped[:30]:
continue
for func in dangerous:
if func in line:
issues.append({
"file": filepath,
"line": i,
"severity": "warning",
"type": "dangerous_function",
"message": f"使用危险函数: {func.strip()}"
})
return issues
def _check_path_traversal(self, filepath: str, content: str) -> list:
"""检查路径穿越风险"""
issues = []
if '../' in content and 'open(' in content:
issues.append({
"file": filepath,
"line": 0,
"severity": "warning",
"type": "path_traversal_risk",
"message": "可能存在路径穿越漏洞"
})
return issues

View File

@@ -1,70 +0,0 @@
"""风格检查器"""
class StyleChecker:
"""风格检查器"""
def check(self, filepath: str, content: str) -> list:
"""执行风格检查"""
issues = []
# 检查行长度
issues.extend(self._check_line_length(filepath, content))
# 检查空行
issues.extend(self._check_blank_lines(filepath, content))
# 检查文件末尾换行
issues.extend(self._check_final_newline(filepath, content))
return issues
def _check_line_length(self, filepath: str, content: str) -> list:
"""检查行长度"""
issues = []
for i, line in enumerate(content.split('\n'), 1):
if len(line) > 120:
issues.append({
"file": filepath,
"line": i,
"severity": "info",
"type": "line_too_long",
"message": f"行过长 ({len(line)} 字符)"
})
return issues
def _check_blank_lines(self, filepath: str, content: str) -> list:
"""检查连续空行"""
issues = []
blank_count = 0
for i, line in enumerate(content.split('\n'), 1):
if line.strip() == '':
blank_count += 1
if blank_count > 2:
issues.append({
"file": filepath,
"line": i,
"severity": "info",
"type": "too_many_blanks",
"message": "连续空行过多"
})
else:
blank_count = 0
return issues
def _check_final_newline(self, filepath: str, content: str) -> list:
"""检查文件末尾换行"""
if content and not content.endswith('\n'):
return [{
"file": filepath,
"line": len(content.split('\n')),
"severity": "info",
"type": "missing_final_newline",
"message": "文件末尾缺少换行符"
}]
return []

View File

@@ -1,94 +0,0 @@
"""代码审查器核心"""
import os
import ast
import json
import time
from pathlib import Path
from typing import Any
from checks.security import SecurityChecker
from checks.quality import QualityChecker
from checks.style import StyleChecker
from checks.references import ReferenceChecker
from report.formatter import ReportFormatter
class CodeReviewer:
"""代码审查器"""
def __init__(self, config: dict):
self.config = config
self.security = SecurityChecker()
self.quality = QualityChecker()
self.style = StyleChecker()
self.references = ReferenceChecker()
self.formatter = ReportFormatter(config.get("report_format", "console"))
def run_check(self, scan_dirs: list) -> dict:
"""执行检查"""
start_time = time.time()
issues = []
files_scanned = 0
for scan_dir in scan_dirs:
if not os.path.exists(scan_dir):
continue
for root, dirs, files in os.walk(scan_dir):
# 排除目录
dirs[:] = [d for d in dirs if d not in self.config.get("exclude_patterns", [])]
for file in files:
if file.endswith('.py'):
filepath = os.path.join(root, file)
file_size = os.path.getsize(filepath)
if file_size > self.config.get("max_file_size", 102400):
continue
issues.extend(self._check_file(filepath))
files_scanned += 1
elapsed = time.time() - start_time
result = {
"status": "completed",
"files_scanned": files_scanned,
"total_issues": len(issues),
"issues": issues,
"scan_time": round(elapsed, 2),
"timestamp": time.time()
}
print(self.formatter.format(result))
return result
def _check_file(self, filepath: str) -> list:
"""检查单个文件"""
issues = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# 安全检查
issues.extend(self.security.check(filepath, content))
# 质量检查
issues.extend(self.quality.check(filepath, content))
# 风格检查
issues.extend(self.style.check(filepath, content))
# 引用检查(新增)
issues.extend(self.references.check(filepath, content))
except Exception as e:
issues.append({
"file": filepath,
"line": 0,
"severity": "error",
"type": "parse_error",
"message": f"文件解析失败: {e}"
})
return issues

View File

@@ -1,138 +0,0 @@
"""依赖解析插件 - 拓扑排序 + 循环依赖检测"""
from typing import Any, Optional
from oss.plugin.types import Plugin, register_plugin_type
class DependencyError(Exception):
"""依赖错误"""
pass
class DependencyResolver:
"""依赖解析器"""
def __init__(self):
self.graph: dict[str, list[str]] = {} # 插件名 -> 依赖列表
def add_plugin(self, name: str, dependencies: list[str]):
"""添加插件及其依赖"""
self.graph[name] = dependencies
def resolve(self) -> list[str]:
"""解析依赖,返回拓扑排序后的插件列表
例如A 依赖 BB 依赖 C
图: A -> [B], B -> [C], C -> []
结果: [C, B, A] (先启动没有依赖的,再启动依赖它们的)
"""
# 检测循环依赖
self._detect_cycles()
# 拓扑排序 (Kahn 算法 - 反向)
# in_degree[name] = name 依赖的插件数量
in_degree: dict[str, int] = {name: 0 for name in self.graph}
# 反向图: who_depends_on[dep] = [name1, name2, ...] (谁依赖 dep)
who_depends_on: dict[str, list[str]] = {name: [] for name in self.graph}
for name, deps in self.graph.items():
for dep in deps:
if dep in in_degree:
in_degree[name] += 1 # name 依赖 dep所以 name 的入度 +1
who_depends_on[dep].append(name) # dep 被 name 依赖
# 从没有依赖的插件开始
queue = [name for name, degree in in_degree.items() if degree == 0]
result = []
while queue:
node = queue.pop(0)
result.append(node)
# node 已启动,减少依赖它的插件的入度
for dependent in who_depends_on.get(node, []):
in_degree[dependent] -= 1
if in_degree[dependent] == 0:
queue.append(dependent)
if len(result) != len(self.graph):
raise DependencyError("无法解析依赖,可能存在循环依赖")
return result
def _detect_cycles(self):
"""检测循环依赖"""
visited = set()
rec_stack = set()
def dfs(node: str) -> bool:
visited.add(node)
rec_stack.add(node)
for dep in self.graph.get(node, []):
if dep not in visited:
if dfs(dep):
return True
elif dep in rec_stack:
raise DependencyError(f"检测到循环依赖: {node} -> {dep}")
rec_stack.remove(node)
return False
for node in self.graph:
if node not in visited:
if dfs(node):
raise DependencyError(f"检测到循环依赖涉及: {node}")
def get_missing(self) -> list[str]:
"""获取缺失的依赖"""
all_deps = set()
for deps in self.graph.values():
all_deps.update(deps)
all_plugins = set(self.graph.keys())
return list(all_deps - all_plugins)
class DependencyPlugin(Plugin):
"""依赖解析插件"""
def __init__(self):
self.resolver = DependencyResolver()
self.plugin_deps: dict[str, list[str]] = {}
def init(self, deps: dict = None):
"""初始化"""
pass
def start(self):
"""启动"""
pass
def stop(self):
"""停止"""
pass
def add_plugin(self, name: str, dependencies: list[str]):
"""添加插件及其依赖"""
self.plugin_deps[name] = dependencies
self.resolver.add_plugin(name, dependencies)
def resolve(self) -> list[str]:
"""解析依赖顺序"""
return self.resolver.resolve()
def get_missing_deps(self) -> list[str]:
"""获取缺失的依赖"""
return self.resolver.get_missing()
def get_order(self) -> list[str]:
"""获取插件加载顺序"""
return self.resolve()
# 注册类型
register_plugin_type("DependencyResolver", DependencyResolver)
register_plugin_type("DependencyError", DependencyError)
def New():
return DependencyPlugin()

View File

@@ -1,197 +0,0 @@
"""热插拔插件 - 运行时加载/卸载/更新插件"""
import sys
import time
import threading
from pathlib import Path
from typing import Any, Optional, Callable
from oss.logger.logger import Log
from oss.plugin.types import Plugin, register_plugin_type
class HotReloadError(Exception):
"""热插拔错误"""
pass
class FileWatcher:
"""文件监听器"""
def __init__(self, watch_dirs: list[str], extensions: list[str], on_change: Callable):
self.watch_dirs = [Path(d) for d in watch_dirs]
self.extensions = extensions
self.on_change = on_change
self._running = False
self._thread: Optional[threading.Thread] = None
self._file_times: dict[str, float] = {}
self._scan_files()
def _scan_files(self):
"""扫描当前文件及其修改时间"""
for watch_dir in self.watch_dirs:
if watch_dir.exists():
for f in watch_dir.rglob("*"):
if f.is_file() and f.suffix in self.extensions:
self._file_times[str(f)] = f.stat().st_mtime
def start(self):
"""开始监听"""
self._running = True
self._thread = threading.Thread(target=self._watch_loop, daemon=True)
self._thread.start()
def stop(self):
"""停止监听"""
self._running = False
if self._thread:
self._thread.join(timeout=5)
def _watch_loop(self):
"""监听循环"""
while self._running:
changed = []
current_files = {}
for watch_dir in self.watch_dirs:
if watch_dir.exists():
for f in watch_dir.rglob("*"):
if f.is_file() and f.suffix in self.extensions:
fpath = str(f)
mtime = f.stat().st_mtime
current_files[fpath] = mtime
# 新文件或修改过
if fpath not in self._file_times:
changed.append(("new", f))
elif mtime > self._file_times[fpath]:
changed.append(("modified", f))
# 检查删除的文件
for fpath in self._file_times:
if fpath not in current_files:
changed.append(("deleted", Path(fpath)))
if changed:
self._file_times = current_files
self.on_change(changed)
time.sleep(1)
class HotReloadPlugin(Plugin):
"""热插拔插件"""
def __init__(self):
self.plugin_loader_instance = None
self.watcher: Optional[FileWatcher] = None
self.watch_dirs: list[str] = []
self.watch_extensions: list[str] = [".py", ".json"]
def init(self, deps: dict = None):
"""初始化"""
pass
def start(self):
"""启动 - 自动开始监听默认目录"""
if not self.watch_dirs:
# 默认监听 store 目录
self.watch_dirs = ["store"]
self.start_watching()
def stop(self):
"""停止"""
if self.watcher:
self.watcher.stop()
def set_plugin_loader(self, plugin_loader):
"""设置插件加载器实例"""
self.plugin_loader_instance = plugin_loader
def set_watch_dirs(self, dirs: list[str]):
"""设置监听目录"""
self.watch_dirs = dirs
def start_watching(self):
"""开始监听文件变化"""
if self.watch_dirs and self.plugin_loader_instance:
self.watcher = FileWatcher(
self.watch_dirs,
self.watch_extensions,
self._on_file_change
)
self.watcher.start()
def _on_file_change(self, changes: list[tuple[str, Path]]):
"""文件变化回调"""
for change_type, fpath in changes:
# 只关心 main.py 和 manifest.json 的变化
if fpath.name not in ("main.py", "manifest.json"):
continue
plugin_dir = fpath.parent
plugin_name = plugin_dir.name
try:
if change_type == "new":
self.load_plugin(plugin_dir)
elif change_type == "modified":
self.reload_plugin(plugin_name, plugin_dir)
elif change_type == "deleted":
self.unload_plugin(plugin_name)
except Exception as e:
Log.error("hot-reload", f"处理变化失败: {type(e).__name__}: {e}")
def load_plugin(self, plugin_dir: Path) -> bool:
"""运行时加载插件"""
try:
plugin_name = plugin_dir.name
if plugin_name in self.plugin_loader_instance.plugins:
raise HotReloadError(f"插件已存在: {plugin_name}")
self.plugin_loader_instance.load(plugin_dir)
info = self.plugin_loader_instance.plugins[plugin_name]
instance = info["instance"]
instance.init()
instance.start()
return True
except Exception as e:
raise HotReloadError(f"加载插件失败: {e}")
def unload_plugin(self, plugin_name: str) -> bool:
"""运行时卸载插件"""
try:
if plugin_name not in self.plugin_loader_instance.plugins:
raise HotReloadError(f"插件不存在: {plugin_name}")
info = self.plugin_loader_instance.plugins[plugin_name]
instance = info["instance"]
instance.stop()
# 从模块缓存中移除
module = info.get("module")
if module and module.__name__ in sys.modules:
del sys.modules[module.__name__]
del self.plugin_loader_instance.plugins[plugin_name]
return True
except Exception as e:
raise HotReloadError(f"卸载插件失败: {e}")
def reload_plugin(self, plugin_name: str, plugin_dir: Path) -> bool:
"""运行时更新插件"""
try:
# 先卸载
self.unload_plugin(plugin_name)
# 再加载
return self.load_plugin(plugin_dir)
except Exception as e:
raise HotReloadError(f"更新插件失败: {e}")
# 注册类型
register_plugin_type("HotReloadError", HotReloadError)
register_plugin_type("FileWatcher", FileWatcher)
def New():
return HotReloadPlugin()

View File

@@ -1,59 +0,0 @@
"""HTTP 事件系统 - 请求/响应生命周期事件"""
from typing import Callable, Any, Optional
from dataclasses import dataclass, field
@dataclass
class HttpEvent:
"""HTTP 事件"""
type: str # request, response, error, etc
request: Any = None
response: Any = None
error: Exception = None
context: dict[str, Any] = field(default_factory=dict)
class HttpEventBus:
"""HTTP 事件总线"""
def __init__(self):
self._handlers: dict[str, list[Callable]] = {}
def on(self, event_type: str, handler: Callable):
"""订阅事件"""
if event_type not in self._handlers:
self._handlers[event_type] = []
self._handlers[event_type].append(handler)
def off(self, event_type: str, handler: Callable):
"""取消订阅"""
if event_type in self._handlers:
try:
self._handlers[event_type].remove(handler)
except ValueError:
pass
def emit(self, event: HttpEvent):
"""发布事件"""
handlers = self._handlers.get(event.type, [])
for handler in handlers:
try:
handler(event)
except Exception as e:
import traceback; print(f"[events.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
pass
def clear(self):
"""清空所有订阅"""
self._handlers.clear()
# 事件类型常量
EVENT_REQUEST = "http.request"
EVENT_BEFORE_ROUTE = "http.before_route"
EVENT_AFTER_ROUTE = "http.after_route"
EVENT_BEFORE_HANDLER = "http.before_handler"
EVENT_AFTER_HANDLER = "http.after_handler"
EVENT_RESPONSE = "http.response"
EVENT_ERROR = "http.error"
EVENT_COMPLETE = "http.complete"

View File

@@ -1,68 +0,0 @@
"""HTTP API 插件 - 分散式布局"""
import json
from oss.plugin.types import Plugin, register_plugin_type
from .server import HttpServer, Response
from .router import Router
from .middleware import MiddlewareChain
class HttpApiPlugin(Plugin):
"""HTTP API 插件"""
def __init__(self):
self.server = None
self.router = Router()
self.middleware = MiddlewareChain()
def init(self, deps: dict = None):
"""初始化"""
# 注册基础路由
self.router.get("/health", self._health_handler)
self.router.get("/api/server/info", self._server_info_handler)
self.router.get("/api/status", self._status_handler)
self.server = HttpServer(self.router, self.middleware)
def start(self):
"""启动"""
self.server.start()
def stop(self):
"""停止"""
if self.server:
self.server.stop()
def _health_handler(self, request):
"""健康检查"""
return Response(
status=200,
body=json.dumps({"status": "ok", "service": "http-api"}),
headers={"Content-Type": "application/json"}
)
def _server_info_handler(self, request):
"""服务器信息"""
return Response(
status=200,
body=json.dumps({
"name": "NebulaShell HTTP API",
"version": "1.0.0",
"endpoints": ["/health", "/api/server/info", "/api/status"]
}),
headers={"Content-Type": "application/json"}
)
def _status_handler(self, request):
"""状态检查"""
return Response(
status=200,
body=json.dumps({"status": "running", "plugins_loaded": True}),
headers={"Content-Type": "application/json"}
)
register_plugin_type("HttpApiPlugin", HttpApiPlugin)
def New():
return HttpApiPlugin()

View File

@@ -1,60 +0,0 @@
"""中间件链 - CORS/日志/限流等"""
from typing import Callable, Optional, Any
from .server import Request, Response
class Middleware:
"""中间件基类"""
def process(self, ctx: dict[str, Any], next_fn: Callable) -> Optional[Response]:
"""处理请求"""
return None
class CorsMiddleware(Middleware):
"""CORS 中间件"""
def process(self, ctx: dict, next_fn: Callable) -> Optional[Response]:
ctx["response_headers"] = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
}
return None
class LoggerMiddleware(Middleware):
"""日志中间件"""
# 静默的路由(不打印日志)
_silent_paths = {"/api/dashboard/stats", "/favicon.ico", "/health"}
def process(self, ctx: dict, next_fn: Callable) -> Optional[Response]:
req = ctx.get("request")
if req and req.path not in self._silent_paths:
print(f"[http-api] {req.method} {req.path}")
return None
class MiddlewareChain:
"""中间件链"""
def __init__(self):
self.middlewares: list[Middleware] = []
self.add(LoggerMiddleware())
self.add(CorsMiddleware())
def add(self, middleware: Middleware):
"""添加中间件"""
self.middlewares.append(middleware)
def run(self, ctx: dict[str, Any]) -> Optional[Response]:
"""执行中间件链"""
idx = 0
def next_fn():
nonlocal idx
if idx < len(self.middlewares):
mw = self.middlewares[idx]
idx += 1
return mw.process(ctx, next_fn)
return None
return next_fn()

View File

@@ -1,18 +0,0 @@
"""路由器 - 路径匹配和处理器分发"""
from typing import Callable, Optional
from oss.shared.router import BaseRouter, match_path
from .server import Request, Response
class Router(BaseRouter):
"""HTTP API 路由器"""
def handle(self, request: Request) -> Response:
"""处理请求"""
result = self.find_route(request.method, request.path)
if result:
route, params = result
# 将路径参数注入到请求中
request.path_params = params
return route.handler(request)
return Response(status=404, body='{"error": "Not Found"}')

View File

@@ -1,34 +0,0 @@
"""HTTP TCP 插件入口"""
from oss.plugin.types import Plugin, register_plugin_type
from .server import TcpHttpServer
from .router import TcpRouter
from .middleware import TcpMiddlewareChain
class HttpTcpPlugin(Plugin):
"""HTTP TCP 插件"""
def __init__(self):
self.server = None
self.router = TcpRouter()
self.middleware = TcpMiddlewareChain()
def init(self, deps: dict = None):
"""初始化"""
self.server = TcpHttpServer(self.router, self.middleware)
def start(self):
"""启动"""
self.server.start()
def stop(self):
"""停止"""
if self.server:
self.server.stop()
register_plugin_type("HttpTcpPlugin", HttpTcpPlugin)
def New():
return HttpTcpPlugin()

View File

@@ -1,21 +0,0 @@
"""TCP HTTP 路由器"""
from typing import Callable, Optional, Any
from oss.shared.router import BaseRouter, match_path
class TcpRouter(BaseRouter):
"""TCP HTTP 路由器"""
def handle(self, request: dict) -> dict:
"""处理请求"""
method = request.get("method", "GET")
path = request.get("path", "/")
result = self.find_route(method, path)
if result:
route, params = result
# 将路径参数注入到请求中
request["path_params"] = params
return route.handler(request)
return {"status": 404, "headers": {}, "body": "Not Found"}

View File

@@ -1,237 +0,0 @@
"""TCP HTTP 服务器核心"""
import socket
import threading
import re
from typing import Any, Callable, Optional
from oss.config import get_config
from .events import TcpEvent, EVENT_CONNECT, EVENT_DISCONNECT, EVENT_DATA, EVENT_REQUEST, EVENT_RESPONSE
class TcpClient:
"""TCP 客户端连接"""
def __init__(self, conn: socket.socket, address: tuple):
self.conn = conn
self.address = address
self.id = f"{address[0]}:{address[1]}"
def send(self, data: bytes):
"""发送数据"""
self.conn.sendall(data)
def close(self):
"""关闭连接"""
self.conn.close()
class TcpHttpServer:
"""TCP HTTP 服务器"""
def __init__(self, router, middleware, event_bus=None, host=None, port=None):
config = get_config()
self.host = host or config.get("HOST", "0.0.0.0")
self.port = port or config.get("HTTP_TCP_PORT", 8082)
self.router = router
self.middleware = middleware
self.event_bus = event_bus
self._server = None
self._thread = None
self._running = False
self._clients: dict[str, TcpClient] = {}
def start(self):
"""启动服务器"""
self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._server.bind((self.host, self.port))
self._server.listen(128)
self._running = True
self._thread = threading.Thread(target=self._accept_loop, daemon=True)
self._thread.start()
print(f"[http-tcp] 服务器启动: {self.host}:{self.port}")
def _accept_loop(self):
"""接受连接循环"""
while self._running:
try:
conn, address = self._server.accept()
client = TcpClient(conn, address)
self._clients[client.id] = client
# 触发连接事件
if self.event_bus:
self.event_bus.emit(TcpEvent(type=EVENT_CONNECT, client=client))
# 启动处理线程
t = threading.Thread(target=self._handle_client, args=(client,), daemon=True)
t.start()
except Exception as e:
if self._running:
print(f"[http-tcp] 接受连接失败: {e}")
def _handle_client(self, client: TcpClient):
"""处理客户端请求"""
buffer = b""
try:
while self._running:
data = client.conn.recv(4096)
if not data:
break
buffer += data
# 检查 HTTP 请求头是否完整
if b"\r\n\r\n" in buffer:
# 先解析请求头以获取 Content-Length
header_end = buffer.find(b"\r\n\r\n")
header_text = buffer[:header_end].decode("utf-8", errors="replace")
# 从请求头中提取 Content-Length
content_length = 0
for line in header_text.split("\r\n")[1:]:
if line.lower().startswith("content-length:"):
content_length = int(line.split(":", 1)[1].strip())
break
# 计算 body 起始位置
body_start_pos = header_end + 4 # \r\n\r\n
body_received = len(buffer) - body_start_pos
# 等待完整 body
if body_received < content_length:
# 继续接收剩余数据
while body_received < content_length:
remaining = content_length - body_received
chunk = client.conn.recv(min(4096, remaining))
if not chunk:
break
buffer += chunk
body_received += len(chunk)
# 现在解析完整请求
request = self._parse_request(buffer)
if request:
# 触发请求事件
if self.event_bus:
self.event_bus.emit(TcpEvent(
type=EVENT_REQUEST,
client=client,
context={"request": request}
))
# 路由处理
response = self.router.handle(request)
# 发送响应
response_bytes = self._format_response(response)
client.send(response_bytes)
# 触发响应事件
if self.event_bus:
self.event_bus.emit(TcpEvent(
type=EVENT_RESPONSE,
client=client,
data=response_bytes
))
buffer = b""
except ConnectionResetError:
# 客户端断开连接,正常情况
pass
except BrokenPipeError:
# 管道破裂,正常情况
pass
except OSError as e:
if self.event_bus:
self.event_bus.emit(TcpEvent(type=EVENT_ERROR, client=client, context={"error": f"OSError: {e}"}))
except Exception as e:
if self.event_bus:
self.event_bus.emit(TcpEvent(type=EVENT_ERROR, client=client, context={"error": f"{type(e).__name__}: {e}"}))
finally:
del self._clients[client.id]
client.close()
if self.event_bus:
self.event_bus.emit(TcpEvent(type=EVENT_DISCONNECT, client=client))
def _parse_request(self, data: bytes) -> Optional[dict]:
"""解析 HTTP 请求"""
try:
text = data.decode("utf-8", errors="replace")
lines = text.split("\r\n")
if not lines:
return None
# 解析请求行
match = re.match(r'(\w+)\s+(\S+)\s+HTTP/(\d\.\d)', lines[0])
if not match:
return None
method, path, version = match.groups()
# 解析头
headers = {}
body_start = 0
for i, line in enumerate(lines[1:], 1):
if line == "":
body_start = i + 1
break
if ":" in line:
key, value = line.split(":", 1)
headers[key.strip()] = value.strip()
# 解析体
content_length = int(headers.get("Content-Length", 0))
body = "\r\n".join(lines[body_start:]) if body_start else ""
return {
"method": method,
"path": path,
"version": version,
"headers": headers,
"body": body,
}
except UnicodeDecodeError:
return None
except ValueError:
return None
except Exception as e:
# 其他解析错误
import traceback; print(f"[http-tcp] HTTP 解析失败:{type(e).__name__}: {e}"); traceback.print_exc()
return None
def _format_response(self, response: dict) -> bytes:
"""格式化 HTTP 响应"""
status = response.get("status", 200)
headers = response.get("headers", {})
body = response.get("body", "")
status_text = {200: "OK", 404: "Not Found", 500: "Internal Server Error"}.get(status, "OK")
response_lines = [
f"HTTP/1.1 {status} {status_text}",
]
if "Content-Type" not in headers:
headers["Content-Type"] = "text/plain; charset=utf-8"
headers["Content-Length"] = str(len(body))
for key, value in headers.items():
response_lines.append(f"{key}: {value}")
response_lines.append("")
response_lines.append(body)
return "\r\n".join(response_lines).encode("utf-8")
def stop(self):
"""停止服务器"""
self._running = False
for client in self._clients.values():
client.close()
if self._server:
self._server.close()
print("[http-tcp] 服务器已停止")
def get_clients(self) -> list[TcpClient]:
"""获取所有客户端"""
return list(self._clients.values())

View File

@@ -1 +0,0 @@
"""i18n 国际化多语言支持插件"""

View File

@@ -1,162 +0,0 @@
"""JSON 编解码器 - 插件间 JSON 数据处理"""
import json
from typing import Any, Callable, Optional
from datetime import datetime
from oss.logger.logger import Log
from oss.plugin.types import Plugin, register_plugin_type
class JsonCodecError(Exception):
"""JSON 编解码错误"""
pass
class JsonSerializer:
"""JSON 序列化器"""
def __init__(self):
self._custom_encoders: dict[type, Callable] = {}
def register_encoder(self, type_class: type, encoder: Callable):
"""注册自定义类型编码器"""
self._custom_encoders[type_class] = encoder
def encode(self, data: Any, pretty: bool = False) -> str:
"""编码为 JSON 字符串"""
def default_handler(obj):
if isinstance(obj, datetime):
return obj.isoformat()
for type_class, encoder in self._custom_encoders.items():
if isinstance(obj, type_class):
return encoder(obj)
raise TypeError(f"无法序列化类型: {type(obj).__name__}")
if pretty:
return json.dumps(data, ensure_ascii=False, indent=2, default=default_handler)
return json.dumps(data, ensure_ascii=False, default=default_handler)
def encode_to_bytes(self, data: Any) -> bytes:
"""编码为字节"""
return self.encode(data).encode("utf-8")
class JsonDeserializer:
"""JSON 反序列化器"""
def __init__(self):
self._custom_decoders: dict[str, Callable] = {}
def register_decoder(self, type_name: str, decoder: Callable):
"""注册自定义类型解码器"""
self._custom_decoders[type_name] = decoder
def decode(self, text: str) -> Any:
"""解码 JSON 字符串"""
try:
return json.loads(text)
except json.JSONDecodeError as e:
raise JsonCodecError(f"JSON 解码失败: {e}")
def decode_bytes(self, data: bytes) -> Any:
"""解码字节"""
return self.decode(data.decode("utf-8"))
def decode_file(self, path: str) -> Any:
"""解码 JSON 文件"""
with open(path, "r", encoding="utf-8") as f:
return self.decode(f.read())
class JsonValidator:
"""JSON 验证器"""
def __init__(self):
self._schemas: dict[str, dict] = {}
def register_schema(self, name: str, schema: dict):
"""注册 schema"""
self._schemas[name] = schema
def validate(self, data: Any, schema_name: str) -> bool:
"""验证数据是否符合 schema"""
if schema_name not in self._schemas:
raise JsonCodecError(f"未知的 schema: {schema_name}")
return self._check_schema(data, self._schemas[schema_name])
def _check_schema(self, data: Any, schema: dict) -> bool:
"""检查 schema 匹配"""
schema_type = schema.get("type")
if schema_type == "object":
if not isinstance(data, dict):
return False
required = schema.get("required", [])
for field in required:
if field not in data:
return False
properties = schema.get("properties", {})
for key, value in data.items():
if key in properties:
if not self._check_schema(value, properties[key]):
return False
return True
elif schema_type == "array":
if not isinstance(data, list):
return False
items_schema = schema.get("items", {})
return all(self._check_schema(item, items_schema) for item in data)
elif schema_type == "string":
return isinstance(data, str)
elif schema_type == "number":
return isinstance(data, (int, float))
elif schema_type == "boolean":
return isinstance(data, bool)
return True
class JsonCodecPlugin(Plugin):
"""JSON 编解码器插件"""
def __init__(self):
self.serializer = JsonSerializer()
self.deserializer = JsonDeserializer()
self.validator = JsonValidator()
def init(self, deps: dict = None):
"""初始化"""
pass
def start(self):
"""启动"""
Log.info("json-codec", "JSON 编解码器已启动")
def stop(self):
"""停止"""
pass
def encode(self, data: Any, pretty: bool = False) -> str:
"""编码 JSON"""
return self.serializer.encode(data, pretty)
def decode(self, text: str) -> Any:
"""解码 JSON"""
return self.deserializer.decode(text)
def validate(self, data: Any, schema_name: str) -> bool:
"""验证 JSON schema"""
return self.validator.validate(data, schema_name)
def register_schema(self, name: str, schema: dict):
"""注册 schema"""
self.validator.register_schema(name, schema)
# 注册类型
register_plugin_type("JsonSerializer", JsonSerializer)
register_plugin_type("JsonDeserializer", JsonDeserializer)
register_plugin_type("JsonValidator", JsonValidator)
register_plugin_type("JsonCodecError", JsonCodecError)
def New():
return JsonCodecPlugin()

View File

@@ -1,150 +0,0 @@
"""生命周期插件 - 管理插件生命周期状态"""
from enum import Enum
from typing import Optional, Callable, Any
from oss.plugin.types import Plugin, register_plugin_type
class LifecycleState(str, Enum):
"""生命周期状态"""
PENDING = "pending"
RUNNING = "running"
STOPPED = "stopped"
class LifecycleError(Exception):
"""生命周期错误"""
pass
class Lifecycle:
"""生命周期管理器"""
VALID_TRANSITIONS = {
LifecycleState.PENDING: [LifecycleState.RUNNING],
LifecycleState.RUNNING: [LifecycleState.STOPPED],
LifecycleState.STOPPED: [LifecycleState.RUNNING],
}
def __init__(self, name: str):
self.name = name
self.state = LifecycleState.PENDING
self._hooks: dict[str, list[Callable]] = {
"before_start": [],
"after_start": [],
"before_stop": [],
"after_stop": [],
}
self._extensions: dict[str, Any] = {} # 扩展能力
def add_extension(self, name: str, extension: Any):
"""添加扩展能力"""
self._extensions[name] = extension
def get_extension(self, name: str) -> Optional[Any]:
"""获取扩展能力"""
return self._extensions.get(name)
def transition(self, target_state: LifecycleState):
"""状态转换"""
if target_state not in self.VALID_TRANSITIONS.get(self.state, []):
raise LifecycleError(
f"插件 '{self.name}' 无法从 {self.state.value} 转换到 {target_state.value}"
)
old_state = self.state
self.state = target_state
def start(self):
"""启动"""
for hook in self._hooks["before_start"]:
hook(self)
self.transition(LifecycleState.RUNNING)
for hook in self._hooks["after_start"]:
hook(self)
def stop(self):
"""停止"""
for hook in self._hooks["before_stop"]:
hook(self)
self.transition(LifecycleState.STOPPED)
for hook in self._hooks["after_stop"]:
hook(self)
def restart(self):
"""重启"""
if self.state == LifecycleState.RUNNING:
self.stop()
self.start()
def on(self, event: str, hook: Callable):
"""注册钩子"""
if event in self._hooks:
self._hooks[event].append(hook)
def is_running(self) -> bool:
return self.state == LifecycleState.RUNNING
def is_stopped(self) -> bool:
return self.state == LifecycleState.STOPPED
def is_pending(self) -> bool:
return self.state == LifecycleState.PENDING
def __repr__(self):
return f"Lifecycle({self.name}, state={self.state.value})"
class LifecyclePlugin(Plugin):
"""生命周期插件"""
def __init__(self):
self.lifecycles: dict[str, Lifecycle] = {}
def init(self, deps: dict = None):
"""初始化"""
pass
def start(self):
"""启动"""
pass
def stop(self):
"""停止"""
pass
def create(self, name: str) -> Lifecycle:
"""创建生命周期"""
lifecycle = Lifecycle(name)
self.lifecycles[name] = lifecycle
return lifecycle
def get(self, name: str) -> Optional[Lifecycle]:
"""获取生命周期"""
return self.lifecycles.get(name)
def start_all(self):
"""启动所有"""
for lc in self.lifecycles.values():
try:
lc.start()
except LifecycleError:
pass
def stop_all(self):
"""停止所有"""
for lc in self.lifecycles.values():
try:
lc.stop()
except LifecycleError:
pass
# 注册类型
register_plugin_type("Lifecycle", Lifecycle)
register_plugin_type("LifecycleState", LifecycleState)
register_plugin_type("LifecycleError", LifecycleError)
def New():
return LifecyclePlugin()

View File

@@ -1,838 +0,0 @@
"""LogTerminal 日志与终端插件"""
import os
import json
import subprocess
import threading
import time
from oss.logger.logger import Log
from oss.plugin.types import Plugin, Response, register_plugin_type
class LogTerminalPlugin(Plugin):
"""日志与终端插件 - 提供日志查看和 SSH 终端功能"""
def __init__(self):
self.webui = None
self.http_api = None
self.views_dir = os.path.join(os.path.dirname(__file__), 'views')
self._log_buffer = []
self._log_lock = threading.Lock()
self._ssh_sessions = {}
self._session_counter = 0
self._log_sync_thread = None
self._running = False
def meta(self):
from oss.plugin.types import Metadata, PluginConfig, Manifest
return Manifest(
metadata=Metadata(
name="log-terminal",
version="1.0.0",
author="NebulaShell",
description="日志查看器与 SSH 终端"
),
config=PluginConfig(enabled=True, args={}),
dependencies=["http-api", "webui"]
)
def set_webui(self, webui):
self.webui = webui
def set_http_api(self, http_api):
self.http_api = http_api
def init(self, deps: dict = None):
if self.webui:
Log.info("log-terminal", "已获取 WebUI 引用")
# 注册日志查看页面
self.webui.register_page(
path='/logs',
content_provider=self._render_logs,
nav_item={'icon': 'ri-file-list-3-line', 'text': '日志'}
)
# 注册终端页面
self.webui.register_page(
path='/terminal',
content_provider=self._render_terminal,
nav_item={'icon': 'ri-terminal-box-line', 'text': '终端'}
)
Log.ok("log-terminal", "已注册日志与终端页面到 WebUI 导航")
else:
Log.warn("log-terminal", "警告: 未找到 WebUI 依赖")
# 注册 API 路由(通过 http-api
if self.http_api and self.http_api.router:
self.http_api.router.get("/api/logs/get", self._handle_get_logs)
self.http_api.router.post("/api/terminal/connect", self._handle_connect_ssh)
self.http_api.router.post("/api/terminal/send", self._handle_send_command)
self.http_api.router.post("/api/terminal/disconnect", self._handle_disconnect_ssh)
self.http_api.router.get("/api/terminal/sessions", self._handle_list_sessions)
Log.ok("log-terminal", "已注册 API 路由")
else:
Log.warn("log-terminal", "警告: 未找到 http-api 依赖")
def start(self):
Log.info("log-terminal", "日志与终端插件启动中...")
self._running = True
# 启动日志同步线程
self._log_sync_thread = threading.Thread(target=self._log_sync_worker, daemon=True)
self._log_sync_thread.start()
# 添加初始化日志
self.add_log_entry("info", "log-terminal", "日志与终端插件已启动")
self.add_log_entry("tip", "log-terminal", "日志查看: /logs | SSH 终端: /terminal")
# 尝试捕获系统日志输出
self._hook_system_log()
Log.ok("log-terminal", "日志与终端插件已启动")
def _hook_system_log(self):
"""拦截系统日志输出到我们的缓冲区"""
try:
from oss.logger.logger import Log as SystemLog
# 保存原始方法
original_info = SystemLog.info
original_warn = SystemLog.warn
original_error = SystemLog.error
original_tip = SystemLog.tip
original_ok = SystemLog.ok
# 创建包装方法
plugin_instance = self
@classmethod
def wrapped_info(cls, tag: str, msg: str):
original_info(tag, msg)
plugin_instance.add_log_entry("info", tag, msg)
@classmethod
def wrapped_warn(cls, tag: str, msg: str):
original_warn(tag, msg)
plugin_instance.add_log_entry("warn", tag, msg)
@classmethod
def wrapped_error(cls, tag: str, msg: str):
original_error(tag, msg)
plugin_instance.add_log_entry("error", tag, msg)
@classmethod
def wrapped_tip(cls, tag: str, msg: str):
original_tip(tag, msg)
plugin_instance.add_log_entry("tip", tag, msg)
@classmethod
def wrapped_ok(cls, tag: str, msg: str):
original_ok(tag, msg)
plugin_instance.add_log_entry("ok", tag, msg)
# 替换方法(注意:这只影响未来的调用)
SystemLog.info = wrapped_info
SystemLog.warn = wrapped_warn
SystemLog.error = wrapped_error
SystemLog.tip = wrapped_tip
SystemLog.ok = wrapped_ok
Log.info("log-terminal", "系统日志拦截器已安装")
except Exception as e:
Log.warn("log-terminal", f"无法拦截系统日志: {e}")
def stop(self):
Log.info("log-terminal", "日志与终端插件停止中...")
self._running = False
# 关闭所有 SSH 会话
for session_id, session in list(self._ssh_sessions.items()):
try:
if 'process' in session:
session['process'].terminate()
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
pass
self._ssh_sessions.clear()
Log.ok("log-terminal", "日志与终端插件已停止")
def _log_sync_worker(self):
"""日志同步工作线程 - 持续捕获项目日志"""
try:
# 尝试从多个位置读取日志
log_files = [
'/var/log/syslog',
'/var/log/messages',
os.path.join(os.path.dirname(__file__), '..', '..', 'data', 'system.log'),
]
last_positions = {}
while self._running:
# 检查日志文件
for log_file in log_files:
if os.path.exists(log_file) and os.path.isfile(log_file):
try:
with open(log_file, 'r', encoding='utf-8', errors='ignore') as f:
# 获取文件位置
if log_file not in last_positions:
# 首次读取,跳到文件末尾
f.seek(0, 2) # 2 = SEEK_END
last_positions[log_file] = f.tell()
else:
f.seek(last_positions[log_file])
# 读取新行
lines = f.readlines()
if lines:
last_positions[log_file] = f.tell()
for line in lines[-50:]: # 每次最多读取50行
line = line.strip()
if line:
self.add_log_entry("info", "system", line)
except Exception as e:
import traceback
traceback.print_exc()
# 等待下一次同步
time.sleep(2)
except Exception as e:
Log.error("log-terminal", f"日志同步线程异常: {type(e).__name__}: {e}")
def add_log_entry(self, level: str, tag: str, message: str):
"""向日志缓冲区添加日志条目"""
import time
timestamp = time.strftime('%Y-%m-%d %H:%M:%S')
entry = {
'timestamp': timestamp,
'level': level,
'tag': tag,
'message': message
}
with self._log_lock:
self._log_buffer.append(entry)
# 限制日志缓冲区大小
if len(self._log_buffer) > 10000:
self._log_buffer = self._log_buffer[-5000:]
def _get_logs(self, limit=100):
"""获取日志列表"""
with self._log_lock:
return self._log_buffer[-limit:]
def _check_ssh_installed(self):
"""检查 SSH 是否已安装"""
try:
result = subprocess.run(['which', 'sshd'], capture_output=True, text=True, timeout=5)
return result.returncode == 0
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
return False
def _install_ssh(self):
"""自动安装 SSH 服务"""
try:
Log.info("log-terminal", "正在安装 SSH 服务...")
# 检测包管理器
for pkg_manager in ['apt-get', 'yum', 'dnf', 'pacman']:
result = subprocess.run(['which', pkg_manager], capture_output=True, timeout=3)
if result.returncode == 0:
if pkg_manager == 'apt-get':
subprocess.run([pkg_manager, 'update'], capture_output=True, timeout=30)
result = subprocess.run(
[pkg_manager, 'install', '-y', 'openssh-server'],
capture_output=True, text=True, timeout=120
)
elif pkg_manager in ['yum', 'dnf']:
result = subprocess.run(
[pkg_manager, 'install', '-y', 'openssh-server'],
capture_output=True, text=True, timeout=120
)
elif pkg_manager == 'pacman':
result = subprocess.run(
[pkg_manager, '-S', '--noconfirm', 'openssh'],
capture_output=True, text=True, timeout=120
)
if result.returncode == 0:
Log.ok("log-terminal", "SSH 服务安装成功")
return True
else:
Log.error("log-terminal", f"SSH 服务安装失败: {result.stderr}")
return False
Log.error("log-terminal", "未找到支持的包管理器")
return False
except Exception as e:
Log.error("log-terminal", f"安装 SSH 服务时出错: {type(e).__name__}: {e}")
return False
def _start_ssh_server(self, port=8022):
"""启动 SSH 服务器"""
try:
# 检查 SSH 服务器是否已在运行
result = subprocess.run(['pgrep', '-f', 'sshd'], capture_output=True, timeout=3)
if result.returncode == 0:
Log.tip("log-terminal", "SSH 服务器已在运行")
return True
# 启动 SSH 服务器
Log.info("log-terminal", f"正在启动 SSH 服务器 (端口: {port})...")
subprocess.run(['sshd', '-p', str(port)], capture_output=True, timeout=10)
# 验证是否启动成功
time.sleep(1)
result = subprocess.run(['pgrep', '-f', f'sshd.*{port}'], capture_output=True, timeout=3)
if result.returncode == 0:
Log.ok("log-terminal", f"SSH 服务器已启动 (端口: {port})")
return True
else:
Log.error("log-terminal", "SSH 服务器启动失败")
return False
except Exception as e:
Log.error("log-terminal", f"启动 SSH 服务器时出错: {type(e).__name__}: {e}")
return False
def _handle_connect_ssh(self, request):
"""处理 SSH 连接请求"""
try:
body = json.loads(request.body)
port = body.get('port', 8022)
auto_install = body.get('auto_install', True)
# 检查 SSH 是否已安装
if not self._check_ssh_installed():
if auto_install:
if not self._install_ssh():
return Response(
status=500,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': 'SSH 安装失败'})
)
else:
return Response(
status=400,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': 'SSH 未安装,请先安装 SSH 服务'})
)
# 启动 SSH 服务器
if not self._start_ssh_server(port):
return Response(
status=500,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': 'SSH 服务器启动失败'})
)
# 创建新的终端会话 (使用 script 命令创建伪终端)
self._session_counter += 1
session_id = self._session_counter
try:
# 使用 script 命令创建交互式终端
process = subprocess.Popen(
['script', '-q', '-c', '/bin/bash', '/dev/null'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
self._ssh_sessions[session_id] = {
'process': process,
'created_at': time.time(),
'port': port
}
Log.info("log-terminal", f"SSH 终端会话 #{session_id} 已创建")
return Response(
status=200,
headers={"Content-Type": "application/json"},
body=json.dumps({
'success': True,
'session_id': session_id,
'message': 'SSH 终端已连接'
})
)
except Exception as e:
Log.error("log-terminal", f"创建终端会话失败: {type(e).__name__}: {e}")
return Response(
status=500,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': str(e)})
)
except Exception as e:
Log.error("log-terminal", f"SSH 连接请求异常: {type(e).__name__}: {e}")
return Response(
status=500,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': str(e)})
)
def _handle_send_command(self, request):
"""处理发送命令到终端"""
try:
body = json.loads(request.body)
session_id = body.get('session_id')
command = body.get('command', '')
if session_id not in self._ssh_sessions:
return Response(
status=400,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': '会话不存在'})
)
session = self._ssh_sessions[session_id]
process = session['process']
# 发送命令
process.stdin.write(command + '\n')
process.stdin.flush()
# 读取输出
time.sleep(0.5) # 等待命令执行
output = ""
try:
while True:
line = process.stdout.readline()
if not line:
break
output += line
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
pass
return Response(
status=200,
headers={"Content-Type": "application/json"},
body=json.dumps({
'success': True,
'output': output
})
)
except Exception as e:
Log.error("log-terminal", f"发送命令时出错: {type(e).__name__}: {e}")
return Response(
status=500,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': str(e)})
)
def _handle_disconnect_ssh(self, request):
"""处理断开 SSH 连接"""
try:
body = json.loads(request.body)
session_id = body.get('session_id')
if session_id in self._ssh_sessions:
session = self._ssh_sessions[session_id]
try:
session['process'].terminate()
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
pass
del self._ssh_sessions[session_id]
Log.info("log-terminal", f"SSH 终端会话 #{session_id} 已断开")
return Response(
status=200,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': True, 'message': '已断开连接'})
)
else:
return Response(
status=400,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': '会话不存在'})
)
except Exception as e:
Log.error("log-terminal", f"断开连接时出错: {type(e).__name__}: {e}")
return Response(
status=500,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': str(e)})
)
def _handle_list_sessions(self, request):
"""列出所有 SSH 会话"""
try:
sessions = []
for session_id, session in self._ssh_sessions.items():
sessions.append({
'session_id': session_id,
'port': session['port'],
'created_at': session['created_at'],
'uptime': time.time() - session['created_at']
})
return Response(
status=200,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': True, 'sessions': sessions})
)
except Exception as e:
return Response(
status=500,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': str(e)})
)
def _handle_get_logs(self, request):
"""获取日志"""
try:
from urllib.parse import parse_qs, urlparse
# 解析路径中的查询参数
parsed = urlparse(request.path)
params = parse_qs(parsed.query)
limit = int(params.get('limit', [100])[0])
source = params.get('source', ['buffer'])[0] # buffer 或 file
logs = []
if source == 'buffer':
# 从内存缓冲区获取
logs = self._get_logs(limit)
else:
# 从系统日志文件获取
logs = self._read_system_logs(limit)
return Response(
status=200,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': True, 'logs': logs})
)
except Exception as e:
return Response(
status=500,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': str(e)})
)
def _read_system_logs(self, limit=100):
"""从系统日志文件读取日志"""
logs = []
log_files = [
'/var/log/syslog',
'/var/log/messages',
'/var/log/kern.log',
]
for log_file in log_files:
if os.path.exists(log_file):
try:
with open(log_file, 'r', encoding='utf-8', errors='ignore') as f:
lines = f.readlines()
for line in lines[-limit:]:
line = line.strip()
if line:
# 尝试解析 syslog 格式
# 格式: "Apr 12 10:30:45 hostname service[pid]: message"
import re
match = re.match(r'(\w+\s+\d+\s+\d+:\d+:\d+)\s+(\S+)\s+(\S+?)(?:\[\d+\])?:\s+(.*)', line)
if match:
logs.append({
'timestamp': match.group(1),
'level': 'info',
'tag': match.group(3),
'message': match.group(4)
})
else:
logs.append({
'timestamp': time.strftime('%b %d %H:%M:%S'),
'level': 'info',
'tag': 'system',
'message': line
})
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
pass
return logs[-limit:]
def _render_logs(self) -> str:
"""渲染日志查看界面 - 纯 HTML/Python 模板"""
try:
logs = self._get_logs(limit=100)
log_rows = ""
for log in logs:
level_class = {
'info': 'log-info',
'warn': 'log-warn',
'error': 'log-error',
'ok': 'log-ok',
'tip': 'log-tip'
}.get(log['level'], 'log-info')
log_rows += f"""
<tr class="{level_class}">
<td>{log['timestamp']}</td>
<td><span class="badge badge-{log['level']}">{log['level']}</span></td>
<td>{log['tag']}</td>
<td>{log['message']}</td>
</tr>"""
html = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>系统日志</title>
<link rel="stylesheet" href="/assets/remixicon.css">
<style>
* {{ margin: 0; padding: 0; box-sizing: border-box; }}
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f6fa; padding: 20px; }}
.container {{ max-width: 1400px; margin: 0 auto; }}
.card {{ background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 20px; margin-bottom: 20px; }}
.card-header {{ display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }}
.card-title {{ font-size: 18px; font-weight: 600; color: #2c3e50; }}
.btn {{ padding: 8px 16px; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; transition: all 0.3s; }}
.btn-primary {{ background: #3498db; color: white; }}
.btn-primary:hover {{ background: #2980b9; }}
.btn-success {{ background: #27ae60; color: white; }}
.btn-success:hover {{ background: #229954; }}
table {{ width: 100%; border-collapse: collapse; }}
th, td {{ padding: 12px; text-align: left; border-bottom: 1px solid #ecf0f1; }}
th {{ background: #f8f9fa; font-weight: 600; color: #2c3e50; position: sticky; top: 0; }}
tr:hover {{ background: #f8f9fa; }}
.log-info {{ border-left: 3px solid #3498db; }}
.log-warn {{ border-left: 3px solid #f39c12; }}
.log-error {{ border-left: 3px solid #e74c3c; }}
.log-ok {{ border-left: 3px solid #27ae60; }}
.log-tip {{ border-left: 3px solid #9b59b6; }}
.badge {{ padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: 600; text-transform: uppercase; }}
.badge-info {{ background: #d6eaf8; color: #3498db; }}
.badge-warn {{ background: #fdebd0; color: #f39c12; }}
.badge-error {{ background: #fadbd8; color: #e74c3c; }}
.badge-ok {{ background: #d5f5e3; color: #27ae60; }}
.badge-tip {{ background: #ebdef0; color: #9b59b6; }}
.log-table-container {{ max-height: 600px; overflow-y: auto; }}
.refresh-indicator {{ font-size: 12px; color: #7f8c8d; }}
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header">
<h2 class="card-title"><i class="ri-file-list-3-line"></i> 系统日志</h2>
<div>
<button class="btn btn-primary" onclick="loadLogs()"><i class="ri-refresh-line"></i> 刷新</button>
<button class="btn btn-success" onclick="clearLogs()"><i class="ri-delete-bin-line"></i> 清空</button>
</div>
</div>
<div class="log-table-container">
<table>
<thead>
<tr>
<th>时间</th>
<th>级别</th>
<th>标签</th>
<th>消息</th>
</tr>
</thead>
<tbody id="log-body">
{log_rows}
</tbody>
</table>
</div>
<p class="refresh-indicator">最后更新:{logs[-1]['timestamp'] if logs else '无数据'}</p>
</div>
</div>
<script>
function loadLogs() {{
fetch('/api/logs/get?limit=100')
.then(r => r.json())
.then(data => {{
if (data.success) {{
location.reload();
}}
}});
}}
function clearLogs() {{
if (confirm('确定要清空日志吗?')) {{
fetch('/api/logs/clear', {{ method: 'POST' }})
.then(r => r.json())
.then(data => {{
if (data.success) {{
location.reload();
}}
}});
}}
}}
// 自动刷新
setTimeout(loadLogs, 30000);
</script>
</body>
</html>"""
return html
except Exception as e:
return f"<p>日志视图渲染出错:{e}</p>"
def _render_terminal(self) -> str:
"""渲染终端界面 - 纯 HTML/Python 模板"""
try:
html = """<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SSH 终端</title>
<link rel="stylesheet" href="/assets/remixicon.css">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #1a1a2e; color: #eee; padding: 20px; height: 100vh; display: flex; flex-direction: column; }
.container { max-width: 1400px; margin: 0 auto; width: 100%; flex: 1; display: flex; flex-direction: column; }
.card { background: #16213e; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.3); padding: 20px; margin-bottom: 20px; }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.card-title { font-size: 18px; font-weight: 600; color: #fff; }
.btn { padding: 8px 16px; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; transition: all 0.3s; }
.btn-primary { background: #0f3460; color: #e94560; }
.btn-primary:hover { background: #1a4a7a; }
.btn-danger { background: #e94560; color: white; }
.btn-danger:hover { background: #c0394d; }
.terminal-container { flex: 1; background: #0f0f1a; border-radius: 8px; padding: 15px; font-family: 'Courier New', monospace; font-size: 14px; overflow: hidden; display: flex; flex-direction: column; }
.terminal-output { flex: 1; overflow-y: auto; white-space: pre-wrap; word-wrap: break-word; color: #0f0; }
.terminal-input { display: flex; margin-top: 10px; }
.terminal-input input { flex: 1; background: #1a1a2e; border: 1px solid #0f3460; color: #0f0; padding: 10px; font-family: 'Courier New', monospace; font-size: 14px; border-radius: 4px; outline: none; }
.terminal-input input:focus { border-color: #e94560; }
.status-bar { display: flex; justify-content: space-between; padding: 10px; background: #16213e; border-radius: 6px; margin-bottom: 15px; }
.status-item { display: flex; align-items: center; gap: 8px; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; }
.status-connected { background: #27ae60; }
.status-disconnected { background: #e74c3c; }
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: #1a1a2e; }
::-webkit-scrollbar-thumb { background: #0f3460; border-radius: 4px; }
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header">
<h2 class="card-title"><i class="ri-terminal-box-line"></i> SSH 终端</h2>
<div>
<button class="btn btn-primary" id="connectBtn" onclick="connectTerminal()">连接</button>
<button class="btn btn-danger" id="disconnectBtn" onclick="disconnectTerminal()" style="display:none;">断开</button>
</div>
</div>
<div class="status-bar">
<div class="status-item">
<span class="status-dot status-disconnected" id="statusDot"></span>
<span id="statusText">未连接</span>
</div>
<div class="status-item">
<span>会话 ID: <strong id="sessionId">-</strong></span>
</div>
</div>
</div>
<div class="terminal-container">
<div class="terminal-output" id="terminalOutput">欢迎使用 SSH 终端!点击"连接"按钮开始...</div>
<div class="terminal-input">
<input type="text" id="commandInput" placeholder="输入命令..." disabled onkeypress="handleKeyPress(event)">
</div>
</div>
</div>
<script>
let sessionId = null;
const output = document.getElementById('terminalOutput');
const input = document.getElementById('commandInput');
const connectBtn = document.getElementById('connectBtn');
const disconnectBtn = document.getElementById('disconnectBtn');
const statusDot = document.getElementById('statusDot');
const statusText = document.getElementById('statusText');
const sessionIdEl = document.getElementById('sessionId');
function connectTerminal() {
output.textContent = '正在连接...';
fetch('/api/terminal/connect', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({port: 8022, auto_install: true})
})
.then(r => r.json())
.then(data => {
if (data.success) {
sessionId = data.session_id;
sessionIdEl.textContent = sessionId;
statusDot.className = 'status-dot status-connected';
statusText.textContent = '已连接';
input.disabled = false;
connectBtn.style.display = 'none';
disconnectBtn.style.display = 'inline-block';
output.textContent = 'SSH 终端已连接。输入命令开始使用...
';
input.focus();
} else {
output.textContent = '连接失败:' + data.error;
}
})
.catch(e => {
output.textContent = '连接错误:' + e.message;
});
}
function disconnectTerminal() {
if (!sessionId) return;
fetch('/api/terminal/disconnect', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({session_id: sessionId})
})
.then(r => r.json())
.then(data => {
if (data.success) {
sessionId = null;
sessionIdEl.textContent = '-';
statusDot.className = 'status-dot status-disconnected';
statusText.textContent = '未连接';
input.disabled = true;
connectBtn.style.display = 'inline-block';
disconnectBtn.style.display = 'none';
output.textContent += '
会话已断开。';
}
});
}
function sendCommand(cmd) {
if (!sessionId) return;
fetch('/api/terminal/send', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({session_id: sessionId, command: cmd})
})
.then(r => r.json())
.then(data => {
if (data.success) {
output.textContent += '$ ' + cmd + '
' + data.output;
output.scrollTop = output.scrollHeight;
} else {
output.textContent += '
命令执行失败:' + data.error;
}
});
}
function handleKeyPress(e) {
if (e.key === 'Enter') {
sendCommand(input.value);
input.value = '';
}
}
</script>
</body>
</html>"""
return html
except Exception as e:
return f"<p>终端视图渲染出错:{e}</p>"
register_plugin_type("LogTerminalPlugin", LogTerminalPlugin)
def New():
return LogTerminalPlugin()

View File

@@ -1,642 +0,0 @@
"""包管理插件 - 提供插件配置管理和商店界面"""
import os
import sys
import json
import html
import urllib.request
from pathlib import Path
from oss.logger.logger import Log
from oss.plugin.types import Plugin, Response, register_plugin_type
# Gitee 仓库配置
GITEE_OWNER = "starlight-apk"
GITEE_REPO = "future-oss"
GITEE_BRANCH = "main"
# 使用 raw 文件 URL不走 API无频率限制
GITEE_RAW_BASE = f"https://gitee.com/{GITEE_OWNER}/{GITEE_REPO}/raw/{GITEE_BRANCH}"
GITEE_API_BASE = f"https://gitee.com/api/v5/repos/{GITEE_OWNER}/{GITEE_REPO}/contents"
# Gitee Token从环境变量读取可选
GITEE_TOKEN = os.environ.get("GITEE_TOKEN", "")
def _gitee_request(url: str, timeout: int = 15):
"""Gitee 请求"""
req = urllib.request.Request(url)
req.add_header("User-Agent", "NebulaShell-PkgManager")
if GITEE_TOKEN:
# Gitee 使用私人令牌认证
req.add_header("Authorization", f"token {GITEE_TOKEN}")
return urllib.request.urlopen(req, timeout=timeout)
class PkgManagerPlugin(Plugin):
"""包管理插件"""
def __init__(self):
self.webui = None
self.storage = None
self.store_dir = Path("./store")
self._remote_cache = None
self._cache_time = 0
self._cache_ttl = 300 # 5分钟缓存
def meta(self):
from oss.plugin.types import Metadata, PluginConfig, Manifest
return Manifest(
metadata=Metadata(
name="pkg-manager",
version="1.0.0",
author="NebulaShell",
description="插件包管理器 - 配置管理和商店"
),
config=PluginConfig(enabled=True, args={}),
dependencies=["http-api", "webui", "plugin-storage"]
)
def set_webui(self, webui):
self.webui = webui
def set_plugin_storage(self, storage):
self.storage = storage
def init(self, deps: dict = None):
"""init 阶段:注册页面到 WebUI"""
if not self.webui:
Log.warn("pkg-manager", "警告: 未找到 WebUI 依赖")
return
self.webui.register_page(
path='/packages',
content_provider=self._packages_content,
nav_item={'icon': 'ri-apps-line', 'text': '插件管理'}
)
self.webui.register_page(
path='/store',
content_provider=self._store_content,
nav_item={'icon': 'ri-store-2-line', 'text': '插件商店'}
)
Log.info("pkg-manager", "已注册到 WebUI 导航")
def start(self):
"""启动阶段:注册 API 路由"""
if not self.webui or not hasattr(self.webui, 'server') or not self.webui.server:
Log.warn("pkg-manager", "警告: WebUI 服务器未就绪")
return
router = self.webui.server.router
# API - 已安装插件
router.get("/api/plugins", self._handle_list_plugins)
router.get("/api/plugins/:name/config", self._handle_get_config)
router.post("/api/plugins/:name/config", self._handle_save_config)
router.get("/api/plugins/:name/info", self._handle_get_plugin_info)
router.post("/api/plugins/:name/uninstall", self._handle_uninstall)
# API - 远程商店
router.get("/api/store/remote", self._handle_remote_store)
router.post("/api/store/install", self._handle_store_install)
Log.info("pkg-manager", "包管理器已启动")
def stop(self):
Log.error("pkg-manager", "包管理器已停止")
# ==================== 页面渲染 ====================
def _packages_content(self) -> str:
"""渲染插件管理页面 - 纯 HTML/Python 模板"""
try:
# 获取已安装的插件列表
plugins = self._get_installed_plugins()
plugin_rows = ""
for pkg_name, info in plugins.items():
status_class = "success" if info.get('enabled', False) else "secondary"
status_text = "已启用" if info.get('enabled', False) else "已禁用"
# XSS 防护:对所有用户数据进行 HTML 转义
safe_pkg_name = html.escape(pkg_name)
safe_version = html.escape(str(info.get('version', '未知')))
safe_author = html.escape(str(info.get('author', '未知')))
plugin_rows += f"""
<tr>
<td>{safe_pkg_name}</td>
<td>{safe_version}</td>
<td>{safe_author}</td>
<td><span class="badge badge-{status_class}">{status_text}</span></td>
<td>
<button class="btn btn-sm btn-primary" onclick="togglePlugin('{safe_pkg_name}')">切换状态</button>
<button class="btn btn-sm btn-danger" onclick="uninstallPlugin('{safe_pkg_name}')">卸载</button>
</td>
</tr>"""
html = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>插件管理</title>
<link rel="stylesheet" href="/assets/remixicon.css">
<style>
* {{ margin: 0; padding: 0; box-sizing: border-box; }}
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f6fa; padding: 20px; }}
.container {{ max-width: 1400px; margin: 0 auto; }}
.card {{ background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 20px; margin-bottom: 20px; }}
.card-header {{ display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }}
.card-title {{ font-size: 18px; font-weight: 600; color: #2c3e50; }}
.btn {{ padding: 8px 16px; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; transition: all 0.3s; }}
.btn-primary {{ background: #3498db; color: white; }}
.btn-primary:hover {{ background: #2980b9; }}
.btn-danger {{ background: #e74c3c; color: white; }}
.btn-danger:hover {{ background: #c0392b; }}
.btn-sm {{ padding: 4px 8px; font-size: 12px; }}
table {{ width: 100%; border-collapse: collapse; }}
th, td {{ padding: 12px; text-align: left; border-bottom: 1px solid #ecf0f1; }}
th {{ background: #f8f9fa; font-weight: 600; color: #2c3e50; }}
tr:hover {{ background: #f8f9fa; }}
.badge {{ padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: 600; }}
.badge-success {{ background: #d5f5e3; color: #27ae60; }}
.badge-secondary {{ background: #e5e7eb; color: #6b7280; }}
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header">
<h2 class="card-title"><i class="ri-plug-line"></i> 插件管理</h2>
<button class="btn btn-primary" onclick="location.href='/store'"><i class="ri-store-line"></i> 前往商店</button>
</div>
<table>
<thead>
<tr>
<th>插件名称</th>
<th>版本</th>
<th>作者</th>
<th>状态</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{plugin_rows}
</tbody>
</table>
</div>
</div>
<script>
function togglePlugin(name) {{
fetch('/api/plugins/toggle', {{
method: 'POST',
headers: {{'Content-Type': 'application/json'}},
body: JSON.stringify({{plugin: name}})
}}).then(() => location.reload());
}}
function uninstallPlugin(name) {{
if (confirm('确定要卸载 ' + name + ' 吗?')) {{
fetch('/api/plugins/uninstall', {{
method: 'POST',
headers: {{'Content-Type': 'application/json'}},
body: JSON.stringify({{plugin: name}})
}}).then(() => location.reload());
}}
}}
</script>
</body>
</html>"""
return html
except Exception as e:
return f"<p>插件管理页面渲染出错:{{e}}</p>"
def _store_content(self) -> str:
"""渲染插件商店页面 - 纯 HTML/Python 模板"""
try:
# 获取可用插件列表
available = self._get_available_plugins()
installed = self._get_installed_plugins()
plugin_cards = ""
for pkg_name, info in available.items():
is_installed = pkg_name in installed
# XSS 防护:对所有用户数据进行 HTML 转义
safe_pkg_name = html.escape(pkg_name)
safe_name = html.escape(str(info.get('name', pkg_name)))
safe_desc = html.escape(str(info.get('description', '暂无描述')))
safe_version = html.escape(str(info.get('version', '未知')))
safe_author = html.escape(str(info.get('author', '未知')))
# JavaScript 中的字符串也需要转义
js_safe_pkg_name = pkg_name.replace('\\', '\\\\').replace("'", "\\'").replace('"', '\\"')
action_btn = f'<button class="btn btn-success" onclick="installPlugin(\'{js_safe_pkg_name}\')">安装</button>' if not is_installed else '<button class="btn btn-secondary" disabled>已安装</button>'
plugin_cards += f"""
<div class="plugin-card">
<div class="plugin-icon"><i class="ri-plug-line"></i></div>
<h3>{safe_name}</h3>
<p class="plugin-desc">{safe_desc}</p>
<div class="plugin-meta">
<span>版本:{safe_version}</span>
<span>作者:{safe_author}</span>
</div>
<div class="plugin-actions">
{action_btn}
</div>
</div>"""
html = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>插件商店</title>
<link rel="stylesheet" href="/assets/remixicon.css">
<style>
* {{ margin: 0; padding: 0; box-sizing: border-box; }}
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f6fa; padding: 20px; }}
.container {{ max-width: 1400px; margin: 0 auto; }}
.card {{ background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 20px; margin-bottom: 20px; }}
.card-header {{ margin-bottom: 20px; }}
.card-title {{ font-size: 18px; font-weight: 600; color: #2c3e50; }}
.btn {{ padding: 8px 16px; border: none; border-radius: 6px; cursor: pointer; font-size: 14px; transition: all 0.3s; }}
.btn-success {{ background: #27ae60; color: white; }}
.btn-success:hover {{ background: #229954; }}
.btn-secondary {{ background: #95a5a6; color: white; cursor: not-allowed; }}
.plugins-grid {{ display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px; }}
.plugin-card {{ background: #f8f9fa; border-radius: 8px; padding: 20px; transition: transform 0.3s; }}
.plugin-card:hover {{ transform: translateY(-5px); box-shadow: 0 5px 15px rgba(0,0,0,0.1); }}
.plugin-icon {{ width: 48px; height: 48px; background: #3498db; border-radius: 8px; display: flex; align-items: center; justify-content: center; color: white; font-size: 24px; margin-bottom: 15px; }}
.plugin-card h3 {{ font-size: 16px; color: #2c3e50; margin-bottom: 10px; }}
.plugin-desc {{ color: #7f8c8d; font-size: 14px; margin-bottom: 15px; line-height: 1.5; }}
.plugin-meta {{ display: flex; justify-content: space-between; font-size: 12px; color: #95a5a6; margin-bottom: 15px; }}
.plugin-actions {{ display: flex; gap: 10px; }}
</style>
</head>
<body>
<div class="container">
<div class="card">
<div class="card-header">
<h2 class="card-title"><i class="ri-store-line"></i> 插件商店</h2>
</div>
<div class="plugins-grid">
{plugin_cards}
</div>
</div>
</div>
<script>
function installPlugin(name) {{
fetch('/api/plugins/install', {{
method: 'POST',
headers: {{'Content-Type': 'application/json'}},
body: JSON.stringify({{plugin: name}})
}}).then(r => r.json()).then(data => {{
if (data.success) {{
alert('安装成功!');
location.reload();
}} else {{
alert('安装失败:' + data.error);
}}
}});
}}
</script>
</body>
</html>"""
return html
except Exception as e:
return f"<p>插件商店页面渲染出错:{{e}}</p>"
# ==================== API 处理 ====================
def _handle_list_plugins(self, request):
"""列出所有已安装插件"""
plugins = self._scan_all_plugins()
return Response(status=200, headers={"Content-Type": "application/json"}, body=json.dumps(plugins, ensure_ascii=False))
def _handle_get_config(self, request):
"""获取插件配置 schema + 当前值"""
plugin_name = request.path_params.get('name', '')
schema = self._load_config_schema(plugin_name)
current = self._load_plugin_config(plugin_name)
return Response(status=200, headers={"Content-Type": "application/json"}, body=json.dumps({
"schema": schema,
"current": current
}, ensure_ascii=False))
def _handle_save_config(self, request):
"""保存插件配置"""
import json as json_mod
try:
body = json_mod.loads(request.body)
plugin_name = request.path_params.get('name', '')
self._save_plugin_config(plugin_name, body)
return Response(status=200, headers={"Content-Type": "application/json"}, body='{"ok":true}')
except Exception as e:
return Response(status=500, headers={"Content-Type": "application/json"}, body=json.dumps({"error": str(e)}))
def _handle_get_plugin_info(self, request):
"""获取插件详细信息"""
plugin_name = request.path_params.get('name', '')
info = self._get_plugin_detailed_info(plugin_name)
return Response(status=200, headers={"Content-Type": "application/json"}, body=json.dumps(info, ensure_ascii=False))
def _handle_uninstall(self, request):
"""卸载插件"""
import shutil
plugin_name = request.path_params.get('name', '')
# 查找插件目录
plugin_dir = self._find_plugin_dir(plugin_name)
if not plugin_dir:
return Response(status=404, body='{"error":"插件未安装"}')
try:
shutil.rmtree(plugin_dir)
return Response(status=200, body='{"ok":true}')
except Exception as e:
return Response(status=500, body=json.dumps({"error": str(e)}))
def _handle_remote_store(self, request):
"""从 Gitee API 获取远程插件列表"""
try:
plugins = self._fetch_remote_plugins()
return Response(status=200, headers={"Content-Type": "application/json"}, body=json.dumps(plugins, ensure_ascii=False))
except Exception as e:
return Response(status=500, body=json.dumps({"error": str(e)}))
def _handle_store_install(self, request):
"""安装插件"""
import json as json_mod
try:
body = json_mod.loads(request.body)
name = body.get("name", "")
author = body.get("author", "NebulaShell")
success = self._install_from_gitee(name, author)
return Response(status=200, body=json.dumps({"ok": success}))
except Exception as e:
return Response(status=500, body=json.dumps({"error": str(e)}))
# ==================== Gitee 远程商店 ====================
def _fetch_remote_plugins(self) -> list:
"""从 Gitee 获取所有可用插件(带缓存+限速+重试)"""
import time
now = time.time()
if self._remote_cache and (now - self._cache_time) < self._cache_ttl:
return self._remote_cache
plugins = []
try:
store_url = f"{GITEE_API_BASE}/store"
# 重试 3 次,每次间隔增加
for attempt in range(3):
try:
with _gitee_request(store_url, timeout=15) as resp:
dirs = json.loads(resp.read().decode("utf-8"))
break
except Exception as e:
if attempt < 2:
time.sleep(1 + attempt)
continue
raise
time.sleep(0.5)
for dir_info in dirs:
if dir_info.get("type") != "dir":
continue
author = dir_info.get("name", "")
if not author.startswith("@{"):
continue
author_url = f"{GITEE_API_BASE}/store/{urllib.parse.quote(author, safe='')}"
for attempt in range(3):
try:
with _gitee_request(author_url, timeout=15) as resp:
plugin_dirs = json.loads(resp.read().decode("utf-8"))
break
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
if attempt < 2:
time.sleep(1 + attempt)
continue
raise
time.sleep(0.5)
for plugin_dir in plugin_dirs:
if plugin_dir.get("type") != "dir":
continue
plugin_name = plugin_dir.get("name", "")
manifest_url = f"{GITEE_API_BASE}/store/{urllib.parse.quote(author, safe='')}/{plugin_name}/manifest.json"
manifest = {}
for attempt in range(3):
try:
with _gitee_request(manifest_url, timeout=15) as resp:
manifest = json.loads(resp.read().decode("utf-8"))
break
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
if attempt < 2:
time.sleep(1 + attempt)
continue
plugins.append({
"name": plugin_name,
"author": author,
"full_name": f"{author}/{plugin_name}",
"metadata": manifest.get("metadata", {}),
"dependencies": manifest.get("dependencies", []),
"has_config": False,
"is_installed": self._is_plugin_installed(plugin_name, author)
})
time.sleep(0.5)
self._remote_cache = plugins
self._cache_time = now
except Exception as e:
Log.error("pkg-manager", f"获取远程插件列表失败: {type(e).__name__}: {e}")
return plugins
def _install_from_gitee(self, plugin_name: str, author: str) -> bool:
"""从 Gitee 下载并安装插件(使用 raw URL"""
import shutil, time
install_dir = self.store_dir / author / plugin_name
install_dir.mkdir(parents=True, exist_ok=True)
try:
# 获取目录结构(需要一次 API 调用)
api_url = f"{GITEE_API_BASE}/store/{author}/{plugin_name}"
with _gitee_request(api_url, timeout=15) as resp:
items = json.loads(resp.read().decode("utf-8"))
time.sleep(0.5)
for item in items:
if item.get("type") == "file":
# 使用 raw URL 下载文件(不走 API
filename = item.get("name")
raw_url = f"{GITEE_RAW_BASE}/store/{author}/{plugin_name}/{filename}"
local_file = install_dir / filename
try:
with _gitee_request(raw_url, timeout=15) as resp:
content = resp.read()
with open(local_file, 'wb') as f:
f.write(content)
except:
pass
elif item.get("type") == "dir":
sub_dir = item.get("name")
self._download_dir_raw(author, plugin_name, sub_dir, install_dir / sub_dir)
time.sleep(0.3)
Log.info("pkg-manager", f"已安装: {author}/{plugin_name}")
return True
except Exception as e:
Log.error("pkg-manager", f"安装失败 {plugin_name}: {type(e).__name__}: {e}")
if install_dir.exists():
shutil.rmtree(install_dir)
return False
def _download_dir_raw(self, author: str, plugin: str, sub_dir: str, local_dir: Path):
"""使用 raw URL 递归下载子目录"""
import time
try:
api_url = f"{GITEE_API_BASE}/store/{author}/{plugin}/{sub_dir}"
with _gitee_request(api_url, timeout=15) as resp:
items = json.loads(resp.read().decode("utf-8"))
local_dir.mkdir(parents=True, exist_ok=True)
for item in items:
if item.get("type") == "file":
filename = item.get("name")
raw_url = f"{GITEE_RAW_BASE}/store/{author}/{plugin}/{sub_dir}/{filename}"
try:
with _gitee_request(raw_url, timeout=15) as resp:
content = resp.read()
with open(local_dir / filename, 'wb') as f:
f.write(content)
except:
pass
elif item.get("type") == "dir":
self._download_dir_raw(author, plugin, f"{sub_dir}/{item.get('name')}", local_dir / item.get("name"))
except:
pass
# ==================== 辅助方法 ====================
def _scan_all_plugins(self) -> list:
"""扫描本地已安装插件"""
plugins = []
if not self.store_dir.exists():
return plugins
for author_dir in self.store_dir.iterdir():
if author_dir.is_dir() and author_dir.name.startswith("@{"):
for plugin_dir in author_dir.iterdir():
if plugin_dir.is_dir() and (plugin_dir / "main.py").exists():
manifest_path = plugin_dir / "manifest.json"
if manifest_path.exists():
with open(manifest_path, 'r', encoding='utf-8') as f:
manifest = json.load(f)
plugins.append({
"name": plugin_dir.name,
"full_name": f"{author_dir.name}/{plugin_dir.name}",
"author": author_dir.name,
"metadata": manifest.get("metadata", {}),
"dependencies": manifest.get("dependencies", []),
"has_config": (plugin_dir / "config.json").exists(),
"is_installed": True
})
return plugins
def _is_plugin_installed(self, plugin_name: str, author: str) -> bool:
"""检查插件是否已安装"""
plugin_dir = self.store_dir / author / plugin_name
return (plugin_dir / "main.py").exists()
def _find_plugin_dir(self, plugin_name: str) -> Path | None:
"""查找插件目录"""
if not self.store_dir.exists():
return None
for author_dir in self.store_dir.iterdir():
if author_dir.is_dir():
plugin_dir = author_dir / plugin_name
if plugin_dir.exists() and (plugin_dir / "main.py").exists():
return plugin_dir
return None
def _load_config_schema(self, plugin_name: str) -> dict:
"""加载插件 config.json schema"""
plugin_dir = self._find_plugin_dir(plugin_name)
if not plugin_dir:
return {}
schema_path = plugin_dir / "config.json"
if not schema_path.exists():
return {}
with open(schema_path, 'r', encoding='utf-8') as f:
return json.load(f)
def _load_plugin_config(self, plugin_name: str) -> dict:
"""加载插件当前配置"""
schema = self._load_config_schema(plugin_name)
defaults = {}
for key, field_def in schema.items():
defaults[key] = field_def.get("default")
if self.storage:
storage_instance = self.storage.get_storage("pkg-manager")
user_config = storage_instance.get(f"plugin_config.{plugin_name}", {})
defaults.update(user_config)
return defaults
def _save_plugin_config(self, plugin_name: str, config: dict):
"""保存插件配置"""
if self.storage:
storage_instance = self.storage.get_storage("pkg-manager")
storage_instance.set(f"plugin_config.{plugin_name}", config)
def _get_plugin_detailed_info(self, plugin_name: str) -> dict:
"""获取插件的依赖、事件、页面信息"""
dependencies = []
events = [] # 事件 = 功能描述
plugin_dir = self._find_plugin_dir(plugin_name)
if plugin_dir:
manifest_path = plugin_dir / "manifest.json"
if manifest_path.exists():
with open(manifest_path, 'r', encoding='utf-8') as f:
manifest = json.load(f)
dependencies = manifest.get("dependencies", [])
# 从 manifest 的 metadata.description 或 type 中提取功能
metadata = manifest.get("metadata", {})
plugin_type = metadata.get("type", "")
if plugin_type:
events.append(f"类型: {plugin_type}")
# 从 manifest config 推断功能
config = manifest.get("config", {})
if config.get("enabled"):
events.append("已启用")
# 只返回该插件自己注册的页面(通过插件名匹配)
pages = []
if self.webui and hasattr(self.webui, 'server') and self.webui.server:
for path, provider in self.webui.server.pages.items():
# 检查 provider 是否属于该插件
provider_name = getattr(provider, '__self__', None)
if provider_name and isinstance(provider_name, PkgManagerPlugin):
continue # 跳过自己的页面
# 通过路径前缀判断dashboard 注册 /dashboard
if path == f'/{plugin_name}' or path.startswith(f'/{plugin_name}/'):
pages.append({"path": path})
# 特殊处理:首页
if plugin_name == 'webui' and path == '/':
pages.append({"path": path})
return {
"name": plugin_name,
"dependencies": dependencies,
"config_fields": list(self._load_config_schema(plugin_name).keys()),
"pages": pages,
"events": events
}
register_plugin_type("PkgManagerPlugin", PkgManagerPlugin)
def New():
return PkgManagerPlugin()

View File

@@ -1,205 +0,0 @@
"""插件桥接器 - 共享事件、广播、桥接"""
from typing import Any, Callable, Optional
from dataclasses import dataclass, field
from oss.logger.logger import Log
from oss.plugin.types import Plugin, register_plugin_type
@dataclass
class BridgeEvent:
"""桥接事件"""
type: str
source_plugin: str
payload: Any = None
context: dict[str, Any] = field(default_factory=dict)
class EventBus:
"""事件总线"""
def __init__(self):
self._handlers: dict[str, list[Callable]] = {}
self._history: list[BridgeEvent] = []
def emit(self, event: BridgeEvent):
"""发布事件"""
self._history.append(event)
handlers = self._handlers.get(event.type, [])
wildcard_handlers = self._handlers.get("*", [])
for handler in handlers + wildcard_handlers:
try:
handler(event)
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
pass
def on(self, event_type: str, handler: Callable):
"""订阅事件"""
if event_type not in self._handlers:
self._handlers[event_type] = []
self._handlers[event_type].append(handler)
def off(self, event_type: str, handler: Callable):
"""取消订阅"""
if event_type in self._handlers:
try:
self._handlers[event_type].remove(handler)
except ValueError:
pass
def once(self, event_type: str, handler: Callable):
"""仅触发一次"""
def wrapper(event):
self.off(event_type, wrapper)
handler(event)
self.on(event_type, wrapper)
def get_history(self, event_type: str = None) -> list[BridgeEvent]:
"""获取事件历史"""
if event_type:
return [e for e in self._history if e.type == event_type]
return self._history.copy()
def clear_history(self):
"""清空事件历史"""
self._history.clear()
class BroadcastManager:
"""广播管理器"""
def __init__(self, event_bus: EventBus):
self.event_bus = event_bus
self._channels: dict[str, list[str]] = {}
def create_channel(self, name: str, plugins: list[str]):
"""创建广播频道"""
self._channels[name] = plugins
def broadcast(self, channel: str, payload: Any, source_plugin: str = ""):
"""广播到指定频道"""
if channel not in self._channels:
return
event = BridgeEvent(
type=f"broadcast.{channel}",
source_plugin=source_plugin,
payload=payload
)
self.event_bus.emit(event)
def get_channels(self) -> dict[str, list[str]]:
"""获取所有频道"""
return self._channels.copy()
class ServiceRegistry:
"""服务注册表RPC"""
def __init__(self):
self._services: dict[str, dict[str, Callable]] = {}
def register(self, plugin_name: str, service_name: str, handler: Callable):
"""注册服务"""
if plugin_name not in self._services:
self._services[plugin_name] = {}
self._services[plugin_name][service_name] = handler
def unregister(self, plugin_name: str, service_name: str = None):
"""注销服务"""
if plugin_name in self._services:
if service_name:
self._services[plugin_name].pop(service_name, None)
else:
del self._services[plugin_name]
def call(self, plugin_name: str, service_name: str, *args, **kwargs) -> Any:
"""远程调用"""
if plugin_name not in self._services:
raise RuntimeError(f"插件 '{plugin_name}' 未注册服务")
if service_name not in self._services[plugin_name]:
raise RuntimeError(f"插件 '{plugin_name}' 未注册服务 '{service_name}'")
return self._services[plugin_name][service_name](*args, **kwargs)
def list_services(self, plugin_name: str = None) -> dict[str, dict[str, Callable]]:
"""列出服务"""
if plugin_name:
return self._services.get(plugin_name, {}).copy()
return {k: v.copy() for k, v in self._services.items()}
class BridgeManager:
"""桥接管理器"""
def __init__(self, event_bus: EventBus):
self.event_bus = event_bus
self._bridges: dict[str, dict[str, Any]] = {}
def create_bridge(self, name: str, from_plugin: str, to_plugin: str, event_mapping: dict[str, str]):
"""创建桥接:将 from_plugin 的事件映射到 to_plugin"""
self._bridges[name] = {
"from": from_plugin,
"to": to_plugin,
"mapping": event_mapping,
}
# 注册桥接处理器
for src_event, dst_event in event_mapping.items():
def handler(event, dst_event=dst_event):
bridged = BridgeEvent(
type=dst_event,
source_plugin=event.source_plugin,
payload=event.payload,
context={**event.context, "_bridged_from": event.type}
)
self.event_bus.emit(bridged)
self.event_bus.on(src_event, handler)
def remove_bridge(self, name: str):
"""移除桥接"""
if name in self._bridges:
del self._bridges[name]
def get_bridges(self) -> dict[str, dict[str, Any]]:
"""获取所有桥接"""
return self._bridges.copy()
class PluginBridgePlugin(Plugin):
"""插件桥接器插件"""
def __init__(self):
self.event_bus = EventBus()
self.broadcast = None
self.bridge = None
self.services = ServiceRegistry()
self.storage = None # 共享存储接口
def init(self, deps: dict = None):
"""初始化"""
self.broadcast = BroadcastManager(self.event_bus)
self.bridge = BridgeManager(self.event_bus)
def start(self):
"""启动"""
Log.info("plugin-bridge", "事件总线、广播、桥接、RPC、共享存储已启动")
def stop(self):
"""停止"""
self.event_bus.clear_history()
def set_plugin_storage(self, storage_plugin):
"""设置存储插件引用"""
if storage_plugin:
self.storage = storage_plugin.get_shared()
# 注册类型
register_plugin_type("BridgeEvent", BridgeEvent)
register_plugin_type("EventBus", EventBus)
register_plugin_type("BroadcastManager", BroadcastManager)
register_plugin_type("BridgeManager", BridgeManager)
register_plugin_type("ServiceRegistry", ServiceRegistry)
def New():
return PluginBridgePlugin()

View File

@@ -1,64 +0,0 @@
"""熔断器实现"""
import time
from typing import Callable, Any
from .state import CircuitState
class CircuitBreaker:
"""熔断器"""
def __init__(self, failure_threshold: int = 3, recovery_timeout: int = 60, half_open_requests: int = 1):
self.failure_threshold = failure_threshold
self.recovery_timeout = recovery_timeout
self.half_open_requests = half_open_requests
self.state = CircuitState.CLOSED
self.failure_count = 0
self.success_count = 0
self.last_failure_time = 0
self.half_open_calls = 0
def call(self, func: Callable, *args, **kwargs) -> Any:
"""执行调用"""
if self.state == CircuitState.OPEN:
if time.time() - self.last_failure_time >= self.recovery_timeout:
self.state = CircuitState.HALF_OPEN
self.half_open_calls = 0
else:
raise Exception("熔断器已打开,调用被拒绝")
try:
result = func(*args, **kwargs)
self._on_success()
return result
except Exception as e:
self._on_failure()
raise
def _on_success(self):
"""成功回调"""
self.failure_count = 0
if self.state == CircuitState.HALF_OPEN:
self.half_open_calls += 1
if self.half_open_calls >= self.half_open_requests:
self.state = CircuitState.CLOSED
self.half_open_calls = 0
def _on_failure(self):
"""失败回调"""
self.failure_count += 1
self.last_failure_time = time.time()
if self.state == CircuitState.HALF_OPEN:
self.state = CircuitState.OPEN
elif self.failure_count >= self.failure_threshold:
self.state = CircuitState.OPEN
def reset(self):
"""重置熔断器"""
self.state = CircuitState.CLOSED
self.failure_count = 0
self.half_open_calls = 0
def get_state(self) -> str:
return self.state

View File

@@ -1,8 +0,0 @@
"""熔断器状态枚举"""
class CircuitState:
"""熔断器状态"""
CLOSED = "closed" # 正常状态
OPEN = "open" # 熔断状态
HALF_OPEN = "half_open" # 半开状态

View File

@@ -1,56 +0,0 @@
"""Pro 配置模型"""
class CircuitBreakerConfig:
"""熔断器配置"""
def __init__(self, config: dict = None):
config = config or {}
self.failure_threshold = config.get("failure_threshold", 3)
self.recovery_timeout = config.get("recovery_timeout", 60)
self.half_open_requests = config.get("half_open_requests", 1)
class RetryConfig:
"""重试配置"""
def __init__(self, config: dict = None):
config = config or {}
self.max_retries = config.get("max_retries", 3)
self.backoff_factor = config.get("backoff_factor", 2)
self.initial_delay = config.get("initial_delay", 1)
class HealthCheckConfig:
"""健康检查配置"""
def __init__(self, config: dict = None):
config = config or {}
self.interval = config.get("interval", 30)
self.timeout = config.get("timeout", 5)
self.max_failures = config.get("max_failures", 5)
class AutoRecoveryConfig:
"""自动恢复配置"""
def __init__(self, config: dict = None):
config = config or {}
self.enabled = config.get("enabled", True)
self.max_attempts = config.get("max_attempts", 3)
self.delay = config.get("delay", 10)
class IsolationConfig:
"""隔离配置"""
def __init__(self, config: dict = None):
config = config or {}
self.enabled = config.get("enabled", True)
self.timeout_per_plugin = config.get("timeout_per_plugin", 30)
class ProConfig:
"""Pro 总配置"""
def __init__(self, config: dict = None):
config = config or {}
self.circuit_breaker = CircuitBreakerConfig(config.get("circuit_breaker"))
self.retry = RetryConfig(config.get("retry"))
self.health_check = HealthCheckConfig(config.get("health_check"))
self.auto_recovery = AutoRecoveryConfig(config.get("auto_recovery"))
self.isolation = IsolationConfig(config.get("isolation"))

View File

@@ -1,209 +0,0 @@
"""插件加载增强器"""
from ..circuit.breaker import CircuitBreaker
from ..recovery.health import HealthChecker
from ..recovery.auto_fix import AutoRecovery
from ..utils.logger import ProLogger
from .config import ProConfig
class PluginLoaderEnhancer:
"""插件加载增强器 - 为现有 plugin-loader 提供高级机制"""
def __init__(self, plugin_manager, config: ProConfig):
self.pm = plugin_manager
self.config = config
self._breakers = {}
self._health_checker = None
self._auto_recovery = AutoRecovery(
config.auto_recovery.max_attempts,
config.auto_recovery.delay
)
self._enhanced = False
def enhance(self):
"""增强 plugin-loader"""
if self._enhanced:
return
ProLogger.info("enhancer", "开始增强 plugin-loader...")
# 1. 为所有插件创建熔断器
self._setup_circuit_breakers()
# 2. 包装启动方法(带重试和容错)
self._wrap_start_methods()
# 3. 启动健康检查
self._start_health_check()
self._enhanced = True
ProLogger.info("enhancer", "增强完成,共增强 {} 个插件".format(
len(self.pm.plugins)
))
def _setup_circuit_breakers(self):
"""为所有插件创建熔断器"""
for name, info in self.pm.plugins.items():
self._breakers[name] = CircuitBreaker(
self.config.circuit_breaker.failure_threshold,
self.config.circuit_breaker.recovery_timeout,
self.config.circuit_breaker.half_open_requests
)
ProLogger.debug("enhancer", f"{name} 创建熔断器")
def _wrap_start_methods(self):
"""包装启动方法"""
original_start_all = getattr(self.pm, 'start_all', None)
if original_start_all:
def wrapped_start_all():
self._safe_start_all()
self.pm.start_all = wrapped_start_all
ProLogger.info("enhancer", "已包装 start_all 方法")
original_init_and_start = getattr(
self.pm, 'init_and_start_all', None
)
if original_init_and_start:
def wrapped_init_and_start():
self._safe_init_and_start_all()
self.pm.init_and_start_all = wrapped_init_and_start
ProLogger.info("enhancer", "已包装 init_and_start_all 方法")
def _safe_init_and_start_all(self):
"""安全的初始化并启动"""
ordered = self._get_ordered_plugins()
# 安全初始化
for name in ordered:
self._safe_call(name, 'init', '初始化')
# 安全启动
for name in ordered:
self._safe_call(name, 'start', '启动')
def _safe_start_all(self):
"""安全启动所有"""
for name in self.pm.plugins:
self._safe_call(name, 'start', '启动')
def _safe_call(self, name: str, method: str, action: str):
"""安全调用插件方法(带熔断和重试)"""
info = self.pm.plugins.get(name)
if not info:
return
instance = info.get("instance")
if not instance or not hasattr(instance, method):
return
breaker = self._breakers.get(name)
if not breaker:
# 没有熔断器,直接调用
try:
getattr(instance, method)()
except Exception as e:
ProLogger.error("safe", f"{name} {action}失败: {type(e).__name__}: {e}")
self._on_plugin_error(name, info, str(e))
return
# 有熔断器,包装调用
def do_call():
return getattr(instance, method)()
try:
breaker.call(do_call)
info["info"].error_count = 0
ProLogger.info("safe", f"{name} {action}成功")
except Exception as e:
ProLogger.error("safe", f"{name} {action}失败: {type(e).__name__}: {e}")
self._on_plugin_error(name, info, str(e))
def _on_plugin_error(self, name: str, info: dict, error: str):
"""插件错误处理"""
info["info"].error_count += 1
info["info"].last_error = error
# 自动恢复
if self.config.auto_recovery.enabled:
plugin_dir = info.get("dir")
module = info.get("module")
if plugin_dir:
result = self._auto_recovery.attempt_recovery(
name, plugin_dir, module, info.get("instance")
)
if result:
info["instance"] = result
info["info"].error_count = 0
ProLogger.info("recovery", f"{name} 自动恢复成功")
def _start_health_check(self):
"""启动健康检查"""
self._health_checker = HealthChecker(
self.config.health_check.interval,
self.config.health_check.timeout,
self.config.health_check.max_failures
)
for name, info in self.pm.plugins.items():
self._health_checker.add_plugin(name, info["instance"])
self._health_checker.start(
on_failure_callback=self._on_health_check_failure
)
ProLogger.info("enhancer", "健康检查已启动")
def _on_health_check_failure(self, name: str):
"""健康检查失败回调"""
ProLogger.error("health", f"插件 {name} 健康检查失败")
info = self.pm.plugins.get(name)
if not info:
return
plugin_dir = info.get("dir")
module = info.get("module")
if plugin_dir:
result = self._auto_recovery.attempt_recovery(
name, plugin_dir, module, info.get("instance")
)
if result:
info["instance"] = result
self._health_checker.reset_failure_count(name)
ProLogger.info("recovery", f"{name} 健康恢复成功")
def _get_ordered_plugins(self) -> list[str]:
"""获取按依赖排序的插件列表"""
ordered = []
visited = set()
def visit(name):
if name in visited:
return
visited.add(name)
info = self.pm.plugins.get(name)
if not info:
return
for dep in info["info"].dependencies:
clean_dep = dep.rstrip("}")
if clean_dep in self.pm.plugins:
visit(clean_dep)
ordered.append(name)
for name in self.pm.plugins:
visit(name)
return ordered
def disable(self):
"""禁用增强器"""
if self._health_checker:
self._health_checker.stop()
self._enhanced = False
ProLogger.info("enhancer", "增强器已禁用")

View File

@@ -1,278 +0,0 @@
"""插件加载 Pro - 核心管理器"""
import sys
import json
import importlib.util
from pathlib import Path
from typing import Any, Optional
from oss.plugin.types import Plugin
from .config import ProConfig
from .registry import CapabilityRegistry
from .proxy import PluginProxy, PermissionError
from ..models.plugin_info import PluginInfo
from ..circuit.breaker import CircuitBreaker
from ..retry.handler import RetryHandler
from ..fallback.handler import FallbackHandler
from ..recovery.health import HealthChecker
from ..recovery.auto_fix import AutoRecovery
from ..isolation.timeout import TimeoutController, TimeoutError
from ..utils.logger import ProLogger
from oss.plugin.capabilities import scan_capabilities
class ProPluginManager:
"""Pro 插件管理器"""
def __init__(self, config: ProConfig):
self.config = config
self.plugins: dict[str, dict[str, Any]] = {}
self.capability_registry = CapabilityRegistry()
self._breakers: dict[str, CircuitBreaker] = {}
self._health_checker = HealthChecker(
config.health_check.interval,
config.health_check.timeout,
config.health_check.max_failures
)
self._auto_recovery = AutoRecovery(
config.auto_recovery.max_attempts,
config.auto_recovery.delay
)
def load_all(self, store_dir: str = "store"):
"""加载所有插件"""
ProLogger.info("loader", "开始扫描插件...")
self._load_from_dir(Path(store_dir))
ProLogger.info("loader", f"共加载 {len(self.plugins)} 个插件")
def _load_from_dir(self, store_dir: Path):
"""从目录加载插件"""
if not store_dir.exists():
return
for author_dir in store_dir.iterdir():
if not author_dir.is_dir():
continue
for plugin_dir in author_dir.iterdir():
if not plugin_dir.is_dir():
continue
main_file = plugin_dir / "main.py"
if not main_file.exists():
continue
self._load_single_plugin(plugin_dir)
def _load_single_plugin(self, plugin_dir: Path) -> Optional[Any]:
"""加载单个插件"""
main_file = plugin_dir / "main.py"
manifest_file = plugin_dir / "manifest.json"
try:
manifest = {}
if manifest_file.exists():
with open(manifest_file, "r", encoding="utf-8") as f:
manifest = json.load(f)
spec = importlib.util.spec_from_file_location(
f"pro_plugin.{plugin_dir.name}", str(main_file)
)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
if not hasattr(module, "New"):
return None
instance = module.New()
plugin_name = plugin_dir.name.rstrip("}")
permissions = manifest.get("permissions", [])
if permissions:
instance = PluginProxy(
plugin_name, instance, permissions, self.plugins
)
info = PluginInfo()
meta = manifest.get("metadata", {})
info.name = meta.get("name", plugin_name)
info.version = meta.get("version", "1.0.0")
info.author = meta.get("author", "")
info.description = meta.get("description", "")
info.dependencies = manifest.get("dependencies", [])
info.capabilities = scan_capabilities(plugin_dir)
for cap in info.capabilities:
self.capability_registry.register_provider(
cap, plugin_name, instance
)
self._breakers[plugin_name] = CircuitBreaker(
self.config.circuit_breaker.failure_threshold,
self.config.circuit_breaker.recovery_timeout,
self.config.circuit_breaker.half_open_requests
)
self.plugins[plugin_name] = {
"instance": instance,
"module": module,
"info": info,
"permissions": permissions,
"dir": plugin_dir
}
ProLogger.info("loader", f"已加载: {plugin_name} v{info.version}")
return instance
except Exception as e:
ProLogger.error("loader", f"加载失败 {plugin_dir.name}: {type(e).__name__}: {e}")
return None
def init_and_start_all(self):
"""初始化并启动所有插件"""
ProLogger.info("manager", "开始初始化所有插件...")
self._inject_dependencies()
ordered = self._get_ordered_plugins()
for name in ordered:
self._safe_init(name)
ProLogger.info("manager", "开始启动所有插件...")
for name in ordered:
self._safe_start(name)
self._health_checker.start(
on_failure_callback=self._on_plugin_failure
)
def _safe_init(self, name: str):
"""安全初始化插件"""
info = self.plugins[name]
instance = info["instance"]
breaker = self._breakers[name]
try:
breaker.call(instance.init)
info["info"].status = "initialized"
ProLogger.info("manager", f"已初始化: {name}")
except Exception as e:
ProLogger.error("manager", f"初始化失败 {name}: {type(e).__name__}: {e}")
info["info"].status = "error"
info["info"].error_count += 1
info["info"].last_error = str(e)
def _safe_start(self, name: str):
"""安全启动插件"""
info = self.plugins[name]
instance = info["instance"]
breaker = self._breakers[name]
try:
breaker.call(instance.start)
info["info"].status = "running"
self._health_checker.add_plugin(name, instance)
ProLogger.info("manager", f"已启动: {name}")
except Exception as e:
ProLogger.error("manager", f"启动失败 {name}: {type(e).__name__}: {e}")
info["info"].status = "error"
info["info"].error_count += 1
info["info"].last_error = str(e)
def stop_all(self):
"""停止所有插件"""
self._health_checker.stop()
for name in reversed(list(self.plugins.keys())):
self._safe_stop(name)
def _safe_stop(self, name: str):
"""安全停止插件"""
info = self.plugins[name]
instance = info["instance"]
try:
instance.stop()
info["info"].status = "stopped"
ProLogger.info("manager", f"已停止: {name}")
except Exception as e:
ProLogger.warn("manager", f"停止异常 {name}: {type(e).__name__}: {e}")
def _on_plugin_failure(self, name: str):
"""插件失败回调"""
ProLogger.error("recovery", f"插件 {name} 健康检查失败")
if not self.config.auto_recovery.enabled:
return
info = self.plugins.get(name)
if not info:
return
plugin_dir = info.get("dir")
module = info.get("module")
instance = info.get("instance")
if plugin_dir:
result = self._auto_recovery.attempt_recovery(
name, plugin_dir, module, instance
)
if result:
info["instance"] = result
info["info"].status = "running"
self._health_checker.reset_failure_count(name)
def _inject_dependencies(self):
"""注入依赖"""
name_map = {}
for name in self.plugins:
clean = name.rstrip("}")
name_map[clean] = name
name_map[clean + "}"] = name
for name, info in self.plugins.items():
deps = info["info"].dependencies
if not deps:
continue
for dep_name in deps:
actual_dep = name_map.get(dep_name) or name_map.get(dep_name + "}")
if actual_dep and actual_dep in self.plugins:
dep_instance = self.plugins[actual_dep]["instance"]
setter = f"set_{dep_name.replace('-', '_')}"
if hasattr(info["instance"], setter):
try:
getattr(info["instance"], setter)(dep_instance)
ProLogger.info("inject", f"{name} <- {actual_dep}")
except Exception as e:
ProLogger.error("inject", f"注入失败 {name}.{setter}: {type(e).__name__}: {e}")
def _get_ordered_plugins(self) -> list[str]:
"""获取插件顺序"""
ordered = []
visited = set()
def visit(name):
if name in visited:
return
visited.add(name)
info = self.plugins.get(name)
if not info:
return
for dep in info["info"].dependencies:
clean_dep = dep.rstrip("}")
if clean_dep in self.plugins:
visit(clean_dep)
ordered.append(name)
for name in self.plugins:
visit(name)
return ordered

View File

@@ -1,36 +0,0 @@
"""插件代理 - 防越级访问"""
class PermissionError(Exception):
"""权限错误"""
pass
class PluginProxy:
"""插件代理"""
def __init__(self, plugin_name: str, plugin_instance: any,
allowed_plugins: list[str], all_plugins: dict[str, dict]):
self._plugin_name = plugin_name
self._plugin_instance = plugin_instance
self._allowed_plugins = set(allowed_plugins)
self._all_plugins = all_plugins
def get_plugin(self, name: str) -> any:
"""获取其他插件实例(带权限检查)"""
if name not in self._allowed_plugins and "*" not in self._allowed_plugins:
raise PermissionError(
f"插件 '{self._plugin_name}' 无权访问插件 '{name}'"
)
if name not in self._all_plugins:
return None
return self._all_plugins[name]["instance"]
def list_plugins(self) -> list[str]:
"""列出有权限访问的插件"""
if "*" in self._allowed_plugins:
return list(self._all_plugins.keys())
return [n for n in self._allowed_plugins if n in self._all_plugins]
def __getattr__(self, name: str):
return getattr(self._plugin_instance, name)

View File

@@ -1,51 +0,0 @@
"""能力注册表"""
from typing import Any, Optional
from .proxy import PermissionError
class CapabilityRegistry:
"""能力注册表"""
def __init__(self, permission_check: bool = True):
self.providers: dict[str, dict[str, Any]] = {}
self.consumers: dict[str, list[str]] = {}
self.permission_check = permission_check
def register_provider(self, capability: str, plugin_name: str, instance: Any):
"""注册能力提供者"""
self.providers[capability] = {
"plugin": plugin_name,
"instance": instance,
}
if capability not in self.consumers:
self.consumers[capability] = []
def register_consumer(self, capability: str, plugin_name: str):
"""注册能力消费者"""
if capability not in self.consumers:
self.consumers[capability] = []
if plugin_name not in self.consumers[capability]:
self.consumers[capability].append(plugin_name)
def get_provider(self, capability: str, requester: str = "",
allowed_plugins: list[str] = None) -> Optional[Any]:
"""获取能力提供者实例(带权限检查)"""
if capability not in self.providers:
return None
if self.permission_check and allowed_plugins is not None:
provider_name = self.providers[capability]["plugin"]
if (provider_name != requester and
provider_name not in allowed_plugins and
"*" not in allowed_plugins):
raise PermissionError(
f"插件 '{requester}' 无权使用能力 '{capability}'"
)
return self.providers[capability]["instance"]
def has_capability(self, capability: str) -> bool:
return capability in self.providers
def get_consumers(self, capability: str) -> list[str]:
return self.consumers.get(capability, [])

View File

@@ -1,49 +0,0 @@
"""降级处理器"""
from typing import Callable, Any, Optional
from ..utils.logger import ProLogger
class FallbackStrategy:
"""降级策略枚举"""
RETURN_DEFAULT = "return_default"
RETURN_CACHE = "return_cache"
RETURN_NULL = "return_null"
CALL_ALTERNATIVE = "call_alternative"
class FallbackHandler:
"""降级处理器"""
def __init__(self, strategy: str = FallbackStrategy.RETURN_NULL,
default_value: Any = None,
alternative_func: Callable = None):
self.strategy = strategy
self.default_value = default_value
self.alternative_func = alternative_func
self._cache = {}
def execute(self, func: Callable, plugin_name: str, *args, **kwargs) -> Any:
"""执行降级逻辑"""
try:
result = func(*args, **kwargs)
self._cache[plugin_name] = result
return result
except Exception as e:
ProLogger.warn("fallback", f"插件 {plugin_name} 执行失败,触发降级: {type(e).__name__}: {e}")
return self._apply_fallback(plugin_name)
def _apply_fallback(self, plugin_name: str) -> Any:
"""应用降级策略"""
if self.strategy == FallbackStrategy.RETURN_DEFAULT:
return self.default_value
elif self.strategy == FallbackStrategy.RETURN_CACHE:
return self._cache.get(plugin_name)
elif self.strategy == FallbackStrategy.RETURN_NULL:
return None
elif self.strategy == FallbackStrategy.CALL_ALTERNATIVE:
if self.alternative_func:
try:
return self.alternative_func()
except Exception as e:
ProLogger.error("fallback", f"备选方案也失败了: {type(e).__name__}: {e}")
return None

View File

@@ -1,60 +0,0 @@
"""自动修复器"""
import time
import importlib
import sys
from pathlib import Path
from ..utils.logger import ProLogger
class AutoRecovery:
"""自动修复器"""
def __init__(self, max_attempts: int = 3, delay: int = 10):
self.max_attempts = max_attempts
self.delay = delay
self._recovery_attempts: dict[str, int] = {}
def attempt_recovery(self, name: str, plugin_dir: Path,
module: any, instance: any) -> bool:
"""尝试恢复插件"""
attempts = self._recovery_attempts.get(name, 0)
if attempts >= self.max_attempts:
ProLogger.error("recovery", f"插件 {name} 已达到最大恢复次数,放弃恢复")
return False
ProLogger.warn("recovery", f"尝试恢复插件 {name} (第 {attempts + 1} 次)")
try:
time.sleep(self.delay)
# 重新加载模块
if module and hasattr(module, '__file__'):
module_path = Path(module.__file__)
if module_path.exists():
spec = importlib.util.spec_from_file_location(
module.__name__, str(module_path)
)
new_module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = new_module
spec.loader.exec_module(new_module)
if hasattr(new_module, 'New'):
new_instance = new_module.New()
ProLogger.info("recovery", f"插件 {name} 恢复成功")
self._recovery_attempts[name] = 0
return new_instance
except Exception as e:
ProLogger.error("recovery", f"恢复插件 {name} 失败: {type(e).__name__}: {e}")
self._recovery_attempts[name] = attempts + 1
return False
def reset_attempts(self, name: str):
"""重置恢复尝试次数"""
self._recovery_attempts[name] = 0
def get_attempts(self, name: str) -> int:
"""获取恢复尝试次数"""
return self._recovery_attempts.get(name, 0)

View File

@@ -1,39 +0,0 @@
"""重试处理器"""
import time
import random
from typing import Callable, Any
from ..core.config import RetryConfig
from ..utils.logger import ProLogger
class RetryHandler:
"""重试处理器"""
def __init__(self, config: RetryConfig = None):
config = config or RetryConfig()
self.max_retries = config.max_retries
self.backoff_factor = config.backoff_factor
self.initial_delay = config.initial_delay
def execute(self, func: Callable, *args, **kwargs) -> Any:
"""执行带重试的调用"""
last_exception = None
for attempt in range(self.max_retries + 1):
try:
return func(*args, **kwargs)
except Exception as e:
last_exception = e
if attempt < self.max_retries:
delay = self._calculate_delay(attempt)
ProLogger.warn("retry", f"{attempt + 1} 次重试,等待 {delay:.1f}s: {e}")
time.sleep(delay)
raise last_exception
def _calculate_delay(self, attempt: int) -> float:
"""计算退避延迟"""
delay = self.initial_delay * (self.backoff_factor ** attempt)
jitter = random.uniform(0, delay * 0.1)
return delay + jitter

View File

@@ -1,60 +0,0 @@
"""插件加载 Pro - 日志工具"""
import sys
class ProLogger:
"""Pro 日志记录器 - 智能颜色识别"""
_COLORS = {
"reset": "\033[0m",
"white": "\033[0;37m",
"yellow": "\033[1;33m",
"blue": "\033[1;34m",
"red": "\033[1;31m",
}
@staticmethod
def _colorize(text: str, color: str) -> str:
"""添加颜色(终端支持时)"""
if not sys.stdout.isatty():
return text
return f"{ProLogger._COLORS.get(color, '')}{text}{ProLogger._COLORS['reset']}"
@staticmethod
def info(component: str, message: str):
"""正常日志 - 白色"""
tag = ProLogger._colorize(f"[pro:{component}]", "white")
msg = ProLogger._colorize(message, "white")
print(f"{tag} {msg}")
@staticmethod
def warn(component: str, message: str):
"""警告日志 - 黄色"""
tag = ProLogger._colorize(f"[pro:{component}]", "yellow")
icon = ProLogger._colorize("", "yellow")
msg = ProLogger._colorize(message, "yellow")
print(f"{tag} {icon} {msg}")
@staticmethod
def error(component: str, message: str):
"""错误日志 - 红色"""
tag = ProLogger._colorize(f"[pro:{component}]", "red")
icon = ProLogger._colorize("", "red")
msg = ProLogger._colorize(message, "red")
print(f"{tag} {icon} {msg}")
@staticmethod
def debug(component: str, message: str):
"""调试日志 - 蓝色"""
tag = ProLogger._colorize(f"[pro:{component}]", "blue")
icon = ProLogger._colorize("", "blue")
msg = ProLogger._colorize(message, "blue")
print(f"{tag} {icon} {msg}")
@staticmethod
def tip(component: str, message: str):
"""提示日志 - 蓝色(用于小提示/额外信息)"""
tag = ProLogger._colorize(f"[pro:{component}]", "blue")
icon = ProLogger._colorize("", "blue")
msg = ProLogger._colorize(message, "blue")
print(f"{tag} {icon} {msg}")

View File

@@ -1,269 +0,0 @@
"""WebUI 服务器 - 容器模式"""
import subprocess
import os
import tempfile
from oss.plugin.types import Response
from pathlib import Path
class WebUIServer:
"""WebUI 服务器"""
def __init__(self, router, config: dict):
self.router = router
self.config = config
self.frontend_dir = Path(__file__).parent.parent / "frontend"
# 页面注册表
self.pages = {} # path -> content_provider
self.nav_items = [] # 导航项列表
def start(self):
"""注册默认路由"""
# 静态资源
self.router.get("/static/css/main.css", self._handle_css)
self.router.get("/static/js/main.js", self._handle_js)
self.router.get("/health", self._handle_health)
# TUI 接口 - 供 TUI 转换层访问
self.router.get("/tui/index.html", self._handle_tui_index)
self.router.get("/tui/page", self._handle_tui_page)
self.router.get("/tui/css", self._handle_tui_css)
self.router.get("/tui/pages", self._handle_tui_pages)
def register_page(self, path: str, content_provider, nav_item: dict = None):
"""供其他插件注册页面"""
self.pages[path] = content_provider
if nav_item:
nav_item['url'] = path
self.nav_items.append(nav_item)
# 注册路由
self.router.get(path, lambda req: self._render_page(path, req))
def _render_page(self, path: str, request):
"""渲染页面布局 + 内容"""
provider = self.pages.get(path)
content = provider() if provider else ""
# 排序导航项(首页在前)
sorted_nav = sorted(self.nav_items, key=lambda x: 0 if x.get('url') == '/' else 1)
# 构建导航项 HTML
nav_html = ""
icon_map = {
'🏠': 'ri-home-4-line',
'📊': 'ri-dashboard-line',
'📋': 'ri-file-list-3-line',
'🧩': 'ri-puzzle-line',
'⚙️': 'ri-settings-3-line',
'🔌': 'ri-plug-line',
'📦': 'ri-box-3-line',
'🌐': 'ri-global-line',
}
for item in sorted_nav:
url = item.get('url', '#')
is_active = 'active' if url == path else ''
icon = item.get('icon', 'ri-dashboard-line')
text = item.get('text', '')
ri_icon = icon_map.get(icon, icon)
title = text
nav_html += f'''
<a href="{url}" class="nav-item {is_active}" title="{title}">
<i class="{ri_icon}"></i>
</a>
'''
page_title = self.config.get("title", "NebulaShell")
# 读取 HTML 模板
template_file = self.frontend_dir / "views" / "layout.html"
with open(template_file, 'r', encoding='utf-8') as f:
html_template = f.read()
html = html_template.replace('{{ pageTitle }}', page_title)
html = html.replace('{{ navItems }}', nav_html)
html = html.replace('{{ content }}', content)
return Response(
status=200,
headers={"Content-Type": "text/html; charset=utf-8"},
body=html
)
def _default_home_content(self) -> str:
"""默认首页内容"""
return """
<div class="home-content">
<div class="welcome-banner">
<h2>👋 欢迎使用 NebulaShell</h2>
<p>一切皆为插件的轻量级框架</p>
</div>
</div>
"""
def _execute_php(self, php_file: str, variables: dict = None) -> str:
"""执行 PHP 文件"""
variables = variables or {}
# 构建 PHP 变量注入
php_vars = ""
for key, value in variables.items():
if isinstance(value, dict):
php_vars += f"${key} = {self._php_array(value)};\n"
elif isinstance(value, list):
php_vars += f"${key} = {self._php_array_list(value)};\n"
elif isinstance(value, str):
php_vars += f"${key} = '{value.replace(chr(39), chr(92) + chr(39))}';\n"
else:
php_vars += f"${key} = {str(value).lower() if isinstance(value, bool) else value};\n"
with open(php_file, 'r', encoding='utf-8') as f:
php_content = f.read()
# 临时文件必须和 views 在同一目录,这样 __DIR__ 才能正确解析
views_dir = str(Path(php_file).parent)
tmp_file = os.path.join(views_dir, '.temp_render.php')
try:
with open(tmp_file, 'w', encoding='utf-8') as f:
f.write(f"<?php\n{php_vars}\n?>\n{php_content}")
result = subprocess.run(
["php", "-f", tmp_file],
capture_output=True, text=True, timeout=10, cwd=views_dir,
encoding='utf-8', errors='replace'
)
if result.returncode != 0:
print(f"[webui] PHP 执行错误: {result.stderr}")
return f"<div class='error'>PHP Error: {result.stderr}</div>"
return result.stdout
finally:
try:
if os.path.exists(tmp_file):
os.unlink(tmp_file)
except:
pass
def _php_array(self, py_dict: dict) -> str:
"""Python Dict -> PHP Array"""
items = []
for key, value in py_dict.items():
if isinstance(value, str):
items.append(f"'{key}' => '{value.replace(chr(39), chr(92) + chr(39))}'")
elif isinstance(value, dict):
items.append(f"'{key}' => {self._php_array(value)}")
else:
items.append(f"'{key}' => {value}")
return "[" + ", ".join(items) + "]"
def _php_array_list(self, py_list: list) -> str:
"""Python List -> PHP Array"""
items = []
for item in py_list:
if isinstance(item, dict):
items.append(self._php_array(item))
elif isinstance(item, str):
items.append(f"'{item.replace(chr(39), chr(92) + chr(39))}'")
else:
items.append(str(item))
return "[" + ", ".join(items) + "]"
def _handle_css(self, request):
css_file = self.frontend_dir / "assets" / "css" / "main.css"
with open(css_file, 'r', encoding='utf-8') as f:
css = f.read()
return Response(status=200, headers={"Content-Type": "text/css; charset=utf-8"}, body=css)
def _handle_js(self, request):
js_file = self.frontend_dir / "assets" / "js" / "main.js"
with open(js_file, 'r', encoding='utf-8') as f:
js = f.read()
return Response(status=200, headers={"Content-Type": "application/javascript; charset=utf-8"}, body=js)
def _handle_health(self, request):
import json
return Response(status=200, headers={"Content-Type": "application/json"}, body=json.dumps({"status": "ok"}))
# ========== TUI 接口实现 ==========
def _handle_tui_index(self, request):
"""处理 /tui/index.html 请求 - TUI 入口点
返回特殊标记的 HTMLTUI 转换层会识别并转换。
此 HTML 不含用户可见内容,仅包含 data-tui-* 标记和配置脚本。
"""
html = """<!DOCTYPE html>
<html class="tui-page" data-tui-version="2.0">
<head>
<meta charset="UTF-8">
<title>NebulaShell TUI</title>
<!-- TUI 标记:此页面专为终端渲染 -->
<style type="text/x-tui-css">
.tui-page { background-color: #000000; color: #ffffff; }
.tui-body { font-family: monospace; }
.bold { font-weight: bold; }
.underline { text-decoration: underline; }
</style>
</head>
<body class="tui-body">
<div class="tui-container" data-tui-layout="vertical">
<header data-tui-type="header">
<h1>NebulaShell TUI</h1>
<p>终端界面就绪</p>
</header>
<separator data-tui-char=""/>
<nav data-tui-type="nav" data-tui-layout="horizontal">
<a href="/" data-tui-action="navigate" data-tui-key="1">首页</a>
<a href="/dashboard" data-tui-action="navigate" data-tui-key="2">仪表盘</a>
<a href="/logs" data-tui-action="navigate" data-tui-key="3">日志</a>
</nav>
</div>
<script type="application/x-tui-keys">
{"1": {"action": "navigate", "target": "/"}, "2": {"action": "navigate", "target": "/dashboard"}, "3": {"action": "navigate", "target": "/logs"}}
</script>
</body>
</html>"""
return Response(status=200, headers={"Content-Type": "text/html; charset=utf-8"}, body=html)
def _handle_tui_page(self, request):
"""处理 /tui/page 请求 - 获取任意页面的 TUI 版本"""
from urllib.parse import parse_qs, urlparse
parsed = urlparse(request.path)
params = parse_qs(parsed.query)
page_path = params.get('path', ['/'])[0]
# 查找已注册的页面
provider = self.pages.get(page_path)
if provider:
content = provider()
html = f"""<!DOCTYPE html>
<html class="tui-page" data-tui-source="webui">
<body class="tui-body">{content}</body>
</html>"""
return Response(status=200, headers={"Content-Type": "text/html; charset=utf-8"}, body=html)
return Response(status=404, headers={"Content-Type": "text/html"}, body="<html><body>Page not found</body></html>")
def _handle_tui_css(self, request):
"""处理 /tui/css 请求 - 返回终端兼容的 CSS"""
css = """/* TUI 兼容 CSS */
.tui-page { background-color: #000000; color: #ffffff; }
.tui-body { font-family: monospace; }
.bold { font-weight: bold; }
.underline { text-decoration: underline; }
[data-tui-action] { cursor: pointer; }
"""
return Response(status=200, headers={"Content-Type": "text/css"}, body=css)
def _handle_tui_pages(self, request):
"""处理 /tui/pages 请求 - 列出所有可用页面"""
import json
pages = list(self.pages.keys())
return Response(
status=200,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': True, 'pages': pages})
)

View File

@@ -1,148 +0,0 @@
"""WebUI - Web 控制台 (容器模式) + TUI 双启动"""
from pathlib import Path
from oss.logger.logger import Log
from oss.plugin.types import Plugin, Response, register_plugin_type
from oss.config import get_config
from .core.server import WebUIServer
class WebUIPlugin(Plugin):
"""WebUI 插件 - 提供页面容器,同时启动 TUI"""
def __init__(self):
self.http_api = None
self.server = None
self.tui = None
self.config = {}
def meta(self):
from oss.plugin.types import Metadata, PluginConfig, Manifest
config = get_config()
return Manifest(
metadata=Metadata(
name="webui",
version="2.1.0",
author="NebulaShell",
description="Web 控制台容器 + TUI 双启动 - 供其他插件注册页面"
),
config=PluginConfig(
enabled=True,
args={
"port": config.get("HTTP_API_PORT", 8080),
"theme": "dark",
"title": "NebulaShell",
"tui_enabled": True # 默认启用 TUI
}
),
dependencies=["http-api"]
)
def set_http_api(self, http_api):
"""注入 http-api"""
self.http_api = http_api
def set_tui(self, tui):
"""注入 tui 引用"""
self.tui = tui
def init(self, deps: dict = None):
"""初始化 WebUI 服务器和 TUI"""
if not self.http_api:
Log.error("webui", "错误:未找到 http-api 依赖")
return
config = {}
if deps:
config = deps.get("config", {})
self.config = {
"port": config.get("port", get_config().get("HTTP_API_PORT", 8080)),
"theme": config.get("theme", "dark"),
"title": config.get("title", "NebulaShell"),
"tui_enabled": config.get("tui_enabled", True)
}
# 使用 http-api 的路由器
self.server = WebUIServer(
self.http_api.router,
self.config
)
Log.info("webui", "容器初始化完成")
# 如果启用了 TUI通知 TUI 插件
if self.config.get("tui_enabled") and self.tui:
Log.info("webui", "TUI 已启用,将双启动")
def start(self):
"""启动服务器(注册默认路由)"""
if self.server:
# 检测仪表盘是否已安装,自动设为首页
self._setup_home_page()
self.server.start()
Log.info("webui", f"WebUI 容器已启动http://localhost:{self.config['port']}")
# 如果启用了 TUI在后台启动
if self.config.get("tui_enabled"):
Log.info("webui", "TUI 双启动中...")
def _setup_home_page(self):
"""设置首页:如果仪表盘已安装则跳转到仪表盘,否则显示默认首页"""
# 通过文件系统检查 dashboard 是否存在
dashboard_exists = False
store_dirs = [
Path("store/@{NebulaShell}/dashboard"),
]
for d in store_dirs:
if d.exists() and (d / "main.py").exists():
dashboard_exists = True
break
if dashboard_exists:
# 仪表盘已安装,注册首页重定向到仪表盘
self.server.router.get("/", self._handle_home_redirect)
Log.info("webui", "检测到仪表盘,首页自动跳转到 /dashboard")
else:
# 默认首页
self.server.register_page(
path="/",
content_provider=self.server._default_home_content,
nav_item={'icon': 'ri-home-4-line', 'text': '首页'}
)
def _handle_home_redirect(self, request):
"""处理首页重定向到仪表盘"""
return Response(
status=302,
headers={"Location": "/dashboard", "Content-Type": "text/html"},
body=""
)
def stop(self):
Log.error("webui", "WebUI 容器已停止")
# --- 公开 API 供其他插件调用 ---
def register_page(self, path: str, content_provider, nav_item: dict = None):
"""
其他插件调用此方法注册页面。
:param path: 路由路径 (e.g., '/dashboard')
:param content_provider: 无参函数,返回 HTML 字符串
:param nav_item: 导航项 {'icon': '📊', 'text': '仪表盘'}
"""
if self.server:
self.server.register_page(path, content_provider, nav_item)
else:
Log.warn("webui", f"警告:试图注册页面 {path},但服务器未初始化")
def add_nav_item(self, item: dict):
"""仅添加导航项(如果页面由其他方式处理)"""
if self.server:
self.server.nav_items.append(item)
register_plugin_type("WebUIPlugin", WebUIPlugin)
def New():
return WebUIPlugin()

View File

@@ -1,112 +0,0 @@
"""静态资源"""
class StaticAssets:
"""静态资源管理器"""
@staticmethod
def get_css() -> str:
return """* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
background: #f5f5f5;
color: #333;
}
.app {
display: flex;
height: 100vh;
}
.sidebar {
width: 240px;
background: #1a1a2e;
color: #fff;
display: flex;
flex-direction: column;
}
.sidebar-header {
padding: 20px;
border-bottom: 1px solid rgba(255,255,255,0.1);
}
.sidebar-header h1 {
font-size: 18px;
}
.sidebar-nav {
flex: 1;
padding: 10px 0;
}
.nav-item {
display: flex;
align-items: center;
padding: 12px 20px;
color: #fff;
text-decoration: none;
transition: background 0.2s;
}
.nav-item:hover {
background: rgba(255,255,255,0.1);
}
.nav-item.active {
background: rgba(255,255,255,0.15);
border-left: 3px solid #4a90d9;
}
.nav-icon {
margin-right: 10px;
}
.sidebar-footer {
padding: 15px 20px;
border-top: 1px solid rgba(255,255,255,0.1);
}
.settings-btn {
width: 100%;
padding: 10px;
background: rgba(255,255,255,0.1);
border: none;
color: #fff;
border-radius: 6px;
cursor: pointer;
}
.content {
flex: 1;
display: flex;
flex-direction: column;
}
.content-header {
padding: 20px 30px;
background: #fff;
border-bottom: 1px solid #e0e0e0;
}
.content-body {
flex: 1;
padding: 30px;
}
.empty-state {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
color: #999;
}"""
@staticmethod
def get_js() -> str:
return """console.log('NebulaShell WebUI loaded');"""

File diff suppressed because it is too large Load Diff

View File

@@ -1,378 +0,0 @@
"""TUI 插件 - 终端用户界面,与 WebUI 双启动"""
import os
import sys
import threading
import time
from pathlib import Path
from oss.logger.logger import Log
from oss.plugin.types import Plugin, Response, register_plugin_type
from .tui.converter import TUIManager, TUIRenderer, HTMLToTUIConverter
class TUIPlugin(Plugin):
"""TUI 插件 - 提供终端界面,通过访问 WebUI 的 /tui 接口获取 HTML"""
def __init__(self):
self.webui = None
self.http_api = None
self.tui_manager = None
self.running = False
self.tui_thread = None
def meta(self):
from oss.plugin.types import Metadata, PluginConfig, Manifest
return Manifest(
metadata=Metadata(
name="tui",
version="1.0.0",
author="NebulaShell",
description="终端用户界面 - 与 WebUI 双启动"
),
config=PluginConfig(enabled=True, args={}),
dependencies=["http-api", "webui"]
)
def set_webui(self, webui):
"""注入 webui 引用"""
self.webui = webui
def set_http_api(self, http_api):
"""注入 http_api 引用"""
self.http_api = http_api
def init(self, deps: dict = None):
"""初始化 TUI"""
Log.info("tui", "TUI 插件初始化中...")
# 创建 TUI 管理器
self.tui_manager = TUIManager.get_instance()
# 注册 /tui 路由供 TUI 访问 WebUI 页面
if self.http_api and self.http_api.router:
# 注册 TUI 专用 API
self.http_api.router.get("/tui/index.html", self._handle_tui_index)
self.http_api.router.get("/tui/page", self._handle_tui_page)
self.http_api.router.get("/tui/css", self._handle_tui_css)
self.http_api.router.post("/tui/interact", self._handle_tui_interact)
Log.ok("tui", "已注册 TUI API 路由")
else:
Log.warn("tui", "警告:未找到 http-api 依赖")
# 加载默认页面(从 WebUI 获取)
self._load_default_pages()
Log.ok("tui", "TUI 插件初始化完成")
def _load_default_pages(self):
"""从 WebUI 加载默认页面到 TUI"""
# 模拟访问 WebUI 页面并缓存
default_pages = ["/", "/dashboard", "/logs", "/terminal"]
for path in default_pages:
try:
# 这里会通过内部调用获取 WebUI 渲染的 HTML
html = self._fetch_webui_page(path)
if html:
self.tui_manager.load_page(path, html)
Log.info("tui", f"已加载页面:{path}")
except Exception as e:
Log.warn("tui", f"加载页面 {path} 失败:{e}")
def _fetch_webui_page(self, path: str) -> str:
"""从 WebUI 获取页面 HTML"""
if not self.webui or not hasattr(self.webui, 'server'):
return ""
# 模拟请求获取 WebUI 页面
# 由于我们在同一进程,可以直接调用 server 的路由处理
try:
from oss.plugin.types import Request
request = Request(method="GET", path=path, headers={}, body="")
# 查找匹配的路由
router = self.webui.server.router
if hasattr(router, 'routes'):
for route_path, handler in router.routes.items():
if route_path == path or (route_path.endswith('*') and path.startswith(route_path[:-1])):
response = handler(request)
if response and hasattr(response, 'body'):
return response.body.decode('utf-8') if isinstance(response.body, bytes) else response.body
except Exception as e:
Log.warn("tui", f"获取 WebUI 页面失败:{e}")
return ""
def start(self):
"""启动 TUI在后台线程运行"""
Log.info("tui", "TUI 启动中...")
self.running = True
# 在后台线程运行 TUI
self.tui_thread = threading.Thread(target=self._tui_loop, daemon=True)
self.tui_thread.start()
Log.ok("tui", "TUI 已启动(后台模式)")
Log.info("tui", "提示:按 'q' 退出 TUIWebUI 仍在运行")
def _tui_loop(self):
"""TUI 主循环"""
try:
# 显示欢迎界面
self._show_welcome()
# 主事件循环
self._event_loop()
except Exception as e:
Log.error("tui", f"TUI 循环异常:{e}")
finally:
self.running = False
def _show_welcome(self):
"""显示欢迎界面"""
welcome_html = """
<!DOCTYPE html>
<html>
<head><title>NebulaShell TUI</title></head>
<body>
<h1>👋 欢迎使用 NebulaShell TUI</h1>
<p>终端用户界面已启动</p>
<p>WebUI 同时运行在http://localhost:8080</p>
<hr>
<h2>可用命令:</h2>
<ul>
<li>[1] 首页</li>
<li>[2] 仪表盘</li>
<li>[3] 日志</li>
<li>[4] 终端</li>
<li>[q] 退出 TUI</li>
<li>[r] 刷新</li>
</ul>
</body>
</html>
"""
self.tui_manager.load_page("/welcome", welcome_html)
self._render_current("/welcome")
def _render_current(self, path: str = None):
"""渲染当前页面到终端"""
if path is None:
path = self.tui_manager.current_page or "/welcome"
output = self.tui_manager.render_page(path)
# 清屏并输出
sys.stdout.write('\x1b[2J\x1b[H')
sys.stdout.write(output)
sys.stdout.write('\n\n')
sys.stdout.write('\x1b[90m提示按数字键导航q 退出\x1b[0m\n')
sys.stdout.flush()
def _event_loop(self):
"""简单的事件循环"""
import sys
import tty
import termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
while self.running:
char = sys.stdin.read(1)
if char == '\x03': # Ctrl+C
break
elif char == '\x04': # Ctrl+D
break
elif char == 'q':
Log.info("tui", "用户退出 TUI")
break
elif char == '1':
self._render_current("/")
elif char == '2':
self._render_current("/dashboard")
elif char == '3':
self._render_current("/logs")
elif char == '4':
self._render_current("/terminal")
elif char == 'r':
self._load_default_pages()
self._render_current()
elif char == '\n' or char == '\r':
# Enter 刷新当前页
self._render_current()
except Exception as e:
Log.error("tui", f"事件循环错误:{e}")
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
def _handle_tui_index(self, request):
"""处理 /tui/index.html 请求"""
# 返回特殊标记的 HTMLTUI 会识别并转换
html = """<!DOCTYPE html>
<html class="tui-page">
<head>
<meta charset="UTF-8">
<title>NebulaShell TUI</title>
<!-- TUI 标记:此页面专为终端渲染 -->
</head>
<body class="tui-body">
<div class="tui-container">
<h1>NebulaShell TUI</h1>
<p>终端界面就绪</p>
<div class="tui-nav">
<a href="/" data-tui-action="navigate">首页</a>
<a href="/dashboard" data-tui-action="navigate">仪表盘</a>
<a href="/logs" data-tui-action="navigate">日志</a>
<a href="/terminal" data-tui-action="navigate">终端</a>
</div>
</div>
<!-- TUI 脚本标记:这些会被转换为键盘绑定 -->
<script type="application/x-tui-keys">
{"1": "/", "2": "/dashboard", "3": "/logs", "4": "/terminal", "q": "quit", "r": "refresh"}
</script>
</body>
</html>"""
return Response(
status=200,
headers={"Content-Type": "text/html; charset=utf-8"},
body=html
)
def _handle_tui_page(self, request):
"""处理 /tui/page 请求 - 获取任意页面的 TUI 版本"""
from urllib.parse import parse_qs, urlparse
parsed = urlparse(request.path)
params = parse_qs(parsed.query)
page_path = params.get('path', ['/'])[0]
# 从 WebUI 获取原始 HTML
html = self._fetch_webui_page(page_path)
if html:
# 添加 TUI 标记
html = html.replace('<html', '<html class="tui-page"')
if '<body' in html:
html = html.replace('<body', '<body class="tui-body"')
else:
html = html.replace('</head>', '<body class="tui-body"></head>')
return Response(
status=200,
headers={"Content-Type": "text/html; charset=utf-8"},
body=html
)
else:
return Response(
status=404,
headers={"Content-Type": "text/html"},
body="<html><body>Page not found</body></html>"
)
def _handle_tui_css(self, request):
"""处理 /tui/css 请求 - 返回终端兼容的 CSS"""
# 只返回终端支持的 CSS 属性
css = """/* TUI 兼容 CSS */
.tui-page {
/* 背景色 - 仅支持 ANSI 颜色 */
background-color: #000000;
color: #ffffff;
}
.tui-body {
font-family: monospace;
font-weight: normal;
}
/* 字体样式 - TUI 支持 */
.bold { font-weight: bold; }
.underline { text-decoration: underline; }
/* 布局 - TUI 简化处理 */
.tui-container {
padding: 0;
margin: 0;
}
/* 交互元素标记 */
[data-tui-action] {
cursor: pointer;
}
"""
return Response(
status=200,
headers={"Content-Type": "text/css"},
body=css
)
def _handle_tui_interact(self, request):
"""处理 TUI 交互请求"""
import json
try:
body = json.loads(request.body)
action = body.get('action', '')
target = body.get('target', '')
# 处理交互
if action == 'navigate':
# 导航到指定页面
html = self._fetch_webui_page(target)
if html:
self.tui_manager.load_page(target, html)
return Response(
status=200,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': True, 'page': target})
)
elif action == 'click':
# 处理点击
return Response(
status=200,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': True})
)
elif action == 'keypress':
# 处理按键
key = body.get('key', '')
return Response(
status=200,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': True, 'key': key})
)
return Response(
status=400,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': 'Unknown action'})
)
except Exception as e:
return Response(
status=500,
headers={"Content-Type": "application/json"},
body=json.dumps({'success': False, 'error': str(e)})
)
def stop(self):
"""停止 TUI"""
Log.info("tui", "TUI 停止中...")
self.running = False
if self.tui_thread:
self.tui_thread.join(timeout=2)
Log.ok("tui", "TUI 已停止")
register_plugin_type("TUIPlugin", TUIPlugin)
def New():
return TUIPlugin()

View File

@@ -1,31 +0,0 @@
"""WebSocket API 插件入口 - 简化版"""
from oss.logger.logger import Log
from oss.plugin.types import Plugin, register_plugin_type
class WsApiPlugin(Plugin):
"""WebSocket API 插件"""
def __init__(self):
self._running = False
def init(self, deps: dict = None):
"""初始化"""
Log.info("ws-api", "初始化完成")
def start(self):
"""启动"""
self._running = True
Log.info("ws-api", "已启动")
def stop(self):
"""停止"""
self._running = False
Log.error("ws-api", "已停止")
register_plugin_type("WsApiPlugin", WsApiPlugin)
def New():
return WsApiPlugin()

View File

@@ -1,44 +0,0 @@
"""WebSocket 中间件链"""
from typing import Callable, Optional, Any
class WsMiddleware:
"""WebSocket 中间件基类"""
async def process(self, client: Any, message: str, next_fn: Callable) -> Optional[str]:
"""处理消息"""
return await next_fn()
class AuthMiddleware(WsMiddleware):
"""认证中间件"""
async def process(self, client, message, next_fn):
# 可以在这里验证 token
return await next_fn()
class WsMiddlewareChain:
"""WebSocket 中间件链"""
def __init__(self):
self.middlewares: list[WsMiddleware] = []
def add(self, middleware: WsMiddleware):
"""添加中间件"""
self.middlewares.append(middleware)
async def run(self, client, message) -> Optional[str]:
"""执行中间件链"""
idx = 0
current_message = message
async def next_fn(msg=None):
nonlocal idx, current_message
if msg is not None:
current_message = msg
if idx < len(self.middlewares):
mw = self.middlewares[idx]
idx += 1
return await mw.process(client, current_message, next_fn)
return current_message
return await next_fn()

View File

@@ -1,39 +0,0 @@
"""WebSocket 路由器"""
import json
import asyncio
from typing import Callable, Optional, Any
from .server import WsClient
class WsRoute:
"""WebSocket 路由"""
def __init__(self, path: str, handler: Callable):
self.path = path
self.handler = handler
class WsRouter:
"""WebSocket 路由器"""
def __init__(self):
self.routes: dict[str, WsRoute] = {}
def on_message(self, path: str, handler: Callable):
"""注册消息路由"""
self.routes[path] = WsRoute(path, handler)
async def handle(self, client: WsClient, path: str, message: str):
"""处理消息"""
# 精确匹配
if path in self.routes:
await self.routes[path].handler(client, message)
return
# 前缀匹配
for route_path, route in self.routes.items():
if path.startswith(route_path):
await route.handler(client, message)
return
# 无匹配路由
await client.send({"error": "No handler for path", "path": path})

View File

@@ -1,125 +0,0 @@
"""WebSocket 服务器核心"""
import asyncio
import websockets
import threading
import json
from typing import Any, Callable, Optional
from .events import WsEvent, EVENT_CONNECT, EVENT_DISCONNECT, EVENT_MESSAGE
class WsClient:
"""WebSocket 客户端连接"""
def __init__(self, websocket, path: str):
self.websocket = websocket
self.path = path
self.id = id(websocket)
self.closed = False
async def send(self, message: Any):
"""发送消息"""
if not self.closed:
data = json.dumps(message, ensure_ascii=False) if isinstance(message, dict) else str(message)
await self.websocket.send(data)
async def close(self):
"""关闭连接"""
self.closed = True
await self.websocket.close()
class WsServer:
"""WebSocket 服务器"""
def __init__(self, router, middleware, event_bus, host="0.0.0.0", port=8081):
self.host = host
self.port = port
self.router = router
self.middleware = middleware
self.event_bus = event_bus
self._server = None
self._loop = None
self._thread = None
self._clients: dict[int, WsClient] = {}
def start(self):
"""启动服务器"""
self._loop = asyncio.new_event_loop()
self._thread = threading.Thread(target=self._run_loop, daemon=True)
self._thread.start()
def _run_loop(self):
"""运行事件循环"""
asyncio.set_event_loop(self._loop)
start_server = websockets.serve(
self._handle_connection,
self.host,
self.port
)
self._loop.run_until_complete(start_server)
self._loop.run_forever()
async def _handle_connection(self, websocket, path=None):
"""处理客户端连接(兼容 websockets 新旧版本)"""
# websockets 16.0+ 只传入 connection 参数
if path is None:
# 新版本:从 websocket.request 获取路径
try:
path = websocket.request.path
except AttributeError:
path = "/"
client = WsClient(websocket, path)
self._clients[client.id] = client
# 触发连接事件
self.event_bus.emit(WsEvent(
type=EVENT_CONNECT,
client=client,
path=path
))
try:
async for message in websocket:
# 触发消息事件
self.event_bus.emit(WsEvent(
type=EVENT_MESSAGE,
client=client,
path=path,
message=message
))
# 路由处理
await self.router.handle(client, path, message)
except websockets.exceptions.ConnectionClosed:
pass
finally:
del self._clients[client.id]
# 触发断开事件
self.event_bus.emit(WsEvent(
type=EVENT_DISCONNECT,
client=client,
path=path
))
def stop(self):
"""停止服务器"""
if self._loop and self._loop.is_running():
self._loop.call_soon_threadsafe(self._loop.stop)
print("[ws-api] 服务器已停止")
def broadcast(self, message: Any, exclude_client: int = None):
"""广播消息"""
async def _broadcast():
for client_id, client in self._clients.items():
if exclude_client and client_id == exclude_client:
continue
await client.send(message)
if self._loop and self._loop.is_running():
asyncio.run_coroutine_threadsafe(_broadcast(), self._loop)
def get_clients(self) -> list[WsClient]:
"""获取所有客户端"""
return list(self._clients.values())

View File

@@ -1,33 +1,14 @@
"""PL 注入 - 向插件加载器注册依赖自动安装功能
此文件通过 PL 注入机制向插件加载器注册以下功能
- auto-dependency:scan: 扫描所有插件的系统依赖声明
- auto-dependency:check: 检查系统依赖是否已安装
- auto-dependency:install: 自动安装缺失的系统依赖
- auto-dependency:info: 获取插件系统信息
"""
def register(injector):
"""向插件加载器注册功能
Args:
injector: PLInjector 实例提供 register_function 等方法
"""
# 注意:实际的功能实现由 main.py 中的 AutoDependencyPlugin 提供
# 这里我们通过导入插件实例来注册功能
from pathlib import Path
# 获取当前插件目录
current_file = Path(__file__)
plugin_dir = current_file.parent.parent
# 导入插件主模块
main_file = plugin_dir / "main.py"
# 创建安全的执行环境来加载插件
# 注意:不能直接使用 __builtins__ 关键字,通过变量间接设置
safe_builtins_dict = {
"True": True, "False": False, "None": None,
"dict": dict, "list": list, "str": str, "int": int,
@@ -52,7 +33,6 @@ def register(injector):
"__file__": str(main_file),
"Path": Path,
}
# 动态设置 builtins避免静态检查
safe_globals["__builtins__"] = safe_builtins_dict
try:
@@ -62,18 +42,15 @@ def register(injector):
code = compile(source, str(main_file), "exec")
exec(code, safe_globals)
# 获取 New 函数并创建插件实例
new_func = safe_globals.get("New")
if new_func and callable(new_func):
plugin_instance = new_func()
# 初始化插件
plugin_instance.init({
"scan_dirs": ["store"],
"auto_install": True
})
# 使用插件实例注册 PL 功能
plugin_instance.register_pl_functions(injector)
except Exception as e:

View File

@@ -1,12 +1,3 @@
"""依赖自动安装插件 - 扫描所有插件的声明文件,检查并安装系统依赖
功能说明
1. 扫描所有插件目录下的 manifest.json 文件
2. 读取每个插件声明的系统依赖 (system_dependencies 字段)
3. 检查这些系统依赖是否已安装
4. 对于未安装的依赖使用系统包管理器自动安装
5. 通过 PL 注入机制向插件加载器注册功能接口
"""
import subprocess
import shutil
import json
@@ -16,20 +7,6 @@ from oss.plugin.types import Plugin
class SystemDependencyChecker:
"""系统依赖检查器"""
def __init__(self):
self.package_managers = {
"apt": ["apt-get", "apt"],
"yum": ["yum", "dnf"],
"pacman": ["pacman"],
"brew": ["brew"],
"apk": ["apk"],
}
self.detected_pm = self._detect_package_manager()
def _detect_package_manager(self) -> str:
"""检测系统包管理器"""
for pm, commands in self.package_managers.items():
for cmd in commands:
if shutil.which(cmd):
@@ -37,11 +14,6 @@ class SystemDependencyChecker:
return "unknown"
def check_command(self, command: str) -> bool:
"""检查命令是否可用"""
return shutil.which(command) is not None
def check_package(self, package: str) -> bool:
"""检查系统包是否已安装"""
if not self.detected_pm or self.detected_pm == "unknown":
return False
@@ -92,66 +64,6 @@ class SystemDependencyChecker:
return False
def install_package(self, package: str) -> bool:
"""安装系统包"""
if not self.detected_pm or self.detected_pm == "unknown":
return False
try:
if self.detected_pm in ["apt", "apt-get"]:
result = subprocess.run(
["apt-get", "install", "-y", package],
capture_output=True,
text=True,
timeout=300
)
return result.returncode == 0
elif self.detected_pm == "yum":
result = subprocess.run(
["yum", "install", "-y", package],
capture_output=True,
text=True,
timeout=300
)
return result.returncode == 0
elif self.detected_pm == "dnf":
result = subprocess.run(
["dnf", "install", "-y", package],
capture_output=True,
text=True,
timeout=300
)
return result.returncode == 0
elif self.detected_pm == "pacman":
result = subprocess.run(
["pacman", "-S", "--noconfirm", package],
capture_output=True,
text=True,
timeout=300
)
return result.returncode == 0
elif self.detected_pm == "brew":
result = subprocess.run(
["brew", "install", package],
capture_output=True,
text=True,
timeout=300
)
return result.returncode == 0
elif self.detected_pm == "apk":
result = subprocess.run(
["apk", "add", package],
capture_output=True,
text=True,
timeout=300
)
return result.returncode == 0
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
pass
return False
def check_and_install(self, package: str, auto_install: bool = True) -> Dict[str, Any]:
"""检查并安装包"""
result = {
"package": package,
"installed": self.check_package(package),
@@ -183,49 +95,23 @@ class SystemDependencyChecker:
class AutoDependencyPlugin(Plugin):
"""依赖自动安装插件"""
def __init__(self):
self.checker = SystemDependencyChecker()
self.scan_dirs: List[str] = []
self.auto_install: bool = True
self._plugin_loader_ref: Optional[Any] = None
def init(self, deps: Optional[Dict[str, Any]] = None):
"""初始化插件"""
if deps:
self.scan_dirs = deps.get("scan_dirs", ["store"])
self.auto_install = deps.get("auto_install", True)
# 获取插件加载器引用(通过依赖注入)
if "plugin-loader" in deps:
self._plugin_loader_ref = deps["plugin-loader"]
def start(self):
"""启动插件"""
pass
def stop(self):
"""停止插件"""
pass
def scan_plugin_manifests(self, base_dir: str = "store") -> List[Dict[str, Any]]:
"""扫描所有插件的 manifest.json 文件
Returns:
包含所有插件信息的列表每个元素包含
- plugin_name: 插件名称
- plugin_dir: 插件目录路径
- manifest: manifest.json 内容
- system_dependencies: 系统依赖列表
"""
results = []
base_path = Path(base_dir)
if not base_path.exists():
return results
# 扫描所有插件目录
for vendor_dir in base_path.iterdir():
if not vendor_dir.is_dir():
continue
@@ -242,7 +128,6 @@ class AutoDependencyPlugin(Plugin):
with open(manifest_file, "r", encoding="utf-8") as f:
manifest = json.load(f)
# 提取系统依赖
system_deps = manifest.get("system_dependencies", [])
results.append({
@@ -258,23 +143,9 @@ class AutoDependencyPlugin(Plugin):
return results
def check_all_dependencies(self, base_dir: str = "store") -> Dict[str, Any]:
"""检查所有插件的系统依赖
Args:
base_dir: 基础扫描目录
Returns:
检查结果字典包含
- total_plugins: 扫描的插件总数
- plugins_with_deps: 有系统依赖的插件数
- dependencies: 依赖检查结果列表
- missing_count: 缺失的依赖数量
- installed_count: 已安装的依赖数量
"""
plugins = self.scan_plugin_manifests(base_dir)
all_deps = {} # {package: [plugin_names]}
for plugin in plugins:
all_deps = {} for plugin in plugins:
for dep in plugin["system_dependencies"]:
if dep not in all_deps:
all_deps[dep] = []
@@ -306,18 +177,6 @@ class AutoDependencyPlugin(Plugin):
}
def install_missing_dependencies(self, base_dir: str = "store") -> Dict[str, Any]:
"""安装所有缺失的系统依赖
Args:
base_dir: 基础扫描目录
Returns:
安装结果字典包含
- total_to_install: 需要安装的包数量
- success_count: 成功安装的包数量
- failed_count: 安装失败的包数量
- results: 每个包的安装结果
"""
check_result = self.check_all_dependencies(base_dir)
to_install = [dep for dep in check_result["dependencies"] if not dep["installed"]]
@@ -344,36 +203,13 @@ class AutoDependencyPlugin(Plugin):
}
def get_system_info(self) -> Dict[str, Any]:
"""获取系统信息"""
return {
"package_manager": self.checker.detected_pm,
"auto_install_enabled": self.auto_install,
"scan_directories": self.scan_dirs
}
def register_pl_functions(self, injector: Any):
"""注册 PL 注入功能
通过 PL 注入机制向插件加载器注册以下功能
- auto-dependency:scan: 扫描所有插件的系统依赖
- auto-dependency:check: 检查依赖安装状态
- auto-dependency:install: 安装缺失的依赖
- auto-dependency:info: 获取插件系统信息
"""
# 注册扫描功能
def scan_deps(scan_dir: str = "store") -> Dict[str, Any]:
"""扫描所有插件的声明文件"""
return self.scan_plugin_manifests(scan_dir)
injector.register_function(
"auto-dependency:scan",
scan_deps,
"扫描所有插件的声明文件,获取系统依赖列表"
)
# 注册检查功能
def check_deps(scan_dir: str = "store") -> Dict[str, Any]:
"""检查所有系统依赖的安装状态"""
return self.check_all_dependencies(scan_dir)
injector.register_function(
@@ -382,20 +218,7 @@ class AutoDependencyPlugin(Plugin):
"检查所有插件声明的系统依赖是否已安装"
)
# 注册安装功能
def install_deps(scan_dir: str = "store") -> Dict[str, Any]:
"""安装所有缺失的系统依赖"""
return self.install_missing_dependencies(scan_dir)
injector.register_function(
"auto-dependency:install",
install_deps,
"自动安装所有缺失的系统依赖"
)
# 注册信息功能
def get_info() -> Dict[str, Any]:
"""获取插件系统信息"""
return self.get_system_info()
injector.register_function(
@@ -406,5 +229,3 @@ class AutoDependencyPlugin(Plugin):
def New() -> AutoDependencyPlugin:
"""创建插件实例"""
return AutoDependencyPlugin()

View File

@@ -0,0 +1,44 @@
def check(self, filepath: str, content: str) -> list:
issues = []
try:
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
lines = node.end_lineno - node.lineno
if lines > 100:
issues.append({
"file": filepath,
"line": node.lineno,
"severity": "warning",
"type": "long_function",
"message": f"函数 {node.name} 过长 ({lines} 行)"
})
except:
pass
return issues
def _check_parameter_count(self, filepath: str, content: str) -> list:
issues = []
try:
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
complexity = self._calculate_complexity(node)
if complexity > 10:
issues.append({
"file": filepath,
"line": node.lineno,
"severity": "warning",
"type": "high_complexity",
"message": f"函数 {node.name} 复杂度过高 (圈复杂度: {complexity})"
})
except:
pass
return issues
def _calculate_complexity(self, node: ast.AST) -> int:

View File

@@ -1,14 +1,4 @@
"""引用检查器 - 检测导入错误、变量错误等"""
import ast
import sys
import os
from pathlib import Path
class ReferenceChecker:
"""引用检查器"""
# Python 标准库模块列表
STD_MODULES = {
'os', 'sys', 'json', 're', 'time', 'datetime', 'pathlib',
'typing', 'collections', 'functools', 'itertools', 'io',
@@ -26,7 +16,6 @@ class ReferenceChecker:
'base64', 'binascii', 'quopri', 'uu',
}
# Python 内置函数和类型(不应报告为未定义)
BUILTINS = {
'print', 'len', 'str', 'int', 'float', 'bool', 'list', 'dict',
'set', 'tuple', 'range', 'enumerate', 'zip', 'map', 'filter',
@@ -52,30 +41,6 @@ class ReferenceChecker:
self._scan_project_modules()
def _scan_project_modules(self):
"""扫描项目中的可用模块"""
# 扫描 oss 目录(框架核心)
oss_dir = self.project_root / "oss"
if oss_dir.exists():
self._available_modules.add("oss")
self._scan_module_dir(oss_dir, "oss")
# 扫描 store 目录下的所有插件
store_dir = self.project_root / "store"
if store_dir.exists():
for author_dir in store_dir.iterdir():
if not author_dir.is_dir():
continue
for plugin_dir in author_dir.iterdir():
if not plugin_dir.is_dir():
continue
plugin_name = plugin_dir.name
# 添加插件名作为可用模块
self._available_modules.add(plugin_name)
# 扫描插件内部的子模块
self._scan_plugin_modules(plugin_dir, plugin_name)
def _scan_module_dir(self, dir_path: Path, base_name: str):
"""扫描模块目录"""
if dir_path.exists():
for item in dir_path.iterdir():
if item.is_file() and item.name.endswith(".py") and item.name != "__init__.py":
@@ -88,19 +53,6 @@ class ReferenceChecker:
self._scan_module_dir(item, full_name)
def _scan_plugin_modules(self, plugin_dir: Path, base_name: str):
"""扫描插件内部的子模块"""
for item in plugin_dir.iterdir():
if item.is_dir() and (item / "__init__.py").exists():
full_name = f"{base_name}.{item.name}"
self._available_modules.add(full_name)
self._scan_module_dir(item, full_name)
elif item.is_file() and item.name.endswith(".py") and item.name != "__init__.py":
module_name = item.name[:-3]
full_name = f"{base_name}.{module_name}"
self._available_modules.add(full_name)
def _add_module_from_dir(self, dir_path: Path, base_name: str):
"""从目录添加模块"""
if dir_path.exists():
for item in dir_path.iterdir():
if item.is_file() and item.name.endswith(".py") and item.name != "__init__.py":
@@ -110,43 +62,14 @@ class ReferenceChecker:
self._add_module_from_dir(item, f"{base_name}.{item.name}")
def check(self, filepath: str, content: str) -> list:
"""执行引用检查"""
issues = []
try:
tree = ast.parse(content)
except SyntaxError as e:
return [{
"file": filepath,
"line": e.lineno or 0,
"severity": "critical",
"type": "syntax_error",
"message": f"语法错误: {e.msg}"
}]
# 检查导入语句(跳过相对导入)
issues.extend(self._check_imports(filepath, tree))
# 检查属性访问错误
issues.extend(self._check_attribute_access(filepath, tree, content))
# 检查函数调用错误
issues.extend(self._check_function_calls(filepath, tree, content))
return issues
def _check_imports(self, filepath: str, tree: ast.AST) -> list:
"""检查导入语句"""
issues = []
file_path = Path(filepath)
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
# 跳过 oss 框架模块(运行时可用)
if alias.name.startswith('oss.') or alias.name == 'oss':
continue
# 跳过 websockets 等第三方库
if alias.name in ('websockets', 'yaml', 'click'):
continue
if not self._is_module_available(alias.name, file_path):
@@ -159,15 +82,12 @@ class ReferenceChecker:
})
elif isinstance(node, ast.ImportFrom):
# 跳过相对导入(以 . 开头)
if node.level and node.level > 0:
continue
# 跳过 oss 框架模块
if node.module and (node.module.startswith('oss.') or node.module == 'oss'):
continue
# 跳过第三方库
if node.module and node.module.split('.')[0] in ('websockets', 'yaml', 'click'):
continue
@@ -184,32 +104,10 @@ class ReferenceChecker:
return issues
def _check_variable_references(self, filepath: str, tree: ast.AST, content: str) -> list:
"""检查变量引用"""
issues = []
lines = content.split('\n')
for node in ast.walk(tree):
if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Load):
# 检查是否引用了未定义的变量
if not self._is_name_defined(node.id, tree, node.lineno):
if node.id not in ('True', 'False', 'None', 'self', 'cls'):
issues.append({
"file": filepath,
"line": node.lineno,
"severity": "warning",
"type": "undefined_variable",
"message": f"使用了未定义的变量: {node.id}"
})
return issues
def _check_attribute_access(self, filepath: str, tree: ast.AST, content: str) -> list:
"""检查属性访问"""
issues = []
for node in ast.walk(tree):
if isinstance(node, ast.Attribute):
# 检查可能的属性错误
if isinstance(node.value, ast.Name):
var_name = node.value.id
if var_name in ('None', 'True', 'False'):
@@ -224,56 +122,28 @@ class ReferenceChecker:
return issues
def _check_function_calls(self, filepath: str, tree: ast.AST, content: str) -> list:
"""检查函数调用"""
issues = []
for node in ast.walk(tree):
if isinstance(node, ast.Call):
# 检查调用不存在的方法
if isinstance(node.func, ast.Attribute):
if isinstance(node.func.value, ast.Constant) and node.func.value.value is None:
issues.append({
"file": filepath,
"line": node.lineno,
"severity": "critical",
"type": "method_call_on_none",
"message": f"在 None 上调用方法: {node.func.attr}"
})
return issues
def _is_module_available(self, module_name: str, file_path: Path = None) -> bool:
"""检查模块是否可用"""
# 检查是否在已扫描的模块中
if module_name in self._available_modules:
return True
# 检查标准库
base_module = module_name.split('.')[0]
if base_module in self.STD_MODULES:
return True
# 检查是否是 oss 框架模块
if module_name.startswith('oss.') or module_name == 'oss':
return True
# 检查是否是常见第三方库
third_party = {'websockets', 'yaml', 'click', 'requests', 'flask', 'django', 'numpy', 'pandas'}
if module_name.split('.')[0] in third_party:
return True
# 检查是否是当前文件的同目录模块(相对导入的情况)
if file_path:
file_dir = file_path.parent
# 检查同级 .py 文件
sibling_module = file_dir / f"{module_name}.py"
if sibling_module.exists():
return True
# 检查同级包
sibling_pkg = file_dir / module_name
if sibling_pkg.is_dir() and (sibling_pkg / "__init__.py").exists():
return True
# 检查 store 目录下的插件
store_dir = self.project_root / "store"
if store_dir.exists():
for author_dir in store_dir.iterdir():
@@ -285,39 +155,3 @@ class ReferenceChecker:
return False
def _is_name_defined(self, name: str, tree: ast.AST, line: int) -> bool:
"""检查名称是否已定义"""
# 检查是否是内置函数/类型
if name in self.BUILTINS:
return True
# 检查是否是函数参数
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
for arg in node.args.args:
if arg.arg == name:
return True
# 检查是否是赋值目标
elif isinstance(node, ast.Assign):
for target in node.targets:
if isinstance(target, ast.Name) and target.id == name:
return True
# 检查是否是循环变量
elif isinstance(node, ast.For):
if isinstance(node.target, ast.Name) and node.target.id == name:
return True
# 检查是否是导入
elif isinstance(node, ast.Import):
for alias in node.names:
if alias.asname == name or alias.name == name:
return True
elif isinstance(node, ast.ImportFrom):
if node.module:
for alias in node.names:
if alias.asname == name or alias.name == name:
return True
return False

View File

@@ -0,0 +1,34 @@
def check(self, filepath: str, content: str) -> list:
issues = []
patterns = ['password', 'secret', 'token', 'api_key', 'access_token']
for i, line in enumerate(content.split('\n'), 1):
stripped = line.strip()
if stripped.startswith(' continue
for pattern in patterns:
if pattern + ' = "' in line.lower() or pattern + " = '" in line.lower():
issues.append({
"file": filepath,
"line": i,
"severity": "critical",
"type": "hardcoded_secret",
"message": f"发现硬编码密钥: {line.strip()[:50]}"
})
return issues
def _check_dangerous_functions(self, filepath: str, content: str) -> list:
issues = []
if '../' in content and 'open(' in content:
issues.append({
"file": filepath,
"line": 0,
"severity": "warning",
"type": "path_traversal_risk",
"message": "可能存在路径穿越漏洞"
})
return issues

View File

@@ -0,0 +1,27 @@
def check(self, filepath: str, content: str) -> list:
issues = []
for i, line in enumerate(content.split('\n'), 1):
if len(line) > 120:
issues.append({
"file": filepath,
"line": i,
"severity": "info",
"type": "line_too_long",
"message": f"行过长 ({len(line)} 字符)"
})
return issues
def _check_blank_lines(self, filepath: str, content: str) -> list:
if content and not content.endswith('\n'):
return [{
"file": filepath,
"line": len(content.split('\n')),
"severity": "info",
"type": "missing_final_newline",
"message": "文件末尾缺少换行符"
}]
return []

View File

@@ -0,0 +1,34 @@
def __init__(self, config: dict):
self.config = config
self.security = SecurityChecker()
self.quality = QualityChecker()
self.style = StyleChecker()
self.references = ReferenceChecker()
self.formatter = ReportFormatter(config.get("report_format", "console"))
def run_check(self, scan_dirs: list) -> dict:
issues = []
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
issues.extend(self.security.check(filepath, content))
issues.extend(self.quality.check(filepath, content))
issues.extend(self.style.check(filepath, content))
issues.extend(self.references.check(filepath, content))
except Exception as e:
issues.append({
"file": filepath,
"line": 0,
"severity": "error",
"type": "parse_error",
"message": f"文件解析失败: {e}"
})
return issues

View File

@@ -1,15 +1,3 @@
"""代码审查器插件"""
import sys
import os
sys.path.insert(0, os.path.dirname(__file__))
from oss.logger.logger import Log
from oss.plugin.types import Plugin, register_plugin_type
from core.reviewer import CodeReviewer
class CodeReviewerPlugin(Plugin):
"""代码审查器插件"""
def __init__(self):
self.reviewer = None
@@ -58,13 +46,3 @@ class CodeReviewerPlugin(Plugin):
Log.error("code-reviewer", "插件已停止")
def check(self, dirs: list = None) -> dict:
"""执行代码检查"""
scan_dirs = dirs or self.config["scan_dirs"]
return self.reviewer.run_check(scan_dirs)
register_plugin_type("CodeReviewerPlugin", CodeReviewerPlugin)
def New():
return CodeReviewerPlugin()

View File

@@ -1,22 +1,8 @@
"""报告格式化器"""
class ReportFormatter:
"""报告格式化器"""
def __init__(self, format_type: str = "console"):
self.format_type = format_type
def format(self, result: dict) -> str:
"""格式化报告"""
if self.format_type == "console":
return self._format_console(result)
elif self.format_type == "json":
return self._format_json(result)
return str(result)
def _format_console(self, result: dict) -> str:
"""控制台格式"""
lines = []
lines.append("=" * 60)
lines.append("代码审查报告")
@@ -26,7 +12,6 @@ class ReportFormatter:
lines.append(f"扫描时间: {result['scan_time']}s")
lines.append("")
# 按严重程度分类
critical = [i for i in result['issues'] if i['severity'] == 'critical']
warning = [i for i in result['issues'] if i['severity'] == 'warning']
info = [i for i in result['issues'] if i['severity'] == 'info']
@@ -44,8 +29,7 @@ class ReportFormatter:
if warning:
lines.append("警告:")
for issue in warning[:10]: # 最多显示10个
lines.append(f" - {issue['file']}:{issue['line']} - {issue['message']}")
for issue in warning[:10]: lines.append(f" - {issue['file']}:{issue['line']} - {issue['message']}")
if len(warning) > 10:
lines.append(f" ... 还有 {len(warning) - 10} 个警告")
lines.append("")
@@ -54,6 +38,3 @@ class ReportFormatter:
return '\n'.join(lines)
def _format_json(self, result: dict) -> str:
"""JSON 格式"""
import json
return json.dumps(result, indent=2, ensure_ascii=False)

View File

@@ -1,24 +1,8 @@
"""Dashboard 仪表盘插件"""
import os
import time
import json
import socket
import subprocess
import platform
import psutil
from collections import deque
from oss.logger.logger import Log
from oss.plugin.types import Plugin, Response, register_plugin_type
class DashboardPlugin(Plugin):
"""仪表盘插件 - 依赖 WebUI 容器"""
def __init__(self):
self.webui = None
self.views_dir = os.path.join(os.path.dirname(__file__), 'views')
self._start_time = time.time() # 记录插件启动时间(即项目启动时间)
self._history_len = 60
self._start_time = time.time() self._history_len = 60
self._cpu_history = deque(maxlen=self._history_len)
self._ram_history = deque(maxlen=self._history_len)
self._net_recv_history = deque(maxlen=self._history_len)
@@ -61,66 +45,12 @@ class DashboardPlugin(Plugin):
Log.warn("dashboard", "警告: 未找到 WebUI 依赖")
def _get_uptime_str(self):
"""计算项目运行时间(从插件启动时算起)"""
elapsed = time.time() - self._start_time
days = int(elapsed // 86400)
hours = int((elapsed % 86400) // 3600)
minutes = int((elapsed % 3600) // 60)
seconds = int(elapsed % 60)
if days > 0:
return f"{days}{hours}{minutes}{seconds}"
elif hours > 0:
return f"{hours}{minutes}{seconds}"
elif minutes > 0:
return f"{minutes}{seconds}"
else:
return f"{seconds}"
def _get_network_stats(self):
try:
net = psutil.net_io_counters()
now = time.time()
if self._last_net is None:
self._last_net = (now, net.bytes_recv, net.bytes_sent)
return {'recv_rate': 0, 'sent_rate': 0, 'total_recv': net.bytes_recv, 'total_sent': net.bytes_sent}
elapsed = now - self._last_net[0]
if elapsed <= 0: elapsed = 1
recv_rate = (net.bytes_recv - self._last_net[1]) / elapsed
sent_rate = (net.bytes_sent - self._last_net[2]) / elapsed
self._last_net = (now, net.bytes_recv, net.bytes_sent)
return {'recv_rate': round(recv_rate, 1), 'sent_rate': round(sent_rate, 1), 'total_recv': net.bytes_recv, 'total_sent': net.bytes_sent}
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
return {'recv_rate': 0, 'sent_rate': 0, 'total_recv': 0, 'total_sent': 0}
def _get_disk_io_stats(self):
try:
disk_io = psutil.disk_io_counters()
if not disk_io:
return {'read_rate': 0, 'write_rate': 0}
now = time.time()
if self._last_disk is None:
self._last_disk = (now, disk_io.read_bytes, disk_io.write_bytes)
return {'read_rate': 0, 'write_rate': 0}
elapsed = now - self._last_disk[0]
if elapsed <= 0: elapsed = 1
read_rate = (disk_io.read_bytes - self._last_disk[1]) / elapsed
write_rate = (disk_io.write_bytes - self._last_disk[2]) / elapsed
self._last_disk = (now, disk_io.read_bytes, disk_io.write_bytes)
return {'read_rate': round(read_rate, 1), 'write_rate': round(write_rate, 1)}
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
return {'read_rate': 0, 'write_rate': 0}
def _get_network_latency(self) -> float:
"""测量到公共 DNS 8.8.8.8 的 TCP 连接延迟(真实网络波动)"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
start = time.time()
s.connect(('8.8.8.8', 53))
elapsed = (time.time() - start) * 1000 # 毫秒
s.close()
elapsed = (time.time() - start) * 1000 s.close()
return round(elapsed, 1)
except Exception as e:
import traceback; print(f"[main.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
@@ -213,32 +143,6 @@ class DashboardPlugin(Plugin):
Log.error("dashboard", "仪表盘已停止")
def _render_content(self) -> str:
"""渲染仪表盘页面 - 纯 HTML/Python 模板"""
try:
import psutil
import platform
cpu_percent = psutil.cpu_percent(interval=0.5)
cpu_cores = psutil.cpu_count(logical=True)
mem = psutil.virtual_memory()
ram_percent = round(mem.percent, 1)
ram_used_gb = round(mem.used / (1024**3), 1)
ram_total_gb = round(mem.total / (1024**3), 1)
disk = psutil.disk_usage('/')
disk_percent = round(disk.percent, 1)
disk_used_gb = round(disk.used / (1024**3), 1)
disk_total_gb = round(disk.total / (1024**3), 1)
circumference = 2 * 3.14159 * 52
cpu_dash_offset = round(circumference - (cpu_percent / 100) * circumference, 1)
ram_dash_offset = round(circumference - (ram_percent / 100) * circumference, 1)
disk_dash_offset = round(circumference - (disk_percent / 100) * circumference, 1)
uptime_str = self._get_uptime_str()
disk_color = 'gauge-green' if disk_percent < 50 else ('gauge-orange' if disk_percent < 80 else 'gauge-blue')
html = f"""<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
@@ -247,31 +151,14 @@ class DashboardPlugin(Plugin):
<link rel="stylesheet" href="/assets/remixicon.css">
<style>
* {{ margin: 0; padding: 0; box-sizing: border-box; }}
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f6fa; padding: 20px; }}
.container {{ max-width: 1400px; margin: 0 auto; }}
body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: .container {{ max-width: 1400px; margin: 0 auto; }}
.card {{ background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); padding: 20px; margin-bottom: 20px; }}
.card-title {{ font-size: 18px; font-weight: 600; color: #2c3e50; margin-bottom: 20px; }}
.stats-grid {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; }}
.stat-card {{ background: #f8f9fa; border-radius: 8px; padding: 20px; text-align: center; }}
.stat-icon {{ width: 60px; height: 60px; margin: 0 auto 15px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 28px; color: white; }}
.stat-icon.cpu {{ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); }}
.stat-icon.ram {{ background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); }}
.stat-icon.disk {{ background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); }}
.stat-value {{ font-size: 24px; font-weight: 700; color: #2c3e50; margin-bottom: 5px; }}
.stat-label {{ font-size: 14px; color: #7f8c8d; }}
.gauge-container {{ position: relative; width: 120px; height: 120px; margin: 0 auto; }}
.card-title {{ font-size: 18px; font-weight: 600; color: .stats-grid {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 20px; }}
.stat-card {{ background: .stat-icon {{ width: 60px; height: 60px; margin: 0 auto 15px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 28px; color: white; }}
.stat-icon.cpu {{ background: linear-gradient(135deg, .stat-icon.ram {{ background: linear-gradient(135deg, .stat-icon.disk {{ background: linear-gradient(135deg, .stat-value {{ font-size: 24px; font-weight: 700; color: .stat-label {{ font-size: 14px; color: .gauge-container {{ position: relative; width: 120px; height: 120px; margin: 0 auto; }}
.gauge-svg {{ transform: rotate(-90deg); }}
.gauge-bg {{ fill: none; stroke: #e5e7eb; stroke-width: 8; }}
.gauge-fill {{ fill: none; stroke: #3498db; stroke-width: 8; stroke-linecap: round; transition: stroke-dashoffset 0.5s; }}
.gauge-green .gauge-fill {{ stroke: #27ae60; }}
.gauge-orange .gauge-fill {{ stroke: #f39c12; }}
.gauge-blue .gauge-fill {{ stroke: #e74c3c; }}
.gauge-text {{ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 18px; font-weight: 600; color: #2c3e50; }}
.info-grid {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; margin-top: 20px; }}
.info-item {{ background: #f8f9fa; padding: 15px; border-radius: 6px; }}
.info-label {{ font-size: 12px; color: #7f8c8d; margin-bottom: 5px; }}
.info-value {{ font-size: 14px; color: #2c3e50; font-weight: 600; }}
</style>
.gauge-bg {{ fill: none; stroke: .gauge-fill {{ fill: none; stroke: .gauge-green .gauge-fill {{ stroke: .gauge-orange .gauge-fill {{ stroke: .gauge-blue .gauge-fill {{ stroke: .gauge-text {{ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 18px; font-weight: 600; color: .info-grid {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; margin-top: 20px; }}
.info-item {{ background: .info-label {{ font-size: 12px; color: .info-value {{ font-size: 14px; color: </style>
</head>
<body>
<div class="container">

View File

@@ -0,0 +1,59 @@
pass
class DependencyResolver:
self.graph[name] = dependencies
def resolve(self) -> list[str]:
self._detect_cycles()
in_degree: dict[str, int] = {name: 0 for name in self.graph}
who_depends_on: dict[str, list[str]] = {name: [] for name in self.graph}
for name, deps in self.graph.items():
for dep in deps:
if dep in in_degree:
in_degree[name] += 1 who_depends_on[dep].append(name)
queue = [name for name, degree in in_degree.items() if degree == 0]
result = []
while queue:
node = queue.pop(0)
result.append(node)
for dependent in who_depends_on.get(node, []):
in_degree[dependent] -= 1
if in_degree[dependent] == 0:
queue.append(dependent)
if len(result) != len(self.graph):
raise DependencyError("无法解析依赖,可能存在循环依赖")
return result
def _detect_cycles(self):
all_deps = set()
for deps in self.graph.values():
all_deps.update(deps)
all_plugins = set(self.graph.keys())
return list(all_deps - all_plugins)
class DependencyPlugin(Plugin):
pass
def start(self):
pass
def add_plugin(self, name: str, dependencies: list[str]):
return self.resolver.resolve()
def get_missing_deps(self) -> list[str]:
return self.resolve()
register_plugin_type("DependencyResolver", DependencyResolver)
register_plugin_type("DependencyError", DependencyError)
def New():
return DependencyPlugin()

View File

@@ -0,0 +1,69 @@
pass
class FileWatcher:
for watch_dir in self.watch_dirs:
if watch_dir.exists():
for f in watch_dir.rglob("*"):
if f.is_file() and f.suffix in self.extensions:
self._file_times[str(f)] = f.stat().st_mtime
def start(self):
self._running = False
if self._thread:
self._thread.join(timeout=5)
def _watch_loop(self):
def __init__(self):
self.plugin_loader_instance = None
self.watcher: Optional[FileWatcher] = None
self.watch_dirs: list[str] = []
self.watch_extensions: list[str] = [".py", ".json"]
def init(self, deps: dict = None):
if not self.watch_dirs:
self.watch_dirs = ["store"]
self.start_watching()
def stop(self):
self.plugin_loader_instance = plugin_loader
def set_watch_dirs(self, dirs: list[str]):
if self.watch_dirs and self.plugin_loader_instance:
self.watcher = FileWatcher(
self.watch_dirs,
self.watch_extensions,
self._on_file_change
)
self.watcher.start()
def _on_file_change(self, changes: list[tuple[str, Path]]):
try:
plugin_name = plugin_dir.name
if plugin_name in self.plugin_loader_instance.plugins:
raise HotReloadError(f"插件已存在: {plugin_name}")
self.plugin_loader_instance.load(plugin_dir)
info = self.plugin_loader_instance.plugins[plugin_name]
instance = info["instance"]
instance.init()
instance.start()
return True
except Exception as e:
raise HotReloadError(f"加载插件失败: {e}")
def unload_plugin(self, plugin_name: str) -> bool:
try:
self.unload_plugin(plugin_name)
return self.load_plugin(plugin_dir)
except Exception as e:
raise HotReloadError(f"更新插件失败: {e}")
register_plugin_type("HotReloadError", HotReloadError)
register_plugin_type("FileWatcher", FileWatcher)
def New():
return HotReloadPlugin()

View File

@@ -0,0 +1,187 @@
"""
CSRF 防护中间件
"""
import hashlib
import secrets
import time
from typing import Dict, Optional
from collections import defaultdict
from oss.config import get_config
from oss.logger.logger import Log
from .server import Request, Response
class CsrfTokenManager:
"""CSRF 令牌管理器"""
def __init__(self):
self.config = get_config()
self.enabled = self.config.get("CSRF_ENABLED", True)
self.token_lifetime = self.config.get("CSRF_TOKEN_LIFETIME", 3600) # 1小时
self.tokens: Dict[str, tuple] = {} # {token: (timestamp, session_id)}
self.session_tokens: Dict[str, str] = defaultdict(str) # {session_id: token}
self.lock = None # 延迟初始化
def _init_lock(self):
"""延迟初始化锁"""
if self.lock is None:
import threading
self.lock = threading.Lock()
def generate_token(self, session_id: str) -> str:
"""生成CSRF令牌"""
if not self.enabled:
return None
self._init_lock()
# 如果已有令牌,直接返回
if session_id in self.session_tokens:
return self.session_tokens[session_id]
# 生成新的令牌
token = secrets.token_urlsafe(32)
timestamp = time.time()
# 存储令牌
self.tokens[token] = (timestamp, session_id)
self.session_tokens[session_id] = token
return token
def validate_token(self, token: str, session_id: str) -> bool:
"""验证CSRF令牌"""
if not self.enabled:
return True
self._init_lock()
# 清理过期令牌
current_time = time.time()
expired_tokens = []
for stored_token, (timestamp, stored_session_id) in self.tokens.items():
if current_time - timestamp > self.token_lifetime:
expired_tokens.append(stored_token)
elif stored_session_id == session_id and stored_token == token:
# 令牌有效,更新时间戳
self.tokens[stored_token] = (current_time, stored_session_id)
return True
# 清理过期令牌
for expired_token in expired_tokens:
if expired_token in self.tokens:
del self.tokens[expired_token]
return False
def cleanup_expired_tokens(self):
"""清理过期令牌"""
if not self.enabled:
return
self._init_lock()
current_time = time.time()
expired_tokens = []
for token, (timestamp, _) in self.tokens.items():
if current_time - timestamp > self.token_lifetime:
expired_tokens.append(token)
for token in expired_tokens:
if token in self.tokens:
del self.tokens[token]
class CsrfMiddleware:
"""CSRF 防护中间件"""
def __init__(self):
self.config = get_config()
self.enabled = self.config.get("CSRF_ENABLED", True)
self.exempt_paths = {
"/health", "/favicon.ico", "/api/status",
"/api/health", "/login", "/logout"
}
# 初始化令牌管理器
self.token_manager = CsrfTokenManager()
def get_session_id(self, request: Request) -> str:
"""获取会话ID"""
# 从Cookie中获取会话ID
session_cookie = request.headers.get("Cookie", "")
for cookie in session_cookie.split(";"):
if "session_id" in cookie:
return cookie.split("=")[1].strip()
# 从Authorization头获取如果使用Bearer token
auth_header = request.headers.get("Authorization", "")
if auth_header.startswith("Bearer "):
return f"token:{auth_header[7:]}"
# 使用IP地址作为会话ID简化实现
return f"ip:{request.headers.get('Remote-Addr', 'unknown')}"
def create_csrf_token_response(self, session_id: str) -> Response:
"""创建CSRF令牌响应"""
token = self.token_manager.generate_token(session_id)
return Response(
status=200,
headers={
"Content-Type": "application/json",
"Set-Cookie": f"csrf_token={token}; Path=/; HttpOnly; SameSite=Lax"
},
body=json.dumps({
"csrf_token": token,
"message": "CSRF token generated"
})
)
def process(self, ctx: dict, next_fn) -> Optional[Response]:
"""处理CSRF防护逻辑"""
if not self.enabled:
return next_fn()
request = ctx.get("request")
if not request:
return next_fn()
# 检查是否为豁免路径
if request.path in self.exempt_paths:
return next_fn()
# 只对需要保护的请求方法进行CSRF检查
if request.method not in ["POST", "PUT", "DELETE", "PATCH"]:
return next_fn()
# 获取会话ID
session_id = self.get_session_id(request)
# 获取CSRF令牌
csrf_token = None
if request.headers.get("Content-Type") == "application/json":
try:
import json
body = json.loads(request.body)
csrf_token = body.get("csrf_token")
except:
pass
# 从Header中获取CSRF令牌
if not csrf_token:
csrf_token = request.headers.get("X-CSRF-Token")
# 验证CSRF令牌
if not csrf_token or not self.token_manager.validate_token(csrf_token, session_id):
Log.warn("csrf", f"CSRF验证失败: {request.method} {request.path}")
return Response(
status=403,
body='{"error": "CSRF token invalid or missing", "message": "请求被拒绝,请刷新页面重试"}',
headers={"Content-Type": "application/json"}
)
return next_fn()

View File

@@ -0,0 +1,21 @@
type: str request: Any = None
response: Any = None
error: Exception = None
context: dict[str, Any] = field(default_factory=dict)
class HttpEventBus:
if event_type not in self._handlers:
self._handlers[event_type] = []
self._handlers[event_type].append(handler)
def off(self, event_type: str, handler: Callable):
handlers = self._handlers.get(event.type, [])
for handler in handlers:
try:
handler(event)
except Exception as e:
import traceback; print(f"[events.py] 错误:{type(e).__name__}:{e}"); traceback.print_exc()
pass
def clear(self):

View File

@@ -0,0 +1,209 @@
"""
输入验证中间件
"""
import json
import re
from typing import Dict, Any, Optional, List
from datetime import datetime
from oss.config import get_config
from oss.logger.logger import Log
from .server import Request, Response
class InputValidator:
"""输入验证器"""
def __init__(self):
self.config = get_config()
self.enabled = self.config.get("INPUT_VALIDATION_ENABLED", True)
# 预定义的模式
self.patterns = {
"email": r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$',
"username": r'^[a-zA-Z0-9_]{3,20}$',
"password": r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d@$!%*?&]{8,}$',
"api_key": r'^[a-zA-Z0-9_-]{32,}$',
"uuid": r'^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$',
"ip_address": r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$',
"url": r'^https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+[/\w\.-]*\??[/\w\.-=&]*$'
}
# 端点特定的验证规则
self.endpoint_rules = {
"/api/auth/login": {
"methods": ["POST"],
"required_fields": ["username", "password"],
"field_rules": {
"username": {"type": "str", "min_length": 3, "max_length": 20, "pattern": "username"},
"password": {"type": "str", "min_length": 8, "max_length": 100}
}
},
"/api/auth/register": {
"methods": ["POST"],
"required_fields": ["username", "email", "password"],
"field_rules": {
"username": {"type": "str", "min_length": 3, "max_length": 20, "pattern": "username"},
"email": {"type": "str", "pattern": "email"},
"password": {"type": "str", "min_length": 8, "max_length": 100, "pattern": "password"}
}
},
"/api/users": {
"methods": ["GET", "POST"],
"field_rules": {
"limit": {"type": "int", "min_value": 1, "max_value": 100},
"offset": {"type": "int", "min_value": 0},
"search": {"type": "str", "max_length": 100}
}
},
"/api/pkg-manager/search": {
"methods": ["GET"],
"field_rules": {
"query": {"type": "str", "min_length": 1, "max_length": 100},
"limit": {"type": "int", "min_value": 1, "max_value": 50},
"page": {"type": "int", "min_value": 1}
}
}
}
def validate_field(self, field_name: str, value: Any, rules: Dict) -> Optional[str]:
"""验证单个字段"""
# 类型验证
if "type" in rules:
expected_type = rules["type"]
if expected_type == "str" and not isinstance(value, str):
return f"{field_name} 必须是字符串"
elif expected_type == "int" and not isinstance(value, int):
return f"{field_name} 必须是整数"
elif expected_type == "float" and not isinstance(value, (int, float)):
return f"{field_name} 必须是数字"
elif expected_type == "bool" and not isinstance(value, bool):
return f"{field_name} 必须是布尔值"
# 长度验证
if isinstance(value, str):
if "min_length" in rules and len(value) < rules["min_length"]:
return f"{field_name} 长度不能少于 {rules['min_length']} 个字符"
if "max_length" in rules and len(value) > rules["max_length"]:
return f"{field_name} 长度不能超过 {rules['max_length']} 个字符"
# 数值范围验证
if isinstance(value, (int, float)):
if "min_value" in rules and value < rules["min_value"]:
return f"{field_name} 不能小于 {rules['min_value']}"
if "max_value" in rules and value > rules["max_value"]:
return f"{field_name} 不能大于 {rules['max_value']}"
# 模式验证
if "pattern" in rules and isinstance(value, str):
pattern = self.patterns.get(rules["pattern"])
if pattern and not re.match(pattern, value):
return f"{field_name} 格式不正确"
# 枚举验证
if "choices" in rules and value not in rules["choices"]:
return f"{field_name} 必须是以下值之一: {', '.join(rules['choices'])}"
return None
def validate_request(self, request: Request) -> Optional[str]:
"""验证请求"""
if not self.enabled:
return None
# 检查是否有对应的验证规则
rules = None
for endpoint, rule in self.endpoint_rules.items():
if request.path.startswith(endpoint):
rules = rule
break
if not rules:
return None
# 检查请求方法
if "methods" in rules and request.method not in rules["methods"]:
return f"不支持的请求方法: {request.method}"
# 解析请求体
body_data = {}
if request.method in ["POST", "PUT", "PATCH"] and request.body:
try:
body_data = json.loads(request.body)
except json.JSONDecodeError:
return "无效的JSON格式"
# 解析查询参数
query_params = {}
if request.query:
try:
query_params = json.loads(request.query)
except:
# 如果不是JSON按简单键值对处理
query_params = {}
# 检查必需字段
if "required_fields" in rules:
for field in rules["required_fields"]:
if field not in body_data and field not in query_params:
return f"缺少必需字段: {field}"
# 验证字段规则
if "field_rules" in rules:
all_data = {**body_data, **query_params}
for field_name, field_rules in rules["field_rules"].items():
if field_name in all_data:
error = self.validate_field(field_name, all_data[field_name], field_rules)
if error:
return error
return None
class InputValidationMiddleware:
"""输入验证中间件"""
def __init__(self):
self.config = get_config()
self.enabled = self.config.get("INPUT_VALIDATION_ENABLED", True)
self.validator = InputValidator()
# 豁免路径(不进行验证)
self.exempt_paths = {
"/health", "/favicon.ico", "/api/status",
"/api/health", "/metrics"
}
def create_validation_error_response(self, error_message: str) -> Response:
"""创建验证错误响应"""
return Response(
status=400,
body=json.dumps({
"error": "Validation Error",
"message": error_message,
"timestamp": datetime.now().isoformat()
}),
headers={"Content-Type": "application/json"}
)
def process(self, ctx: dict, next_fn) -> Optional[Response]:
"""处理输入验证逻辑"""
if not self.enabled:
return next_fn()
request = ctx.get("request")
if not request:
return next_fn()
# 检查是否为豁免路径
if request.path in self.exempt_paths:
return next_fn()
# 验证请求
validation_error = self.validator.validate_request(request)
if validation_error:
Log.warn("validation", f"输入验证失败: {validation_error} ({request.method} {request.path})")
return self.create_validation_error_response(validation_error)
return next_fn()

View File

@@ -0,0 +1,29 @@
def __init__(self):
self.server = None
self.router = Router()
self.middleware = MiddlewareChain()
def init(self, deps: dict = None):
self.server.start()
def stop(self):
return Response(
status=200,
body=json.dumps({"status": "ok", "service": "http-api"}),
headers={"Content-Type": "application/json"}
)
def _server_info_handler(self, request):
return Response(
status=200,
body=json.dumps({"status": "running", "plugins_loaded": True}),
headers={"Content-Type": "application/json"}
)
register_plugin_type("HttpApiPlugin", HttpApiPlugin)
def New():
return HttpApiPlugin()

View File

@@ -0,0 +1,234 @@
"""中间件链 - CORS/鉴权/日志/限流/CSRF/输入验证等"""
import json
import time
from typing import Callable, Optional, Any
from oss.config import get_config
from oss.logger.logger import Log
from .server import Request, Response
from .rate_limiter import RateLimitMiddleware
from .csrf_middleware import CsrfMiddleware
from .input_validation import InputValidationMiddleware
class Middleware:
"""中间件基类"""
def process(self, ctx: dict[str, Any], next_fn: Callable) -> Optional[Response]:
return next_fn()
class CorsMiddleware(Middleware):
"""CORS 中间件"""
def process(self, ctx: dict, next_fn: Callable) -> Optional[Response]:
config = get_config()
allowed_origins = config.get("CORS_ALLOWED_ORIGINS", ["http://localhost:3000", "http://127.0.0.1:3000"])
req = ctx.get("request")
origin = req.headers.get("Origin", "") if req else ""
# 如果没有配置允许的来源或来源为空则不设置CORS头
if not allowed_origins or not origin:
return next_fn()
# 检查请求来源是否在允许列表中
if origin in allowed_origins or "*" in allowed_origins:
ctx["response_headers"] = {
"Access-Control-Allow-Origin": origin,
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
"Access-Control-Allow-Credentials": "true",
}
return next_fn()
class AuthMiddleware(Middleware):
"""鉴权中间件 - Bearer Token 认证"""
_public_paths = {"/health", "/favicon.ico", "/api/status", "/api/health"}
def process(self, ctx: dict, next_fn: Callable) -> Optional[Response]:
config = get_config()
api_key = config.get("API_KEY")
if not api_key:
return next_fn()
req = ctx.get("request")
if req and req.path in self._public_paths:
return next_fn()
if req and req.method == "OPTIONS":
return next_fn()
auth_header = req.headers.get("Authorization", "") if req else ""
token = auth_header.removeprefix("Bearer ").strip()
if token != api_key or not token:
Log.warn("auth", f"鉴权失败: {req.method} {req.path}" if req else "鉴权失败")
return Response(
status=401,
body=json.dumps({"error": "Unauthorized", "message": "需要有效的 API Key"}),
headers={"Content-Type": "application/json"},
)
return next_fn()
class LoggerMiddleware(Middleware):
"""日志中间件"""
_silent_paths = {"/api/dashboard/stats", "/favicon.ico", "/health"}
def process(self, ctx: dict, next_fn: Callable) -> Optional[Response]:
req = ctx.get("request")
if req and req.path not in self._silent_paths:
Log.info("http-api", f"{req.method} {req.path}")
return next_fn()
class RateLimitMiddleware(Middleware):
"""限流中间件 - 防止DoS攻击"""
def __init__(self):
self.config = get_config()
self.enabled = self.config.get("RATE_LIMIT_ENABLED", True)
# 不同端点的限流配置
self.endpoint_limits = {
"/api/dashboard/stats": {
"max_requests": 10,
"time_window": 60
},
"/api/pkg-manager/search": {
"max_requests": 50,
"time_window": 60
}
}
# 全局限流配置
self.global_limit = {
"max_requests": self.config.get("RATE_LIMIT_MAX_REQUESTS", 100),
"time_window": self.config.get("RATE_LIMIT_TIME_WINDOW", 60)
}
# 请求记录
self.requests = {}
self.lock = None # 延迟初始化
def _init_lock(self):
"""延迟初始化锁"""
if self.lock is None:
import threading
self.lock = threading.Lock()
def _get_client_identifier(self, request: Request) -> str:
"""获取客户端标识符"""
# 优先使用IP地址
ip = request.headers.get("X-Forwarded-For", request.headers.get("X-Real-IP", ""))
if not ip:
ip = request.headers.get("Remote-Addr", "unknown")
# 如果有API Key使用Key作为标识符更精确
auth_header = request.headers.get("Authorization", "")
if auth_header.startswith("Bearer "):
return f"api_key:{auth_header[7:]}"
return f"ip:{ip}"
def _is_rate_limited(self, identifier: str, path: str) -> bool:
"""检查是否被限流"""
if not self.enabled:
return False
now = time.time()
limit_key = f"{identifier}:{path}"
# 获取端点特定的限制
endpoint_limit = None
for endpoint, config in self.endpoint_limits.items():
if path.startswith(endpoint):
endpoint_limit = config
break
# 使用端点特定限制或全局限制
limit = endpoint_limit or self.global_limit
max_requests = limit["max_requests"]
time_window = limit["time_window"]
# 清理过期的请求记录
if limit_key not in self.requests:
self.requests[limit_key] = []
request_times = self.requests[limit_key]
while request_times and request_times[0] <= now - time_window:
request_times.popleft()
# 检查是否超过限制
if len(request_times) >= max_requests:
return True
# 记录当前请求
request_times.append(now)
return False
def _create_rate_limit_response(self) -> Response:
"""创建限流响应"""
return Response(
status=429,
headers={
"Content-Type": "application/json",
"Retry-After": str(self.global_limit["time_window"]),
"X-Rate-Limit-Limit": str(self.global_limit["max_requests"]),
"X-Rate-Limit-Window": str(self.global_limit["time_window"]),
},
body='{"error": "Rate limit exceeded", "message": "请稍后再试"}'
)
def process(self, ctx: dict, next_fn: Callable) -> Optional[Response]:
"""处理限流逻辑"""
if not self.enabled:
return next_fn()
request = ctx.get("request")
if not request:
return next_fn()
# 获取客户端标识符
self._init_lock()
identifier = self._get_client_identifier(request)
# 检查是否被限流
if self._is_rate_limited(identifier, request.path):
return self._create_rate_limit_response()
return next_fn()
class MiddlewareChain:
"""中间件链"""
def __init__(self):
self.middlewares: list[Middleware] = []
self.add(CorsMiddleware())
self.add(AuthMiddleware())
self.add(LoggerMiddleware())
self.add(RateLimitMiddleware())
self.add(CsrfMiddleware())
self.add(InputValidationMiddleware())
def add(self, middleware: Middleware):
self.middlewares.append(middleware)
def run(self, ctx: dict[str, Any]) -> Optional[Response]:
idx = 0
def next_fn():
nonlocal idx
if idx < len(self.middlewares):
mw = self.middlewares[idx]
idx += 1
return mw.process(ctx, next_fn)
return None
resp = next_fn()
response_headers = ctx.get("response_headers")
if response_headers:
ctx["_cors_headers"] = response_headers
return resp

View File

@@ -0,0 +1,122 @@
"""
限流中间件 - 防止DoS攻击
"""
import time
import threading
from typing import Dict, Optional
from collections import defaultdict, deque
from oss.config import get_config
from store.NebulaShell.http_api.server import Response
class RateLimiter:
"""令牌桶限流器"""
def __init__(self, max_requests: int = 100, time_window: int = 60):
self.max_requests = max_requests
self.time_window = time_window
self.requests: Dict[str, deque] = defaultdict(deque)
self.lock = threading.Lock()
def is_allowed(self, identifier: str) -> bool:
"""检查是否允许请求"""
with self.lock:
now = time.time()
request_times = self.requests[identifier]
# 清理过期的请求记录
while request_times and request_times[0] <= now - self.time_window:
request_times.popleft()
# 检查是否超过限制
if len(request_times) >= self.max_requests:
return False
# 记录当前请求
request_times.append(now)
return True
class RateLimitMiddleware:
"""限流中间件"""
def __init__(self):
self.config = get_config()
self.limiter = RateLimiter(
max_requests=self.config.get("RATE_LIMIT_MAX_REQUESTS", 100),
time_window=self.config.get("RATE_LIMIT_TIME_WINDOW", 60)
)
self.enabled = self.config.get("RATE_LIMIT_ENABLED", True)
# 不同端点的限流配置
self.endpoint_limits = {
"/api/dashboard/stats": {
"max_requests": 10,
"time_window": 60
},
"/api/pkg-manager/search": {
"max_requests": 50,
"time_window": 60
}
}
def get_client_identifier(self, request) -> str:
"""获取客户端标识符"""
# 优先使用IP地址
ip = request.headers.get("X-Forwarded-For", request.headers.get("X-Real-IP", ""))
if not ip:
ip = request.headers.get("Remote-Addr", "unknown")
# 如果有API Key使用Key作为标识符更精确
auth_header = request.headers.get("Authorization", "")
if auth_header.startswith("Bearer "):
return f"api_key:{auth_header[7:]}"
return f"ip:{ip}"
def get_endpoint_limiter(self, path: str) -> Optional[RateLimiter]:
"""获取端点特定的限流器"""
for endpoint, config in self.endpoint_limits.items():
if path.startswith(endpoint):
return RateLimiter(
max_requests=config["max_requests"],
time_window=config["time_window"]
)
return None
def create_rate_limit_response(self, retry_after: int = 60) -> Response:
"""创建限流响应"""
return Response(
status=429,
headers={
"Content-Type": "application/json",
"Retry-After": str(retry_after),
"X-Rate-Limit-Limit": str(self.limiter.max_requests),
"X-Rate-Limit-Window": str(self.limiter.time_window),
},
body='{"error": "Rate limit exceeded", "message": "请稍后再试"}'
)
def process(self, ctx: dict, next_fn) -> Optional[Response]:
"""处理限流逻辑"""
if not self.enabled:
return next_fn()
request = ctx.get("request")
if not request:
return next_fn()
# 获取客户端标识符
identifier = self.get_client_identifier(request)
# 获取端点特定的限流器
endpoint_limiter = self.get_endpoint_limiter(request.path)
limiter = endpoint_limiter or self.limiter
# 检查是否允许请求
if not limiter.is_allowed(identifier):
retry_after = self.limiter.time_window
return self.create_rate_limit_response(retry_after)
return next_fn()

View File

@@ -0,0 +1,2 @@
def handle(self, request: Request) -> Response:

View File

@@ -1,18 +1,9 @@
"""HTTP TCP 事件定义"""
from dataclasses import dataclass, field
from typing import Any
@dataclass
class TcpEvent:
"""TCP 事件"""
type: str
client: Any = None
data: bytes = b""
context: dict[str, Any] = field(default_factory=dict)
# 事件类型常量
EVENT_CONNECT = "tcp.connect"
EVENT_DISCONNECT = "tcp.disconnect"
EVENT_DATA = "tcp.data"

View File

@@ -0,0 +1,10 @@
def __init__(self):
self.server = None
self.router = TcpRouter()
self.middleware = TcpMiddlewareChain()
def init(self, deps: dict = None):
self.server.start()
def stop(self):

View File

@@ -1,33 +1,10 @@
"""TCP HTTP 中间件链"""
from typing import Callable, Optional, Any
class TcpMiddleware:
"""TCP 中间件基类"""
def process(self, request: dict, next_fn: Callable) -> Optional[dict]:
"""处理请求"""
return next_fn()
class TcpLogMiddleware(TcpMiddleware):
"""日志中间件"""
def process(self, request, next_fn):
print(f"[http-tcp] {request.get('method')} {request.get('path')}")
return next_fn()
class TcpCorsMiddleware(TcpMiddleware):
"""CORS 中间件"""
def process(self, request, next_fn):
response = next_fn()
if response:
response.setdefault("headers", {})
response["headers"]["Access-Control-Allow-Origin"] = "*"
return response
class TcpMiddlewareChain:
"""TCP 中间件链"""
def __init__(self):
self.middlewares: list[TcpMiddleware] = []
@@ -35,11 +12,6 @@ class TcpMiddlewareChain:
self.add(TcpCorsMiddleware())
def add(self, middleware: TcpMiddleware):
"""添加中间件"""
self.middlewares.append(middleware)
def run(self, request: dict) -> Optional[dict]:
"""执行中间件链"""
idx = 0
def next_fn():

View File

@@ -0,0 +1,2 @@
def handle(self, request: dict) -> dict:

View File

@@ -0,0 +1,114 @@
def __init__(self, conn: socket.socket, address: tuple):
self.conn = conn
self.address = address
self.id = f"{address[0]}:{address[1]}"
def send(self, data: bytes):
self.conn.close()
class TcpHttpServer:
self._server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._server.bind((self.host, self.port))
self._server.listen(128)
self._running = True
self._thread = threading.Thread(target=self._accept_loop, daemon=True)
self._thread.start()
print(f"[http-tcp] 服务器启动: {self.host}:{self.port}")
def _accept_loop(self):
buffer = b""
try:
while self._running:
data = client.conn.recv(4096)
if not data:
break
buffer += data
if b"\r\n\r\n" in buffer:
header_end = buffer.find(b"\r\n\r\n")
header_text = buffer[:header_end].decode("utf-8", errors="replace")
content_length = 0
for line in header_text.split("\r\n")[1:]:
if line.lower().startswith("content-length:"):
content_length = int(line.split(":", 1)[1].strip())
break
body_start_pos = header_end + 4 body_received = len(buffer) - body_start_pos
if body_received < content_length:
while body_received < content_length:
remaining = content_length - body_received
chunk = client.conn.recv(min(4096, remaining))
if not chunk:
break
buffer += chunk
body_received += len(chunk)
request = self._parse_request(buffer)
if request:
if self.event_bus:
self.event_bus.emit(TcpEvent(
type=EVENT_REQUEST,
client=client,
context={"request": request}
))
response = self.router.handle(request)
response_bytes = self._format_response(response)
client.send(response_bytes)
if self.event_bus:
self.event_bus.emit(TcpEvent(
type=EVENT_RESPONSE,
client=client,
data=response_bytes
))
buffer = b""
except ConnectionResetError:
pass
except BrokenPipeError:
pass
except OSError as e:
if self.event_bus:
self.event_bus.emit(TcpEvent(type=EVENT_ERROR, client=client, context={"error": f"OSError: {e}"}))
except Exception as e:
if self.event_bus:
self.event_bus.emit(TcpEvent(type=EVENT_ERROR, client=client, context={"error": f"{type(e).__name__}: {e}"}))
finally:
del self._clients[client.id]
client.close()
if self.event_bus:
self.event_bus.emit(TcpEvent(type=EVENT_DISCONNECT, client=client))
def _parse_request(self, data: bytes) -> Optional[dict]:
status = response.get("status", 200)
headers = response.get("headers", {})
body = response.get("body", "")
status_text = {200: "OK", 404: "Not Found", 500: "Internal Server Error"}.get(status, "OK")
response_lines = [
f"HTTP/1.1 {status} {status_text}",
]
if "Content-Type" not in headers:
headers["Content-Type"] = "text/plain; charset=utf-8"
headers["Content-Length"] = str(len(body))
for key, value in headers.items():
response_lines.append(f"{key}: {value}")
response_lines.append("")
response_lines.append(body)
return "\r\n".join(response_lines).encode("utf-8")
def stop(self):
return list(self._clients.values())

View File

@@ -1,27 +1,11 @@
"""i18n 核心引擎"""
import json
import re
from pathlib import Path
from typing import Any, Optional
class I18nEngine:
"""国际化引擎"""
def __init__(self):
self._translations: dict[str, dict[str, Any]] = {} # {locale: {key: value}}
self._current_locale: str = "zh-CN"
self._translations: dict[str, dict[str, Any]] = {} self._current_locale: str = "zh-CN"
self._fallback_locale: str = "en-US"
self._supported_locales: list[str] = []
self._locales_dir: str = ""
def load_locales(self, locales_dir: str, locales: list[str]):
"""加载语言文件
Args:
locales_dir: 语言文件目录路径
locales: 支持的语言列表
"""
self._locales_dir = locales_dir
self._supported_locales = locales
locales_path = Path(locales_dir)
@@ -41,20 +25,9 @@ class I18nEngine:
self._translations[locale] = {}
def set_locale(self, locale: str):
"""设置当前语言"""
if locale in self._supported_locales:
self._current_locale = locale
def get_locale(self) -> str:
"""获取当前语言"""
return self._current_locale
def set_fallback(self, locale: str):
"""设置回退语言"""
self._fallback_locale = locale
def t(self, key: str, locale: Optional[str] = None, **kwargs) -> str:
"""翻译文本
Args:
key: 翻译键 (支持点号分隔的嵌套路径 "user.greeting")
@@ -63,74 +36,36 @@ class I18nEngine:
Returns:
翻译后的文本
"""
target_locale = locale or self._current_locale
# 尝试从指定语言获取
value = self._get_nested(key, self._translations.get(target_locale, {}))
# 如果未找到,尝试从回退语言获取
if value is None and target_locale != self._fallback_locale:
value = self._get_nested(key, self._translations.get(self._fallback_locale, {}))
# 仍未找到,返回键名
if value is None:
return key
# 插值处理: {{name}} 或 {name}
return self._interpolate(value, kwargs)
def _get_nested(self, key: str, data: dict) -> Any:
"""获取嵌套字典值"""
keys = key.split(".")
current = data
for k in keys:
if isinstance(current, dict) and k in current:
current = current[k]
else:
return None
return current
def _interpolate(self, text: str, kwargs: dict) -> str:
"""插值替换: {{name}} 或 {name}"""
# 支持 {{name}} 格式
result = re.sub(r'\{\{(\w+)\}\}', lambda m: str(kwargs.get(m.group(1), m.group(0))), text)
# 支持 {name} 格式 (如果未被 {{}} 替换)
result = re.sub(r'\{(\w+)\}', lambda m: str(kwargs.get(m.group(1), m.group(0))), result)
return result
def get_supported_locales(self) -> list[str]:
"""获取支持的语言列表"""
return self._supported_locales
def is_valid_locale(self, locale: str) -> bool:
"""检查语言是否有效"""
return locale in self._supported_locales
def detect_locale(self, accept_language: Optional[str] = None,
query_lang: Optional[str] = None,
cookie_lang: Optional[str] = None) -> str:
"""检测语言优先级
Args:
accept_language: HTTP Accept-Language
query_lang: URL 查询参数 ?lang=xx
cookie_lang: Cookie 中的语言
Returns:
检测到的语言代码
"""
# 1. 查询参数优先级最高
if query_lang and self.is_valid_locale(query_lang):
return query_lang
# 2. Cookie 次之
if cookie_lang and self.is_valid_locale(cookie_lang):
return cookie_lang
# 3. Accept-Language 头
if accept_language:
# 解析 "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7"
languages = []
for part in accept_language.split(","):
part = part.strip()
@@ -140,17 +75,13 @@ class I18nEngine:
else:
languages.append((part, 1.0))
# 按权重排序
languages.sort(key=lambda x: x[1], reverse=True)
for lang, _ in languages:
# 精确匹配
if self.is_valid_locale(lang):
return lang
# 前缀匹配 (zh 匹配 zh-CN, zh-TW)
for supported in self._supported_locales:
if supported.startswith(lang + "-") or lang.startswith(supported.split("-")[0] + "-"):
return supported
# 4. 默认语言
return self._current_locale

Some files were not shown because too many files have changed in this diff Show More