本次提交包含多项功能更新: 1. 为多个插件(xq0002/0003/0004/0008/0010/0011/0020/0031_xq0032/0033/0035/0037/0045_xq0046/0047/0048)新增 UI 界面及配置支持 2. 新增运行日志集中回传功能,客户端自动上传运行日志到中心机集中存储 3. 新增插件分类字段支持,用于场景库分组管理 4. 优化 xq0034/0037/0048 等插件的功能及描述 5. 修复控制面响应兼容问题,支持新旧版本数据格式 6. 新增 xq0033 插件的状态存储模块,用于历史记录管理 7. 优化 CRM 登录窗口模式,支持全屏置顶
107 lines
4.7 KiB
Python
107 lines
4.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""controlplane-next(新版服务端)集中日志接口集成测试。
|
|
前置:本地起好新版服务(NODE_ENV=local,端口 8001,测试库 37du_center_next_test)。
|
|
"""
|
|
import hashlib
|
|
import json
|
|
import sys
|
|
import urllib.request
|
|
import urllib.error
|
|
|
|
import pymysql
|
|
|
|
BASE = "http://127.0.0.1:8001"
|
|
TOKEN = "test-token-123"
|
|
|
|
# ---------- 1) 种一个管理员账号(pbkdf2 算法与新版 crypto.ts 一致) ----------
|
|
def pbkdf2(pwd: str, salt: str) -> str:
|
|
return hashlib.pbkdf2_hmac("sha256", pwd.encode("utf-8"), salt.encode("utf-8"), 120000, 32).hex()
|
|
|
|
SALT = "aabbccddeeff0011"
|
|
HASH = pbkdf2("123456", SALT)
|
|
c = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123456",
|
|
database="37du_center_next_test")
|
|
with c.cursor() as cur:
|
|
cur.execute(
|
|
"INSERT INTO accounts(account, password_hash, salt, is_admin, dept, nickname, status) "
|
|
"VALUES (%s,%s,%s,%s,%s,%s,%s) ON DUPLICATE KEY UPDATE password_hash=VALUES(password_hash), "
|
|
"salt=VALUES(salt), is_admin=1",
|
|
("puddy", HASH, SALT, 1, "IT", "管理员", 1))
|
|
c.commit()
|
|
c.close()
|
|
print("seeded admin puddy/123456")
|
|
|
|
# ---------- HTTP 辅助 ----------
|
|
def call(method, path, body=None, raw=None, headers=None):
|
|
h = dict(headers or {})
|
|
data = None
|
|
if body is not None:
|
|
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
|
|
h["Content-Type"] = "application/json"
|
|
elif raw is not None:
|
|
data = raw
|
|
# 与 Electron 客户端一致:显式 octet-stream,避免中间件按 form 消费
|
|
h.setdefault("Content-Type", "application/octet-stream")
|
|
req = urllib.request.Request(BASE + path, data=data, method=method, headers=h)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=15) as r:
|
|
return r.status, r.read()
|
|
except urllib.error.HTTPError as e:
|
|
return e.code, e.read()
|
|
|
|
H = {"X-API-Token": TOKEN}
|
|
|
|
# ---------- 2) 客户端回传运行日志(幂等) ----------
|
|
run = {
|
|
"run_id": "abababababababababababababababab",
|
|
"account": "zhangsan", "device_id": "dev-next-01", "plugin_id": "XQ0011",
|
|
"name": "考勤月报封存检查", "version": "1.0.0", "trigger": "schedule", "schedule_id": "s1",
|
|
"status": "error", "ok": False, "error": "登录失败", "summary": "未完成",
|
|
"started": 1700000000, "finished": 1700000060, "duration_sec": 60,
|
|
"logs": [
|
|
{"t": "14:00:01", "level": "INFO", "msg": "开始执行"},
|
|
{"t": "14:00:02", "level": "ERROR", "msg": "登录失败:密码不正确"},
|
|
],
|
|
}
|
|
s, b = call("POST", "/api/runtime/logs", body=run, headers=H)
|
|
print("runtime/logs POST:", s, b.decode("utf-8"))
|
|
s, b = call("POST", "/api/runtime/logs", body=run, headers=H)
|
|
print("runtime/logs POST#2(幂等):", s, b.decode("utf-8"))
|
|
|
|
# ---------- 3) 客户端壳日志(reset 首传 + 追加) ----------
|
|
line1 = "2026-10-22 15:00:01 INFO app 启动完成\n".encode("utf-8")
|
|
qs = "device_id=dev-next-01&account=zhangsan&hostname=PC-NEXT&client_version=1.6.4&reset=1"
|
|
s, b = call("POST", "/api/runtime/shell-log?" + qs, raw=line1, headers=H)
|
|
print("shell-log reset:", s, b.decode("utf-8"))
|
|
line2 = "2026-10-22 15:01:00 ERROR plugin-sync 同步失败\n".encode("utf-8")
|
|
qs2 = "device_id=dev-next-01&account=zhangsan&hostname=PC-NEXT&client_version=1.6.4"
|
|
s, b = call("POST", "/api/runtime/shell-log?" + qs2, raw=line2, headers=H)
|
|
print("shell-log append:", s, b.decode("utf-8"))
|
|
|
|
# ---------- 4) 管理端:登录拿票据 → 查日志 ----------
|
|
s, b = call("POST", "/api/admin/auth", body={"account": "puddy", "password": "123456"})
|
|
auth = json.loads(b.decode("utf-8"))
|
|
print("admin/auth:", s, "is_admin=", auth.get("is_admin"))
|
|
A = {"X-Admin-Token": auth.get("token", "")}
|
|
|
|
s, b = call("GET", "/api/admin/run-logs?plugin=XQ0011&page=1&page_size=10", headers=A)
|
|
print("admin run-logs:", s, b.decode("utf-8")[:360])
|
|
s, b = call("GET", "/api/admin/run-logs/abababababababababababababababab", headers=A)
|
|
print("admin run-logs detail:", s, b.decode("utf-8")[:300])
|
|
s, b = call("GET", "/api/admin/device-logs", headers=A)
|
|
print("admin device-logs:", s, b.decode("utf-8")[:300])
|
|
s, b = call("GET", "/api/admin/device-logs/dev-next-01", headers=A)
|
|
print("admin device-log detail:", s, b.decode("utf-8")[:300])
|
|
s, b = call("GET", "/api/admin/device-logs/dev-next-01/download", headers=A)
|
|
print("admin device-log download:", s, b.decode("utf-8"))
|
|
s, b = call("GET", "/api/admin/run-logs/../etc/passwd", headers=A)
|
|
print("admin path-traversal run-logs:", s)
|
|
|
|
# ---------- 5) 鉴权 ----------
|
|
s, b = call("POST", "/api/runtime/logs", body=run)
|
|
print("no-token POST:", s)
|
|
|
|
# ---------- 6) 清理 ----------
|
|
s, b = call("POST", "/api/admin/logs/cleanup", headers=A)
|
|
print("admin logs/cleanup:", s, b.decode("utf-8"))
|