// 织忆 MemoryWeave — 认证 + Per-Agent 令牌桶限流 package middleware import ( "crypto/rand" "encoding/hex" "fmt" "log" "net/http" "os" "strings" "sync" "time" ) // ─── Per-Agent 令牌桶 ──────────────────────────────────────── type tokenBucket struct { tokens float64 lastRefill time.Time rate float64 // tokens/sec burst float64 mu sync.Mutex } func newTokenBucket(rate, burst float64) *tokenBucket { return &tokenBucket{ tokens: burst, lastRefill: time.Now(), rate: rate, burst: burst, } } func (tb *tokenBucket) allow() bool { tb.mu.Lock() defer tb.mu.Unlock() now := time.Now() elapsed := now.Sub(tb.lastRefill).Seconds() tb.tokens += elapsed * tb.rate if tb.tokens > tb.burst { tb.tokens = tb.burst } tb.lastRefill = now if tb.tokens >= 1.0 { tb.tokens -= 1.0 return true } return false } // ─── Per-Agent 配置 ────────────────────────────────────────── type AgentRateConfig struct { RecallQPS float64 CommitQPS float64 Burst float64 } var agentRates = map[string]AgentRateConfig{ "hermes": {RecallQPS: 10, CommitQPS: 2, Burst: 20}, "hermes-a06": {RecallQPS: 10, CommitQPS: 2, Burst: 20}, "openclaw": {RecallQPS: 10, CommitQPS: 2, Burst: 20}, "cron-job": {RecallQPS: 5, CommitQPS: 1, Burst: 10}, } var defaultRate = AgentRateConfig{RecallQPS: 5, CommitQPS: 1, Burst: 10} // ─── RateLimiter ───────────────────────────────────────────── type RateLimiter struct { mu sync.Mutex buckets map[string]*tokenBucket // agentID → bucket maxPerMinute int // 全局兜底 } func NewRateLimiter(maxPerMinute int) *RateLimiter { return &RateLimiter{ buckets: make(map[string]*tokenBucket), maxPerMinute: maxPerMinute, } } // Allow 检查 agent 是否允许请求 func (rl *RateLimiter) Allow(agentID string, endpointType string) bool { rl.mu.Lock() defer rl.mu.Unlock() // 获取 agent 配置 cfg, ok := agentRates[agentID] if !ok { cfg = defaultRate } // 根据端点类型选择 QPS var rate float64 switch endpointType { case "recall": rate = cfg.RecallQPS case "commit": rate = cfg.CommitQPS default: rate = cfg.RecallQPS } bucketKey := agentID + ":" + endpointType bucket, exists := rl.buckets[bucketKey] if !exists { bucket = newTokenBucket(rate, cfg.Burst) rl.buckets[bucketKey] = bucket } return bucket.allow() } // ─── 全局实例 ──────────────────────────────────────────────── var GlobalLimiter *RateLimiter func init() { GlobalLimiter = NewRateLimiter(120) } // ─── HTTP 中间件 ───────────────────────────────────────────── // t_4fab32c1 AC2: 密钥强制(无硬编码回落)。 // 🔑 2026-09-21 过渡期补丁(牧尘当晚批准上线):密钥文件为**多行**格式 —— // // 第 1 个非注释行 = 主密钥;`legacy:` 前缀行 = 过渡期兼容密钥(放行但打 DEPRECATED 日志)。 // // 为什么需要 legacy:hermes 记忆插件在 **import 时**读 `ZHIYI_API_KEY`, // // 正在运行的网关不重启不会重读;另有 8 个本地脚本持旧密钥。 // 若直接轮换值 → 瞬间打断记忆读写(recall/commit 全 401)。 // // 收口条件:客户端迁移完(网关重启后)删除 key 文件里的 legacy 行。 const keyFile = "/var/lib/memoryweave/.api-key" // parseKeys 纯解析:data=key 文件内容,envKey=环境变量值。抽出来是为了可被单测直接覆盖。 func parseKeys(data, envKey string) (primary string, legacy []string) { primary = strings.TrimSpace(envKey) for _, line := range strings.Split(data, "\n") { line = strings.TrimSpace(line) if line == "" || strings.HasPrefix(line, "#") { continue } if strings.HasPrefix(line, "legacy:") { if k := strings.TrimSpace(strings.TrimPrefix(line, "legacy:")); k != "" { legacy = append(legacy, k) } continue } if primary == "" { primary = line } } return primary, legacy } // loadKeys 返回 (主密钥, 过渡期兼容密钥列表) func loadKeys() (primary string, legacy []string) { data := "" if b, err := os.ReadFile(keyFile); err == nil { data = string(b) } return parseKeys(data, os.Getenv("API_KEY")) } var ( legacyUseMu sync.Mutex legacyLast = map[string]time.Time{} ) // noteLegacyUse 对 legacy 密钥放行做限频告警(每密钥 10 分钟一次) func noteLegacyUse(key string) { legacyUseMu.Lock() last := legacyLast[key] legacyLast[key] = time.Now() legacyUseMu.Unlock() if time.Since(last) > 10*time.Minute { shown := key if len(shown) > 6 { shown = shown[:6] + "…" } log.Printf("[auth] DEPRECATED: 放行了过渡期 legacy 密钥(%s)。请迁移调用方到主密钥,并删除 %s 的 legacy 行收口。", shown, keyFile) } } func isLegacy(list []string, key string) bool { for _, k := range list { if k == key { return true } } return false } func Auth(next http.Handler) http.Handler { apiKey, legacyKeys := loadKeys() if apiKey == "" { // 生成随机密钥并持久化(无任何可用密钥时拒绝启动或落盘随机密钥) buf := make([]byte, 32) if _, err := rand.Read(buf); err != nil { log.Fatalf("[auth] API_KEY 未设且无法生成随机密钥: %v — 拒绝启动", err) } apiKey = hex.EncodeToString(buf) if err := os.MkdirAll("/var/lib/memoryweave", 0700); err != nil { log.Printf("[auth] WARNING: 已生成随机密钥但无法创建目录: %v — 密钥仅存于内存,重启后失效", err) } else if err := os.WriteFile(keyFile, []byte(apiKey+"\n"), 0600); err != nil { log.Printf("[auth] WARNING: 已生成随机密钥但写入 %s 失败: %v — 密钥仅存于内存,重启后失效", keyFile, err) } else { log.Printf("[auth] API_KEY 未设 — 已生成随机密钥并写入 %s (0600)。请同步给调用方(zhiyi-cli / hermes 等)。", keyFile) } } if len(legacyKeys) > 0 { log.Printf("[auth] 已加载 %d 个过渡期 legacy 密钥(放行+告警);收口:迁移调用方后删除 %s 的 legacy 行", len(legacyKeys), keyFile) } return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // t_4fab32c1 AC3: /metrics 纳入鉴权(旧版在免鉴权白名单内,无 key 即可 // 读取 episode 总数/进程 RSS/队列深度等运维指标,暴露系统内部状态)。 // /health 保留免鉴权(供 systemd / 监控探针使用); // /static/* 和 / 保留免鉴权(Web UI 静态资源)。 if r.URL.Path == "/health" || strings.HasPrefix(r.URL.Path, "/static/") || r.URL.Path == "/" { next.ServeHTTP(w, r) return } key := r.Header.Get("X-API-Key") if key != apiKey && !isLegacy(legacyKeys, key) { // t_2d4e3b92: 统一错误体 schema — 非 2xx + {"error":{"code","message","retry_after_s"}} // 此前为扁平 {"error":"..."},调用方难以与成功体区分(曾导致把 401/429 误读为「召回 0 条」)。 w.Header().Set("Content-Type", "application/json") w.WriteHeader(401) w.Write([]byte(`{"error":{"code":"unauthorized","message":"invalid or missing X-API-Key","retry_after_s":0}}`)) return } if key != apiKey { noteLegacyUse(key) // 过渡期:legacy 密钥放行但留审计痕迹 } // 提取 agent ID(从 X-Agent-ID 头或 URL 路径) agentID := r.Header.Get("X-Agent-ID") if agentID == "" { agentID = "unknown" } // 确定端点类型 endpointType := "recall" if r.URL.Path == "/api/v1/commit" || r.URL.Path == "/api/v1/batch-commit" { endpointType = "commit" } // Per-agent 令牌桶检查 // t_2d4e3b92: 统一错误体 schema — 非 2xx + {"error":{"code","message","retry_after_s"}} if !GlobalLimiter.Allow(agentID, endpointType) { w.Header().Set("Content-Type", "application/json") w.Header().Set("Retry-After", "1") w.Header().Set("X-RateLimit-Reset", fmt.Sprintf("%d", time.Now().Unix()+1)) w.WriteHeader(429) w.Write([]byte(fmt.Sprintf( `{"error":{"code":"rate_limit_exceeded","message":"rate limit exceeded for agent %s","retry_after_s":1}}`, agentID, ))) log.Printf("[ratelimit] %s exceeded for agent %s", endpointType, agentID) return } next.ServeHTTP(w, r) }) } // ─── 兼容旧接口: RateLimit (全局兜底) ───────────────────────── // RateLimit 返回带 Retry-After 头的限流中间件(全局兜底) func RateLimit(maxPerMinute int) func(http.Handler) http.Handler { var lastReset time.Time var count int var mu sync.Mutex return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { mu.Lock() defer mu.Unlock() now := time.Now() if now.Sub(lastReset) > time.Minute { count = 0 lastReset = now } count++ resetTime := lastReset.Add(time.Minute).Unix() if count > maxPerMinute { w.Header().Set("Retry-After", fmt.Sprintf("%d", int(time.Until(lastReset.Add(time.Minute)).Seconds()+1))) w.Header().Set("X-RateLimit-Reset", fmt.Sprintf("%d", resetTime)) w.Header().Set("Content-Type", "application/json") w.WriteHeader(429) // t_2d4e3b92: 统一错误体 schema w.Write([]byte(`{"error":{"code":"rate_limit_exceeded","message":"global rate limit exceeded","retry_after_s":60}}`)) return } next.ServeHTTP(w, r) }) } }