Documentation
¶
Overview ¶
Package preview is the reverse proxy behind the right pane's browser: a slot origin of the gateway shows a local dev server port (with its neighbour ports) or an external site, named by a claim the gateway signs with the pair's secret. It rewrites what a dev server needs rewritten to work inside the slot origin, injects the page's bridge, passes WebSockets (HMR) through, refuses private addresses, and records what the page did for its DevTools.
Index ¶
- Constants
- func CookieTag(t devtools.Target, secret []byte) string
- func DomainOrigins(domain, id string, n int) []string
- func IsForbiddenAddress(address string, local map[string]bool) bool
- func NormalizeDomain(raw string) (string, error)
- func NormalizeIPv4(host string) string
- func OriginsHint(checks []OriginCheck) string
- func ParseTarget(v any) (devtools.Target, bool)
- func PreviewID(named, machine, dataDir string) (string, error)
- func PrivateHosts(list []string) map[string]bool
- func RewriteHTMLDocumentPaths(doc []byte, prefix, docPath string) []byte
- func RewriteHTMLRootPaths(doc []byte, prefix string) []byte
- func SelfAddresses(named string) map[string]bool
- func SystemLookup(ctx context.Context, host string) ([]string, error)
- func ValidID(id string) bool
- func WritePage(w http.ResponseWriter, status int, text string)
- type AssetCache
- type BodyError
- type Claim
- type Config
- type ContextLookup
- type Headers
- type Lookup
- type OriginCheck
- type Proxy
- type Resolved
- type Stands
- type Timeouts
Constants ¶
const ( DefaultMaxTotalBytes = 200 * 1024 * 1024 DefaultMaxFileBytes = 20 * 1024 * 1024 )
const ( BridgePath = "/__tk/bridge.js" DevtoolsConfigPath = "/__tk/devtools/config" DevtoolsReportPath = "/__tk/devtools/report" )
The slot's own paths, answered by the runtime before any routing to a dev server: the bridge script, and the DevTools paths the page's bridge reads its conditions from and reports what it saw to.
const ( OriginOK = "ok" // the name resolves to an address of this machine OriginNotFound = "not-found" // NXDOMAIN or no address at all OriginElsewhere = "elsewhere" // resolves, but to no address of this machine OriginError = "error" // the resolver failed (timeout, refused) OriginLiteral = "literal" // an IP address or localhost: there is no name to resolve )
Origin check states.
const BodyLimit = 1 << 20
BodyLimit is the previous runtime's default request body limit.
const PreviewIDFile = "preview-id"
PreviewIDFile is where a runtime keeps the id it made for its slots' names.
Variables ¶
This section is empty.
Functions ¶
func CookieTag ¶
CookieTag names a target in its cookies on the slot host, so targets taking turns in a slot never share cookies. Keyed with the secret: a page cannot compute another target's tag.
func DomainOrigins ¶
DomainOrigins are the n slots' origins under domain for id: https://s1-<id>.<domain> … https://s<n>-<id>.<domain>.
func IsForbiddenAddress ¶
IsForbiddenAddress: internal, special or this machine's own; not an address at all is refused too.
func NormalizeDomain ¶
NormalizeDomain is RUNTIME_PREVIEW_DOMAIN as a lowercase name without a scheme, a leading "*." or a trailing dot; an error says why it is not a domain.
func NormalizeIPv4 ¶
NormalizeIPv4 is inet_aton: 2130706433, 0177.0.0.1, 0x7f.1 are all 127.0.0.1, as the URL parser and getaddrinfo read them; "" for anything else.
func OriginsHint ¶
func OriginsHint(checks []OriginCheck) string
OriginsHint is what to do about the worst of the checks, "" when every name leads here.
func ParseTarget ¶
ParseTarget is a preview target as a request body names it: {kind:"port",port} or {kind:"url",origin} with the origin spelled exactly as the URL parser prints it.
func PreviewID ¶
PreviewID is the id of this runtime's slot names: named is RUNTIME_PREVIEW_ID, machine the hub's machine id ("" when there is none). Without either the id is read from dataDir/preview-id, made and written there on the first start.
func PrivateHosts ¶ added in v0.16.123
PrivateHosts reads the deployment's list of hosts that may resolve into a private network: names only, written lower-case without a trailing dot; an address or localhost in the list is ignored.
func RewriteHTMLDocumentPaths ¶
RewriteHTMLDocumentPaths is RewriteHTMLRootPaths for a page the browser opens at docPath (the path without the prefix): its relative addresses get the prefix too. A page with a <base href> of its own resolves them against that base, which gets the prefix here instead; the rest is then the browser's.
func RewriteHTMLRootPaths ¶ added in v0.16.154
RewriteHTMLRootPaths gives a document's root-absolute addresses the prefix, inline scripts' imports included.
func SelfAddresses ¶
SelfAddresses are the machine's own: its interfaces and the public ones named for it (behind NAT the public address is on no interface).
func SystemLookup ¶
SystemLookup is the system resolver.
Types ¶
type AssetCache ¶
type AssetCache struct {
// contains filtered or unexported fields
}
AssetCache is one directory of kept answers, oldest use first.
func NewAssetCache ¶
func NewAssetCache(dir string, maxTotal, maxFile int64) *AssetCache
type BodyError ¶
BodyError is the dispatcher's plain-text refusal of a body: 413 too large, 400 not JSON.
type Claim ¶
type Claim struct {
Sub string
Target devtools.Target
SlotOrigin string
PortSlots map[string]string
// ParentOrigin is the platform's page origin the in-page bridge talks to; "" when the gateway sent none, and then
// there is no bridge.
ParentOrigin string
// Carried: the gateway took the slot's session from the address, not a cookie (an app's view frame sends none). Such
// a session is a bearer link, so the proxy opens only the apps' view server with it (Config.CarriedPort).
Carried bool
// PathPrefix is the slot's path on the page's own origin when the slot goes by path (<appBase>/__pv/<n>, a
// standalone runtime's preview without origins of its own); "" for a slot origin of its own. SlotOrigin ends with it.
PathPrefix string
}
Claim is what the gateway says a request may reach.
type Config ¶
type Config struct {
// Path is the proxy's own path; the rest of a request's path is the upstream's.
Path string
// ClaimHeader carries the gateway's claim; Audience is the claim's.
ClaimHeader string
Audience string
Secret []byte
// PrivateHeaderPrefix marks the headers only the gateway sets; none of them reaches an upstream.
PrivateHeaderPrefix string
// Forbidden are the ports never opened: the runtime's own and the platform's.
Forbidden func() map[int]bool
Stands Stands
// SelfIPs are the machine's public addresses no interface carries (behind NAT), comma-separated.
SelfIPs string
// PrivateHosts may resolve into a private network (GitLab behind an overlay network): the deployment names them.
PrivateHosts []string
Store *devtools.Store
// Cache keeps stand assets; nil keeps none.
Cache *AssetCache
// Bridge is the page bridge's script; with none the request goes to Fallback (the previous runtime, which has it).
Bridge func() ([]byte, bool)
Fallback http.HandlerFunc
// LocalOrigins: the claims come from this runtime's own slot listeners (a standalone runtime), whose slot and page
// origins may be plain http and carry a port, like http://localhost:8023. The fleet gateway's are always https.
LocalOrigins bool
// CarriedPort is the only port a session carried in the address may open (the apps' view server); 0 or nil: none.
CarriedPort func() int
// Tests: where names point, the address of a port, the CA of an https target, shorter timeouts.
Resolve func(host string) Resolved
UpstreamHost string
RootCAs *x509.CertPool
Timeouts Timeouts
// Dial connects to a checked address; tests point a stand's port 443 at their own server.
Dial func(ctx context.Context, address string) (net.Conn, error)
}
Config is what the deployment and the gate give the proxy.
type ContextLookup ¶
ContextLookup resolves a name to its addresses.
type Headers ¶
Headers are a message's headers the way the previous runtime's HTTP library held them: lower-case names, Set-Cookie as a list, a repeated Cookie joined with "; ", other repeats joined with ", " except those of which only the first counts.
type OriginCheck ¶
type OriginCheck struct {
Origin string `json:"origin"`
Host string `json:"host"`
State string `json:"state"`
Addresses []string `json:"addresses,omitempty"`
Error string `json:"error,omitempty"`
}
OriginCheck is what DNS says of one slot origin.
func CheckOrigins ¶
func CheckOrigins(ctx context.Context, origins []string, lookup ContextLookup, self map[string]bool) []OriginCheck
CheckOrigins resolves the host of every origin and says whether it leads to this machine (self: its addresses, SelfAddresses). A name of another machine is not wrong in itself: a reverse proxy or a balancer may stand there.
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy is the preview's reverse proxy.
func (*Proxy) AnswerDevtools ¶
func (p *Proxy) AnswerDevtools(w http.ResponseWriter, r *http.Request, path, slot string, target devtools.Target)
AnswerDevtools answers a slot's own DevTools paths for its current target.
type Resolved ¶
Resolved is where a name points, or why it is refused: "forbidden" or "not-found".
func LiteralAddress ¶
LiteralAddress is the address a host names by itself (an IPv6 literal, an IPv4 in any spelling); Family 0 for a name.
func ResolveListed ¶ added in v0.16.123
ResolveListed resolves a host the deployment named (RUNTIME_PREVIEW_PRIVATE_HOSTS, GitLab behind an overlay network, say): it may point into a private network, never into the machine itself or a special range.
type Stands ¶
type Stands struct {
// Domain is the stands' domain with its leading dot; "" makes no host a stand.
Domain string
// SignInHosts are where a stand sends a person without a session.
SignInHosts []string
// SessionHeader carries the person's stand session from the gateway.
SessionHeader string
}
Stands are the deployment's feature stands (https://<label><domain>): the one kind of external site that gets the person's own sign-in with the request, passed by the gateway in its own header.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package devtools is the DevTools record of the preview slots: what a slot's page did on the network and in its console, and the conditions it runs under (throttling, blocked addresses, cache).
|
Package devtools is the DevTools record of the preview slots: what a slot's page did on the network and in its console, and the conditions it runs under (throttling, blocked addresses, cache). |
|
Package ports lists the dev servers a preview can open (the listeners of this machine grouped by the conversation's work folders and the agents' worktrees) and maps a component picked in a preview to its source files.
|
Package ports lists the dev servers a preview can open (the listeners of this machine grouped by the conversation's work folders and the agents' worktrees) and maps a component picked in a preview to its source files. |
|
Package weburl parses URLs the way browsers and Node's URL class do (the WHATWG URL standard) for the http(s) and ws(s) schemes the preview deals with: lowercase scheme and host, default port dropped, dot segments resolved, the same characters percent-encoded.
|
Package weburl parses URLs the way browsers and Node's URL class do (the WHATWG URL standard) for the http(s) and ws(s) schemes the preview deals with: lowercase scheme and host, default port dropped, dot segments resolved, the same characters percent-encoded. |