Files
ServerKit/agent/internal/cloudflared/cloudflared.go
T
Juan Denis 8ddd60f83d Add Cloudflared tunnel management
Introduce Cloudflare Tunnel management end-to-end.

Highlights:
- Add agent/internal/cloudflared package (Manager interface, Linux impl, non-Linux stub) to shell out to cloudflared and detect auth via cert.pem.
- Wire cloudflared Manager into the agent and register cloudflared:* handlers for status/list/create/route/delete.
- Extend protocol.Capabilities and agent payloads with runtimes detection and a cloudflared capability; implement runtime version probes.
- Add backend API endpoints and RemoteCloudflaredService that forward cloudflared actions to agents; update agent registry to store runtimes.
- Frontend: new CloudflaredTab UI, API client methods for remote cloudflared operations, styles, and UX tweaks (plus minor component/class refinements in LogViewer, ProcessTable, FileManager, StyleGuide, terminal styles).

Purpose: allow the panel to manage Cloudflare named tunnels by delegating commands to the agent's cloudflared CLI, while surfacing binary/auth state (install vs authenticated) and detected language runtimes.
2026-05-01 02:26:15 -04:00

90 lines
3.3 KiB
Go

// Package cloudflared manages Cloudflare named tunnels via the
// cloudflared CLI. Linux-only.
//
// Auth model (approach A):
//
// The user runs `cloudflared tunnel login` once per host. That writes
// ~/.cloudflared/cert.pem (or /etc/cloudflared/cert.pem when run as
// root). The cert is the long-lived "I'm allowed to manage tunnels
// for account X" credential. We never see, copy, or store the user's
// Cloudflare API token; the panel only ever shells out to
// `cloudflared` and trusts whatever auth the binary already has.
//
// Status() reports two flags:
// - Available: cloudflared is on PATH
// - Authenticated: cert.pem is present in one of the standard
// locations
//
// The panel uses both: the tab is gated on `capabilities.cloudflared`
// (binary installed, set by capabilities.Probe at agent startup) and
// the tab body shows an "authenticate first" affordance when
// Authenticated=false.
//
// Tunnel lifecycle:
//
// 1. Create: `cloudflared tunnel create <name>`
// (writes <UUID>.json credentials file)
// 2. Route: `cloudflared tunnel route dns <name|UUID> <hostname>`
// (creates a Cloudflare DNS record pointing the
// hostname at the tunnel — *the* useful action)
// 3. Run: out of scope here; the operator wires up
// `cloudflared tunnel run <name>` as a systemd service
// with a config.yml ingress mapping. Future iteration.
// 4. Delete: `cloudflared tunnel delete <name|UUID>`
package cloudflared
import (
"context"
"os/exec"
)
// Status reports usability and auth state.
type Status struct {
Available bool `json:"available"` // binary installed
Authenticated bool `json:"authenticated"` // cert.pem present
CertPath string `json:"cert_path,omitempty"` // detected cert.pem path, if any
Reason string `json:"reason,omitempty"` // explanation when Available=false
LoginHint string `json:"login_hint,omitempty"` // canned "run this command" when Authenticated=false
Version string `json:"version,omitempty"` // cloudflared --version line, best-effort
}
// Tunnel mirrors `cloudflared tunnel list --output json` rows.
type Tunnel struct {
ID string `json:"id"`
Name string `json:"name"`
CreatedAt string `json:"created_at,omitempty"`
Connections []string `json:"connections,omitempty"`
}
// CreateRequest creates a new named tunnel.
type CreateRequest struct {
Name string `json:"name"`
}
// RouteRequest binds a hostname to an existing tunnel via Cloudflare
// DNS. TunnelRef can be the tunnel name or UUID — cloudflared accepts
// either.
type RouteRequest struct {
TunnelRef string `json:"tunnel_ref"`
Hostname string `json:"hostname"`
}
// Manager is the platform-agnostic interface; the Linux build wires
// it up with real exec calls, non-Linux builds return "unsupported."
type Manager interface {
Status(ctx context.Context) (*Status, error)
List(ctx context.Context) ([]Tunnel, error)
Create(ctx context.Context, req CreateRequest) (*Tunnel, error)
Route(ctx context.Context, req RouteRequest) error
Delete(ctx context.Context, ref string) error
}
// hasCloudflared is shared between the Linux backend and tests.
func hasCloudflared() (string, bool) {
p, err := exec.LookPath("cloudflared")
if err != nil {
return "", false
}
return p, true
}