mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 03:56:38 +08:00
Fix logo assets and license heartbeat
This commit is contained in:
+112
-27
@@ -18,6 +18,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/service"
|
||||
)
|
||||
@@ -27,6 +28,10 @@ const (
|
||||
licenseHMACSecretSetting = "license.hmac_secret" // #nosec G101 -- setting key name, not the HMAC secret value.
|
||||
licenseDeviceIDSetting = "license.device_id"
|
||||
licenseDeviceNameSetting = "license.device_name"
|
||||
|
||||
licenseHeartbeatInterval = 12 * time.Hour
|
||||
licenseHeartbeatCheckInterval = 30 * time.Minute
|
||||
licenseHeartbeatStartupDelay = 2 * time.Minute
|
||||
)
|
||||
|
||||
type licenseActivateReq struct {
|
||||
@@ -150,41 +155,121 @@ func licenseStatusHandler(svc *service.Container) gin.HandlerFunc {
|
||||
|
||||
func licenseHeartbeatHandler(svc *service.Container) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
client, err := newLicenseClient(c.Request.Context(), svc)
|
||||
state, err := sendLicenseHeartbeat(c.Request.Context(), svc)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
oldState, _ := loadLicenseState(c.Request.Context(), svc)
|
||||
deviceID, err := ensureLicenseDeviceID(c.Request.Context(), svc, oldState.DeviceID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
deviceName, _ := svc.Repo.Setting.Get(c.Request.Context(), licenseDeviceNameSetting)
|
||||
if strings.TrimSpace(deviceName) == "" {
|
||||
deviceName = defaultLicenseDeviceName()
|
||||
_ = svc.Repo.Setting.Set(c.Request.Context(), licenseDeviceNameSetting, deviceName)
|
||||
}
|
||||
var upstream licenseServerSignedResp
|
||||
if err := client.post(c.Request.Context(), "/api/v1/heartbeat", licenseHeartbeatPayload(oldState, deviceID, deviceName), &upstream); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := client.verifySigned(&upstream); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
state := licenseStateFromSigned(upstream, deviceID, deviceName)
|
||||
state.LicenseKey = oldState.LicenseKey
|
||||
if err := persistLicenseState(c.Request.Context(), svc, state); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, licenseActivationView(state))
|
||||
}
|
||||
}
|
||||
|
||||
// RunLicenseHeartbeatLoop keeps the license server aware of active deployments.
|
||||
// The loop checks periodically, but only sends when the last stored heartbeat is
|
||||
// older than licenseHeartbeatInterval.
|
||||
func RunLicenseHeartbeatLoop(ctx context.Context, svc *service.Container) {
|
||||
if svc == nil {
|
||||
return
|
||||
}
|
||||
run := func() {
|
||||
state, sent, err := maybeSendLicenseHeartbeat(ctx, svc, licenseHeartbeatInterval)
|
||||
if err != nil {
|
||||
if svc.Log != nil {
|
||||
svc.Log.Warn("license heartbeat failed", zap.Error(err))
|
||||
}
|
||||
return
|
||||
}
|
||||
if sent && svc.Log != nil {
|
||||
svc.Log.Info("license heartbeat sent", zap.String("device_id", state.DeviceID))
|
||||
}
|
||||
}
|
||||
|
||||
timer := time.NewTimer(licenseHeartbeatStartupDelay)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-timer.C:
|
||||
run()
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(licenseHeartbeatCheckInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
run()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func maybeSendLicenseHeartbeat(ctx context.Context, svc *service.Container, interval time.Duration) (service.LicenseActivationState, bool, error) {
|
||||
state, err := loadLicenseState(ctx, svc)
|
||||
if err != nil {
|
||||
return state, false, nil
|
||||
}
|
||||
if !licenseHeartbeatEligible(state) || !licenseHeartbeatDue(state, interval) {
|
||||
return state, false, nil
|
||||
}
|
||||
next, err := sendLicenseHeartbeat(ctx, svc)
|
||||
if err != nil {
|
||||
return state, false, err
|
||||
}
|
||||
return next, true, nil
|
||||
}
|
||||
|
||||
func licenseHeartbeatEligible(state service.LicenseActivationState) bool {
|
||||
return strings.TrimSpace(state.LicenseKey) != "" || state.Valid
|
||||
}
|
||||
|
||||
func licenseHeartbeatDue(state service.LicenseActivationState, interval time.Duration) bool {
|
||||
if interval <= 0 {
|
||||
return true
|
||||
}
|
||||
updatedAt := strings.TrimSpace(state.UpdatedAt)
|
||||
if updatedAt == "" {
|
||||
return true
|
||||
}
|
||||
for _, layout := range []string{time.RFC3339, "2006-01-02 15:04:05", "2006-01-02"} {
|
||||
if t, err := time.Parse(layout, updatedAt); err == nil {
|
||||
return time.Since(t) >= interval
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func sendLicenseHeartbeat(ctx context.Context, svc *service.Container) (service.LicenseActivationState, error) {
|
||||
client, err := newLicenseClient(ctx, svc)
|
||||
if err != nil {
|
||||
return service.LicenseActivationState{}, err
|
||||
}
|
||||
oldState, _ := loadLicenseState(ctx, svc)
|
||||
deviceID, err := ensureLicenseDeviceID(ctx, svc, oldState.DeviceID)
|
||||
if err != nil {
|
||||
return service.LicenseActivationState{}, err
|
||||
}
|
||||
deviceName, _ := svc.Repo.Setting.Get(ctx, licenseDeviceNameSetting)
|
||||
if strings.TrimSpace(deviceName) == "" {
|
||||
deviceName = defaultLicenseDeviceName()
|
||||
_ = svc.Repo.Setting.Set(ctx, licenseDeviceNameSetting, deviceName)
|
||||
}
|
||||
var upstream licenseServerSignedResp
|
||||
if err := client.post(ctx, "/api/v1/heartbeat", licenseHeartbeatPayload(oldState, deviceID, deviceName), &upstream); err != nil {
|
||||
return service.LicenseActivationState{}, err
|
||||
}
|
||||
if err := client.verifySigned(&upstream); err != nil {
|
||||
return service.LicenseActivationState{}, err
|
||||
}
|
||||
state := licenseStateFromSigned(upstream, deviceID, deviceName)
|
||||
state.LicenseKey = oldState.LicenseKey
|
||||
if err := persistLicenseState(ctx, svc, state); err != nil {
|
||||
return service.LicenseActivationState{}, err
|
||||
}
|
||||
return state, nil
|
||||
}
|
||||
|
||||
type licenseClient struct {
|
||||
baseURL string
|
||||
hmacSecret string
|
||||
|
||||
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/service"
|
||||
)
|
||||
@@ -90,3 +91,30 @@ func TestLicenseHeartbeatPayloadIncludesStoredLicenseKey(t *testing.T) {
|
||||
t.Fatalf("heartbeat should include stored license key for server-side backfill: %#v", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLicenseHeartbeatDueUsesTwelveHourWindow(t *testing.T) {
|
||||
state := service.LicenseActivationState{
|
||||
Valid: true,
|
||||
UpdatedAt: time.Now().Add(-11 * time.Hour).Format(time.RFC3339),
|
||||
}
|
||||
if licenseHeartbeatDue(state, 12*time.Hour) {
|
||||
t.Fatalf("heartbeat should not be due before interval")
|
||||
}
|
||||
|
||||
state.UpdatedAt = time.Now().Add(-13 * time.Hour).Format(time.RFC3339)
|
||||
if !licenseHeartbeatDue(state, 12*time.Hour) {
|
||||
t.Fatalf("heartbeat should be due after interval")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLicenseHeartbeatEligibleRequiresActivationState(t *testing.T) {
|
||||
if licenseHeartbeatEligible(service.LicenseActivationState{DeviceID: "device-only"}) {
|
||||
t.Fatalf("device id alone should not trigger automatic license heartbeat")
|
||||
}
|
||||
if !licenseHeartbeatEligible(service.LicenseActivationState{LicenseKey: "MS-KEY"}) {
|
||||
t.Fatalf("stored license key should trigger automatic license heartbeat")
|
||||
}
|
||||
if !licenseHeartbeatEligible(service.LicenseActivationState{Valid: true}) {
|
||||
t.Fatalf("valid license state should trigger automatic license heartbeat")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user