Files
job-search/internal/config/conf_server_proxy.go
T
bwilliams a279405cae Added the HTTP server and serve command, with proxy, CSRF, TLS and security header config; wsserve is now ws serve, alongside a new ws dial
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.
2026-10-15 17:47:40 +01:00

89 lines
2.9 KiB
Go

package config
import (
"reflect"
"time"
)
// JSCKServerProxy is the server proxy section's configuration key.
const JSCKServerProxy string = "proxy"
// JSCServerProxy configures which proxies the server trusts and how it finds
// the client's address behind them. TrustedProxies are IP addresses and CIDR
// ranges; empty means no proxy is trusted. ClientIPHeaders are the headers
// read for the client's address from a trusted proxy, in priority order;
// empty means the connection's own address is always used.
type JSCServerProxy struct {
TrustedProxies []string `cfg:"trusted" default:"127.0.0.1/32"`
ClientIPHeaders []string `cfg:"client_ip_headers" default:"X-Forwarded-For, Forwarded, X-Real-IP"`
StripUntrusted bool `cfg:"strip_untrusted" default:"true"`
ProxyProtocol JSCServerProxyProtocol `cfg:"proxy_protocol"`
}
// ---------- Section methods (see Section) ----------
// Map implements Section.
func (c JSCServerProxy) Map() map[string]any { return mapOf(c) }
// Flatten implements Section.
func (c JSCServerProxy) Flatten() map[string]any {
return flattenOf(JSCKServerProxy, c)
}
// TypeOf implements Section.
func (c JSCServerProxy) TypeOf(key string) (reflect.Kind, error) {
return typeOf(c, key)
}
// ValueOf implements Section.
func (c JSCServerProxy) ValueOf(key string) (any, error) {
return valueOf(c, key)
}
// SetValue implements Section.
func (c *JSCServerProxy) SetValue(key string, value any) error {
return setValue(c, key, value)
}
// ========== PROXY protocol ==========
// JSCKServerProxyProtocol is the server proxy PROXY protocol section's
// configuration key.
const JSCKServerProxyProtocol string = "proxy_protocol"
// JSCServerProxyProtocol configures accepting the PROXY protocol from
// trusted proxies. TrustedSources may send PROXY headers; empty means
// Proxy.TrustedProxies. ReadHeaderTimeout is how long to wait for the PROXY
// header; 0 or less means no limit.
type JSCServerProxyProtocol struct {
Enabled bool `cfg:"enabled" default:"false"`
TrustedSources []string `cfg:"trusted_sources"`
Required bool `cfg:"required" default:"false"`
ReadHeaderTimeout time.Duration `cfg:"read_header_timeout" default:"5s"`
}
// ---------- Section methods (see Section) ----------
// Map implements Section.
func (c JSCServerProxyProtocol) Map() map[string]any { return mapOf(c) }
// Flatten implements Section.
func (c JSCServerProxyProtocol) Flatten() map[string]any {
return flattenOf(JSCKServerProxyProtocol, c)
}
// TypeOf implements Section.
func (c JSCServerProxyProtocol) TypeOf(key string) (reflect.Kind, error) {
return typeOf(c, key)
}
// ValueOf implements Section.
func (c JSCServerProxyProtocol) ValueOf(key string) (any, error) {
return valueOf(c, key)
}
// SetValue implements Section.
func (c *JSCServerProxyProtocol) SetValue(key string, value any) error {
return setValue(c, key, value)
}