feat(sdk): 加密缓存、GracePolicy、时钟校正与剩余规划 API
- 加密租约缓存(AES-256-GCM + HKDF(指纹)):重启恢复 GRACE,401/410 自动重新激活 - WithGracePolicy(KeepBooleans)、WithLicenseBytes、WithHeartbeatInterval、WithHTTPClient、WithAutoActivate、WithOnEntitlementsChange、Deactivate - 心跳/激活 server_time 参与可信时间校正 - 新增缓存/降级/时钟/重激活/回调测试
This commit is contained in:
6 files changed
+760
-17
No files matched your search
+58
-3
@@ -47,14 +47,17 @@ func (c *Client) Activate(ctx context.Context, licenseKey string) error {
|
||||
if err := c.postJSON(ctx, "/v1/activate", "", body, &resp); err != nil {
|
||||
return err
|
||||
}
|
||||
c.applyServerTime(resp.ServerTime)
|
||||
if err := c.installVerified(resp.Lease); err != nil {
|
||||
return fmt.Errorf("sdk: 服务端 lease 验证失败: %w", err)
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.actToken = resp.Token
|
||||
c.heartbeatEvery = resp.HeartbeatInterval
|
||||
c.heartbeatEvery = c.effectiveHeartbeat(resp.HeartbeatInterval)
|
||||
c.gracePeriod = resp.GracePeriod
|
||||
c.mu.Unlock()
|
||||
c.fromCache = false
|
||||
c.saveCache()
|
||||
c.opts.logger.Info("激活成功", "logger", logField, "activation", resp.ActivationID)
|
||||
return nil
|
||||
}
|
||||
@@ -69,6 +72,7 @@ type activateResp struct {
|
||||
LeaseTTL int64 `json:"lease_ttl"`
|
||||
HeartbeatInterval int64 `json:"heartbeat_interval"`
|
||||
GracePeriod int64 `json:"grace_period"`
|
||||
ServerTime int64 `json:"server_time"`
|
||||
}
|
||||
|
||||
type heartbeatResp struct {
|
||||
@@ -156,6 +160,8 @@ func (c *Client) renewProxy(ctx context.Context) error {
|
||||
if s.state == StateGrace {
|
||||
c.transition(c.snap.Load(), StateActive, 0)
|
||||
}
|
||||
c.fromCache = false
|
||||
c.saveCache()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -183,9 +189,19 @@ func (c *Client) heartbeatOnce(ctx context.Context) error {
|
||||
var resp heartbeatResp
|
||||
err := c.postJSON(ctx, "/v1/heartbeat", token, body, &resp)
|
||||
if err != nil {
|
||||
var se *statusError
|
||||
if errors.As(err, &se) && (se.code == http.StatusUnauthorized || se.code == http.StatusGone) {
|
||||
if key := c.activationKey(); key != "" {
|
||||
c.opts.logger.Info("激活凭证失效,尝试重新激活", "logger", logField, "status", se.code)
|
||||
if aerr := c.Activate(ctx, key); aerr == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
c.enterGrace(grace, err)
|
||||
return err
|
||||
}
|
||||
c.applyServerTime(resp.ServerTime)
|
||||
|
||||
// 先应用 CRL:命中当前 jti 直接转 REVOKED(docs/design/04-crypto.md §4)
|
||||
if resp.CRL != "" {
|
||||
@@ -215,6 +231,11 @@ func (c *Client) heartbeatOnce(ctx context.Context) error {
|
||||
ns := c.snap.Load()
|
||||
c.transition(ns, StateActive, 0)
|
||||
}
|
||||
c.mu.Lock()
|
||||
c.heartbeatEvery = c.effectiveHeartbeat(c.heartbeatEvery)
|
||||
c.mu.Unlock()
|
||||
c.fromCache = false
|
||||
c.saveCache()
|
||||
_ = grace
|
||||
return nil
|
||||
}
|
||||
@@ -292,12 +313,46 @@ func (c *Client) postJSONTo(ctx context.Context, base, path, bearer string, body
|
||||
defer resp.Body.Close()
|
||||
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("sdk: %s 状态码 %d", path, resp.StatusCode)
|
||||
return &statusError{path: path, code: resp.StatusCode}
|
||||
}
|
||||
return json.Unmarshal(raw, out)
|
||||
}
|
||||
|
||||
const sdkVersion = "0.1.0"
|
||||
// statusError 携带 HTTP 状态码(心跳据此判断是否需要重新激活)。
|
||||
type statusError struct {
|
||||
path string
|
||||
code int
|
||||
}
|
||||
|
||||
func (e *statusError) Error() string { return fmt.Sprintf("sdk: %s 状态码 %d", e.path, e.code) }
|
||||
|
||||
// postNoContent 发送 POST 并接受 200/204(停用端点)。
|
||||
func (c *Client) postNoContent(ctx context.Context, path, bearer string, body any) error {
|
||||
b, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.opts.remoteServer+path, bytes.NewReader(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if bearer != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+bearer)
|
||||
}
|
||||
resp, err := c.hc.Do(req)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sdk: 请求 %s: %w", path, err)
|
||||
}
|
||||
defer resp.Body.Close() //nolint:errcheck,gosec
|
||||
io.Copy(io.Discard, io.LimitReader(resp.Body, 1<<20)) //nolint:errcheck,gosec
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
|
||||
return &statusError{path: path, code: resp.StatusCode}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
const sdkVersion = "0.4.0"
|
||||
|
||||
// defaultFingerprint 是自动采集失败时的最后退路(仅 hostname),用于无绑定 license 的席位标识。
|
||||
// 有 bind.fp 的 license 在文件模式会 fail-closed(见 MachineFingerprint / verifyToken)。
|
||||
|
||||
Reference in new issue
Block a user