cfapi

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: AGPL-3.0 Imports: 23 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 cfapi is the Cloudflare WARP registration client and the importer for wgcf profiles. It is used by the panel: one address registers every node, and the agent stays free of the uTLS stack. Nothing here runs by itself: every call to Cloudflare is an explicit call of Register, Fetch or Delete, which the panel makes only on the owner's click (or the opt-in auto re-registration).

The constants a registration request is built from changed twice in 2026 and broke every client each time (Cloudflare answers HTTP 429 to a request that does not look like the Android app: API version in the path, User-Agent, CF-Client-Version and the TLS ClientHello). They are Params, data the panel stores and the owner can change, not code. The ClientHello is chosen by name (TLSSpecID) from a registry.

The request format, headers and the uTLS ClientHello are taken from wgcf v2.3.0 (MIT, see tls.go for the notice). Tests talk only to a fake API server (NewTLS test server through Client.BaseURL and Client.RootCAs); the live service is never contacted from the test suite.

Index

Constants

View Source
const (
	// BaseURL is the registration API.
	BaseURL = "https://api.cloudflareclient.com"
	// TOSURL is the terms page the UI links next to the register button.
	TOSURL = "https://www.cloudflare.com/application/terms/"
)

Variables

This section is empty.

Functions

func GenerateKey

func GenerateKey(r io.Reader) (priv, pub string, err error)

GenerateKey makes a WireGuard key pair (base64): a clamped X25519 private key and its public key.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports an HTTP 404 (registration unknown or already deleted).

func IsRateLimited

func IsRateLimited(err error) bool

IsRateLimited reports an HTTP 429: Cloudflare dislikes the caller (TLS fingerprint, headers, too many registrations from one address). The remedy is to import a wgcf profile, not to hammer.

func RegisterTLSSpec

func RegisterTLSSpec(id string, spec func() *utls.ClientHelloSpec)

RegisterTLSSpec adds a named ClientHello (for the day Cloudflare changes what it accepts and the owner ships a new one without waiting for a release). An existing name is replaced.

func TLSSpecIDs

func TLSSpecIDs() []string

TLSSpecIDs lists the known ClientHello names.

Types

type APIError

type APIError struct {
	Status int
	// Body is the first bytes of the answer (Cloudflare error JSON; it carries no secrets).
	Body string
}

APIError is a non-200 answer of the API.

func (*APIError) Error

func (e *APIError) Error() string

type Account

type Account struct {
	ID          string
	Token       Secret // Bearer secret for Fetch/Delete; empty for an imported profile without wgcf-account.toml
	License     Secret
	AccountType string // "free" | "plus" | ""
	ClientID    string // base64, 3 bytes: the source of Reserved

	PrivateKey    Secret // base64 X25519
	PeerPublicKey string
	EndpointHost  string // "engage.cloudflareclient.com:2408": informational, never dialed
	EndpointV4    string
	EndpointV6    string
	Ports         []uint16
	AddressV4     string // "172.16.0.2/32"
	AddressV6     string // "" = none
	MTU           uint16
	Reserved      []byte // 0 or 3 bytes
}

Account is a WARP device account in the forms the panel stores and the agent's WarpSpec needs. Endpoints are IP literals without a port; addresses carry their prefix length.

func ParseWgcf

func ParseWgcf(profileConf, accountTOML string) (Account, error)

ParseWgcf builds an Account from the text of wgcf-profile.conf (required) and, when given, wgcf-account.toml (adds the device id, the access token and the license key, which enable Fetch and Delete; the client id, and so the "reserved" bytes, are then read with Fetch). An endpoint that is a host name stays in EndpointHost with empty EndpointV4/V6: the caller resolves it once (ResolveEndpoint) and stores the literal. Errors never contain key material.

func (*Account) ResolveEndpoint

func (a *Account) ResolveEndpoint(ctx context.Context, lookup func(ctx context.Context, host string) ([]netip.Addr, error), withV6 bool) error

ResolveEndpoint turns a host-name endpoint into IP literals once, through lookup (the panel's resolver): the IPv4 address is taken, and the IPv6 one only when withV6 is set (new accounts often have no working IPv6). A profile whose endpoint already was a literal is left alone.

type Client

type Client struct {
	Params Params
	// BaseURL overrides BaseURL (tests).
	BaseURL string
	// RootCAs verifies the server certificate; nil = system roots. Tests put their server's CA here.
	RootCAs *x509.CertPool
	// Timeout for one call, dial and handshake included (30 s).
	Timeout time.Duration
	// Rand is the key source (crypto/rand).
	Rand io.Reader
}

Client talks to the API. The zero value is usable (defaults, the real base URL).

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, id, token string) error

Delete removes our registration at Cloudflare (DELETE /reg/{id}); the token is required.

func (*Client) Fetch

func (c *Client) Fetch(ctx context.Context, id, token, privateKey string) (Account, error)

Fetch reads the registration again (GET /reg/{id}): endpoint, addresses and client id may have changed. It creates nothing. privateKey is the account's own key (Cloudflare never returns it); it is copied into the result.

func (*Client) Register

func (c *Client) Register(ctx context.Context, tos time.Time) (Account, error)

Register creates a new anonymous WARP device: a fresh key pair, POST /reg. tos is when the owner accepted the terms (TOSURL); it goes into the request like the official app does. Creates a Cloudflare account: only call it on the owner's action.

type Params

type Params struct {
	// APIVersion is the path segment before /reg ("v0a5641").
	APIVersion string
	// UserAgent and CFClientVersion imitate the Android app.
	UserAgent       string
	CFClientVersion string
	// TLSSpecID names the uTLS ClientHello ("wgcf_v2.3.0"), see TLSSpecIDs.
	TLSSpecID string
}

Params are the registration constants. Empty fields fall back to Defaults.

func Defaults

func Defaults() Params

Defaults are the values of wgcf v2.3.0 (2026-09-18).

func (Params) Validate

func (p Params) Validate() error

Validate rejects values that cannot go into a URL or a header, or name an unknown ClientHello.

func (Params) WithDefaults

func (p Params) WithDefaults() Params

WithDefaults fills empty fields from Defaults.

type Secret

type Secret string

Secret is a string that prints as [REDACTED] everywhere it is formatted or logged; Reveal returns the value.

func (Secret) GoString

func (Secret) GoString() string

func (Secret) LogValue

func (Secret) LogValue() slog.Value

func (Secret) MarshalText

func (s Secret) MarshalText() ([]byte, error)

func (Secret) Reveal

func (s Secret) Reveal() string

func (Secret) String

func (Secret) String() string

Jump to

Keyboard shortcuts

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