share

package
v0.4.111 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 33 Imported by: ? 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 share publishes apps of this box on the internet through the relay.

Index

Constants

View Source
const (
	DefaultACMEDirectory = "https://acme-v02.api.letsencrypt.org/directory"
	TOSURL               = "https://letsencrypt.org/repository/"
)
View Source
const StateFile = "share.json"

StateFile is kept in the fleet's state directory, under the root.

View Source
const TunnelPath = "/v1/share/tunnel"

TunnelPath is the relay route a box dials to publish its apps.

Variables

View Source
var (
	ErrRelayNoShare   = errors.New("this relay does not support public sharing")
	ErrWaitMaster     = errors.New("waiting for the master to register this box at the relay")
	ErrNoName         = errors.New("this box has no public name yet")
	ErrNameTaken      = errors.New("this name is already taken")
	ErrNameReserved   = errors.New("this name is reserved")
	ErrNameRevoked    = errors.New("this name was revoked and cannot be used")
	ErrNameQuarantine = errors.New("this name was released recently and is not available yet")
)
View Source
var (
	ErrTOS        = errors.New("the terms of service of Let's Encrypt must be accepted")
	ErrNameSet    = errors.New("this box already has a public name")
	ErrUnknownApp = errors.New("this app is not installed")
	ErrNoRelay    = errors.New("remote access is off: public sharing goes through its relay")
	ErrNoPort     = errors.New("this app has no web port to share")
)
View Source
var (
	// ErrBadLabel is Label's error.
	ErrBadLabel = errors.New("the app's name cannot be made into a DNS label")
	// ErrBadName is ValidName's error.
	ErrBadName = errors.New("a box name is 3 to 32 lowercase letters, digits or dashes, not starting or ending with a dash")
)
View Source
var Default = New("/")

Default is the box's own; main wires its funcs.

View Source
var ErrGatewayNoShare = errors.New("the gateway on this box does not support public sharing yet")

ErrGatewayNoShare is what the gateway of a box without public sharing gives.

Functions

func Label

func Label(composeName string) (string, error)

Label turns an app's compose name into the DNS label of its host.

func Serve

func Serve(ctx context.Context, s *Session, shareAddr func() (string, error)) int

Serve copies every accepted stream to a fresh TCP connection to the address shareAddr gives at that stream, at most maxStreams at a time (the others are closed at once), until the session ends or ctx is done. It returns the close code.

func ValidName

func ValidName(name string) error

ValidName reports whether name can be the name of a box.

Types

type ACME

type ACME struct {
	Directory         string // "" → CASAOS_SHARE_ACME_DIRECTORY or DefaultACMEDirectory
	CAFile            string // "" → CASAOS_SHARE_ACME_CA; a PEM root replacing the system pool (tests, Pebble)
	AccountFile       string // {"key": "<base64url P-256 PKCS8>"}
	KeyFile, CertFile string
	TXT               TXT
	Now               func() time.Time // nil → time.Now
	// contains filtered or unexported fields
}

ACME gets and renews the wildcard certificate of this box from Let's Encrypt by DNS-01.

func (*ACME) Loop

func (a *ACME) Loop(ctx context.Context, wildcard func() string, tick time.Duration)

Loop checks every tick and calls Obtain when Needs; after an error it waits an hour (measured with Now), doubling up to a day. An empty wildcard() means nothing is shared: idle.

func (*ACME) Needs

func (a *ACME) Needs(wildcard string, now time.Time) bool

Needs is true when there is no certificate for wildcard, or it expires within renewBefore.

func (*ACME) Obtain

func (a *ACME) Obtain(ctx context.Context, host string) (err error)

Obtain gets *.<host>: it writes KeyFile, then CertFile, only once the CA has issued.

func (*ACME) Status

func (a *ACME) Status() CertStatus

func (*ACME) Wake

func (a *ACME) Wake()

Wake makes Loop run its check now rather than at the next tick. It never blocks.

type App

type App struct {
	Port   string `json:"port"`
	Scheme string `json:"scheme"`
}

App is what an app that is shared is reached on.

type AppStatus

type AppStatus struct {
	Name   string `json:"name"`
	Label  string `json:"label"`
	Shared bool   `json:"shared"`
	URL    string `json:"url"`
	Port   string `json:"port"`
	Scheme string `json:"scheme"`
	Error  string `json:"error"`
}

type Apps

type Apps struct{ RuntimePath func() string }

Apps is AppManagement, reached through the runtime directory.

func (Apps) List

func (a Apps) List(ctx context.Context) ([]Installed, error)

List returns the installed apps, sorted by name.

type BusyError

type BusyError struct{ RetryAfter time.Duration }

BusyError is the relay asking to come back later.

func (BusyError) Error

func (e BusyError) Error() string

type CertStatus

type CertStatus struct {
	NotAfter time.Time
	Pending  bool
	Error    string
	Names    []string
}

CertStatus is what the certificate on disk and the last attempt say.

type CertView

type CertView struct {
	NotAfter *time.Time `json:"not_after"`
	Pending  bool       `json:"pending"`
	Error    string     `json:"error"`
}

type DialError

type DialError struct {
	StatusCode int
	RetryAfter time.Duration
	Err        error
}

DialError is a refused upgrade (401, 403, 429, 503...). StatusCode is 0 when the relay did not answer; RetryAfter comes from a 429's Retry-After header, 0 otherwise.

func (*DialError) Error

func (e *DialError) Error() string

func (*DialError) Unwrap

func (e *DialError) Unwrap() error

type ErrLabelTaken

type ErrLabelTaken struct{ Other string }

ErrLabelTaken is a Share refused because another shared app has the same address.

func (ErrLabelTaken) Error

func (e ErrLabelTaken) Error() string

type Gateway

type Gateway struct{ RuntimePath func() string }

Gateway is the box's gateway, reached through the runtime directory.

func (Gateway) DeleteHost

func (g Gateway) DeleteHost(ctx context.Context, host string) error

DeleteHost stops the gateway sending requests for host anywhere.

func (Gateway) SetHost

func (g Gateway) SetHost(ctx context.Context, host, target string) error

SetHost makes the gateway send requests for host to target.

func (Gateway) ShareAddr

func (g Gateway) ShareAddr() (string, error)

ShareAddr is the address the gateway hands the relay's connections to.

type Info

type Info struct {
	Name         string `json:"name"`
	Host         string `json:"host"`
	Domain       string `json:"domain"`
	TunnelOnline bool   `json:"tunnel_online"`
}

Info is a box's public name at the relay.

type Installed

type Installed struct{ Name, Title, Port, Scheme string }

Installed is an installed app, by its compose name.

type Relay

type Relay struct {
	// Signed sends a request signed with this box's key (wired by main to the fleet's relay client).
	Signed func(ctx context.Context, method, path string, body []byte) (status int, reply []byte, retryAfter time.Duration, err error)
	// RegisterSelf links this box's key at the relay (wired to the fleet).
	RegisterSelf func(ctx context.Context) error
}

Relay talks to the relay as this box.

func (*Relay) ClaimName

func (r *Relay) ClaimName(ctx context.Context, name string) (Info, error)

ClaimName asks for name as this box's public name.

func (*Relay) ClearTXT

func (r *Relay) ClearTXT(ctx context.Context) error

ClearTXT removes the ACME challenge record.

func (*Relay) ReleaseName

func (r *Relay) ReleaseName(ctx context.Context) error

ReleaseName gives the public name back (also fine when there was none).

func (*Relay) SetTXT

func (r *Relay) SetTXT(ctx context.Context, value string) error

SetTXT publishes value as the ACME challenge record of this box's name.

func (*Relay) Status

func (r *Relay) Status(ctx context.Context) (Info, error)

Status tells what the relay knows of this box's name and tunnel.

type Service

type Service struct {
	Root        string
	RuntimePath func() string
	Relay       *Relay
	Gateway     Gateway
	Apps        Apps
	ACME        *ACME
	BoxKey      func() (ed25519.PrivateKey, error)
	RelayURL    func() (string, bool)
	Dialer      *websocket.Dialer // nil → Dial's default
	Log         func(msg string, err error)
	Now         func() time.Time
	// contains filtered or unexported fields
}

Service shares the apps of this box: its name at the relay, the Gateway's host routes, the tunnel and the certificate.

func New

func New(root string) *Service

New keeps the state and the certificate under root.

func (*Service) OnAppEvent

func (s *Service) OnAppEvent(name string, properties map[string]string)

OnAppEvent follows AppManagement (Hub.OnAppEvent). It returns at once.

func (*Service) Reset

func (s *Service) Reset(ctx context.Context)

Reset forgets the sharing of a box leaving its relay: the host routes are deleted first (local and fast), then the name is released, best effort, then the tunnel stopped and the state cleared. The certificate stays. It runs while the fleet holds its relay lock: it only calls the relay's Signed and the Gateway.

func (*Service) Run

func (s *Service) Run(ctx context.Context)

Run keeps the host routes, the tunnel and the certificate until ctx is done. Without a name and an app shared, nothing goes out. A second Run while one is under way returns at once.

func (*Service) SetName

func (s *Service) SetName(ctx context.Context, name string, acceptTOS bool) error

SetName claims name at the relay and saves it. The tunnel waits for an app to be shared.

func (*Service) Share

func (s *Service) Share(ctx context.Context, composeName string) error

Share publishes an installed app at https://<label>.<host>.

func (*Service) Status

func (s *Service) Status(ctx context.Context) Status

func (*Service) StopAll

func (s *Service) StopAll(ctx context.Context) error

StopAll unshares every app; the name stays.

func (*Service) Unshare

func (s *Service) Unshare(ctx context.Context, composeName string) error

Unshare stops publishing an app; an app that is not shared is fine.

type Session

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

Session is the box end of a tunnel. The relay opens the streams, so there is no Open: the relay drops the tunnel of a box that opens one.

func Dial

func Dial(ctx context.Context, relayURL string, key ed25519.PrivateKey, d *websocket.Dialer) (*Session, error)

Dial opens the tunnel to relayURL (https://host, or http:// in tests), signing GET TunnelPath with key. d nil means a dialer with a 15 s handshake timeout that honours the proxy environment.

func (*Session) Accept

func (s *Session) Accept() (net.Conn, error)

Accept returns the next visitor stream; it fails once the tunnel is closed.

func (*Session) Close

func (s *Session) Close() error

Close ends the tunnel and its streams.

func (*Session) CloseCode

func (s *Session) CloseCode() int

CloseCode is the code the relay closed the tunnel with: 4403 revoked, 4409 replaced, 1001 shutdown, 0 otherwise (network drop, local close, normal closure).

type State

type State struct {
	Name          string         `json:"name"`
	Host          string         `json:"host"`
	Domain        string         `json:"domain"`
	TOSAcceptedAt time.Time      `json:"tos_accepted_at,omitzero"`
	Apps          map[string]App `json:"apps"`
}

State is what this box shares, as saved in StateFile.

type Status

type Status struct {
	Supported bool        `json:"supported"`
	Reason    string      `json:"reason"` // remote-access, gateway, relay, master
	Name      string      `json:"name"`
	Host      string      `json:"host"`
	Domain    string      `json:"domain"`
	TOSURL    string      `json:"tos_url"`
	Apps      []AppStatus `json:"apps"`
	Cert      CertView    `json:"cert"`
	Tunnel    TunnelView  `json:"tunnel"`
}

Status is what the API answers.

type TXT

type TXT interface {
	SetTXT(ctx context.Context, value string) error
	ClearTXT(ctx context.Context) error
}

TXT publishes the DNS-01 record through the relay (a *Relay does).

type TunnelView

type TunnelView struct {
	Connected bool   `json:"connected"`
	Error     string `json:"error"`
}

Jump to

Keyboard shortcuts

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