api

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 19 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 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

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

func IsLoopback(addr string) bool

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

func LoadOrCreateToken(path string) (token string, created bool, err error)

LoadOrCreateToken reads the token at path, or writes a new random one there, readable only by its owner, if the file does not exist.

func ReadToken

func ReadToken(path string) (string, error)

ReadToken reads a token file, as written by LoadOrCreateToken.

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
}

func New

func New(addr string, st *stats.Stats, hub *events.Hub) (*Server, error)

New builds the handler for an API listening on addr ("127.0.0.1:5380").

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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