Documentation
¶
Overview ¶
Package proxy is the hub's data plane: an http.Handler that picks the peer a request targets and dials it through that peer's tunnel.
A request names its peer either with the x-tunnel-peer header (works anywhere, no DNS needed) or with a subdomain of a base domain (alice.peers.example.com targets "alice"). 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:
p := proxy.New(registry,
proxy.WithRouting(proxy.RoutingBoth, "peers.example.com"),
proxy.WithErrorHook(func(ctx context.Context, reason string) {
errors.Add(ctx, 1, metric.WithAttributes(attribute.String("reason", reason)))
}),
)
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.
Variables ¶
var ErrNoDomain = errors.New("needs a base domain, e.g. peers.example.com")
ErrNoDomain is returned by Validate when a subdomain strategy has no base domain to match against. Without one, every host would look like a peer and route to nonsense.
var ErrUnknownRouting = errors.New("unknown routing strategy")
ErrUnknownRouting is returned by Validate 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 by Validate 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 Option ¶
type Option func(*Proxy)
Option configures a Proxy.
func WithErrorHook ¶
WithErrorHook registers an observer for requests that could not be proxied. See ErrorHook.
func WithRouting ¶
WithRouting sets how the target peer is picked. domain is the base domain for the subdomain strategies (ignored by RoutingHeader). Validate the pair at boot with Routing.Validate: an unusable combination resolves no peer here rather than erroring.
type Peers ¶
type Peers interface {
// Attached reports whether the peer has a live tunnel right now.
Attached(peer string) bool
// RoundTripper dials the peer through its attached tunnel.
RoundTripper(peer string) http.RoundTripper
}
Peers is the live-tunnel half of the hub the proxy dials through. *hub.Registry satisfies it; a fake satisfies it in tests.
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.
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver maps an inbound proxy request to the peer it targets. The header strategy reads x-tunnel-peer; the subdomain strategy takes the label(s) in front of a base domain, so alice.peers.example.com targets "alice". With both, the header wins when present — it is the explicit signal.
func NewResolver ¶
NewResolver builds the resolver for a routing strategy. Each strategy reads exactly what it names: RoutingHeader ignores the domain, RoutingSubdomain ignores the header. An unknown strategy resolves nothing, so every request lands on the "no peer named" path rather than routing somewhere unintended; call Routing.Validate at boot to reject it loudly instead.
type Routing ¶
type Routing string
Routing is how the proxy picks the target peer for a request. The header works anywhere (no DNS needed); subdomain routing gives every peer its own hostname, which is what ordinary clients (browsers, webhooks, anything that only takes a URL) can actually use.
const ( RoutingHeader Routing = "header" RoutingSubdomain Routing = "subdomain" RoutingBoth Routing = "both" )
The routing strategies. The zero value is not one of them: pick explicitly, or let New default to RoutingHeader.
func (Routing) Validate ¶
Validate reports whether the strategy can be served with the given base domain, so a misconfiguration fails at boot rather than once per request. Callers can wrap the result with their own configuration hint; errors.Is against ErrNoDomain, ErrUnusedDomain, and ErrUnknownRouting keeps working through the wrap.