flared

package module
v0.0.0-...-e8d4ece Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 14 Imported by: 0

README

flared

flared is a Go library that allows you to run Cloudflare Tunnels (cloudflared) directly from your Go code.

It supports both Quick Tunnels (temporary tunnels via trycloudflare.com) and Named Tunnels (custom domains via Cloudflare DNS).

Installation

go get github.com/lucanhost/flared

Quick Start

Quick Tunnel (trycloudflare.com)

Creates a temporary, random URL to expose your local service.

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"

	"github.com/lucanhost/flared"
)

func main() {
	// 1. Start a simple HTTP server
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Welcome to the Go HTTP Server (Quick Tunnel)!")
	})
	go func() {
		if err := http.ListenAndServe(":8080", nil); err != nil {
			log.Fatal(err)
		}
	}()

	// 2. Start Cloudflare Tunnel
	opts := flared.Options{
		OriginURL: "http://localhost:8080", // Local URL to expose
		ShowLog:   false,                   // Toggle cloudflared internal logs
	}

	tunnel, err := flared.Start(context.Background(), opts)
	if err != nil {
		log.Fatal(err)
	}
	defer tunnel.Close()

	fmt.Printf("Tunnel URL: %s\n", tunnel.URL())
	tunnel.Wait()
}
Named Tunnel (Custom Domain)

Creates and routes a stable tunnel to your custom domain. Requires a valid cert.pem on the host. If the certificate is missing, the library will automatically trigger the cloudflared login flow via your terminal.

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"

	"github.com/lucanhost/flared"
)

func main() {
	// 1. Start a simple HTTP server
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "Welcome to the Go HTTP Server (Named Tunnel)!")
	})
	go func() {
		if err := http.ListenAndServe(":8080", nil); err != nil {
			log.Fatal(err)
		}
	}()

	// 2. Start Cloudflare Tunnel
	opts := flared.Options{
		OriginURL: "http://localhost:8080",
		Name:      "my-demo-tunnel",
		Domain:    "api.example.com", 
		ShowLog:   false,
	}

	tunnel, err := flared.Start(context.Background(), opts)
	if err != nil {
		log.Fatal(err)
	}
	defer tunnel.Close()

	fmt.Printf("Tunnel URL: %s\n", tunnel.URL())
	tunnel.Wait()
}

How it works

Under the hood, flared imports the official github.com/cloudflare/cloudflared module and invokes its internal CLI commands directly in your process. It intercepts standard outputs using an in-memory pipe (os.Pipe) to parse necessary information (like the Quick Tunnel URL) securely, with zero disk I/O.

[!WARNING] Binary Size Impact Because flared statically compiles the entire cloudflared core into your application, your final compiled binary size will increase by approximately 30-40 MB. This is the expected trade-off for achieving a seamless, zero-dependency deployment for your users.

Configuration (Options)

  • OriginURL (string): The local service URL to expose (e.g., http://localhost:8080). (Required)
  • Name (string): The stable name of the tunnel. Used alongside Domain to create and route a Named Tunnel.
  • Domain (string): The public hostname to route traffic through (e.g., api.example.com).
  • ShowLog (bool): Toggles the output of internal cloudflared logs to os.Stderr.

Note: If both Name and Domain are omitted, the library defaults to creating a Quick Tunnel.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Name is the stable name to identify the tunnel. Used along with Domain to create, route, and run a tunnel.
	// Requires cloudflare credentials (cert.pem) to be present.
	Name string
	// Domain is the expected public hostname for Named Tunnels (e.g., "app.example.com").
	// If provided alongside Name, it will be used to route traffic.
	Domain string
	// OriginURL is the local service URL to expose (e.g. "http://127.0.0.1:8080").
	OriginURL string
	// ShowLog determines whether cloudflared's internal logs should be printed to os.Stderr.
	ShowLog bool
	// LogWriter is an optional writer to receive cloudflared's internal logs.
	// If nil and ShowLog is false, logs are suppressed.
	LogWriter io.Writer
	// Timeout is the maximum time to wait for a Quick Tunnel URL.
	// Defaults to 15 seconds if zero.
	Timeout time.Duration
}

Options contains the configuration for starting a cloudflared tunnel.

type Tunnel

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

Tunnel represents an active, in-process Cloudflare Tunnel.

func Start

func Start(ctx context.Context, opts Options) (*Tunnel, error)

Start creates and runs a tunnel in-process. It blocks until the tunnel is established.

func (*Tunnel) Close

func (t *Tunnel) Close() error

Close gracefully shuts down the tunnel and stops the internal cloudflared process.

func (*Tunnel) URL

func (t *Tunnel) URL() string

URL returns the public URL where the tunnel is accessible. For Quick Tunnels, this is the generated trycloudflare.com address. For Named Tunnels, this is the Domain provided in Options.

func (*Tunnel) Wait

func (t *Tunnel) Wait() error

Wait blocks until the tunnel is closed or encounters a fatal error.

Directories

Path Synopsis
examples
basic command
named command

Jump to

Keyboard shortcuts

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