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:
- `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.
- `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.
- `cloudflared tunnel route dns` points the hostname at the tunnel (a CNAME), replacing a record only with ConnectOptions.OverwriteDNS (--overwrite-dns).
- config.yml and tunnel.json are written into the tunnel directory (0600).
- 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 ¶
const ( ConfigFile = "config.yml" CredentialsFile = "credentials.json" RecordFile = "tunnel.json" )
Files inside the tunnel directory.
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.
const DefaultNamePrefix = "mcp-gatekeeper-"
DefaultNamePrefix starts the name of a tunnel the connector creates, when Options.NamePrefix is empty.
Variables ¶
var ErrHostname = errors.New("bad hostname")
ErrHostname marks a hostname the command refuses.
Functions ¶
This section is empty.
Types ¶
type ConnectOptions ¶
ConnectOptions are the choices of one connect.
type Connector ¶
type Connector struct {
// contains filtered or unexported fields
}
Connector runs the connect flow.
type ExecRunner ¶
type ExecRunner struct {
Binary string
}
ExecRunner runs the cloudflared binary at Binary (a name looked up in PATH, or a path).
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 ¶
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.