Documentation
¶
Overview ¶
Package proxy is the embedded per-container reverse proxy: phase 2 of NEXT.md's "Per-container domain + TLS". Phase 1 (internal/store's domain_mappings + internal/api's domain_handlers) recorded intent only — "route this public domain to this project's service+port" — with nothing listening. This package makes that real, for local-host projects only (see docs/projects.md and NEXT.md for the still-open remote-host phase).
The design deliberately reuses two things already in the codebase rather than adding a dependency: golang.org/x/crypto/acme/autocert (already used by internal/acme for Docker Commander's own admin-domain TLS) and stdlib net/http/httputil.ReverseProxy. A second autocert.Manager lives here, separate from the admin one, with its own cert cache directory and a DYNAMIC HostPolicy (internal/acme's is a fixed autocert.HostWhitelist, which can't express "whatever domain_mappings currently contains").
cmd/dockercmd/main.go combines this package's Handler/HostPolicy with the admin server's own chi mux and autocert manager onto ONE shared listener, dispatched by SNI (for certificates) and Host header (for requests) — see dispatch.go for exactly how, and why naive SNI/Host trust is dangerous.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CanonicalHost ¶
CanonicalHost (exported so the API's admin-domain collision check compares names exactly the way dispatch does) normalizes a hostname the SAME way autocert.Manager does internally, via idna.Lookup.ToASCII — not just strings.ToLower. DNS names are case-insensitive (ToASCII folds that too), and this app's ACME config accepts Unicode hostnames, which a client's TLS/HTTP layer virtually always sends already Punycode-encoded ("bücher.example" arrives on the wire as "xn--bcher-kva.example"). Matching only on case would still leave a Unicode-configured admin domain unable to match its own ASCII wire form, misrouting real admin traffic to the proxy branch. A name ToASCII can't parse falls back to a plain lowercase trim: it will then simply fail to match anything (deny-by-default), never panic or match the wrong thing.
func CombinedGetCertificate ¶
func CombinedGetCertificate(adminMgr, proxyMgr tlsCertGetter, adminDomains []string) func(*tls.ClientHelloInfo) (*tls.Certificate, error)
CombinedGetCertificate returns a tls.Config.GetCertificate that dispatches by SNI: an admin domain goes to adminMgr (Docker Commander's own, unchanged autocert manager); everything else falls to proxyMgr, whose own HostPolicy (see hostpolicy.go) is what actually decides whether an unrecognized name gets refused rather than silently attempted.
func CombinedHandler ¶
CombinedHandler dispatches an HTTP request: an admin domain goes to adminHandler (today's single chi mux, unchanged); a domain with a live local mapping goes to the proxy; anything else is rejected — see the default-deny note below, this is NOT "fall through to the admin app."
Types ¶
type Config ¶
Config configures the proxy's own ACME manager. Mirrors internal/acme's NewManager parameters, but the resulting Manager's HostPolicy is dynamic (see hostpolicy.go), not a fixed whitelist.
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy holds everything the embedded reverse proxy needs: the store (to resolve domain_mappings and their owning project), the Docker manager (to find where a project's service is actually listening), its own autocert manager, and a short-lived cache of resolved backends.
func New ¶
New builds a Proxy and its own autocert.Manager (HostPolicy wired to this Proxy's own dynamic check — see hostpolicy.go). Does not start anything; the manager's GetCertificate is only ever called on demand, during a TLS handshake, by the caller's tls.Config (see dispatch.go).
func (*Proxy) ACMEManager ¶
ACMEManager returns the proxy's own autocert.Manager, for dispatch.CombinedGetCertificate to delegate to.
func (*Proxy) HostPolicy ¶
HostPolicy is the dynamic replacement for autocert.HostWhitelist: it allows certificate issuance for host only when a domain_mappings row exists for it AND that mapping's project is eligible for this phase (exists, and is local — see eligibleMapping in proxy.go). This is the actual security boundary for what this instance will request/serve a certificate for — it does not lean on the app-level project-delete cascade (domain_mappings rows are deleted when their project is) as the only thing standing between "mapping row still exists" and "safe to issue a cert for," in case that invariant is ever violated by a bug elsewhere or a row edited directly.
The returned error is intentionally uniform: it never tells a caller (autocert's own TLS-handshake-time caller, ultimately a network client) WHY a host was refused — "unmapped", "mapped to a remote project", and "mapped to a since-deleted project" all look identical from outside, so probing SNI values can't be used to enumerate which domains exist.
func (*Proxy) TryServeHTTP ¶
TryServeHTTP serves r if r.Host resolves to a live, locally-routable domain_mappings entry, and reports whether it did.
Returns false WITHOUT writing anything when there is no such mapping — the caller (dispatch.CombinedHandler) decides the fallback, which must never be "serve it anyway." Returns true in every other case, including when the mapping exists but has no live backend right now (a container that isn't running, or doesn't publish the mapped port): that is still a definite answer — StatusBadGateway — not a fallthrough to some other handler, because from the caller's perspective "this domain is a known proxy target, just currently down" must never be confused with "this domain isn't a proxy target at all."