Documentation
¶
Overview ¶
Package gate is the runtime gate: the process on the runtime's port. It answers what the Go runtime already does — slice by slice, each switched per machine in state-mode.json — and hands everything else, sockets included, to the old runtime on an inner port. Callers (the cluster through the machine agent's tunnels, the agent's relays, the CLIs) see one port.
Index ¶
- Constants
- Variables
- func CLIHeaders(raw string) []string
- func Encode(v any) []byte
- func WriteJSON(w http.ResponseWriter, status int, v any, h http.Header)
- type BodyRoute
- type CLIToken
- func (t *CLIToken) Accepts(v string) (Principal, bool)
- func (t *CLIToken) AgentOf(r *http.Request) (session string, agent bool)
- func (t *CLIToken) IssueAgent(session string, ttl time.Duration) (string, error)
- func (t *CLIToken) OK(r *http.Request) bool
- func (t *CLIToken) PrincipalOf(r *http.Request) (Principal, bool)
- type Config
- type Gate
- func (f *Gate) Add(routes ...Route)
- func (f *Gate) AsBodyRoutes(routes ...BodyRoute) []Route
- func (f *Gate) AsRoutes(routes ...JSONRoute) []Route
- func (f *Gate) LogCounts()
- func (f *Gate) Proxy(w http.ResponseWriter, r *http.Request)
- func (f *Gate) RefreshSessions(s SessionRefresh)
- func (f *Gate) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (f *Gate) SetFallback(h http.Handler)
- func (f *Gate) SliceOn(slice string) bool
- func (f *Gate) Standalone() bool
- type JSONRoute
- type Principal
- type Raw
- type Route
- type SessionRefresh
- type Slices
Constants ¶
const AgentTokenFile = "agent-token-key"
AgentTokenFile is the file the agent tokens' key is kept in, in the data folder.
const DefaultCLIHeader = "x-cli-token"
DefaultCLIHeader is the header the harnessly CLI and its hooks carry the cli-token in when nothing names another (HARNESSLY_CLI_HEADER).
Variables ¶
var ErrAgentSession = errors.New("invalid agent token session")
ErrAgentSession: the session of an agent token is not one agentSessionRe takes.
Functions ¶
func CLIHeaders ¶
CLIHeaders reads RUNTIME_CLI_TOKEN_HEADERS (comma-separated) and always ends with DefaultCLIHeader: a product that names headers of its own still lets in the harnessly CLI's hooks, which carry the token in the default header — the chat's status hooks of a branded machine were refused (403) and its feed never saw a prompt taken. The product's own headers stay first, so what the runtime sends is unchanged.
Types ¶
type BodyRoute ¶
type BodyRoute struct {
Slice string
Match func(*http.Request) bool
// Serve writes the whole answer, a refusal included.
Serve http.HandlerFunc
}
BodyRoute is a route whose answer is not only JSON — a file, a picture, a range of a video —, answered while its slice is on.
type CLIToken ¶ added in v0.15.40
type CLIToken struct {
DataDir string
// Headers carry the cli-token; a request is let in by the first one it has.
Headers []string
// Now is the clock agent tokens expire by; nil is the machine's.
Now func() time.Time
// contains filtered or unexported fields
}
CLIToken checks the runtime's cli-token, kept in cli-token in the data folder: the CLIs and the agent hooks carry it.
It also checks the agent tokens it issues (IssueAgent): `agent.<session>.<expMs>.<hmac>`, the HMAC-SHA256 of "agent|<session>|<expMs>" under a key of its own in the data folder (agent-token-key). An agent token is the token of one launch of an agent; the routes that change the apps refuse it (AgentOf), whatever the agent's tools are allowed.
func (*CLIToken) Accepts ¶ added in v0.16.86
Accepts is the principal of one token value: the cli-token or a valid agent token; false for anything else.
func (*CLIToken) AgentOf ¶ added in v0.16.86
AgentOf says whether the request is an agent's: a valid agent token, not the person's cli-token.
func (*CLIToken) IssueAgent ¶ added in v0.16.86
IssueAgent is an agent token for a session that holds for ttl.
type Config ¶
type Config struct {
// Listen is the runtime's port, the one every caller knows.
Listen string
// Upstream is the old runtime on its inner port; empty runs the gate standalone (see Standalone).
Upstream string
// DataDir holds state-mode.json, where the machine agent writes which slices are on.
DataDir string
Version string
// CountEvery is how often the paths still handed to the old runtime are logged.
CountEvery time.Duration
// TokenHeaders name the headers that carry the runtime's cli-token.
TokenHeaders []string
}
Config is what the gate takes from its environment.
func (Config) Standalone ¶
Standalone: no old runtime behind the gate. Every slice is on, whatever state-mode.json says (there is nobody else to answer), no shadow runs (there is nothing to compare with), and what no route takes gets the fallback's answer.
type Gate ¶
type Gate struct {
// contains filtered or unexported fields
}
Gate is the handler on the runtime's port.
func (*Gate) AsBodyRoutes ¶
AsBodyRoutes turns body routes into gate routes.
func (*Gate) AsRoutes ¶
AsRoutes turns JSON routes into gate routes, answered while the slice is on.
func (*Gate) LogCounts ¶
func (f *Gate) LogCounts()
LogCounts writes, and resets, which paths went to the old runtime, how often and how long they took: the day it stays empty the old runtime can go, and until then it shows what the hop costs.
func (*Gate) Proxy ¶
func (f *Gate) Proxy(w http.ResponseWriter, r *http.Request)
Proxy hands a request to the old runtime as the gate does for paths it does not answer (to the fallback on a standalone gate).
func (*Gate) RefreshSessions ¶
func (f *Gate) RefreshSessions(s SessionRefresh)
RefreshSessions turns the refresh on for every API request a Go route answers.
func (*Gate) SetFallback ¶
SetFallback names what answers, on a standalone gate, a request no route took (by default the old runtime's "Not found"); call before serving.
func (*Gate) SliceOn ¶
SliceOn is whether a slice is switched on (every one but the shadows on a standalone gate).
func (*Gate) Standalone ¶
Standalone says whether the gate runs without an old runtime behind it.
type JSONRoute ¶
type JSONRoute struct {
Slice string
Match func(*http.Request) bool
// Compute is the whole answer, a refusal to an unauthorized caller included: status, value (encoded as JSON; nil
// for an empty body) and extra headers.
Compute func(*http.Request) (int, any, http.Header)
}
JSONRoute is a route a slice answers with JSON.
type Principal ¶ added in v0.16.86
Principal is who a request with a token is: the person's CLI (the cli-token), or an agent's launch.
type Route ¶
Route is one thing a slice answers itself. A route without a slice is always the Go runtime's.
type SessionRefresh ¶
type SessionRefresh struct {
Secret []byte
// Cookie is the session cookie's name, MaxAge its lifetime.
Cookie string
MaxAge time.Duration
// Skip: the request is let in without a session (an open path, or a valid cli-token, which is checked first).
Skip func(r *http.Request, apiPath string) bool
// contains filtered or unexported fields
}
SessionRefresh re-signs a page's session past half its age, as the previous runtime's API did on every request it answered, so a person who keeps working never meets the sign-in page.