160 lines
9.0 KiB
Go
160 lines
9.0 KiB
Go
package httpapi
|
||
|
||
import (
|
||
"embed"
|
||
"fmt"
|
||
"net/http"
|
||
"strings"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
//go:embed integration_assets/*
|
||
var integrationAssets embed.FS
|
||
|
||
const defaultIntegrationBaseURL = "https://license.yihuiyong.cn"
|
||
|
||
var integrationLanguages = map[string]string{
|
||
"python": "Python 3.11+",
|
||
"go": "Go 1.22+",
|
||
"java": "Java 17+",
|
||
"dotnet": ".NET 8+",
|
||
"nodejs": "Node.js 20+",
|
||
}
|
||
|
||
var integrationModes = map[string]string{
|
||
"desktop": "桌面客户端",
|
||
"service": "常驻服务",
|
||
"container": "容器部署",
|
||
}
|
||
|
||
type integrationKitResponse struct {
|
||
ProtocolVersion string `json:"protocol_version"`
|
||
PlatformBaseURL string `json:"platform_base_url"`
|
||
Language string `json:"language"`
|
||
LanguageLabel string `json:"language_label"`
|
||
Mode string `json:"mode"`
|
||
ModeLabel string `json:"mode_label"`
|
||
Endpoints []kitAPI `json:"endpoints"`
|
||
SecurityRules []string `json:"security_rules"`
|
||
Prompt string `json:"prompt"`
|
||
TestVector kitVector `json:"payload_example"`
|
||
Protocol string `json:"protocol"`
|
||
Upgrade string `json:"upgrade"`
|
||
}
|
||
|
||
type kitAPI struct {
|
||
Method string `json:"method"`
|
||
Path string `json:"path"`
|
||
Purpose string `json:"purpose"`
|
||
}
|
||
|
||
type kitVector struct {
|
||
Version string `json:"version"`
|
||
InstanceID string `json:"instance_id"`
|
||
State string `json:"state"`
|
||
ServerTime string `json:"server_time"`
|
||
EntitlementExpiresAt string `json:"entitlement_expires_at"`
|
||
LeaseExpiresAt string `json:"lease_expires_at"`
|
||
}
|
||
|
||
func integrationKitInput(c *gin.Context) (string, string, bool) {
|
||
language, mode := c.DefaultQuery("language", "python"), c.DefaultQuery("mode", "service")
|
||
if _, ok := integrationLanguages[language]; !ok {
|
||
apiError(c, http.StatusBadRequest, "invalid_request", "不支持的客户端语言")
|
||
return "", "", false
|
||
}
|
||
if _, ok := integrationModes[mode]; !ok {
|
||
apiError(c, http.StatusBadRequest, "invalid_request", "不支持的接入模式")
|
||
return "", "", false
|
||
}
|
||
return language, mode, true
|
||
}
|
||
|
||
func buildIntegrationKit(language, mode string) integrationKitResponse {
|
||
label, modeLabel := integrationLanguages[language], integrationModes[mode]
|
||
protocol, _ := integrationAsset("protocol.md")
|
||
upgrade, _ := integrationAsset("upgrade.md")
|
||
return integrationKitResponse{
|
||
ProtocolVersion: "license-v2 / lease-v2",
|
||
PlatformBaseURL: defaultIntegrationBaseURL,
|
||
Language: language,
|
||
LanguageLabel: label,
|
||
Mode: mode,
|
||
ModeLabel: modeLabel,
|
||
Endpoints: []kitAPI{
|
||
{Method: "POST", Path: "/api/v1/activation/instances/{instance_id}", Purpose: "首次激活,提交 license_key、secret 和 Idempotency-Key"},
|
||
{Method: "POST", Path: "/api/v1/instances/{instance_id}/heartbeat", Purpose: "刷新已签名运行租约,提交 secret、client_time 和 Idempotency-Key"},
|
||
{Method: "POST", Path: "/api/v1/customers/{customer_id}/releases/{release_id}/download", Purpose: "以实例凭据下载当前有权访问的发布包"},
|
||
},
|
||
SecurityRules: []string{
|
||
"仅信任随软件或受控部署渠道固定分发的 P-256 公钥;不得将 keyring 发现接口作为信任根。",
|
||
"secret 只可放在系统密钥库、Vault、Docker/Kubernetes Secret 或受限加密文件中;不得进入 URL、日志、镜像、源码或浏览器 LocalStorage。",
|
||
"每次网络重试复用同一 Idempotency-Key;新的业务操作必须生成新的键。",
|
||
"验签后核对 version、key_id、instance_id、state、授权到期和租约到期;仅 active 且未到期时可执行业务。",
|
||
"保存服务端时间与本地时间高水位,拒绝显著回拨;正常心跳最短 60 秒,租约最长 14 天。",
|
||
"403/409 进入不可用或人工处理状态;429 遵循 Retry-After;不得无限自动重试。",
|
||
},
|
||
Protocol: string(protocol),
|
||
Upgrade: string(upgrade),
|
||
Prompt: integrationPrompt(label, modeLabel) + "\n\n" + languageRequirements(language) + "\n\n必须阅读接入包 protocol.md、upgrade.md 与 openapi.json。签名规范化不是 JSON:固定顺序 key=value、LF 连接、UTF-8、无末尾换行;签名是 DER ECDSA 的无填充标准 Base64。先通过包内随机测试公钥的 license-v2 和 lease-v2 验签向量,再连接部署环境。测试公钥不得在生产中信任。\n\n客户端升级:当前仅可复用已授权发布包下载,尚未实现客户端版本检查或签名升级清单。先按 upgrade.md 的第一阶段完成受控人工升级;自动升级必须等待第二阶段升级元数据、独立发布信任根和灰度策略落地。",
|
||
TestVector: kitVector{
|
||
Version: "lease-v2", InstanceID: "11111111-2222-3333-4444-555555555555", State: "active",
|
||
ServerTime: "2026-10-09T03:00:00Z", EntitlementExpiresAt: "2027-10-09T03:00:00Z", LeaseExpiresAt: "2026-10-23T03:00:00Z",
|
||
},
|
||
}
|
||
}
|
||
|
||
func integrationAsset(name string) ([]byte, error) {
|
||
content, err := integrationAssets.ReadFile("integration_assets/" + name)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return []byte(strings.ReplaceAll(string(content), "https://license.example.com", defaultIntegrationBaseURL)), nil
|
||
}
|
||
|
||
func integrationPrompt(language, mode string) string {
|
||
return fmt.Sprintf(`你正在为 %s %s 接入授权平台。实现可复用的 LicenseClient,不要调用管理员接口,也不要把实例 secret 暴露给浏览器。
|
||
|
||
默认授权平台根地址:%s。将它作为 SDK 的默认值,同时允许部署通过 LICENSE_API_BASE_URL 覆盖;配置为根地址,不要附加 /api/v1,不要在业务代码中散落硬编码 URL。
|
||
|
||
首次设置由管理员在“授权 → 实例 → 创建实例并获取激活凭据”交付 instance_id、license_key、secret。支持管理台导出的 .license.json 文件导入,严格遵循 protocol.md“授权文件导入”的格式、大小限制、地址确认/允许列表和受保护存储要求;导入后仍需在线激活与验签,不能作为离线授权证书。UI 字段统一为“实例 ID”“授权码”“激活密钥”。实例 ID 不是数据库数字 ID;授权码是后台授权密钥;secret 持续用于心跳和下载;配置加密留存后可重复查看,旧实例无法从哈希恢复。公钥及绑定信息从对应管理后台实例页导出 yhy-license-trust v1 JSON(详见 protocol.md),客户端实现严格导入并通过受控渠道预置,管理员密码与私钥绝不能要求最终用户输入。已激活后从受保护存储恢复,不要每次启动重新填写。当前每条授权只绑定一个实例,旧实例丢失 secret 无法恢复。
|
||
|
||
实现激活 POST /api/v1/activation/instances/{instance_id},提交 license_key、secret 和 Idempotency-Key;实现心跳 POST /api/v1/instances/{instance_id}/heartbeat,提交 secret、RFC3339 UTC client_time 和 Idempotency-Key。
|
||
|
||
必须使用受控渠道预置的 ECDSA P-256 公钥验证 license-v2 / lease-v2 信封,验证 key_id、version、instance_id、state、entitlement_expires_at、lease_expires_at。仅 active 且授权/租约均未过期时允许业务运行。持久化并原子更新已接受租约及本地、服务端时间高水位。secret 不得写入 URL、日志、异常、镜像或源码;403/409 停止自动重试,429 遵循 Retry-After。
|
||
|
||
请交付:LicenseClient、受保护状态存储接口、激活与刷新租约实现、验签单元测试、网络/时钟回拨/过期租约负向测试,以及业务侧 canRun 调用示例。`, language, mode, defaultIntegrationBaseURL)
|
||
}
|
||
|
||
func (a *BusinessAPI) integrationKit(c *gin.Context) {
|
||
language, mode, ok := integrationKitInput(c)
|
||
if !ok {
|
||
return
|
||
}
|
||
c.JSON(http.StatusOK, gin.H{"data": buildIntegrationKit(language, mode)})
|
||
}
|
||
|
||
func (a *BusinessAPI) downloadIntegrationKit(c *gin.Context) {
|
||
language, mode, ok := integrationKitInput(c)
|
||
if !ok {
|
||
return
|
||
}
|
||
kit := buildIntegrationKit(language, mode)
|
||
var b strings.Builder
|
||
fmt.Fprintf(&b, "# 授权平台客户端接入包\n\n协议版本:%s\n\n默认授权平台根地址:%s\n\n", kit.ProtocolVersion, kit.PlatformBaseURL)
|
||
fmt.Fprintf(&b, "目标:%s,%s\n\n## 工程提示词\n\n%s\n\n## 接口\n\n", kit.LanguageLabel, kit.ModeLabel, kit.Prompt)
|
||
for _, endpoint := range kit.Endpoints {
|
||
fmt.Fprintf(&b, "- `%s %s`:%s\n", endpoint.Method, endpoint.Path, endpoint.Purpose)
|
||
}
|
||
b.WriteString("\n## 安全检查\n\n")
|
||
for _, rule := range kit.SecurityRules {
|
||
fmt.Fprintf(&b, "- %s\n", rule)
|
||
}
|
||
fmt.Fprintf(&b, "\n## 租约字段示例(不是签名测试向量)\n\n```json\n{\n \"version\": \"%s\",\n \"instance_id\": \"%s\",\n \"state\": \"%s\",\n \"server_time\": \"%s\",\n \"entitlement_expires_at\": \"%s\",\n \"lease_expires_at\": \"%s\"\n}\n```\n", kit.TestVector.Version, kit.TestVector.InstanceID, kit.TestVector.State, kit.TestVector.ServerTime, kit.TestVector.EntitlementExpiresAt, kit.TestVector.LeaseExpiresAt)
|
||
fmt.Fprintf(&b, "\n\n%s\n\n%s\n", kit.Protocol, kit.Upgrade)
|
||
c.Header("Cache-Control", "no-store")
|
||
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=license-platform-%s-%s-integration.md", language, mode))
|
||
c.Data(http.StatusOK, "text/markdown; charset=utf-8", []byte(b.String()))
|
||
}
|