engine

package
v0.1.90-dev.2 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: 10 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

This section is empty.

Variables

This section is empty.

Functions

func CertificateAlert

func CertificateAlert(alert uint8) bool

CertificateAlert reports a TLS alert that refuses the peer's certificate (RFC 8446 section 6.2): bad_certificate (42), unsupported_certificate (43), certificate_revoked (44), certificate_expired (45), certificate_unknown (46) and unknown_ca (48). An app that sends one got the node's certificate and did not trust it (agent.proto LinkCounters.tls_cert_refused).

Types

type ApplyReport

type ApplyReport struct {
	SpecHash  string // statehash.Spec of the spec it now runs
	CredCount int
	Restarted bool
	Cert      CertInfo // what the inbound serves; zero if the protocol has no TLS
	Warnings  []string // non-fatal credential errors; the accepted credentials were applied
}

type Capabilities

type Capabilities struct {
	RateLimitPerCred bool // honours UserCred.RateLimitBps (the device limit)
	// RateLimitPerUser: honours UserCred.UserRateLimitBps, one bucket per user shared by all the user's credentials on the
	// node (Env.UserLimits). An engine with RateLimitPerCred but not this one makes the agent withhold "user-rate-limit/1".
	RateLimitPerUser bool
	HardExpiry       bool // honours UserCred.ValidUntil itself (the agent also enforces it, this is an optimisation)
}

type Cert

type Cert struct {
	GetCertificate func(*tls.ClientHelloInfo) (*tls.Certificate, error)
	Info           func() CertInfo
}

type CertInfo

type CertInfo struct {
	PinSHA256 string // hex of sha256(DER leaf)
	NotAfter  time.Time
}

type CertSource

type CertSource interface {
	// Acquire returns a live certificate source for an inbound; renewals replace it transparently.
	Acquire(ctx context.Context, inboundID string, t plugin.TLS) (Cert, error)
	Release(inboundID string)
}

type Collected

type Collected struct {
	Traffic  []plugin.UserTraffic // non-zero deltas only
	Sessions []plugin.Session
	Links    []plugin.LinkStat // non-zero connection-counter deltas
}

type Egress

type Egress interface {
	TCP(addr string) (net.Conn, error)
	UDP(addr string) (EgressUDP, error)
	CheckUDP(addr string) error
}

Egress has the exact shape of hysteria server.Outbound, so a WARP/netstack dialer drops in without an adapter for Hysteria2, and other engines can wrap it.

type EgressUDP

type EgressUDP interface {
	ReadFrom(b []byte) (int, string, error)
	WriteTo(b []byte, addr string) (int, error)
	Close() error
}

type Engine

type Engine interface {
	// Protocol is the plugin id ("hysteria2"), equal to InboundSpec.Protocol it serves.
	Protocol() string
	// Version is shown in Hello and on the node page: "hysteria core v2.13.0".
	Version() string
	Capabilities() Capabilities

	// Apply makes the inbound match (spec, creds). Idempotent.
	//  - New inbound: bind, start serving.
	//  - Same spec_hash, different creds: swap the credential index. NO restart, open sessions of kept
	//    credentials survive, sessions of removed credentials are closed at their next traffic.
	//  - Different spec_hash: restart that inbound (and only it); report Restarted. One exception: an engine MAY
	//    apply the change in place when only fields it changes live differ (awg: mtu) and then reports
	//    Restarted=false. A different port, version, key or obfuscation always recreates the interface.
	//  - spec.Enabled == false: stop the listener, keep nothing.
	// It returns an error only for a failure to reach the target state (bind failed, bad settings);
	// the agent records it in the inbound result and keeps the other inbounds running.
	Apply(ctx context.Context, spec plugin.InboundSpec, creds []plugin.UserCred) (ApplyReport, error)

	// Remove stops the inbound and forgets it. Unknown id is not an error.
	Remove(ctx context.Context, inboundID string) error

	// Collect returns traffic deltas accumulated since the previous Collect (it resets them) and an
	// absolute snapshot of open sessions. The agent owns delivery: a delta that was handed out is the
	// agent's responsibility (it queues it until the panel acks), so Collect never replays.
	Collect(ctx context.Context) (Collected, error)

	// Kick closes the sessions of these credentials (best effort; Hysteria2 closes at the next packet).
	// Returns how many credentials had an open session.
	Kick(ctx context.Context, credIDs []string) (int, error)

	// Observed reports what the engine holds right now, for the state hash and drift detection.
	Observed() []statehash.Inbound

	Health() []plugin.EngineHealth

	// Close stops everything; bounded by ctx.
	Close(ctx context.Context) error
}

Engine runs all inbounds of ONE protocol on the node. Implementations: hysteria2, awg, (v1.x) xray. It may live in-process or wrap a child process; the agent does not care.

Concurrency: the agent calls Apply/Remove/Kick from one goroutine (the reconcile loop) and Collect/ Observed/Health from others; implementations must make these safe. Hot paths (authentication, traffic accounting) must not take a global mutex: use an atomic pointer to an immutable credential index and per-credential atomic counters.

type Env

type Env struct {
	Log *slog.Logger
	// Event reports protocol-level detections to the owning agent. Implementations must keep event parameters
	// bounded and must not include credentials or other secrets.
	Event func(Event)
	// Certs issues/renews certificates per inbound (ACME domain, ACME IP, self-signed).
	Certs CertSource
	// Egress returns the outbound for InboundSpec.Egress: "direct", or "warp" (the WARP manager's, bound to the
	// mgwarp device). "warp" while the node has no WARP configuration is an error: there is no silent direct exit.
	Egress func(name string) (Egress, error)
	// DNS returns the node's resolvers (NodeSettings.dns_resolvers or the geo default); called per use so
	// a settings change needs no restart.
	DNS func() []string
	// Masquerade serves unauthenticated requests: the decoy site. The same handler is used
	// for HTTP/3 (the core's MasqHandler) and for TCP 443 (hysteria extras/masq.MasqTCPServer).
	Masquerade func(inboundID string) http.Handler
	Now        func() time.Time
	// UserLimits holds the person limit buckets, shared by every engine of the agent so that a person's hysteria2 and vless
	// credentials draw on one bucket. Nil: the engine keeps a registry of its own.
	UserLimits *ratelimit.Registry
	// Nets resolves client addresses inside an engine and records only ASN-level connection events. Nil disables counting.
	Nets *nets.Aggregator
}

Env is everything the agent (the owner of host state) gives an engine at construction. Engines never touch nftables, sysctl, the resolver configuration or certificates directly.

type Event added in v0.1.5

type Event struct {
	Code      string
	InboundID string
	Warning   bool
	Params    map[string]string
}

Event is a small, protocol-neutral signal from an engine to the node agent.

type Factory

type Factory func(Env) (Engine, error)

Factory builds an engine; registered from main: engine.Register("hysteria2", hysteria2.New).

type TCPPortUser added in v0.1.31

type TCPPortUser interface {
	TCPClaims(ctx context.Context, claimed map[uint16]string)
}

TCPPortUser lets an engine give up a TCP side listener when another inbound claims that port. claimed maps enabled TCP listener ports to their inbound ids and must be treated as read-only.

Jump to

Keyboard shortcuts

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