mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
Strangler-fig migration of 15 WebSocket handlers from V1 (Hub method, *Client) to V2 (pure functions: Command, ClientInfo, deps -> Result). V2 handlers are testable without a running Hub and produce declarative Result values that the dispatch loop applies. New abstractions: - Command interface + typed constructors with input validation - 7 Event routing interfaces (Channel, ExcludeSender, SequencedDM, UserTargeted, BroadcastAll, VoiceChannel, VoiceChannelGuarded) - Per-domain deps structs (PingDeps, ChatDeps, PresenceDeps, ReactionDeps, VoiceDeps) with interface-based DI - EmitEvents router matching events to delivery mechanisms - DispatchV2 with panic recovery and runtime.Stack logging Security hardening: - Pre-sanitize byte length guard before bluemonday (DoS prevention) - GetRoleForUser single-JOIN query avoids password hash on hot path - channel_id positivity enforced in all command constructors - Log injection prevention: msgType/reqID capped to 64 chars - Nil KeyHolder dep returns ErrCodeInternal (not silent bypass) - VoiceChannelGuardedEvent atomic check-and-send under h.mu.RLock V1-only (complex state/mutex requirements): voice_join, voice_leave. All tests pass with -race. No CI regressions expected.
23 lines
669 B
Go
23 lines
669 B
Go
package ws
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// handlePingV2 is the V2 handler for ping (heartbeat) messages.
|
|
// It rate-limits and returns a pong reply on success.
|
|
func handlePingV2(_ context.Context, cmd Command, info ClientInfo, deps any) Result {
|
|
d := deps.(PingDeps)
|
|
if d.Limiter != nil && !d.Limiter.Allow(fmt.Sprintf("ping:%d", info.UserID), 2, time.Second) {
|
|
return Result{} // rate limited: silent drop
|
|
}
|
|
return Result{Reply: buildJSON(map[string]any{"type": MsgTypePong})}
|
|
}
|
|
|
|
// registerPingHandler registers the ping/pong handler (V2).
|
|
func registerPingHandler(r *HandlerRegistry, deps PingDeps) {
|
|
r.RegisterV2(MsgTypePing, handlePingV2, deps)
|
|
}
|