gate

package
v0.16.355 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: AGPL-3.0 Imports: 23 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 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

View Source
const AgentTokenFile = "agent-token-key"

AgentTokenFile is the file the agent tokens' key is kept in, in the data folder.

View Source
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

View Source
var ErrAgentSession = errors.New("invalid agent token session")

ErrAgentSession: the session of an agent token is not one agentSessionRe takes.

Functions

func CLIHeaders

func CLIHeaders(raw string) []string

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.

func Encode

func Encode(v any) []byte

Encode is how an answer is written: like the previous runtime's JSON.stringify, no escaping of <, > and &, no trailing newline. A route that hashes its body (an ETag) hashes these bytes.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any, h http.Header)

WriteJSON writes a computed answer as a JSON route does (the hub answers some of the runtime's routes itself).

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

func (t *CLIToken) Accepts(v string) (Principal, bool)

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

func (t *CLIToken) AgentOf(r *http.Request) (session string, agent bool)

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

func (t *CLIToken) IssueAgent(session string, ttl time.Duration) (string, error)

IssueAgent is an agent token for a session that holds for ttl.

func (*CLIToken) OK added in v0.15.40

func (t *CLIToken) OK(r *http.Request) bool

OK checks a request to the internal routes: the runtime's cli-token, or an agent token of its own, in one of the headers.

func (*CLIToken) PrincipalOf added in v0.16.86

func (t *CLIToken) PrincipalOf(r *http.Request) (Principal, bool)

PrincipalOf is the principal of the first header with a token that holds; false when none does.

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 LoadConfig

func LoadConfig() (Config, error)

LoadConfig reads the environment.

func (Config) Standalone

func (c Config) Standalone() bool

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 New

func New(cfg Config, routes ...Route) (*Gate, error)

func (*Gate) Add

func (f *Gate) Add(routes ...Route)

Add appends routes; call before serving.

func (*Gate) AsBodyRoutes

func (f *Gate) AsBodyRoutes(routes ...BodyRoute) []Route

AsBodyRoutes turns body routes into gate routes.

func (*Gate) AsRoutes

func (f *Gate) AsRoutes(routes ...JSONRoute) []Route

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) ServeHTTP

func (f *Gate) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Gate) SetFallback

func (f *Gate) SetFallback(h http.Handler)

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

func (f *Gate) SliceOn(slice string) bool

SliceOn is whether a slice is switched on (every one but the shadows on a standalone gate).

func (*Gate) Standalone

func (f *Gate) Standalone() bool

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

type Principal struct {
	Agent   bool
	Session string
}

Principal is who a request with a token is: the person's CLI (the cli-token), or an agent's launch.

type Raw

type Raw struct {
	ContentType string
	Body        []byte
}

Raw is an answer that is not JSON: its bytes as they are, with their content type.

type Route

type Route struct {
	Slice   string
	Match   func(*http.Request) bool
	Handler http.Handler
}

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.

type Slices

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

Slices says which slices are on, re-reading state-mode.json at most once a second.

func NewSlices

func NewSlices(dataDir string) *Slices

func (*Slices) On

func (s *Slices) On(name string) bool

Jump to

Keyboard shortcuts

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