Documentation
¶
Overview ¶
Package warp is the node's WARP egress.
One Cloudflare WARP account per node, run as a plain WireGuard tunnel (device "mgwarp"). Two kinds of traffic leave through it:
- Hysteria2 inbounds with egress "warp": their dialers are bound to the device (egress.WithDevice), an "oif" rule sends that to the WARP routing table.
- AWG inbounds with egress "warp": "ip rule from <client subnet>" sends the forwarded traffic into the same table, and the node masquerades it to the WARP address (WARP accepts only its own address as source).
Fail closed. The WARP table always ends in "unreachable default", so a dead or missing tunnel makes the selected traffic fail with "no route to host" instead of leaving the node directly; the rules for the client subnets stay while the tunnel is paused or gone. Only Cleanup removes everything.
The package has three layers: the Manager (state machine, health, the recovery ladder; portable, unit-tested with fakes), the dataplane (Linux: netlink, wgctrl or amneziawg-go on a TUN, nft; other OSes: a stub that reports UNAVAILABLE) and cfapi (the Cloudflare registration client, used by the panel, not by the agent).
Index ¶
- Constants
- Variables
- func CleanupHost(ctx context.Context, s Settings) error
- type Event
- type Finding
- type Health
- type Manager
- func (m *Manager) Apply(ctx context.Context, spec *plugin.WarpSpec) error
- func (m *Manager) CheckNow(ctx context.Context) (checked, thresholdDown bool, err error)
- func (m *Manager) Cleanup(ctx context.Context) error
- func (m *Manager) Configured() bool
- func (m *Manager) Egress() engine.Egress
- func (m *Manager) Health() (Health, bool)
- func (m *Manager) Preflight(ctx context.Context) []Finding
- func (m *Manager) Reconnect(ctx context.Context) (bool, error)
- func (m *Manager) Run(ctx context.Context)
- func (m *Manager) SetRoutedSubnets(ctx context.Context, subnets []netip.Prefix) error
- type Options
- type ProbeResult
- type Settings
- type State
Constants ¶
const ( // DefaultIface is the tunnel device. It has no fwmark (an encapsulated packet must not enter the WARP table). DefaultIface = "mgwarp" // DefaultTable is the routing table of the WARP path; the two preferences are the "oif" rule and the // "from <client subnet>" rules. Preflight reports a clash with another tool on the host. DefaultTable = 51820 DefaultOifPref = 90 DefaultSubnetPref = 110 // NftTable is the WARP nft table (masquerade, MSS clamp, optional "reserved" stamping). The doctor treats it as ours. NftTable = "mistgate_warp" )
const ( DefaultProbeA = "http://connectivity.cloudflareclient.com/cdn-cgi/trace" DefaultProbeB = "http://connectivitycheck.gstatic.com/generate_204" )
Default probe targets: A is Cloudflare's own trace (tells warp=on|plus|off and the colo), B must not be Cloudflare (a handshake can work while everything else is black-holed, wgcf issue 619).
Variables ¶
var ErrNotActive = errors.New("warp: egress not active")
ErrNotActive is what the WARP egress returns while the node has no WARP, it is paused or unavailable: there is no silent direct exit.
ErrUnavailable means no WireGuard backend can run on this host.
Functions ¶
func CleanupHost ¶
CleanupHost removes everything a run of the manager leaves on the host, by name, preference and table number only (no state needed), so the unit's ExecStopPost can call it after a crash: an orphaned "unreachable default" would black-hole every socket that still selects the table. It removes only what is ours: the rules with our two preferences that look up the WARP table, the routes of the table that go through the device, the fail-closed "unreachable default metric 4096" and the "throw" routes of our client subnets, the device and the nft table; one failure does not stop the rest. The table number is a setting because it is wg-quick's default (51820) too: another tool's rules and routes in the same table are left alone.
Types ¶
type Event ¶
Event is what the manager tells the agent (which turns it into an agent.v1.Event). Codes: "warp_state" {state, from}, "warp_needs_attention" {reason}.
type Finding ¶
type Finding struct {
// "table_in_use" | "rule_pref_in_use" | "forward_drop" | "interface_in_use"
ID string
Detail string
}
Finding is one preflight result (the doctor shows it); a clash the owner has to resolve.
type Health ¶
type Health struct {
State State
// "kernel" | "userspace" | "".
Backend string
// ip:port in use right now (after a rotation).
Endpoint string
LastHandshake time.Time
// Probe A: "on" | "plus" | "off" | "" (failed).
WarpFlag string
Colo string
ProbeCloudflareOK bool
ProbeOtherOK bool
// Consecutive failed checks.
Failures uint32
RxBytes uint64
TxBytes uint64
// Short English fact for the LATEST check ("" when it passed), e.g. "probe_other_failed", "handshake_never", then
// "; ladder: <note>" with the last recovery action. The codes are listed in web/src/lib/warp-error.ts.
LastError string
// The latest round of each probe; nil when the round did not run it (link down, no stats).
ProbeCloudflare, ProbeOther *ProbeResult
// When the latest health check finished (zero before the first one).
CheckedAt time.Time
}
Health mirrors agent.v1.WarpHealth; the agent converts it.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager owns the WARP tunnel of the node. The agent calls Apply with every DesiredState (nil = no WARP), SetRoutedSubnets with the client subnets of AWG inbounds whose egress is "warp", hands Egress() to the engines, runs Run in a goroutine and calls Cleanup on the way out. Safe for concurrent use.
func (*Manager) Apply ¶
Apply makes the node match spec: nil removes WARP (the tunnel goes, the client-subnet rules stay closed while subnets are still routed), Enabled=false pauses it. A spec equal to the applied one only re-asserts routing and repairs a vanished device; the WireGuard peer is never touched without a difference.
func (*Manager) CheckNow ¶ added in v0.1.1
CheckNow reruns one bounded health check only when the configured tunnel is up/starting and its latest check failed. thresholdDown reports whether the manager is Down at the failure threshold, including when a scheduled check reached the threshold just before this call. It serializes with the periodic check and never resets a tunnel.
func (*Manager) Cleanup ¶
Cleanup removes everything the manager installed: rules, routes of the table, the device, the nft table. Called by the agent on shutdown (and by the stateless CleanupHost from the unit's ExecStopPost).
func (*Manager) Configured ¶
Configured reports whether the node has a WARP configuration (paused included). The agent refuses to start an inbound with egress "warp" while it is false.
func (*Manager) Egress ¶
Egress is the engine.Egress for inbounds with egress "warp". Every dial goes through the tunnel device; while the node has no WARP, it is paused or unavailable, every dial fails with ErrNotActive, and a dead tunnel fails in the kernel ("no route to host"): never a silent direct exit.
func (*Manager) Health ¶
Health returns the last measured state; ok=false when the node has no WARP (nothing to report).
func (*Manager) Preflight ¶
Preflight reports clashes with the host (table, rule preferences, device name, a FORWARD drop policy).
func (*Manager) Reconnect ¶
Reconnect rebuilds the current tunnel only while it is down. It leaves the configured account and routed subnets intact; the routes remain fail-closed while the tunnel comes back. A fresh doctor result may make this a no-op.
func (*Manager) Run ¶
Run drives the handshake watch and the health checks until ctx ends. It does not clean up; the agent calls Cleanup.
func (*Manager) SetRoutedSubnets ¶
SetRoutedSubnets sets the client subnets whose forwarded traffic must leave through WARP (AWG inbounds with egress "warp"). The rules stay while the tunnel is down or paused: those clients fail closed.
type Options ¶
type Options struct {
Log *slog.Logger
Settings Settings
// DNS is the node's resolver list (engine.Env.DNS); the WARP egress resolves names like the direct one.
DNS func() []string
// Emit receives state-change events; nil drops them.
Emit func(Event)
Now func() time.Time
// Interval between health checks (30 s); FastInterval while the tunnel is still starting (5 s).
Interval, FastInterval time.Duration
// HandshakeWait is how long a fresh tunnel may go without a handshake before the next endpoint is tried (15 s).
HandshakeWait time.Duration
// ProbeAURL and ProbeBURL override the probe targets (tests); see DefaultProbeA and DefaultProbeB.
ProbeAURL, ProbeBURL string
// ProbeTimeout bounds one probe, dial included (20 s by default).
ProbeTimeout time.Duration
// AllowPrivate lets the tunnel egress dial private and documentation-range addresses (tests, dev).
AllowPrivate bool
}
Options configures a Manager. Everything has a default.
type ProbeResult ¶
ProbeResult is one probe of a health check: ok, how long it took (a timeout shows the timeout) and when it ended.
type Settings ¶
Settings are the host-level knobs. The zero value means the defaults. The table and the preferences can clash with other tools (Tailscale, wg-quick), which is why they are settings; Preflight detects a clash.