tunnel

package
v0.12.21 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package tunnel provides management for tunnel services like Cloudflare and ngrok.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTunnelExists is returned when trying to create a tunnel with an existing ID.
	ErrTunnelExists = errors.New("tunnel already exists")
	// ErrTunnelNotFound is returned when a tunnel ID is not found.
	ErrTunnelNotFound = errors.New("tunnel not found")
	// ErrTunnelAmbiguous is returned when a fuzzy lookup matches multiple tunnels.
	ErrTunnelAmbiguous = errors.New("tunnel ID is ambiguous - multiple matches")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Provider   Provider
	LocalPort  int
	LocalHost  string // defaults to "localhost"
	BinaryPath string // optional: path to tunnel binary, otherwise uses PATH
	ID         string // tunnel identifier
	Path       string // project path for session scoping
}

Config holds tunnel configuration.

type Manager

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

Manager manages tunnel instances.

func NewManager

func NewManager() *Manager

NewManager creates a new tunnel manager.

func (*Manager) ActiveCount

func (m *Manager) ActiveCount() int

ActiveCount returns the number of active tunnels.

func (*Manager) Get

func (m *Manager) Get(id string) (*Tunnel, error)

Get returns a tunnel by ID with fuzzy matching support. First tries exact match, then looks for tunnels where the ID contains the search string as a component (for compound IDs).

func (*Manager) GetWithPathFilter added in v0.8.0

func (m *Manager) GetWithPathFilter(id, pathFilter string) (*Tunnel, error)

GetWithPathFilter retrieves a tunnel by ID with fuzzy matching, filtered by path. If pathFilter is non-empty, only tunnels with matching Path are considered for fuzzy lookup. Exact matches are always returned regardless of path filter.

func (*Manager) List

func (m *Manager) List() []TunnelInfo

List returns information about all tunnels.

func (*Manager) ListByPath added in v0.8.0

func (m *Manager) ListByPath(pathFilter string) []TunnelInfo

ListByPath returns tunnels filtered by project path. If pathFilter is empty, returns all tunnels.

func (*Manager) Shutdown

func (m *Manager) Shutdown(ctx context.Context) error

Shutdown stops all tunnels.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context, id string, config Config) (*Tunnel, error)

Start starts a tunnel for the given proxy.

func (*Manager) Stop

func (m *Manager) Stop(ctx context.Context, id string) error

Stop stops a tunnel by ID.

func (*Manager) StopAll added in v0.7.6

func (m *Manager) StopAll(ctx context.Context) error

StopAll stops all running tunnels. Unlike Shutdown, this does NOT set shuttingDown flag, allowing new tunnels to be started afterward. This is used for cleanup when the last client disconnects.

func (*Manager) StopByProjectPath added in v0.8.0

func (m *Manager) StopByProjectPath(ctx context.Context, projectPath string) ([]string, error)

StopByProjectPath stops all tunnels for a specific project path. This is used for session-scoped cleanup when a client disconnects. Returns the list of stopped tunnel IDs.

type Provider

type Provider string

Provider represents a tunnel service provider.

const (
	// ProviderCloudflare uses cloudflared for Cloudflare Quick Tunnels.
	ProviderCloudflare Provider = "cloudflare"
	// ProviderNgrok uses ngrok for tunneling.
	ProviderNgrok Provider = "ngrok"
)

type State

type State uint32

State represents the tunnel state.

const (
	StateIdle State = iota
	StateStarting
	StateConnected
	StateFailed
	StateStopped
)

func (State) String

func (s State) String() string

type Tunnel

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

Tunnel represents a running tunnel instance.

func New

func New(config Config) *Tunnel

New creates a new tunnel with the given configuration.

func (*Tunnel) Done

func (t *Tunnel) Done() <-chan struct{}

Done returns a channel that's closed when the tunnel exits.

func (*Tunnel) ID added in v0.8.0

func (t *Tunnel) ID() string

ID returns the tunnel ID.

func (*Tunnel) Info

func (t *Tunnel) Info() TunnelInfo

Info returns information about the tunnel.

func (*Tunnel) OnURL

func (t *Tunnel) OnURL(fn func(url string))

OnURL sets a callback that's invoked when the public URL is discovered.

func (*Tunnel) Path added in v0.8.0

func (t *Tunnel) Path() string

Path returns the project path for this tunnel.

func (*Tunnel) PublicURL

func (t *Tunnel) PublicURL() string

PublicURL returns the public URL if available.

func (*Tunnel) Start

func (t *Tunnel) Start(ctx context.Context) error

Start starts the tunnel and returns immediately. Use WaitForURL to wait for the public URL to be available.

func (*Tunnel) State

func (t *Tunnel) State() State

State returns the current tunnel state.

func (*Tunnel) Stop

func (t *Tunnel) Stop(ctx context.Context) error

Stop stops the tunnel.

func (*Tunnel) WaitForURL

func (t *Tunnel) WaitForURL(ctx context.Context) (string, error)

WaitForURL waits for the public URL to be available or timeout.

type TunnelInfo

type TunnelInfo struct {
	ID        string   `json:"id"`
	Provider  Provider `json:"provider"`
	State     string   `json:"state"`
	PublicURL string   `json:"public_url,omitempty"`
	LocalAddr string   `json:"local_addr"`
	Path      string   `json:"path,omitempty"`
	Error     string   `json:"error,omitempty"`
}

TunnelInfo contains information about a tunnel.

Jump to

Keyboard shortcuts

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