cloudflare

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: MIT Imports: 13 Imported by: ? 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 cloudflare creates an application's Cloudflare Tunnel with the cloudflared CLI, the way findmy-mcp-bridge does (src/tunnel.mjs), with no Cloudflare API token. Family Hub's `hub admin cloudflare connect` is a thin command over Connector.Connect:

  1. `cloudflared tunnel login` prints an authorization link; the operator opens it and picks the zone. cloudflared then saves the account certificate (cert.pem) in a temporary HOME.
  2. `cloudflared tunnel create` makes the tunnel and writes its credentials into the tunnel directory, unless the directory already holds a tunnel of an earlier run, which is reused.
  3. `cloudflared tunnel route dns` points the hostname at the tunnel (a CNAME), replacing a record only with ConnectOptions.OverwriteDNS (--overwrite-dns).
  4. config.yml and tunnel.json are written into the tunnel directory (0600).
  5. The account certificate is deleted, whatever happened: only the tunnel's own credentials stay.

The cloudflared service of the application's stack runs the tunnel from that directory, mounted at the same path. The cloudflared binary is reached through Runner, so tests (and applications that run it elsewhere) inject their own.

Index

Constants

View Source
const (
	ConfigFile      = "config.yml"
	CredentialsFile = "credentials.json"
	RecordFile      = "tunnel.json"
)

Files inside the tunnel directory.

View Source
const (
	LoginTimeout   = 15 * time.Minute // a hard cap: cloudflared itself stops waiting for the operator after a few minutes
	CommandTimeout = 2 * time.Minute
)

Timeouts of the cloudflared steps.

View Source
const DefaultNamePrefix = "mcp-gatekeeper-"

DefaultNamePrefix starts the name of a tunnel the connector creates, when Options.NamePrefix is empty.

Variables

View Source
var ErrHostname = errors.New("bad hostname")

ErrHostname marks a hostname the command refuses.

Functions

This section is empty.

Types

type ConnectOptions

type ConnectOptions struct {
	Hostname     string
	OverwriteDNS bool
}

ConnectOptions are the choices of one connect.

type Connector

type Connector struct {
	// contains filtered or unexported fields
}

Connector runs the connect flow.

func New

func New(o Options) (*Connector, error)

New validates o and returns a Connector.

func (*Connector) Connect

func (c *Connector) Connect(ctx context.Context, o ConnectOptions, out io.Writer) (Record, error)

Connect runs the whole flow, printing progress and the authorization link to out.

func (*Connector) Dir

func (c *Connector) Dir() string

Dir is the tunnel directory.

type ExecRunner

type ExecRunner struct {
	Binary string
}

ExecRunner runs the cloudflared binary at Binary (a name looked up in PATH, or a path).

func (ExecRunner) Run

func (r ExecRunner) Run(ctx context.Context, env, args []string, line func(string)) (string, error)

Run implements Runner.

type Options

type Options struct {
	// Runner runs cloudflared; nil is ExecRunner{Binary: "cloudflared"}.
	Runner Runner
	// Dir is the tunnel directory, the same path in the application and in cloudflared. Required.
	Dir string
	// Service is where the tunnel sends the traffic, e.g. http://fh-hub:8080. Required.
	Service string
	// TempDir is where the temporary HOME is made; "" is os.TempDir.
	TempDir string
	// NamePrefix starts the tunnel's name (the prefix and the host with dashes, e.g. "family-hub-hub-example-com");
	// "" is DefaultNamePrefix. An application keeps its value: it is the name of tunnels already created.
	NamePrefix string
	// StartHint is printed last, how to start the tunnel (e.g. "docker compose restart cloudflared"); "" prints
	// nothing.
	StartHint string
}

Options configure a Connector.

type Record

type Record struct {
	Name     string `json:"name"`
	ID       string `json:"id"`
	Hostname string `json:"hostname,omitempty"`
}

Record is tunnel.json: which tunnel the directory holds and where it points.

func ReadRecord

func ReadRecord(dir string) (Record, error)

ReadRecord reads tunnel.json of dir; a missing file is the zero Record and no error.

type Runner

type Runner interface {
	// Run runs cloudflared with args in exactly the environment env, calls line (when not nil) with every line of its
	// output as it comes, and returns the whole output. A non-zero exit is an error.
	Run(ctx context.Context, env, args []string, line func(string)) (string, error)
}

Runner runs the cloudflared CLI.

Jump to

Keyboard shortcuts

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