The serve command runs internal/server: the API under /api (health, and the CSRF token endpoint) and the WebSocket hub under /ws, over HTTP or TLS, with an optional HTTP to HTTPS redirect, the PROXY protocol from trusted sources and a graceful shutdown that closes WebSockets first. Its middleware covers the client IP behind trusted proxies, request IDs, request logging, panic recovery, global and API rate limits, Host and body size checks, security response headers, compression, CSRF protection (cross-origin checks and signed double-submit tokens) and JSON-only API bodies. Middleware that logs takes a WithLogger option. New server config sections: proxy, csrf, tls, headers and frontend (not used yet), plus max_header_bytes, max_body_bytes, global and API rate limits and HTTP read, write and idle timeouts. Every setting documents what 0 or empty means, and internal/config/README.md is reorganised by section. server.timeouts.read_header must be greater than 0, and a header or idle timeout of 0 means no limit rather than the read timeout. WebSocket upgrades are protected by the Origin check: the server's own origin, or one of csrf.cross_origin_protection.trusted_origins, which the hub now takes with WithTrustedOrigins. wsserve becomes "ws serve". "ws dial" opens a WebSocket on /ws to test it: --ping sends a ping and waits for the pong, --message sends messages and prints the replies, --origin tests the Origin check, and with neither --ping nor --message each line of stdin is sent until Ctrl+C.
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package websocket
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
ws "github.com/coder/websocket"
|
|
|
|
"git.avsdev.uk/Go/job-search/internal/middleware"
|
|
)
|
|
|
|
// ServeHTTP upgrades an http connection and runs the client until it closes.
|
|
// A browser's upgrade must come from the server's own origin or a trusted one
|
|
// (see WithTrustedOrigins); a request without an Origin header, which only a
|
|
// non-browser client sends, is accepted.
|
|
func (h *Hub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
mode := ws.CompressionDisabled
|
|
if h.conf.EnableCompression {
|
|
mode = ws.CompressionContextTakeover
|
|
}
|
|
|
|
// Accept enforces the Origin check: same host, or one of OriginPatterns.
|
|
// That is the defence against cross-site WebSocket hijacking. A pattern
|
|
// holding "://" is matched against the origin's scheme and host.
|
|
conn, err := ws.Accept(w, r, &ws.AcceptOptions{
|
|
CompressionMode: mode,
|
|
OriginPatterns: h.trustedOrigins,
|
|
})
|
|
if err != nil {
|
|
h.logger().Debug("upgrade failed", "error", err)
|
|
return // Accept has already written the HTTP error.
|
|
}
|
|
defer conn.CloseNow() //nolint:errcheck // finished with; it may already be closed
|
|
conn.SetReadLimit(int64(h.maxMessageSize()))
|
|
|
|
ip := middleware.ClientIP(r)
|
|
ctx, cancel := context.WithCancel(r.Context())
|
|
client := newClient(h, conn, ip, cancel)
|
|
|
|
if !h.register(client) {
|
|
cancel()
|
|
conn.Close(ws.StatusGoingAway, "server shutting down") //nolint:errcheck,gosec // turned away
|
|
return
|
|
}
|
|
defer h.unregister(client)
|
|
|
|
h.logger().Info("client connected", "client", client.id, "ip", ip)
|
|
client.run(ctx)
|
|
h.logger().Info("client disconnected", "client", client.id, "ip", ip)
|
|
}
|