devshare

package
v0.189.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: Apache-2.0 Imports: 23 Imported by: 0 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 devshare opens a local dev server on a phone on the same Wi-Fi (ADR-087): a reverse proxy for one loopback origin, on a listener of its own, bound to one private address of this machine on a random port, behind an unguessable link.

A dev server listens on localhost, so a phone cannot reach it. The usual fix (`--host`, 0.0.0.0) puts it on every interface with no guard. A share is the opposite: one named address, one origin, a token, and an end — Stop sharing, 30 minutes without the phone, 8 hours at most, or the daemon stopping. Nothing here is remembered across a restart.

What a request may do is fixed when the share starts. The scheme, host and port forwarded to are the chosen origin's; a request names a path and nothing else, the dialer reaches only loopback, and no environment proxy is used. The first request opens /__caprock/open/<token>, which sets a cookie and redirects to the page; every request after that needs the cookie.

Index

Constants

View Source
const (
	DefaultIdle   = 30 * time.Minute
	DefaultMaxAge = 8 * time.Hour
	DefaultMax    = 3
	// OpenPrefix is the path of the link the QR carries, before the token.
	OpenPrefix = "/__caprock/open/"
	// ViaMark is put on every forwarded request, so a share that points at a
	// Caprock port is a stranger to its gate (internal/api lanauth).
	ViaMark = "1.1 caprock-share"
)

Defaults for a share's life and how many may be open at once.

Variables

View Source
var (
	ErrNotLocal  = errors.New("only a local dev server's address can be opened on a phone: localhost, *.localhost, 127.x or [::1]")
	ErrOwnPort   = errors.New("that is Caprock's own port, which is never shared")
	ErrTooMany   = errors.New("three pages are shared already: stop one first")
	ErrNoAddress = errors.New("this computer has no address on a local network: is Wi-Fi on?")
	ErrClosed    = errors.New("sharing is shut down")
)

Errors a caller tells apart.

Functions

func PickAddress

func PickAddress() (net.IP, error)

PickAddress is the address a phone on the same Wi-Fi reaches: the first private (RFC 1918) one, else a Tailscale one. Never 0.0.0.0.

Types

type Manager

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

Manager holds the open shares.

func New

func New(opt Options) *Manager

New returns a Manager with nothing shared.

func (*Manager) Close

func (m *Manager) Close()

Close ends every share; the Manager refuses new ones after.

func (*Manager) Idle

func (m *Manager) Idle() time.Duration

Idle is how long a share lasts without a phone.

func (*Manager) List

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

List returns the open shares, oldest first.

func (*Manager) Max

func (m *Manager) Max() int

Max is how many shares may be open at once.

func (*Manager) MaxAge

func (m *Manager) MaxAge() time.Duration

MaxAge is the longest a share lasts.

func (*Manager) Start

func (m *Manager) Start(raw string) (Share, error)

Start shares the dev server at raw, or returns the share already open for its origin (its idle clock restarted).

func (*Manager) Stop

func (m *Manager) Stop(id string) bool

Stop ends one share; false when there was none by that id.

func (*Manager) Sweep

func (m *Manager) Sweep() []string

Sweep ends every share past its idle time or its longest life. The loop calls it every Options.Sweep; tests call it after moving the clock.

type Options

type Options struct {
	// Address returns the address to bind. Default: PickAddress.
	Address func() (net.IP, error)
	// Forbid reports whether a target port must never be shared (the
	// daemon's own).
	Forbid func(port int) bool
	Idle   time.Duration
	MaxAge time.Duration
	Max    int
	Log    *slog.Logger
	// Now is the clock; tests move it.
	Now func() time.Time
	// Sweep is how often expiry is checked. Default: a minute.
	Sweep time.Duration
}

Options configure a Manager. Zero values are the defaults.

type Share

type Share struct {
	ID string `json:"id"`
	// Target is the origin forwarded to, e.g. http://localhost:5173.
	Target string `json:"target"`
	// Path is where the link lands: the page the tab showed.
	Path string `json:"path"`
	// URL is the link for the phone, token included. Loopback eyes only.
	URL string `json:"url"`
	// Address is the host:port the phone reaches, without the token.
	Address string `json:"address"`
	// Tunnelled says the address is Tailscale's rather than the Wi-Fi's.
	Tunnelled bool      `json:"tunnelled,omitempty"`
	StartedAt time.Time `json:"started_at"`
	// LastUsedAt is the last request or WebSocket traffic from a phone that
	// had the cookie; StartedAt until then.
	LastUsedAt time.Time `json:"last_used_at"`
	// ExpiresAt is when the share ends by itself if nothing happens.
	ExpiresAt time.Time `json:"expires_at"`
}

Share is one origin opened on the network, as the dashboard sees it.

type Target

type Target struct {
	Origin *url.URL // scheme://host:port, no path
	Path   string   // path and query to land on, "/" at least
	Port   int
}

Target is a parsed local origin and the path to land on.

func ParseTarget

func ParseTarget(raw string) (Target, error)

ParseTarget accepts a local dev server's address by devservers.ts's rules: http(s) on localhost, *.localhost, 127.x or [::1]; 0.0.0.0 and [::] become localhost. Anything else — another host, a LAN address, user info, another scheme — is ErrNotLocal.

Jump to

Keyboard shortcuts

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