This commit is contained in:
zeaslity
2025-03-13 11:22:44 +08:00
parent 34147b2f69
commit 4f8a8a6ff2
6 changed files with 117 additions and 51 deletions

View File

@@ -3,6 +3,7 @@ package services
import (
"cmii-uav-watchdog-common/models"
"errors"
"log"
"time"
)
@@ -27,6 +28,11 @@ func (hs *HeartbeatService) ProcessHeartbeat(req models.HeartbeatRequest) (*mode
return nil, errors.New("无效的时间戳")
}
secondTOTPSecret := hs.authService.authorizationInfo.SecondTOTPSecret
if secondTOTPSecret == "" {
return nil, errors.New("二级TOTP密钥为空")
}
// 添加主机信息到集合
hs.authService.AddHostInfo(req.HostInfo)
@@ -36,17 +42,26 @@ func (hs *HeartbeatService) ProcessHeartbeat(req models.HeartbeatRequest) (*mode
Authorized: false,
TOTPCode: "",
Timestamp: time.Now().Unix(),
SecondTOTPSecret: "",
SecondTOTPSecret: secondTOTPSecret,
}, nil
}
// 检查totp码是有效
// 检查totp验证码是有效
if !hs.totpService.VerifyTierTwoTOTPCode(req.TOTPCode, secondTOTPSecret) {
// 解析认证主机的相关信息
// 计算 请求时间与当前时间的时间差
diff := time.Now().Unix() - req.Timestamp
log.Printf("心跳请求时间与当前时间的时间差: %d", diff)
return nil, errors.New("无效的TOTP验证码请检查系统时间是否正确")
}
// 检查主机是否已授权
authorized := hs.authService.IsHostAuthorized(req.HostInfo)
// 生成TOTP验证码
totpCode, err := hs.totpService.GenerateTOTP()
totpCode, err := hs.totpService.GenerateTierTwoTOTPCode(secondTOTPSecret)
if err != nil {
return nil, err
}