revproxy

package
v0.31.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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:

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)))
	}),
)

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

View Source
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.

View Source
const RouteHeader = "x-tunnel-peer"

RouteHeader is the HTTP header that names the target peer.

Variables

View Source
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.

View Source
var ErrUnknownRouting = errors.New("unknown routing strategy")

ErrUnknownRouting is returned for a strategy that is not one of header, subdomain, or both.

View Source
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

type ErrorHook func(ctx context.Context, reason string)

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

func WithErrorHook(hook ErrorHook) Option

WithErrorHook registers an observer for requests that could not be proxied. See ErrorHook.

func WithResolvers

func WithResolvers(resolvers ...Resolver) Option

WithResolvers sets how the target peer is picked: the resolvers are tried in order and the first peer named wins. Build the chain from configuration with Routing.Resolvers — which rejects unusable strategy/domain pairs at boot — and slot in anything else that implements Resolver. Repeatable; later calls append to the chain, and any call replaces the header-routing default.

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.

func New

func New(peers Peers, opts ...Option) *Proxy

New builds a proxy over the given peers. Without options it routes on the x-tunnel-peer header alone.

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP routes the request to the peer it names. A request that names none gets the landing page rather than a proxied request, so hitting the proxy root never turns into a 502.

type Resolver

type Resolver interface {
	Peer(req *http.Request) string
}

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

func ResolveBySubdomain(domain string) (Resolver, error)

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

func (r Routing) Resolvers(domain string) ([]Resolver, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL