用户问:我的 pfx 可能一个文件包含很多个证书,能处理吗?
实测三种形态,行为全部对齐签名工具(osslsigncode / OpenSSL PKCS12_parse):
1) 单身份(1 证书 + 1 私钥):普通情况。
2) 链式(叶子 + 中间 CA + 根,证书多但私钥一把):这是好事,签名会把整条链
写进去,验签更容易过。原来「取第一个证书当签名证书」的做法在这里是错的
—— 链包里第一张可能是中间 CA。改为按「公钥与私钥配对」认叶子证书。
3) 多身份(一个文件里好几套 私钥+证书):签名工具只会用第一套,原来完全没提示。
现在上传时就解析出每套身份(序号/主题/到期),库里存 cert_count / key_count,
列表显示「2 张 / 2 套私钥」,加壳页选中时明确提示「仅使用第一套(CN=…)」。
其它:
- 只有证书没有私钥的 pfx 直接拒(原来会存下来,直到加壳时才炸),报错翻成人话
- 解析全部纯 Go(go-pkcs12 + 自解析 PKCS#1/SEC1),因为生产镜像是 Alpine 没有 openssl CLI
- 签名证书列表新增「证书数」列;schema 补 cert_count/key_count
实测(keytool 造 2 套私钥 p12、openssl 造 3 张链式 pfx、cert-only pfx):
- 单证书 → 1 张 / 1 把
- 链式 → 3 张 / 1 把,签名证书正确识别为叶子;osslsigncode verify 产物里
同时含 Leaf Signer 与 Test Root CA(链完整)
- 多身份 → 2 张 / 2 把,身份列表 Signer A + Signer B;实际签名者为 Signer A,
与面板报告一致(不是按到期时间瞎排序)
- 只有证书 → 400「这个文件里只有证书、没有私钥(不是标准 pfx),签不了名」
149 lines
4.4 KiB
Go
149 lines
4.4 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"verification-platform-backend/internal/database"
|
|
"verification-platform-backend/internal/model"
|
|
)
|
|
|
|
type CleanupConfig struct {
|
|
EnableAutoCleanup bool
|
|
CleanupIntervalHours int
|
|
CaptchaRetentionDays int
|
|
VerifyCodeRetentionDays int
|
|
ApiUsageRetentionDays int
|
|
WebhookLogRetentionDays int
|
|
DeviceSessionRetentionDays int
|
|
}
|
|
|
|
var defaultConfig = CleanupConfig{
|
|
EnableAutoCleanup: false,
|
|
CleanupIntervalHours: 24,
|
|
CaptchaRetentionDays: 1,
|
|
VerifyCodeRetentionDays: 7,
|
|
ApiUsageRetentionDays: 30,
|
|
WebhookLogRetentionDays: 30,
|
|
DeviceSessionRetentionDays: 7,
|
|
}
|
|
|
|
func GetCleanupConfig() CleanupConfig {
|
|
cfg := defaultConfig
|
|
|
|
var settings []model.Setting
|
|
database.DB.Where("category = ?", "cleanup").Find(&settings)
|
|
|
|
for _, s := range settings {
|
|
switch s.Key {
|
|
case "enable_auto_cleanup":
|
|
cfg.EnableAutoCleanup = s.Value == "true"
|
|
case "cleanup_interval_hours":
|
|
cfg.CleanupIntervalHours = parseVal(s.Value, defaultConfig.CleanupIntervalHours)
|
|
case "captcha_retention_days":
|
|
cfg.CaptchaRetentionDays = parseVal(s.Value, defaultConfig.CaptchaRetentionDays)
|
|
case "verify_code_retention_days":
|
|
cfg.VerifyCodeRetentionDays = parseVal(s.Value, defaultConfig.VerifyCodeRetentionDays)
|
|
case "api_usage_retention_days":
|
|
cfg.ApiUsageRetentionDays = parseVal(s.Value, defaultConfig.ApiUsageRetentionDays)
|
|
case "webhook_log_retention_days":
|
|
cfg.WebhookLogRetentionDays = parseVal(s.Value, defaultConfig.WebhookLogRetentionDays)
|
|
case "device_session_retention_days":
|
|
cfg.DeviceSessionRetentionDays = parseVal(s.Value, defaultConfig.DeviceSessionRetentionDays)
|
|
}
|
|
}
|
|
|
|
return cfg
|
|
}
|
|
|
|
func parseVal(value string, defaultVal int) int {
|
|
if value == "" {
|
|
return defaultVal
|
|
}
|
|
var result int
|
|
if _, err := fmt.Sscanf(value, "%d", &result); err != nil || result <= 0 {
|
|
return defaultVal
|
|
}
|
|
return result
|
|
}
|
|
|
|
func StartCleanupScheduler() {
|
|
go func() {
|
|
for {
|
|
cfg := GetCleanupConfig()
|
|
interval := time.Duration(cfg.CleanupIntervalHours) * time.Hour
|
|
if interval < time.Hour {
|
|
interval = time.Hour
|
|
}
|
|
time.Sleep(interval)
|
|
|
|
if !cfg.EnableAutoCleanup {
|
|
continue
|
|
}
|
|
|
|
RunCleanup(cfg)
|
|
}
|
|
}()
|
|
log.Println("[Scheduler] Cleanup scheduler started")
|
|
}
|
|
|
|
func RunCleanup(cfg CleanupConfig) {
|
|
now := time.Now()
|
|
totalCleaned := 0
|
|
|
|
if cfg.CaptchaRetentionDays > 0 {
|
|
cutoff := now.AddDate(0, 0, -cfg.CaptchaRetentionDays)
|
|
result := database.DB.Where("expires_at < ?", cutoff).Delete(&model.Captcha{})
|
|
if result.RowsAffected > 0 {
|
|
log.Printf("[Cleanup] Deleted %d expired captchas", result.RowsAffected)
|
|
totalCleaned += int(result.RowsAffected)
|
|
}
|
|
}
|
|
|
|
if cfg.VerifyCodeRetentionDays > 0 {
|
|
cutoff := now.AddDate(0, 0, -cfg.VerifyCodeRetentionDays)
|
|
emailResult := database.DB.Where("expires_at < ? AND used = ?", cutoff, true).Delete(&model.EmailVerifyCode{})
|
|
smsResult := database.DB.Where("expires_at < ? AND used = ?", cutoff, true).Delete(&model.SmsVerifyCode{})
|
|
count := emailResult.RowsAffected + smsResult.RowsAffected
|
|
if count > 0 {
|
|
log.Printf("[Cleanup] Deleted %d expired verify codes", count)
|
|
totalCleaned += int(count)
|
|
}
|
|
}
|
|
|
|
if cfg.ApiUsageRetentionDays > 0 {
|
|
cutoff := now.AddDate(0, 0, -cfg.ApiUsageRetentionDays)
|
|
result := database.DB.Where("created_at < ?", cutoff).Delete(&model.ApiUsage{})
|
|
if result.RowsAffected > 0 {
|
|
log.Printf("[Cleanup] Deleted %d old api usage records", result.RowsAffected)
|
|
totalCleaned += int(result.RowsAffected)
|
|
}
|
|
}
|
|
|
|
if cfg.WebhookLogRetentionDays > 0 {
|
|
cutoff := now.AddDate(0, 0, -cfg.WebhookLogRetentionDays)
|
|
result := database.DB.Where("created_at < ?", cutoff).Delete(&model.WebhookLog{})
|
|
if result.RowsAffected > 0 {
|
|
log.Printf("[Cleanup] Deleted %d old webhook logs", result.RowsAffected)
|
|
totalCleaned += int(result.RowsAffected)
|
|
}
|
|
}
|
|
|
|
if cfg.DeviceSessionRetentionDays > 0 {
|
|
cutoff := now.AddDate(0, 0, -cfg.DeviceSessionRetentionDays)
|
|
result := database.DB.Where("last_heartbeat < ? AND last_heartbeat IS NOT NULL", cutoff).Delete(&model.DeviceSession{})
|
|
if result.RowsAffected > 0 {
|
|
log.Printf("[Cleanup] Deleted %d expired device sessions", result.RowsAffected)
|
|
totalCleaned += int(result.RowsAffected)
|
|
}
|
|
}
|
|
|
|
if totalCleaned > 0 {
|
|
log.Printf("[Cleanup] Total cleaned: %d records", totalCleaned)
|
|
}
|
|
|
|
// 顺带回收没有版本引用的单文件,省一个单独的触发入口
|
|
RunContentStoreGC()
|
|
}
|