@wq-hook/anti-cheating-monitor 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Anti-Cheating Monitor Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,332 @@
1
+ # 🛡️ Anti-Cheating Monitor
2
+
3
+ [![npm version](https://badge.fury.io/js/anti-cheating-monitor.svg)](https://badge.fury.io/js/anti-cheating-monitor)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.9+-blue.svg)](https://www.typescriptlang.org/)
6
+ [![React](https://img.shields.io/badge/React-18.0+-61dafb.svg)](https://reactjs.org/)
7
+
8
+ 基于阿里云视觉智能平台的实时反作弊监控系统,提供人脸检测、异常行为分析和完整的在线监考解决方案。
9
+
10
+ ## ✨ 特性
11
+
12
+ - 🎯 **实时监控** - 基于摄像头的持续行为分析
13
+ - 🧠 **智能检测** - 多维度异常行为识别
14
+ - ☁️ **云端处理** - 阿里云 VIAPI 高精度分析
15
+ - 📊 **数据统计** - 完整的监控数据和性能指标
16
+ - 🔒 **安全可靠** - 完善的错误处理和资源管理
17
+ - 🚀 **高性能** - 节流控制和优化策略
18
+ - 📱 **响应式** - 适配各种设备和屏幕尺寸
19
+ - ⚙️ **可配置** - 灵活的阈值配置和热更新
20
+ - 📝 **审计日志** - 完整的配置变更追踪
21
+ - 🎣 **Hook集成** - 现代React Hook API
22
+
23
+ ## 🚀 快速开始
24
+
25
+ ### 安装
26
+
27
+ ```bash
28
+ # 使用 npm
29
+ npm install anti-cheating-monitor
30
+
31
+ # 使用 yarn
32
+ yarn add anti-cheating-monitor
33
+
34
+ # 使用 pnpm
35
+ pnpm add anti-cheating-monitor
36
+ ```
37
+
38
+ ### 基础用法
39
+
40
+ ```tsx
41
+ import React from 'react';
42
+ import { useAntiCheatingMonitor } from 'anti-cheating-monitor';
43
+
44
+ const ExamMonitor = () => {
45
+ const {
46
+ videoRef,
47
+ canvasRef,
48
+ startMonitoring,
49
+ stopMonitoring,
50
+ isMonitoring,
51
+ latestResult,
52
+ stats,
53
+ config,
54
+ updateConfig
55
+ } = useAntiCheatingMonitor({
56
+ credentials: {
57
+ accessKeyId: process.env.VITE_ACCESS_KEY_ID!,
58
+ accessKeySecret: process.env.VITE_ACCESS_KEY_SECRET!,
59
+ securityToken: process.env.VITE_SECURITY_TOKEN,
60
+ },
61
+ config: {
62
+ checkInterval: 3000, // 检测间隔(毫秒)
63
+ detectMultiplePeople: true, // 检测多人
64
+ detectDeviceUsage: true, // 检测设备使用
65
+ detectAbsence: true, // 检测离席
66
+ resolution: { width: 640, height: 480 },
67
+ },
68
+ onViolation: (violation) => {
69
+ console.log('检测到违规:', violation);
70
+ // 处理违规行为,如记录日志、发送通知等
71
+ },
72
+ onDetectionResult: (result) => {
73
+ console.log('检测结果:', result);
74
+ if (result.violations.length > 0) {
75
+ // 处理异常行为
76
+ // 使用更友好的通知方式替代 alert
77
+ console.warn(`检测到异常: ${result.violations.map(v => v.description).join(', ')}`);
78
+ // 这里可以集成通知组件库,如 antd 的 message 或 notification
79
+ // message.error(`检测到异常: ${result.violations.map(v => v.description).join(', ')}`);
80
+ }
81
+ },
82
+ onError: (error) => {
83
+ console.error('监控错误:', error);
84
+ },
85
+ });
86
+
87
+ return (
88
+ <div className="monitor-container">
89
+ <video
90
+ ref={videoRef}
91
+ autoPlay
92
+ muted
93
+ playsInline
94
+ className="monitor-video"
95
+ />
96
+ <canvas ref={canvasRef} style={{ display: 'none' }} />
97
+
98
+ <div className="control-panel">
99
+ <button
100
+ onClick={startMonitoring}
101
+ disabled={isMonitoring}
102
+ className="btn btn-primary"
103
+ >
104
+ {isMonitoring ? '监控中...' : '开始监控'}
105
+ </button>
106
+
107
+ <button
108
+ onClick={stopMonitoring}
109
+ disabled={!isMonitoring}
110
+ className="btn btn-secondary"
111
+ >
112
+ 停止监控
113
+ </button>
114
+ </div>
115
+
116
+ {isMonitoring && (
117
+ <div className="stats-panel">
118
+ <div>检测次数: {stats.checkCount}</div>
119
+ <div>异常次数: {stats.abnormalCount}</div>
120
+ <div>网络延迟: {stats.latency}ms</div>
121
+ <div>网络质量: {stats.networkQuality}</div>
122
+ </div>
123
+ )}
124
+ </div>
125
+ );
126
+ };
127
+
128
+ export default ExamMonitor;
129
+ ```
130
+
131
+ ## 📖 API 文档
132
+
133
+ ### useAntiCheatingMonitor
134
+
135
+ 核心 Hook,提供完整的反作弊监控功能。
136
+
137
+ #### 参数
138
+
139
+ ```typescript
140
+ interface UseAntiCheatingMonitorProps {
141
+ credentials: AntiCheatingCredentials; // 阿里云凭证
142
+ config?: Partial<DetectionConfig>; // 配置选项
143
+ onViolation?: (violation: CheatingViolation) => void; // 违规回调
144
+ onDetectionResult?: (result: DetectionResult) => void; // 检测结果回调
145
+ onError?: (error: Error) => void; // 错误回调
146
+ onConfigChange?: (config: DetectionConfig) => void; // 配置变更回调
147
+ }
148
+ ```
149
+
150
+ #### 返回值
151
+
152
+ ```typescript
153
+ interface UseAntiCheatingMonitorReturn {
154
+ videoRef: React.RefObject<HTMLVideoElement | null>; // 视频元素引用
155
+ canvasRef: React.RefObject<HTMLCanvasElement | null>; // 画布元素引用
156
+ startMonitoring: () => Promise<void>; // 开始监控
157
+ stopMonitoring: () => void; // 停止监控
158
+ forceCheck: () => Promise<void>; // 强制检测
159
+ isMonitoring: boolean; // 监控状态
160
+ latestResult: DetectionResult | null; // 最新检测结果
161
+ stats: MonitorStats; // 统计信息
162
+ config: DetectionConfig; // 当前配置
163
+ updateConfig: (config: Partial<DetectionConfig>) => Promise<{success: boolean, errors?: string[]}>; // 更新配置
164
+ getConfigManager: () => ConfigManager; // 获取配置管理器
165
+ }
166
+ ```
167
+
168
+ ### 检测配置
169
+
170
+ ```typescript
171
+ interface DetectionConfig {
172
+ checkInterval: number; // 检测间隔(毫秒)
173
+ detectMultiplePeople: boolean; // 是否检测多人
174
+ detectDeviceUsage: boolean; // 是否检测设备使用
175
+ detectAbsence: boolean; // 是否检测离席
176
+ resolution: { width: number; height: number }; // 视频分辨率
177
+ type: number; // 检测类型
178
+ earphoneThreshold: number; // 耳机检测阈值
179
+ cellphoneThreshold: number; // 手机检测阈值
180
+ headPoseThreshold: { // 头部姿态阈值
181
+ pitch: number; // 俯仰角
182
+ roll: number; // 翻滚角
183
+ yaw: number; // 偏航角
184
+ };
185
+ faceCompletenessThreshold: number; // 人脸完整度阈值
186
+ centeringThreshold: number; // 人脸居中阈值
187
+ movementThreshold: number; // 可疑移动阈值
188
+ }
189
+ ```
190
+
191
+ ### 违规类型
192
+
193
+ ```typescript
194
+ enum CheatingType {
195
+ PERSON_COUNT_ANOMALY = "PERSON_COUNT_ANOMALY", // 人数异常
196
+ EARPHONE_DETECTED = "EARPHONE_DETECTED", // 检测到耳机
197
+ CELLPHONE_DETECTED = "CELLPHONE_DETECTED", // 检测到手机
198
+ HEAD_POSTURE_ABNORMAL = "HEAD_POSTURE_ABNORMAL", // 头部姿态异常
199
+ FACE_MISSING = "FACE_MISSING", // 人脸缺失
200
+ FACE_OCCLUDED = "FACE_OCCLUDED", // 人脸被遮挡
201
+ NO_FACE_DETECTED = "NO_FACE_DETECTED", // 未检测到人脸
202
+ MULTIPLE_FACES_DETECTED = "MULTIPLE_FACES_DETECTED", // 检测到多张人脸
203
+ NOT_CENTERED = "NOT_CENTERED", // 未居中
204
+ SUSPICIOUS_MOVEMENT = "SUSPICIOUS_MOVEMENT" // 可疑移动
205
+ }
206
+ ```
207
+
208
+ ## 🔧 高级配置
209
+
210
+ ### 配置管理
211
+
212
+ ```typescript
213
+ import { ConfigManager } from 'anti-cheating-monitor';
214
+
215
+ // 获取配置管理器实例
216
+ const configManager = new ConfigManager();
217
+
218
+ // 更新配置
219
+ const result = configManager.updateConfig({
220
+ checkInterval: 5000,
221
+ detectMultiplePeople: true
222
+ }, {
223
+ source: 'local',
224
+ operator: 'admin'
225
+ });
226
+
227
+ if (result.success) {
228
+ console.log('配置更新成功');
229
+ } else {
230
+ console.error('配置更新失败:', result.errors);
231
+ }
232
+
233
+ // 获取审计日志
234
+ const auditLogs = configManager.getAuditLogs();
235
+ console.log('配置变更历史:', auditLogs);
236
+ ```
237
+
238
+ ### 检测引擎
239
+
240
+ ```typescript
241
+ import { DetectionEngine } from 'anti-cheating-monitor';
242
+
243
+ const engine = new DetectionEngine(config, credentials);
244
+
245
+ // 执行检测
246
+ const result = await engine.detect(imageData);
247
+ console.log('检测结果:', result);
248
+
249
+ // 获取统计信息
250
+ const stats = engine.getDetectionStats();
251
+ console.log('检测统计:', stats);
252
+ ```
253
+
254
+ ## 🔒 安全最佳实践
255
+
256
+ ### 1. 凭证管理
257
+
258
+ ```typescript
259
+ // ❌ 不推荐:硬编码凭证
260
+ const credentials = {
261
+ accessKeyId: "LTAI5t...",
262
+ accessKeySecret: "secretpassword"
263
+ };
264
+
265
+ // ✅ 推荐:使用环境变量
266
+ const credentials = {
267
+ accessKeyId: process.env.VITE_ACCESS_KEY_ID!,
268
+ accessKeySecret: process.env.VITE_ACCESS_KEY_SECRET!,
269
+ securityToken: process.env.VITE_SECURITY_TOKEN, // STS 临时凭证
270
+ };
271
+ ```
272
+
273
+ ### 2. STS 临时凭证
274
+
275
+ ```typescript
276
+ // 获取 STS 临时凭证
277
+ const getTemporaryCredentials = async () => {
278
+ const response = await fetch('/api/sts-token');
279
+ return await response.json();
280
+ };
281
+
282
+ // 在组件中使用
283
+ const [credentials, setCredentials] = useState();
284
+
285
+ useEffect(() => {
286
+ getTemporaryCredentials().then(setCredentials);
287
+ }, []);
288
+ ```
289
+
290
+ ## 🌍 浏览器支持
291
+
292
+ - Chrome >= 88
293
+ - Firefox >= 85
294
+ - Safari >= 14
295
+ - Edge >= 88
296
+
297
+ ## 📦 依赖
298
+
299
+ ### Peer Dependencies
300
+
301
+ - React >= 18.0.0
302
+ - React-DOM >= 18.0.0
303
+
304
+ ### Dependencies
305
+
306
+ - ali-oss: 阿里云对象存储SDK
307
+
308
+ ## 🤝 贡献
309
+
310
+ 欢迎贡献代码!请查看 [贡献指南](../docs/CONTRIBUTING.md) 了解详细信息。
311
+
312
+ ## 📄 许可证
313
+
314
+ 本项目采用 [MIT 许可证](./LICENSE)。
315
+
316
+ ## 🙏 致谢
317
+
318
+ - [阿里云视觉智能开放平台](https://vision.aliyun.com/) - 提供强大的人脸识别和行为分析能力
319
+ - [React](https://reactjs.org/) - 用户界面构建
320
+ - [Vite](https://vitejs.dev/) - 现代化构建工具
321
+ - [TypeScript](https://www.typescriptlang.org/) - 类型安全的 JavaScript
322
+
323
+ ## 📞 支持
324
+
325
+ - 📧 邮箱: 819049639@qq.com
326
+ - 🐛 问题反馈: [GitHub Issues](https://github.com/coding-daily-wq/anti-cheating-monitor/issues)
327
+ - 💬 讨论: [GitHub Discussions](https://github.com/coding-daily-wq/anti-cheating-monitor/discussions)
328
+ - 📖 文档: [在线文档](https://coding-daily-wq.github.io/anti-cheating-monitor)
329
+
330
+ ---
331
+
332
+ ⭐ 如果这个项目对您有帮助,请给我们一个 Star!