Documentation
¶
Overview ¶
Package proxy is the hub's data plane: an http.Handler that picks the peer a request targets — by x-tunnel-peer header, or by subdomain of a base domain — and dials it through that peer's tunnel. A request that names no peer, or names one that is not attached, never reaches a backend: it gets a bare page that says nothing about the hub.
Header routing needs no configuration:
mux.Handle("/", proxy.New(registry))
where registry is a *hub.Registry (or anything else satisfying Peers). Subdomain routing and an observability hook are options:
resolvers, err := proxy.RoutingBoth.Resolvers("peers.example.com")
// handle err — a bad strategy/domain pair fails here, at boot
p := proxy.New(registry,
proxy.WithResolvers(resolvers...),
proxy.WithErrorHook(func(ctx context.Context, reason string) {
errors.Add(ctx, 1, metric.WithAttributes(attribute.String("reason", reason)))
}),
)
The request log is a wrapper, not an option: the proxy carries requests and announces where each one went (see RouteHeader), and whoever wants the stream composes it on top:
handler := reqlog.Middleware(hook, p, reqlog.WithPeerHeader(proxy.RouteHeader))
Transport encryption is the deployment's job: put a TLS edge, ingress, or mesh in front of the hub, same as its other listeners.
Index ¶
Constants ¶
const ( // ReasonNoPeer is a request that named no peer at all: no route // header, and no host under the base domain. The value stays // "no-header" so existing dashboards keep their series. ReasonNoPeer = "no-header" // ReasonNotAttached is a named peer with no live tunnel. ReasonNotAttached = "not-attached" // ReasonTransport is a live tunnel that failed mid-request. ReasonTransport = "transport" )
The reasons an ErrorHook is called with.
const RouteHeader = "x-tunnel-peer"
RouteHeader is the HTTP header that names the target peer. The proxy also normalises it to the routing outcome as it serves: once ServeHTTP returns, the header on the request names the peer it was routed to, and is absent when it reached none. An observer wrapping the proxy reads it there (reqlog.WithPeerHeader does).
Variables ¶
var ErrNoDomain = errors.New("needs a base domain, e.g. peers.example.com")
ErrNoDomain is returned for a subdomain strategy with no base domain to match against.
var ErrUnknownRouting = errors.New("unknown routing strategy")
ErrUnknownRouting is returned for a strategy that is not one of header, subdomain, or both.
var ErrUnusedDomain = errors.New("header routing does not use a base domain; use \"both\" to serve subdomains too")
ErrUnusedDomain is returned when header routing is paired with a base domain. The pair is ambiguous — it reads as "serve subdomains too", which header routing does not do — so it is refused rather than quietly resolved one way.
Functions ¶
This section is empty.
Types ¶
type ErrorHook ¶
ErrorHook observes requests that could not be proxied. It is called with the request context and a low-cardinality reason, which makes it a metric counter's attribute as-is. The response the caller sees is unaffected: the hook only watches.
type Middleware ¶ added in v0.45.0
Middleware is one stage of the data plane: it wraps the handler below it and sees every request on the way through.
type Option ¶
type Option func(*config)
Option configures a Proxy.
func WithErrorHook ¶
WithErrorHook registers an observer for requests that could not be proxied. See ErrorHook.
func WithMeterProvider ¶ added in v0.34.0
func WithMeterProvider(mp metric.MeterProvider) Option
WithMeterProvider sets the OTel MeterProvider the data-plane instruments are built from. Default: the global provider, a no-op until an SDK is installed.
func WithMiddleware ¶ added in v0.45.0
func WithMiddleware(mw ...Middleware) Option
WithMiddleware adds stages to the data plane, in the order given. They run inside the built-in instruments and outside routing: a stage may mutate the request (add headers, rewrite), the resolvers see what the stages left, and a stage that does not call next short-circuits the proxy. Observation is a stage like any other: wrap the Proxy, or add it here (reqlog.Middleware, for one).
func WithResolvers ¶
WithResolvers sets how the target peer is picked: resolvers are tried in order, first peer named wins. Repeatable; later calls append, and any call replaces the header-routing default.
type Peers ¶
type Peers interface {
Attached(peer string) bool
RoundTripper(peer string) http.RoundTripper
}
Peers is the live-tunnel half of the hub the proxy dials through; *hub.Registry satisfies it.
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy routes inbound requests to attached peers. Build it with New; the zero value is not usable.
The data plane is a middleware chain around a routing core: every request crosses the stages, then routing picks the tunnel. The built-in observation (metrics, the request log) is itself stages in that chain, not a special case in the core.
type Resolver ¶
Resolver maps an inbound proxy request to the peer it targets, or "" when the request names none (a bare visit to the proxy, or a host outside the base domain).
func ResolveByHeader ¶
func ResolveByHeader() Resolver
ResolveByHeader routes on the RouteHeader header. It works anywhere — no DNS needed — and is the proxy's default.
func ResolveBySubdomain ¶
ResolveBySubdomain routes on the label(s) in front of domain, so alice.peers.example.com targets "alice" — every peer gets its own hostname, which is what ordinary clients (browsers, webhooks, anything that only takes a URL) can actually use. Without a domain to strip every host would look like a peer, so a blank one is ErrNoDomain instead of a resolver.
type Routing ¶
type Routing string
Routing is the configuration vocabulary for the built-in strategies, as spelled in CLI flags and deployment values. It is turned into behavior in exactly one place — Resolvers — so an invalid strategy or a mismatched domain is an error there, never a proxy that silently drops every request.
const ( RoutingHeader Routing = "header" RoutingSubdomain Routing = "subdomain" RoutingBoth Routing = "both" )
The built-in strategies.
func (Routing) Resolvers ¶
Resolvers builds the resolver chain the configured strategy names — tried in order, first peer named wins — with domain as the base domain for the subdomain strategies. Callers can wrap the error with their own configuration hint; errors.Is against ErrNoDomain, ErrUnusedDomain, and ErrUnknownRouting keeps working through the wrap.