UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

57 lines (56 loc) • 1.98 kB
import { j as resolveIntegerOption } from "./number-coercion-CuVhdCBs.mjs"; import { t as pruneMapToMaxSize } from "./map-size-BlrH_CnE.mjs"; //#region src/gateway/server/ws-connection/handshake-auth-log-limiter.ts /** Per-key log limiter that reports suppressed auth attempts on the next emitted log. */ var HandshakeAuthLogLimiter = class { constructor(options) { this.entries = /* @__PURE__ */ new Map(); this.intervalMs = resolveIntegerOption(options?.intervalMs, 3e4, { min: 1 }); this.maxEntries = resolveIntegerOption(options?.maxEntries, 256, { min: 1 }); } /** Register one auth event key and decide whether it should be logged now. */ register(key, nowMs = Date.now()) { const entry = this.entries.get(key); if (!entry) { pruneMapToMaxSize(this.entries, this.maxEntries - 1); this.entries.set(key, { lastLoggedAtMs: nowMs, suppressedSinceLastLog: 0 }); return { shouldLog: true, suppressedSinceLastLog: 0 }; } if (nowMs - entry.lastLoggedAtMs < this.intervalMs) { entry.suppressedSinceLastLog += 1; return { shouldLog: false, suppressedSinceLastLog: 0 }; } const suppressedSinceLastLog = entry.suppressedSinceLastLog; entry.lastLoggedAtMs = nowMs; entry.suppressedSinceLastLog = 0; return { shouldLog: true, suppressedSinceLastLog }; } }; /** Build the limiter key from auth failure context. */ function buildHandshakeAuthLogKey(params) { return [ params.reason ?? "unknown", params.remoteAddr ?? "?", params.client ?? "?", params.mode ?? "?", params.authProvided ?? "?" ].join("|"); } /** Return whether a missing-credential failure should use log rate limiting. */ function shouldLimitMissingCredentialAuthLog(params) { return params.authProvided === "none" && (params.reason === "token_missing" || params.reason === "password_missing"); } //#endregion export { buildHandshakeAuthLogKey as n, shouldLimitMissingCredentialAuthLog as r, HandshakeAuthLogLimiter as t };