Documentation
¶
Overview ¶
Package api serves eventhorizon's live data to local viewers like the TUI:
GET /api/stats JSON snapshot: totals, per second history, top lists
GET /api/events newline delimited JSON: recent events, then live ones
GET /api/rules the user's own allow and block rules
POST /api/rules {"domain": "...", "action": "allow"|"block"}
DELETE /api/rules?domain=...
POST /api/reload download the blocklists again
The data is effectively the machine's browsing history. Without a token the API only answers requests addressed to a loopback name. That stops DNS rebinding: a web page that points its own domain at 127.0.0.1 still sends its own domain in the Host header, and gets refused. The Host check is not a login, though: any program that can connect may send any Host. With a token set (see token.go), every request must prove it knows the token instead, which also works across the network.
Changes need one more defense. Browsers let any web page send a simple POST to 127.0.0.1, with the right Host header, without asking (they only hide the reply). So every change request must carry the ChangeHeader. A browser will not add a custom header to a cross site request without first asking permission (a CORS preflight), which this API never grants.
Index ¶
Constants ¶
const ChangeHeader = "X-Eventhorizon-Change"
ChangeHeader must be present, with any value, on every request that changes something.
Variables ¶
This section is empty.
Functions ¶
func IsLoopback ¶
IsLoopback reports whether addr's host is a loopback address, so the caller can warn when the API is exposed to the network.
func LoadOrCreateToken ¶
LoadOrCreateToken reads the token at path, or writes a new random one there, readable only by its owner, if the file does not exist.
Types ¶
type Control ¶
type Control interface {
Rules() []rules.Rule
SetRule(domain, action string) (rules.Rule, error)
RemoveRule(domain string) (bool, error)
Reload(ctx context.Context) error
}
Control is what the change endpoints act on (see internal/control).
type Server ¶
type Server struct {
Control Control // nil: change endpoints answer 501
Stats *stats.Stats
Hub *events.Hub
Sizes func() (blocklist, cache int) // optional, filled into snapshots
Upstreams func() []upstream.Status // optional, filled into snapshots
Token string // optional; when set, required on every request
TopN int
// contains filtered or unexported fields
}