web

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: GPL-3.0 Imports: 21 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

Index

Constants

View Source
const (
	// CookieName is the HttpOnly session cookie for the Web gateway.
	CookieName = "mihari_web_auth"
	// AuthPath is the login endpoint reserved under a mihari prefix to avoid panel SPA clashes.
	AuthPath = "/__mihari/auth"
)

Variables

This section is empty.

Functions

func NewControllerProxy

func NewControllerProxy(options ProxyOptions) (*httputil.ReverseProxy, error)

NewControllerProxy builds a reverse proxy that strips client Authorization and injects the controller secret.

func WriteReject

func WriteReject(w http.ResponseWriter, action Action)

WriteReject writes a JSON error for a classified reject action without contacting mihomo.

Types

type Action

type Action int

Action is the gateway disposition for a browser API request.

const (
	// ActionProxyRead forwards a safe read to the mihomo controller with injected secret.
	ActionProxyRead Action = iota
	// ActionProxyWS upgrades and proxies a streaming endpoint.
	ActionProxyWS
	// ActionProxyStorage forwards a /storage/{key} read or write (GET/HEAD/PUT/DELETE) to
	// the mihomo controller. Storage is a generic KV store isolated from mihari-managed
	// config, so writes pass through rather than routing through the coordinator.
	ActionProxyStorage
	// ActionMutateSelectProxy submits group selection through the coordinator.
	ActionMutateSelectProxy
	// ActionMutateClose closes one or all connections through the coordinator.
	ActionMutateClose
	// ActionMutateDelayTest runs a delay test through the coordinator.
	ActionMutateDelayTest
	// ActionMutateRestart restarts the supervised core through the coordinator.
	ActionMutateRestart
	// ActionMutateConfigs applies allowlisted config field patches through the coordinator.
	ActionMutateConfigs
	// ActionRejectUpgrade rejects core upgrade attempts owned by mihari.
	ActionRejectUpgrade
	// ActionRejectManaged rejects writes to mihari-managed controller/port/UI fields.
	ActionRejectManaged
	// ActionRejectUnknown is the default-deny for unrecognized write methods/paths.
	ActionRejectUnknown
	// ActionNotAPI marks non-API traffic (static/auth) that the classifier does not handle.
	ActionNotAPI
)

func Classify

func Classify(method, path string) Action

Classify decides how the Web gateway handles a method+path pair. Unknown writes never fall through to the controller.

func ClassifyUpgrade

func ClassifyUpgrade(method, path string, connectionUpgrade bool) Action

ClassifyUpgrade distinguishes REST GET /connections from WebSocket upgrade.

type Authenticator

type Authenticator struct {
	// WebCredential is the daemon-owned browser access token (not the controller secret).
	WebCredential string
	// ControllerSecret is retained only to reject mistaken use as a Web password in tests/docs;
	// authentication never succeeds by matching the controller secret alone when it differs.
	ControllerSecret string
	// CookieSecure sets the Secure flag (false for local http://127.0.0.1).
	CookieSecure bool
	// SessionTTL bounds cookie Max-Age.
	SessionTTL time.Duration
}

Authenticator validates browser access using the dedicated Web credential. The controller secret is never accepted as a Web password.

func (Authenticator) AuthenticateForm

func (a Authenticator) AuthenticateForm(password string) bool

AuthenticateForm validates a posted password field against the Web credential only.

func (Authenticator) Authorized

func (a Authenticator) Authorized(r *http.Request) bool

Authorized reports whether the request carries a valid Web session cookie or Bearer token.

func (Authenticator) ClearSessionCookie

func (a Authenticator) ClearSessionCookie(w http.ResponseWriter)

ClearSessionCookie expires the Web session cookie.

func (Authenticator) RejectsControllerSecretAsPassword

func (a Authenticator) RejectsControllerSecretAsPassword() bool

RejectsControllerSecretAsPassword documents that using only the controller secret fails when it is not equal to the Web credential.

func (Authenticator) SetSessionCookie

func (a Authenticator) SetSessionCookie(w http.ResponseWriter)

SetSessionCookie writes the Web session cookie.

type Mutator

type Mutator interface {
	SelectProxy(ctx context.Context, group, name string) error
	CloseConnection(ctx context.Context, id string) error
	CloseAllConnections(ctx context.Context) error
	// ApplyConfigPatch applies allowlisted config mutations (currently TUN only).
	ApplyConfigPatch(ctx context.Context, patch map[string]any) error
}

Mutator handles allowlisted browser writes through the daemon coordinator. When nil, allowlisted mutations are rejected as unsupported.

type Options

type Options struct {
	Addr             string
	Auth             Authenticator
	ControllerURL    string
	ControllerSecret string
	Panel            PanelSource
	Mutator          Mutator
	Transport        http.RoundTripper
}

Options configures a Web gateway server.

type PanelSource

type PanelSource interface {
	ActiveDir() (string, error)
	SetupPath(gatewayHost string) string
	// PanelDir returns the static file root for an installed panel, or empty when missing.
	PanelDir(panelID string) (string, error)
	// SetupPathFor returns the same-origin setup deep-link for a specific panel.
	SetupPathFor(panelID, gatewayHost string) string
}

PanelSource provides panel static trees and setup deep-links. Multiple installed panels may be served concurrently under /__mihari/panels/{panelID}/; ActiveDir/SetupPath describe the default panel for root "/".

type ProxyOptions

type ProxyOptions struct {
	// ControllerURL is the loopback controller base, e.g. http://127.0.0.1:9090.
	ControllerURL string
	// ControllerSecret is injected as Bearer; never returned to the browser.
	ControllerSecret string
	// Transport is optional for tests.
	Transport http.RoundTripper
}

ProxyOptions configures the secret-injecting reverse proxy to the mihomo controller.

type Server

type Server struct {
	Addr             string
	Auth             Authenticator
	Proxy            *httputil.ReverseProxy
	ControllerURL    string
	ControllerSecret string
	Panel            PanelSource
	Mutator          Mutator
	HTTPClient       *http.Client
	// contains filtered or unexported fields
}

Server is the loopback Web gateway: auth, static panel hosting, and API proxy.

func New

func New(options Options) (*Server, error)

New builds a gateway server. Listen is deferred until Serve or ListenAndServe.

func (*Server) ListenAddr

func (s *Server) ListenAddr() string

ListenAddr returns the bound address after Serve starts, or the configured Addr.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context) error

Serve listens on Addr until ctx is cancelled, then shuts down gracefully.

func (*Server) SessionCount

func (s *Server) SessionCount() int

SessionCount approximates concurrent authenticated request handlers.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown stops the HTTP server if running.

Jump to

Keyboard shortcuts

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