Fix license sync and cloud scan scheduling

This commit is contained in:
ShukeBta
2026-06-18 01:39:39 +08:00
parent 692b7fb9df
commit b492d9f867
6 changed files with 215 additions and 36 deletions
+54 -14
View File
@@ -53,15 +53,16 @@ type licenseServerSignedResp struct {
}
type licenseServerStatusResp struct {
Valid bool `json:"valid"`
LicenseType *string `json:"license_type"`
ExpiryDate *string `json:"expiry_date"`
MaxDevices int `json:"max_devices"`
MaxUsers *int `json:"max_users"`
UnlimitedUsers bool `json:"unlimited_users"`
DaysRemaining *int `json:"days_remaining"`
DeviceName string `json:"device_name"`
IsActive bool `json:"is_active"`
Valid bool `json:"valid"`
LicenseType *string `json:"license_type"`
ExpiryDate *string `json:"expiry_date"`
MaxDevices int `json:"max_devices"`
MaxUsers *int `json:"max_users"`
UnlimitedUsers bool `json:"unlimited_users"`
DaysRemaining *int `json:"days_remaining"`
DeviceName string `json:"device_name"`
IsActive bool `json:"is_active"`
HeartbeatRequested bool `json:"heartbeat_requested"`
}
func licenseActivateHandler(svc *service.Container) gin.HandlerFunc {
@@ -129,13 +130,16 @@ func licenseStatusHandler(svc *service.Container) gin.HandlerFunc {
nextState := licenseStateFromSigned(signed, deviceID, deviceName)
nextState.LicenseKey = state.LicenseKey
state = nextState
if refreshed, ok, _, refreshErr := refreshLicenseServerStatus(c.Request.Context(), client, state, deviceID); refreshErr == nil && ok {
refreshed.LicenseKey = state.LicenseKey
state = refreshed
}
_ = persistLicenseState(c.Request.Context(), svc, state)
} else {
var upstream licenseServerStatusResp
if getErr := client.get(c.Request.Context(), "/api/v1/status/"+url.PathEscape(deviceID), &upstream); getErr == nil && upstream.Valid {
applyLicenseStatus(&state, upstream, deviceID)
if refreshed, ok, _, getErr := refreshLicenseServerStatus(c.Request.Context(), client, state, deviceID); getErr == nil && ok {
state = refreshed
_ = persistLicenseState(c.Request.Context(), svc, state)
} else if getErr == nil && !upstream.Valid {
} else if getErr == nil {
state.Valid = false
_ = persistLicenseState(c.Request.Context(), svc, state)
}
@@ -210,9 +214,27 @@ func maybeSendLicenseHeartbeat(ctx context.Context, svc *service.Container, inte
if err != nil {
return state, false, nil
}
if !licenseHeartbeatEligible(state) || !licenseHeartbeatDue(state, interval) {
if !licenseHeartbeatEligible(state) {
return state, false, nil
}
if !licenseHeartbeatDue(state, interval) {
client, clientErr := newLicenseClient(ctx, svc)
if clientErr != nil {
return state, false, nil
}
deviceID, idErr := ensureLicenseDeviceID(ctx, svc, state.DeviceID)
if idErr != nil {
return state, false, idErr
}
refreshed, ok, requested, refreshErr := refreshLicenseServerStatus(ctx, client, state, deviceID)
if refreshErr == nil && ok {
state = refreshed
_ = persistLicenseState(ctx, svc, state)
}
if !requested {
return state, false, nil
}
}
next, err := sendLicenseHeartbeat(ctx, svc)
if err != nil {
return state, false, err
@@ -264,12 +286,30 @@ func sendLicenseHeartbeat(ctx context.Context, svc *service.Container) (service.
}
state := licenseStateFromSigned(upstream, deviceID, deviceName)
state.LicenseKey = oldState.LicenseKey
if refreshed, ok, _, refreshErr := refreshLicenseServerStatus(ctx, client, state, deviceID); refreshErr == nil && ok {
refreshed.LicenseKey = state.LicenseKey
state = refreshed
}
if err := persistLicenseState(ctx, svc, state); err != nil {
return service.LicenseActivationState{}, err
}
return state, nil
}
func refreshLicenseServerStatus(ctx context.Context, client *licenseClient, state service.LicenseActivationState, deviceID string) (service.LicenseActivationState, bool, bool, error) {
var upstream licenseServerStatusResp
if err := client.get(ctx, "/api/v1/status/"+url.PathEscape(deviceID), &upstream); err != nil {
return state, false, false, err
}
if !upstream.Valid {
state.Valid = false
state.UpdatedAt = time.Now().Format(time.RFC3339)
return state, false, upstream.HeartbeatRequested, nil
}
applyLicenseStatus(&state, upstream, deviceID)
return state, true, upstream.HeartbeatRequested, nil
}
type licenseClient struct {
baseURL string
hmacSecret string
+39
View File
@@ -1,6 +1,8 @@
package handler
import (
"net/http"
"net/http/httptest"
"testing"
"time"
@@ -79,6 +81,43 @@ func TestApplyLicenseStatusReflectsUnlimitedUsers(t *testing.T) {
}
}
func TestRefreshLicenseServerStatusReflectsEditedLimitAndHeartbeatRequest(t *testing.T) {
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/status/device-1" {
t.Fatalf("unexpected path %s", r.URL.Path)
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{
"valid": true,
"license_type": "subscription",
"max_devices": 5,
"max_users": 60,
"unlimited_users": false,
"device_name": "NAS",
"heartbeat_requested": true,
"is_active": true
}`))
}))
defer upstream.Close()
state := service.LicenseActivationState{Valid: true, UnlimitedUsers: true}
client := &licenseClient{baseURL: upstream.URL, httpClient: upstream.Client()}
refreshed, ok, requested, err := refreshLicenseServerStatus(t.Context(), client, state, "device-1")
if err != nil {
t.Fatalf("refresh status: %v", err)
}
if !ok || !requested {
t.Fatalf("expected valid status with requested heartbeat, ok=%v requested=%v", ok, requested)
}
if refreshed.MaxUsers == nil || *refreshed.MaxUsers != 60 || refreshed.UnlimitedUsers {
t.Fatalf("edited user limit was not reflected: %+v", refreshed)
}
if refreshed.MaxDevices != 5 || refreshed.DeviceName != "NAS" {
t.Fatalf("server status fields were not applied: %+v", refreshed)
}
}
func TestLicenseHeartbeatPayloadIncludesStoredLicenseKey(t *testing.T) {
payload := licenseHeartbeatPayload(service.LicenseActivationState{
LicenseKey: "MS-ABCD-EFGH-JKLM-NPQR",