在启动服务前,请确保已安装以下依赖:
| 组件 | 最低版本 | 检查命令 | 安装方法 |
|---|---|---|---|
| Redis | 7.x | redis-server --version |
安装指南 |
| Go | 1.21+ | go version |
安装指南 |
| Node.js | 18+ | node --version |
安装指南 |
| Rust | 1.75+ | rustc --version |
安装指南 |
brew install redis
brew services start redissudo apt-get update
sudo apt-get install redis-server
sudo systemctl start redis-serversudo yum install redis
sudo systemctl start redisredis-cli ping
# 应返回: PONG访问 https://go.dev/dl/ 下载适合您系统的版本。
brew install gowget https://go.dev/dl/go1.22.0.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrcgo version
# 应显示: go version go1.22.0 ...brew install nodecurl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejscurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc
nvm install 20
nvm use 20node --version # 应显示: v20.x.x
npm --version # 应显示: 10.x.xcurl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source ~/.cargo/envrustup target add wasm32-unknown-unknown
cargo install wasm-packrustc --version # 应显示: rustc 1.75.0 ...
wasm-pack --versioncd /Users/houshuai/project/ai_project/safe
# 启动所有服务
./start-services.sh
# 查看日志
tail -f logs/backend.log # 后端日志
tail -f logs/frontend.log # 前端日志
# 停止所有服务
./stop-services.sh# 检查 Redis 是否运行
redis-cli ping
# 如果未运行,启动 Redis
redis-server --daemonize yes
# 或者使用系统服务
# macOS: brew services start redis
# Linux: sudo systemctl start redis-servercd backend
# 首次运行:安装依赖
go mod tidy
# 启动服务
go run main.go
# 或者编译后运行
go build -o server main.go
./server后端运行在: http://localhost:8080
cd frontend
# 首次运行:安装依赖
npm install
# 启动开发服务器
npm run dev前端运行在: http://localhost:5173
redis-cli ping
# 预期输出: PONGcurl http://localhost:8080/health
# 预期输出: {"status":"ok","redis":true}在浏览器中访问: http://localhost:5173
应该看到安全 SDK 演示页面。
-
打开前端页面: http://localhost:5173
-
检查控制台日志:
🚀 开始初始化安全 SDK... 📦 加载 WebAssembly 模块... ✅ WebAssembly 加载成功 🔍 采集浏览器指纹... 🔑 获取签名密钥... ✅ SDK 初始化完成 -
查看 SDK 状态面板:
- 指纹 ID: 应显示唯一指纹
- 页面 ID: 随机 UUID
- 当前 Seq: 从 0 开始
- 最后滚动/鼠标: 显示时间差
-
测试 API 调用:
- 点击"测试 API 调用"按钮
- 应返回 200 OK 和模拟数据
- 查看风险评分信息
-
测试行为追踪:
- 滚动页面
- 移动鼠标
- 观察"最后滚动"和"最后鼠标"数值变化
# 无安全头的请求应被拦截
curl -X POST http://localhost:8080/api/v4/feed \
-H "Content-Type: application/json" \
-d '{"test": true}'
# 预期输出: {"error":"missing security headers"}手动修改前端代码,跳过序列号:
// 在 getCurrentBehaviorData() 中
seq: this.context.seq + 10; // 强制跳号应返回: {"error":"invalid sequence"}
停止心跳发送,等待 10 秒后发送请求。
应返回: {"error":"heartbeat timeout"}
tail -f logs/backend.log正常日志示例:
✅ Redis 连接成功
🚀 服务器启动在 http://localhost:8080
🔑 生成密钥: fingerprint=xxx, key=xxx...
💓 心跳: pageID=xxx
📊 Feed 请求: fingerprint=xxx, risk=...
tail -f logs/frontend.log或者在浏览器开发者工具中查看。
# 连接 Redis
redis-cli
# 查看所有键
KEYS *
# 查看特定键
GET key:xxx
GET seq:xxx
GET hb:xxx
# 退出
exit错误: Redis 连接失败
解决:
# 检查 Redis 是否运行
ps aux | grep redis
# 重启 Redis
redis-server --daemonize yes
# 或使用系统服务
brew services restart redis # macOS
sudo systemctl restart redis-server # Linux错误: address already in use: 8080 或 5173
解决:
# 查找占用端口的进程
lsof -i :8080
lsof -i :5173
# 停止进程
kill -9 <PID>错误: WebAssembly 加载失败
解决:
# 重新构建 WASM
cd wasm-sdk
./build.sh
# 检查文件是否存在
ls -lh ../frontend/src/sdk/wasm/behavior_sdk_bg.wasm解决:
cd frontend
# 清除缓存
rm -rf node_modules package-lock.json
# 重新安装
npm install解决:
# 使用国内镜像
export GOPROXY=https://goproxy.cn,direct
# 重新下载依赖
go mod tidy#!/bin/bash
echo "🧪 开始完整测试..."
# 1. 检查服务状态
echo "检查 Redis..."
redis-cli ping || exit 1
echo "检查后端..."
curl -s http://localhost:8080/health | grep "ok" || exit 1
echo "检查前端..."
curl -s http://localhost:5173 > /dev/null || exit 1
# 2. 测试 API
echo "测试 API..."
# 正常请求(需要从前端获取 token,这里仅测试健康检查)
curl -s http://localhost:8080/health
# 测试无效请求
RESULT=$(curl -s -X POST http://localhost:8080/api/v4/feed \
-H "Content-Type: application/json" \
-d '{}')
if echo "$RESULT" | grep -q "error"; then
echo "✅ 防御测试通过(正确拦截无效请求)"
else
echo "❌ 防御测试失败"
fi
echo "✅ 所有测试完成"# 测试后端健康检查接口
ab -n 1000 -c 10 http://localhost:8080/health
# 预期结果:
# - Requests per second: > 1000
# - Time per request: < 10ms# 安装 wrk
# macOS: brew install wrk
# Ubuntu: sudo apt-get install wrk
# 测试
wrk -t4 -c100 -d30s http://localhost:8080/health
# 预期结果:
# - Latency avg: < 20ms
# - Requests/sec: > 5000使用此清单确保所有功能正常:
- Redis 成功启动并可连接
- 后端健康检查返回 OK
- 前端页面正常加载
- SDK 成功初始化
- 浏览器指纹正确生成
- WebAssembly 模块加载成功
- 行为采集器工作正常
- 心跳定时发送
- API 请求自动签名
- 后端正确验证签名
- 无效请求被拦截
- seq 连续性检查生效
- 心跳超时检测生效
- 风险评分正确计算
- Redis 数据正确存储
如果遇到问题:
- 查看日志:
logs/backend.log和logs/frontend.log - 检查 Redis:
redis-cli monitor查看实时操作 - 浏览器控制台: 按 F12 查看前端错误
- 网络面板: 检查请求和响应
文档版本: v1.0 最后更新: 2026-01-08