fix(sdk): 离线 license 默认自动校验机器指纹
- 新增 sdk.MachineFingerprint:Linux machine-id+DMI、macOS IOPlatformUUID,容器内 fail-closed - 文件模式在 bind.fp 存在时自动校验,无需显式 WithFingerprint;采集失败拒绝绑定 license - 新增 WithFingerprintFunc 支持容器读取挂载宿主 machine-id;在线激活同步使用自动指纹 - license 包新增 NormalizeFingerprint(sha256:64hex 校验与小写归一化) - 新增指纹绑定/采集失败/无绑定跳过等测试
This commit is contained in:
10 files changed
+433
-21
No files matched your search
+34
-18
@@ -53,17 +53,18 @@ func (wallClock) Now() int64 { return time.Now().Unix() }
|
||||
var defaultTierOrder = []string{"free", "starter", "pro", "business", "enterprise", "ultimate"}
|
||||
|
||||
type options struct {
|
||||
licenseFile string
|
||||
remoteServer string
|
||||
licenseKey string
|
||||
productKey string
|
||||
fingerprint string
|
||||
trust *crypto.TrustChain
|
||||
tierOrder []string
|
||||
clock Clock
|
||||
logger *slog.Logger
|
||||
onState func(from, to State)
|
||||
pollInterval time.Duration
|
||||
licenseFile string
|
||||
remoteServer string
|
||||
licenseKey string
|
||||
productKey string
|
||||
fingerprint string
|
||||
fingerprintFn func() (string, error)
|
||||
trust *crypto.TrustChain
|
||||
tierOrder []string
|
||||
clock Clock
|
||||
logger *slog.Logger
|
||||
onState func(from, to State)
|
||||
pollInterval time.Duration
|
||||
|
||||
// Proxy 模式(docs/design/08-k8s-proxy.md §4)
|
||||
proxyAddr string
|
||||
@@ -74,11 +75,19 @@ type options struct {
|
||||
// Option 配置项(docs/design/07-sdk.md §3.1)。
|
||||
type Option func(*options)
|
||||
|
||||
func WithLicenseFile(path string) Option { return func(o *options) { o.licenseFile = path } }
|
||||
func WithRemoteServer(baseURL string) Option { return func(o *options) { o.remoteServer = baseURL } }
|
||||
func WithLicenseKey(key string) Option { return func(o *options) { o.licenseKey = key } }
|
||||
func WithProductKey(pk string) Option { return func(o *options) { o.productKey = pk } }
|
||||
func WithFingerprint(fp string) Option { return func(o *options) { o.fingerprint = fp } }
|
||||
func WithLicenseFile(path string) Option { return func(o *options) { o.licenseFile = path } }
|
||||
func WithRemoteServer(baseURL string) Option { return func(o *options) { o.remoteServer = baseURL } }
|
||||
func WithLicenseKey(key string) Option { return func(o *options) { o.licenseKey = key } }
|
||||
func WithProductKey(pk string) Option { return func(o *options) { o.productKey = pk } }
|
||||
|
||||
// WithFingerprint 显式指定机器指纹;省略时 SDK 自动采集(见 MachineFingerprint)。
|
||||
// 自定义采集(如容器内读取挂载的宿主 machine-id)使用 WithFingerprintFunc。
|
||||
func WithFingerprint(fp string) Option { return func(o *options) { o.fingerprint = fp } }
|
||||
|
||||
// WithFingerprintFunc 自定义机器指纹采集器;仅在未显式 WithFingerprint 时生效。
|
||||
func WithFingerprintFunc(fn func() (string, error)) Option {
|
||||
return func(o *options) { o.fingerprintFn = fn }
|
||||
}
|
||||
func WithTrustChain(tc *crypto.TrustChain) Option { return func(o *options) { o.trust = tc } }
|
||||
func WithTierOrder(order []string) Option { return func(o *options) { o.tierOrder = order } }
|
||||
func WithClock(c Clock) Option { return func(o *options) { o.clock = c } }
|
||||
@@ -226,8 +235,15 @@ func (c *Client) verifyToken(token string) (*license.Payload, error) {
|
||||
if p.Prod != c.opts.productKey {
|
||||
return nil, fmt.Errorf("sdk: 产品不符(license=%s, 期望=%s)", p.Prod, c.opts.productKey)
|
||||
}
|
||||
if c.opts.fingerprint != "" && p.Bind != nil && p.Bind.FP != "" && p.Bind.FP != c.opts.fingerprint {
|
||||
return nil, errors.New("sdk: 机器指纹绑定不符")
|
||||
// bind.fp 存在时必须校验:显式指纹 > 自定义采集器 > 默认机器采集;采集失败 fail-closed。
|
||||
if p.Bind != nil && p.Bind.FP != "" {
|
||||
fp, err := c.effectiveFingerprint()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if fp != p.Bind.FP {
|
||||
return nil, errors.New("sdk: 机器指纹绑定不符")
|
||||
}
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user