proxy

package
v0.28.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: 0 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:

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

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

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

ErrUnknownRouting is returned by Validate 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 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

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 WithRouting

func WithRouting(routing Routing, domain string) Option

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.

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

func NewResolver(routing Routing, domain string) Resolver

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.

func (Resolver) Peer

func (r Resolver) Peer(req *http.Request) string

Peer returns the target peer id, or "" when the request names none (a bare visit to the proxy, or a host outside the base domain).

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

func (r Routing) Validate(domain string) error

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.

Jump to

Keyboard shortcuts

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