-
-
Notifications
You must be signed in to change notification settings - Fork 631
Expand file tree
/
Copy pathdashboard_handler.go
More file actions
496 lines (435 loc) · 15.2 KB
/
dashboard_handler.go
File metadata and controls
496 lines (435 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
package handler
import (
"fmt"
"gpt-load/internal/encryption"
app_errors "gpt-load/internal/errors"
"gpt-load/internal/i18n"
"gpt-load/internal/models"
"gpt-load/internal/response"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
// Stats Get dashboard statistics
func (s *Server) Stats(c *gin.Context) {
var activeKeys, invalidKeys int64
s.DB.Model(&models.APIKey{}).Where("status = ?", models.KeyStatusActive).Count(&activeKeys)
s.DB.Model(&models.APIKey{}).Where("status = ?", models.KeyStatusInvalid).Count(&invalidKeys)
now := time.Now()
rpmStats, err := s.getRPMStats(now)
if err != nil {
response.ErrorI18nFromAPIError(c, app_errors.ErrDatabase, "database.rpm_stats_failed")
return
}
twentyFourHoursAgo := now.Add(-24 * time.Hour)
fortyEightHoursAgo := now.Add(-48 * time.Hour)
currentPeriod, err := s.getHourlyStats(twentyFourHoursAgo, now)
if err != nil {
response.ErrorI18nFromAPIError(c, app_errors.ErrDatabase, "database.current_stats_failed")
return
}
previousPeriod, err := s.getHourlyStats(fortyEightHoursAgo, twentyFourHoursAgo)
if err != nil {
response.ErrorI18nFromAPIError(c, app_errors.ErrDatabase, "database.previous_stats_failed")
return
}
// 计算请求量趋势
reqTrend := 0.0
reqTrendIsGrowth := true
if previousPeriod.TotalRequests > 0 {
// 有前期数据,计算百分比变化
reqTrend = (float64(currentPeriod.TotalRequests-previousPeriod.TotalRequests) / float64(previousPeriod.TotalRequests)) * 100
reqTrendIsGrowth = reqTrend >= 0
} else if currentPeriod.TotalRequests > 0 {
// 前期无数据,当前有数据,视为100%增长
reqTrend = 100.0
reqTrendIsGrowth = true
} else {
// 前期和当前都无数据
reqTrend = 0.0
reqTrendIsGrowth = true
}
// 计算当前和前期错误率
currentErrorRate := 0.0
if currentPeriod.TotalRequests > 0 {
currentErrorRate = (float64(currentPeriod.TotalFailures) / float64(currentPeriod.TotalRequests)) * 100
}
previousErrorRate := 0.0
if previousPeriod.TotalRequests > 0 {
previousErrorRate = (float64(previousPeriod.TotalFailures) / float64(previousPeriod.TotalRequests)) * 100
}
// 计算错误率趋势
errorRateTrend := 0.0
errorRateTrendIsGrowth := false
if previousPeriod.TotalRequests > 0 {
// 有前期数据,计算百分点差异
errorRateTrend = currentErrorRate - previousErrorRate
errorRateTrendIsGrowth = errorRateTrend < 0 // 错误率下降是好事
} else if currentPeriod.TotalRequests > 0 {
// 前期无数据,当前有数据
errorRateTrend = currentErrorRate // 显示当前错误率
errorRateTrendIsGrowth = false // 有错误是坏事(如果错误率>0)
if currentErrorRate == 0 {
errorRateTrendIsGrowth = true // 如果当前无错误,标记为正面
}
} else {
// 都无数据
errorRateTrend = 0.0
errorRateTrendIsGrowth = true
}
// 获取安全警告信息
securityWarnings := s.getSecurityWarnings(c)
stats := models.DashboardStatsResponse{
KeyCount: models.StatCard{
Value: float64(activeKeys),
SubValue: invalidKeys,
SubValueTip: i18n.Message(c, "dashboard.invalid_keys"),
},
RPM: rpmStats,
RequestCount: models.StatCard{
Value: float64(currentPeriod.TotalRequests),
Trend: reqTrend,
TrendIsGrowth: reqTrendIsGrowth,
},
ErrorRate: models.StatCard{
Value: currentErrorRate,
Trend: errorRateTrend,
TrendIsGrowth: errorRateTrendIsGrowth,
},
SecurityWarnings: securityWarnings,
}
response.Success(c, stats)
}
// Chart Get dashboard chart data
func (s *Server) Chart(c *gin.Context) {
groupID := c.Query("groupId")
now := time.Now()
endHour := now.Truncate(time.Hour)
startHour := endHour.Add(-23 * time.Hour)
var hourlyStats []models.GroupHourlyStat
query := s.DB.Table("group_hourly_stats").
Where("time >= ? AND time < ?", startHour, endHour.Add(time.Hour))
if groupID != "" {
query = query.Where("group_id = ?", groupID)
} else {
query = query.Where("group_id NOT IN (?)",
s.DB.Table("groups").Select("id").Where("group_type = ?", "aggregate"))
}
if err := query.Order("time asc").Find(&hourlyStats).Error; err != nil {
response.ErrorI18nFromAPIError(c, app_errors.ErrDatabase, "database.chart_data_failed")
return
}
statsByHour := make(map[time.Time]map[string]int64)
for _, stat := range hourlyStats {
hour := stat.Time.Local().Truncate(time.Hour)
if _, ok := statsByHour[hour]; !ok {
statsByHour[hour] = make(map[string]int64)
}
statsByHour[hour]["success"] += stat.SuccessCount
statsByHour[hour]["failure"] += stat.FailureCount
}
var labels []string
var successData, failureData []int64
for i := range 24 {
hour := startHour.Add(time.Duration(i) * time.Hour)
labels = append(labels, hour.Format(time.RFC3339))
if data, ok := statsByHour[hour]; ok {
successData = append(successData, data["success"])
failureData = append(failureData, data["failure"])
} else {
successData = append(successData, 0)
failureData = append(failureData, 0)
}
}
chartData := models.ChartData{
Labels: labels,
Datasets: []models.ChartDataset{
{
Label: i18n.Message(c, "dashboard.success_requests"),
Data: successData,
Color: "rgba(10, 200, 110, 1)",
},
{
Label: i18n.Message(c, "dashboard.failed_requests"),
Data: failureData,
Color: "rgba(255, 70, 70, 1)",
},
},
}
response.Success(c, chartData)
}
type hourlyStatResult struct {
TotalRequests int64
TotalFailures int64
}
func (s *Server) getHourlyStats(startTime, endTime time.Time) (hourlyStatResult, error) {
var result hourlyStatResult
err := s.DB.Table("group_hourly_stats").
Where("time >= ? AND time < ?", startTime, endTime).
Where("group_id NOT IN (?)",
s.DB.Table("groups").Select("id").Where("group_type = ?", "aggregate")).
Select("COALESCE(SUM(success_count), 0) + COALESCE(SUM(failure_count), 0) as total_requests, COALESCE(SUM(failure_count), 0) as total_failures").
Scan(&result).Error
return result, err
}
type rpmStatResult struct {
CurrentRequests int64
PreviousRequests int64
}
func (s *Server) getRPMStats(now time.Time) (models.StatCard, error) {
tenMinutesAgo := now.Add(-10 * time.Minute)
twentyMinutesAgo := now.Add(-20 * time.Minute)
var result rpmStatResult
err := s.DB.Model(&models.RequestLog{}).
Select("count(case when timestamp >= ? then 1 end) as current_requests, count(case when timestamp >= ? and timestamp < ? then 1 end) as previous_requests", tenMinutesAgo, twentyMinutesAgo, tenMinutesAgo).
Where("timestamp >= ? AND request_type = ?", twentyMinutesAgo, models.RequestTypeFinal).
Scan(&result).Error
if err != nil {
return models.StatCard{}, err
}
currentRPM := float64(result.CurrentRequests) / 10.0
previousRPM := float64(result.PreviousRequests) / 10.0
rpmTrend := 0.0
rpmTrendIsGrowth := true
if previousRPM > 0 {
rpmTrend = (currentRPM - previousRPM) / previousRPM * 100
rpmTrendIsGrowth = rpmTrend >= 0
} else if currentRPM > 0 {
rpmTrend = 100.0
rpmTrendIsGrowth = true
}
return models.StatCard{
Value: currentRPM,
Trend: rpmTrend,
TrendIsGrowth: rpmTrendIsGrowth,
}, nil
}
// getSecurityWarnings 检查安全配置并返回警告信息
func (s *Server) getSecurityWarnings(c *gin.Context) []models.SecurityWarning {
var warnings []models.SecurityWarning
// 获取AUTH_KEY和ENCRYPTION_KEY
authConfig := s.config.GetAuthConfig()
encryptionKey := s.config.GetEncryptionKey()
// 检查AUTH_KEY
if authConfig.Key == "" {
warnings = append(warnings, models.SecurityWarning{
Type: "AUTH_KEY",
Message: i18n.Message(c, "dashboard.auth_key_missing"),
Severity: "high",
Suggestion: i18n.Message(c, "dashboard.auth_key_required"),
})
} else {
authWarnings := checkPasswordSecurity(c, authConfig.Key, "AUTH_KEY")
warnings = append(warnings, authWarnings...)
}
// 检查ENCRYPTION_KEY
if encryptionKey == "" {
warnings = append(warnings, models.SecurityWarning{
Type: "ENCRYPTION_KEY",
Message: i18n.Message(c, "dashboard.encryption_key_missing"),
Severity: "high",
Suggestion: i18n.Message(c, "dashboard.encryption_key_recommended"),
})
} else {
encryptionWarnings := checkPasswordSecurity(c, encryptionKey, "ENCRYPTION_KEY")
warnings = append(warnings, encryptionWarnings...)
}
// 检查系统级代理密钥
systemSettings := s.SettingsManager.GetSettings()
if systemSettings.ProxyKeys != "" {
proxyKeys := strings.Split(systemSettings.ProxyKeys, ",")
for i, key := range proxyKeys {
key = strings.TrimSpace(key)
if key != "" {
keyName := fmt.Sprintf("%s #%d", i18n.Message(c, "dashboard.global_proxy_key"), i+1)
proxyWarnings := checkPasswordSecurity(c, key, keyName)
warnings = append(warnings, proxyWarnings...)
}
}
}
// 检查分组级代理密钥
var groups []models.Group
if err := s.DB.Where("proxy_keys IS NOT NULL AND proxy_keys != ''").Find(&groups).Error; err == nil {
for _, group := range groups {
if group.ProxyKeys != "" {
proxyKeys := strings.Split(group.ProxyKeys, ",")
for i, key := range proxyKeys {
key = strings.TrimSpace(key)
if key != "" {
keyName := fmt.Sprintf("%s [%s] #%d", i18n.Message(c, "dashboard.group_proxy_key"), group.Name, i+1)
proxyWarnings := checkPasswordSecurity(c, key, keyName)
warnings = append(warnings, proxyWarnings...)
}
}
}
}
}
return warnings
}
// checkPasswordSecurity 综合检查密码安全性
func checkPasswordSecurity(c *gin.Context, password, keyType string) []models.SecurityWarning {
var warnings []models.SecurityWarning
// 1. 长度检查
if len(password) < 16 {
warnings = append(warnings, models.SecurityWarning{
Type: keyType,
Message: i18n.Message(c, "security.password_too_short", map[string]any{"keyType": keyType, "length": len(password)}),
Severity: "high", // 长度不足是高风险
Suggestion: i18n.Message(c, "security.password_recommendation_16"),
})
} else if len(password) < 32 {
warnings = append(warnings, models.SecurityWarning{
Type: keyType,
Message: i18n.Message(c, "security.password_short", map[string]any{"keyType": keyType, "length": len(password)}),
Severity: "medium",
Suggestion: i18n.Message(c, "security.password_recommendation_32"),
})
}
// 2. 常见弱密码检查
lower := strings.ToLower(password)
weakPatterns := []string{
"password", "123456", "admin", "secret", "test", "demo",
"sk-123456", "key", "token", "pass", "pwd", "qwerty",
"abc", "default", "user", "login", "auth", "temp",
}
for _, pattern := range weakPatterns {
if strings.Contains(lower, pattern) {
warnings = append(warnings, models.SecurityWarning{
Type: keyType,
Message: i18n.Message(c, "security.password_weak_pattern", map[string]any{"keyType": keyType, "pattern": pattern}),
Severity: "high",
Suggestion: i18n.Message(c, "security.password_avoid_common"),
})
break
}
}
// 3. 复杂度检查(仅在长度足够时检查)
if len(password) >= 16 && !hasGoodComplexity(password) {
warnings = append(warnings, models.SecurityWarning{
Type: keyType,
Message: i18n.Message(c, "security.password_low_complexity", map[string]any{"keyType": keyType}),
Severity: "medium",
Suggestion: i18n.Message(c, "security.password_complexity"),
})
}
return warnings
}
// hasGoodComplexity 检查密码复杂度
func hasGoodComplexity(password string) bool {
var hasUpper, hasLower, hasDigit, hasSpecial bool
for _, char := range password {
switch {
case char >= 'A' && char <= 'Z':
hasUpper = true
case char >= 'a' && char <= 'z':
hasLower = true
case char >= '0' && char <= '9':
hasDigit = true
case !((char >= 'A' && char <= 'Z') || (char >= 'a' && char <= 'z') || (char >= '0' && char <= '9')):
hasSpecial = true
}
}
// 至少包含3种类型的字符
count := 0
if hasUpper {
count++
}
if hasLower {
count++
}
if hasDigit {
count++
}
if hasSpecial {
count++
}
return count >= 3
}
// Encryption scenario types
const (
ScenarioNone = ""
ScenarioDataNotEncrypted = "data_not_encrypted"
ScenarioKeyNotConfigured = "key_not_configured"
ScenarioKeyMismatch = "key_mismatch"
)
// EncryptionStatus checks if ENCRYPTION_KEY is configured but keys are not encrypted
func (s *Server) EncryptionStatus(c *gin.Context) {
hasMismatch, scenarioType, message, suggestion := s.checkEncryptionMismatch(c)
response.Success(c, gin.H{
"has_mismatch": hasMismatch,
"scenario_type": scenarioType,
"message": message,
"suggestion": suggestion,
})
}
// checkEncryptionMismatch detects encryption configuration mismatches
func (s *Server) checkEncryptionMismatch(c *gin.Context) (bool, string, string, string) {
encryptionKey := s.config.GetEncryptionKey()
// Sample check API keys
var sampleKeys []models.APIKey
if err := s.DB.Limit(20).Where("key_hash IS NOT NULL AND key_hash != ''").Find(&sampleKeys).Error; err != nil {
logrus.WithError(err).Error("Failed to fetch sample keys for encryption check")
return false, ScenarioNone, "", ""
}
if len(sampleKeys) == 0 {
// No keys in database, no mismatch
return false, ScenarioNone, "", ""
}
// Check hash consistency with unencrypted data
noopService, err := encryption.NewService("")
if err != nil {
logrus.WithError(err).Error("Failed to create noop encryption service")
return false, ScenarioNone, "", ""
}
unencryptedHashMatchCount := 0
for _, key := range sampleKeys {
// For unencrypted data: key_hash should match SHA256(key_value)
expectedHash := noopService.Hash(key.KeyValue)
if expectedHash == key.KeyHash {
unencryptedHashMatchCount++
}
}
unencryptedConsistencyRate := float64(unencryptedHashMatchCount) / float64(len(sampleKeys))
// If ENCRYPTION_KEY is configured, also check if current key can decrypt the data
var currentKeyHashMatchCount int
if encryptionKey != "" {
currentService, err := encryption.NewService(encryptionKey)
if err == nil {
for _, key := range sampleKeys {
// Try to decrypt and re-hash to check if current key matches
decrypted, err := currentService.Decrypt(key.KeyValue)
if err == nil {
// Successfully decrypted, check if hash matches
expectedHash := currentService.Hash(decrypted)
if expectedHash == key.KeyHash {
currentKeyHashMatchCount++
}
}
}
}
}
currentKeyConsistencyRate := float64(currentKeyHashMatchCount) / float64(len(sampleKeys))
// Scenario A: ENCRYPTION_KEY configured but data not encrypted
if encryptionKey != "" && unencryptedConsistencyRate > 0.8 {
return true,
ScenarioDataNotEncrypted,
i18n.Message(c, "dashboard.encryption_key_configured_but_data_not_encrypted"),
i18n.Message(c, "dashboard.encryption_key_migration_required")
}
// Scenario B: ENCRYPTION_KEY not configured but data is encrypted
if encryptionKey == "" && unencryptedConsistencyRate < 0.2 {
return true,
ScenarioKeyNotConfigured,
i18n.Message(c, "dashboard.data_encrypted_but_key_not_configured"),
i18n.Message(c, "dashboard.configure_same_encryption_key")
}
// Scenario C: ENCRYPTION_KEY configured but doesn't match encrypted data
if encryptionKey != "" && unencryptedConsistencyRate < 0.2 && currentKeyConsistencyRate < 0.2 {
return true,
ScenarioKeyMismatch,
i18n.Message(c, "dashboard.encryption_key_mismatch"),
i18n.Message(c, "dashboard.use_correct_encryption_key")
}
return false, ScenarioNone, "", ""
}