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 ¶
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 ¶
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 ¶
Types ¶
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager holds the open shares.
func (*Manager) Close ¶
func (m *Manager) Close()
Close ends every share; the Manager refuses new ones after.
func (*Manager) Start ¶
Start shares the dev server at raw, or returns the share already open for its origin (its idle clock restarted).
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 {
Target string `json:"target"`
Path string `json:"path"`
URL string `json:"url"`
Address string `json:"address"`
// Tunnelled says the address is Tailscale's rather than the Wi-Fi's.
// had the cookie; StartedAt until then.
LastUsedAt time.Time `json:"last_used_at"`
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 ¶
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.