gateway

package
v0.5.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 16 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 gateway serves devrun's services over HTTP — an index at / and a reverse proxy for everything below it.

It is deliberately ignorant of how it is reached. It holds a Snapshot of what the daemon last told it and answers requests from that; it knows nothing about cloudflared, the Unix socket, or who refreshes the snapshot. That keeps the routing and access rules testable with httptest alone.

Index

Constants

View Source
const (
	AuthAuto   = "auto"
	AuthAlways = "always"
	AuthNone   = "none"
)

Auth modes. Auto asks for a token only once the posture is Published.

View Source
const (
	PostureAuto   = "auto"
	PostureForced = "published"
)

Posture settings. PostureAuto derives it per request; PostureForced is for a gateway someone else publishes, where devrun cannot tell from its own state.

View Source
const (
	HostUpstream = "upstream" // rewrite to localhost:<port>
	HostPreserve = "preserve" // pass the eyeball's Host through
)

Host header handling for the upstream request.

View Source
const RefreshEvery = 2 * time.Second

RefreshEvery is how often the route table is re-read. It matches the TUI's poll, which is the established way to follow the daemon's state.

Variables

This section is empty.

Functions

func NewToken

func NewToken() string

NewToken mints a key for a gateway: 128 bits of randomness, hex so it survives a URL, a shell and a copy-paste without escaping.

The daemon calls this and passes the result down, so the same key survives a restart; a gateway started by hand calls it for itself.

func Run

func Run(ctx context.Context, cfg Config, fetch Fetcher, announce func(addr string)) error

Run binds the gateway and serves until ctx ends. It returns the address it actually listened on through addr, which matters when Config.Port is 0 and the kernel picks one.

The first fetch happens before the listener is announced, so the gateway never serves a confidently empty index while the daemon has services.

Types

type Config

type Config struct {
	Bind       string // "127.0.0.1:7788" — the address actually listened on
	Mode       Mode
	Posture    string // PostureAuto | PostureForced
	Auth       string // AuthAuto | AuthAlways | AuthNone
	HostHeader string // HostUpstream | HostPreserve
	// PublicHostname addresses services by a domain of their own, as a
	// template with one {service} placeholder. Empty keeps the prepend, which
	// is what *.localhost needs locally. See hostname.go.
	PublicHostname string
	Rules          map[string]Rule
	Token          string
	Version        string
}

Config is the gateway's static configuration, fixed for the life of the process. Everything that changes as services come and go lives in Snapshot.

type Fetcher

type Fetcher func(ctx context.Context) (Snapshot, error)

Fetcher returns the current view of what the daemon is running. The gateway does not know, and must not know, how that is obtained — keeping it a function is what lets the whole handler be tested without a daemon.

type Mode

type Mode string

Mode is how the index advertises a service's URL. Both shapes are always accepted — see Resolve.

const (
	Subdomain Mode = "subdomain"
	Path      Mode = "path"
)

type Outcome

type Outcome int

Outcome says why a request did not resolve to something proxyable.

const (
	OK          Outcome = iota
	NoSuchRoute         // nothing of that name is configured
	NotRunning          // configured, but not up or no port known
	NotExposed          // up, but may not leave this machine
)

type Posture

type Posture string

Posture says whether a request must be treated as coming from off this machine. It decides whether the allowlist and the auth token apply: a gateway reachable only from loopback grants nothing that localhost:<port> did not already grant the same caller, so it asks for nothing.

const (
	Local     Posture = "local"
	Published Posture = "published"
)

type Route

type Route struct {
	Name  string
	State string
	Port  int // 0 when the daemon has not detected one yet
}

Route is one service as the gateway sees it.

func (Route) Reachable

func (r Route) Reachable() bool

Reachable reports whether a request can actually be proxied to this service. A route that is not reachable is still listed on the index, with the reason — a link that 503s is worse than a row that explains itself.

type Rule

type Rule struct {
	Service string
	Strip   bool
}

Rule is one entry of an explicit routes table: which service a path prefix belongs to, and whether the prefix is the service's or the gateway's.

Strip false is what buys production parity — the backend keeps seeing /api/..., so a frontend's relative URLs behave the same in dev, published and in production.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server answers requests from a Snapshot it does not own.

func New

func New(cfg Config) *Server

func (*Server) Config

func (s *Server) Config() Config

Config returns the static configuration. It is a copy: Rules is shared, and callers must not write to it.

func (*Server) Listing

func (s *Server) Listing(r *http.Request) []Route

Listing is what the index shows: the services this request is allowed to see, in name order.

func (*Server) NeedsToken

func (s *Server) NeedsToken(r *http.Request) bool

NeedsToken reports whether this request must carry the token.

func (*Server) Posture

func (s *Server) Posture(r *http.Request) Posture

Posture reports whether this request must be treated as coming from off the machine, which is what turns the allowlist and the token on.

Deriving it from devrun's own state alone — "is the bind non-loopback" — fails open: `ngrok http 7788` satisfies that test and nothing else, so the gateway would serve every running service unauthenticated while ngrok published it. The request's own Host is the second signal, and it is the one that carries the weight. A Host the gateway does not recognise as itself arrived through something, and that something is not on this machine.

A third signal used to sit above these: a flag on Snapshot saying devrun had started a tunnel, which forced Published for every request. It was never set by anything but a test, and it is gone rather than wired up, because the case it guarded does not exist. devrun's tunnel never passes --http-host-header, so a request through it carries the public hostname and fails hostIsSelf here; and a Host spoofed to look local does not survive Cloudflare, which routes by hostname and answers 403 before the tunnel sees it (measured against a live named tunnel, 2026-10-07).

Forcing Published for every request also withheld services from a browser on this machine, which is not what publishing should mean.

This is still not airtight. A proxy configured to rewrite Host to localhost slips past it — and the flag could never have caught that either, since devrun would not have started that proxy. Config.Posture forces Published outright for exactly this case: the heuristic is the floor, not the guarantee.

func (*Server) Resolve

func (s *Server) Resolve(r *http.Request) (Target, Outcome)

Resolve picks the service a request is for.

Both URL shapes are accepted whatever Config.Mode says — the first Host label, then the first path segment. Mode governs only which form the index advertises. Accepting both costs nothing (neither reaches a service the other could not, and the allowlist applies either way) and avoids a sharp edge: subdomain routing locally means web.localhost:7788, which browsers resolve but curl and some Linux resolvers do not, so a path URL has to keep working.

An explicit routes table wins over both, since it was written on purpose.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP gates a request, then routes it to its service or renders the index.

func (*Server) SetFetcher

func (s *Server) SetFetcher(f Fetcher)

SetFetcher lets the server re-read its own route table when a request names something it has not heard of. Run sets it; tests usually leave it nil.

func (*Server) SetSnapshot

func (s *Server) SetSnapshot(snap Snapshot)

SetSnapshot replaces what the gateway knows. Safe to call from the refresh loop while requests are being served.

func (*Server) Snapshot

func (s *Server) Snapshot() Snapshot

Snapshot returns the current view.

type Snapshot

type Snapshot struct {
	Routes  []Route
	Exposed []string // may leave this machine; applies only when Published
}

Snapshot is what the gateway last learned from the daemon. It is replaced wholesale rather than mutated, so a request always sees one coherent view.

type Target

type Target struct {
	Service string
	Prefix  string // path segment to strip before proxying; "" strips nothing
}

Target is the service a request resolved to, and how much of the path belongs to the gateway rather than to the service.

Jump to

Keyboard shortcuts

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