Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/ntsd/gache/v2 v2.0.0-20250101163922-c8d7e530ffbd
github.com/rs/zerolog v1.34.0
github.com/valyala/fasthttp v1.68.0
github.com/zeebo/xxh3 v1.0.2
go.uber.org/mock v0.6.0
google.golang.org/protobuf v1.36.10
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -27,6 +26,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/savsgio/gotils v0.0.0-20240704082632-aef3928b8a38 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
golang.org/x/net v0.46.0 // indirect
golang.org/x/sys v0.37.0 // indirect
)
2 changes: 1 addition & 1 deletion server/pkg/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type handler struct {
// id yet) and both store, fragmenting the room under one public id.
// Holding this mutex for the probe plus the store makes the
// check-and-create atomic across modes. The paths that mint generated
// ids (random uuid, IP xxh3 hash) also take the lock, but their probe
// ids (random uuid, IP FNV hash) also take the lock, but their probe
// always misses and their id can never collide, so the guard is a
// no-op there — it just costs one uncontended lock. The WebSocket
// upgrade (and a failed-upgrade rollback) happens AFTER the lock is
Expand Down
2 changes: 1 addition & 1 deletion server/pkg/handler/hub_id_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func TestCreateHubRandomStillCreatesAfterHubIdValidation(t *testing.T) {
}

// TestJoinOrCreateHubIPStillCreatesAfterHubIdValidation is the IP-hub
// counterpart: the xxh3 hash of the client IP is never empty, so the
// counterpart: the FNV hash of the client IP is never empty, so the
// shared-helper validation must not misfire.
func TestJoinOrCreateHubIPStillCreatesAfterHubIdValidation(t *testing.T) {
h := newTestHandler()
Expand Down
4 changes: 2 additions & 2 deletions server/pkg/handler/join_or_create_hub_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// calls for the same ID always land in the same hub.
func (h *handler) JoinOrCreateHubByID(ctx *fasthttp.RequestCtx, zh zerohub.ZeroHub, hubId string) error {
// Static callers pass the `id` param verbatim; IP-hub callers pass a
// generated xxh3 hash. The shared validation helper rejects an
// generated FNV hash. The shared validation helper rejects an
// empty/whitespace id and oversized params before any hub state is
// touched, and answers an oversized hubMetadata with 413 before its
// JSON shape is checked (the DoS guard).
Expand All @@ -25,7 +25,7 @@ func (h *handler) JoinOrCreateHubByID(ctx *fasthttp.RequestCtx, zh zerohub.ZeroH
// chosen id, then run GetOrCreateHub — all while holding the lock, so
// a join-or-create that would create a new hub can't race a create in
// another mode into fragmenting the room under one public id. IP-hub
// callers pass an xxh3 hash, which no sibling holds, so the probe is a
// callers pass an FNV hash, which no sibling holds, so the probe is a
// no-op there.
newHub, created, err, rejected := h.claimGetOrCreateHub(ctx, hubId, h.siblingInstances(zh),
func() (hub.Hub, bool, error) {
Expand Down
14 changes: 12 additions & 2 deletions server/pkg/handler/join_or_create_hub_ip.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package handler

import (
"hash/fnv"
"strconv"

"github.com/hotcode-dev/zerohub/pkg/zerohub"
"github.com/valyala/fasthttp"
"github.com/zeebo/xxh3"
)

// fnvHash64a returns the FNV-1a 64-bit hash of s as a base-10 string. The
// IP-hub key is a server-generated opaque id; FNV-1a is a stable stdlib
// hash (no third-party dep) whose output is fine for an opaque,
// collision-resistant-enough key over IPv4/IPv6 literals.
func fnvHash64a(s string) string {
h := fnv.New64a()
_, _ = h.Write([]byte(s))
return strconv.FormatUint(h.Sum64(), 10)
}

// JoinOrCreateHubIP joins or creates a hub using the client's IP address as
// the hub ID. The key comes from h.clientIP so that, behind a trusted
// reverse proxy (APP_TRUST_PROXY=true), it is derived from the real client IP
Expand All @@ -22,5 +32,5 @@ func (h *handler) JoinOrCreateHubIP(ctx *fasthttp.RequestCtx, zh zerohub.ZeroHub
return h.ForwardMigrate(ctx)
}

return h.JoinOrCreateHubByID(ctx, zh, strconv.FormatUint(xxh3.HashString(h.clientIP(ctx)), 10))
return h.JoinOrCreateHubByID(ctx, zh, fnvHash64a(h.clientIP(ctx)))
}
4 changes: 1 addition & 3 deletions server/pkg/handler/join_or_create_hub_ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ package handler

import (
"net"
"strconv"
"strings"
"testing"

hub "github.com/hotcode-dev/zerohub/pkg/hub"
"github.com/hotcode-dev/zerohub/pkg/zerohub"
"github.com/valyala/fasthttp"
"github.com/zeebo/xxh3"
"go.uber.org/mock/gomock"
)

// ipHubKeyFor returns the hub ID the handler should derive for the given
// client IP — the exact hashing scheme JoinOrCreateHubIP applies.
func ipHubKeyFor(clientIP string) string {
return strconv.FormatUint(xxh3.HashString(clientIP), 10)
return fnvHash64a(clientIP)
}

// captureHubKey builds a mock ZeroHub that records the hub ID passed to
Expand Down
2 changes: 1 addition & 1 deletion server/pkg/handler/join_or_create_upgrade_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestJoinOrCreateIPRemovesOrphanOnUpgradeFailure(t *testing.T) {
}

// Mirror joinOrCreateHubIPRequest: set the socket peer so the derived
// key is the xxh3 hash of the client IP, then a plain GET so the
// key is the FNV hash of the client IP, then a plain GET so the
// handshake fails.
ctx := &fasthttp.RequestCtx{}
ctx.SetRemoteAddr(&net.TCPAddr{IP: net.ParseIP(socketIP)})
Expand Down
4 changes: 2 additions & 2 deletions server/pkg/handler/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
// validHubId reports whether a hub ID is usable: non-empty and not
// whitespace-only. Callers of the shared by-ID helpers pass either the
// static ID read verbatim from the `id` query parameter or a
// server-generated ID (uuid for random hubs, xxh3 hash for IP hubs) — the
// server-generated ID (uuid for random hubs, FNV hash for IP hubs) — the
// latter are never empty, so this check is a no-op for those paths.
// Rejecting an empty ID up front stops a request with a missing/empty `id`
// from silently creating a hub under the empty-string key, which would
Expand Down Expand Up @@ -94,7 +94,7 @@ func (h *handler) rejectInvalidJSONParam(ctx *fasthttp.RequestCtx, param string)
// The ordering is intentional: the 400 for a missing id wins over any 413
// or 409, and an empty/whitespace id must not reach hub state, where it
// would silently create/join a hub under the empty-string key and squat
// that key forever. Generated ids (uuid for random hubs, xxh3 hash for IP
// that key forever. Generated ids (uuid for random hubs, FNV hash for IP
// hubs) are never empty and far under the cap, so the id checks are no-ops
// on those paths. hubMetadata is size-capped BEFORE the JSON shape check:
// it is stored and re-sent to every joiner, and the size guard answers an
Expand Down
Loading