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
- func NewToken() string
- func Run(ctx context.Context, cfg Config, fetch Fetcher, announce func(addr string)) error
- type Config
- type Fetcher
- type Mode
- type Outcome
- type Posture
- type Route
- type Rule
- type Server
- func (s *Server) Config() Config
- func (s *Server) Listing(r *http.Request) []Route
- func (s *Server) NeedsToken(r *http.Request) bool
- func (s *Server) Posture(r *http.Request) Posture
- func (s *Server) Resolve(r *http.Request) (Target, Outcome)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) SetFetcher(f Fetcher)
- func (s *Server) SetSnapshot(snap Snapshot)
- func (s *Server) Snapshot() Snapshot
- type Snapshot
- type Target
Constants ¶
const ( AuthAuto = "auto" AuthAlways = "always" AuthNone = "none" )
Auth modes. Auto asks for a token only once the posture is Published.
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.
const ( HostUpstream = "upstream" // rewrite to localhost:<port> HostPreserve = "preserve" // pass the eyeball's Host through )
Host header handling for the upstream request.
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 ¶
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 ¶
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.
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.
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.
type Rule ¶
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 (*Server) Config ¶
Config returns the static configuration. It is a copy: Rules is shared, and callers must not write to it.
func (*Server) Listing ¶
Listing is what the index shows: the services this request is allowed to see, in name order.
func (*Server) NeedsToken ¶
NeedsToken reports whether this request must carry the token.
func (*Server) 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 ¶
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 ¶
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 ¶
SetSnapshot replaces what the gateway knows. Safe to call from the refresh loop while requests are being served.