Documentation
¶
Overview ¶
Package share publishes apps of this box on the internet through the relay.
Index ¶
- Constants
- Variables
- func CodeHash(secret []byte, code string) string
- func Label(composeName string) (string, error)
- func NewCode() string
- func NormalizeCode(s string) string
- func Serve(ctx context.Context, s *Session, shareAddr func() (string, error)) int
- func ValidName(name string) error
- type ACME
- type App
- type AppStatus
- type Apps
- type BanReport
- type BanRules
- type BusyError
- type CertStatus
- type CertView
- type Countries
- type DialError
- type ErrGuard
- type ErrLabelTaken
- type Gateway
- func (g Gateway) DeleteHost(ctx context.Context, host string) error
- func (g Gateway) GuardBans(ctx context.Context) (json.RawMessage, error)
- func (g Gateway) GuardEvents(ctx context.Context, app, reason string, limit int) (json.RawMessage, error)
- func (g Gateway) GuardStatus(ctx context.Context) (GuardGatewayStatus, bool, error)
- func (g Gateway) Hosts(ctx context.Context) ([]string, error)
- func (g Gateway) SetGuard(ctx context.Context, cfg GatewayGuardConfig) error
- func (g Gateway) SetHost(ctx context.Context, host, target string) error
- func (g Gateway) ShareAddr() (string, error)
- func (g Gateway) Unban(ctx context.Context, key string) error
- type GatewayGuardApp
- type GatewayGuardConfig
- type GatewayPerson
- type GeoFetcher
- type GeoStatus
- type GuardApp
- type GuardAppView
- type GuardGatewayStatus
- type GuardGeo
- type GuardRelay
- type GuardState
- type GuardView
- type GuardWave
- type Info
- type Installed
- type Person
- type PersonView
- type Policy
- type PolicyOverride
- type Relay
- func (r *Relay) ClaimName(ctx context.Context, name string) (Info, error)
- func (r *Relay) ClearTXT(ctx context.Context) error
- func (r *Relay) Refusals(ctx context.Context) (json.RawMessage, error)
- func (r *Relay) ReleaseName(ctx context.Context) error
- func (r *Relay) ReportBans(ctx context.Context, bans []BanReport) error
- func (r *Relay) SetProtection(ctx context.Context, filter, shareBans bool) (RelayProtection, error)
- func (r *Relay) SetTXT(ctx context.Context, value string) error
- func (r *Relay) Status(ctx context.Context) (Info, error)
- type RelayProtection
- type Service
- func (s *Service) AddPerson(ctx context.Context, name string, apps []string) (PersonView, string, error)
- func (s *Service) DeletePerson(ctx context.Context, id string) error
- func (s *Service) GuardBans(ctx context.Context) (json.RawMessage, error)
- func (s *Service) GuardEvents(ctx context.Context, app, reason string, limit int) (json.RawMessage, error)
- func (s *Service) GuardReset() bool
- func (s *Service) GuardView(ctx context.Context) (GuardView, error)
- func (s *Service) NewPersonCode(ctx context.Context, id string) (string, error)
- func (s *Service) OnAppEvent(name string, properties map[string]string)
- func (s *Service) RelayRefusals(ctx context.Context) (json.RawMessage, error)
- func (s *Service) Reset(ctx context.Context)
- func (s *Service) Run(ctx context.Context)
- func (s *Service) SetAppGuard(ctx context.Context, name, access string, o *PolicyOverride) (GuardView, error)
- func (s *Service) SetBoxPolicy(ctx context.Context, p Policy) (GuardView, error)
- func (s *Service) SetName(ctx context.Context, name string, acceptTOS bool) error
- func (s *Service) SetProtection(ctx context.Context, on bool) (GuardView, error)
- func (s *Service) SetRelay(ctx context.Context, filter, shareBans *bool, consent bool) (GuardView, error)
- func (s *Service) Share(ctx context.Context, composeName string) error
- func (s *Service) Status(ctx context.Context) Status
- func (s *Service) StopAll(ctx context.Context) error
- func (s *Service) Unban(ctx context.Context, key string) error
- func (s *Service) Unshare(ctx context.Context, composeName string) error
- func (s *Service) UpdatePerson(ctx context.Context, id, name string, apps []string) (PersonView, error)
- type Session
- type State
- type Status
- type TXT
- type TunnelView
Constants ¶
const ( DefaultACMEDirectory = "https://acme-v02.api.letsencrypt.org/directory" TOSURL = "https://letsencrypt.org/repository/" )
const GuardFile = "share-guard.json"
GuardFile is kept next to StateFile: the protection settings, the people and the secret.
const StateFile = "share.json"
StateFile is kept in the fleet's state directory, under the root.
const TunnelPath = "/v1/share/tunnel"
TunnelPath is the relay route a box dials to publish its apps.
Variables ¶
var ( 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") // ErrRelayRejected is a 400: the relay will never accept this request as sent. ErrRelayRejected = errors.New("the relay refused") )
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") )
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") )
var Default = New("/")
Default is the box's own; main wires its funcs.
var ErrGatewayNoGuard = errors.New("the gateway on this box cannot protect shared apps")
ErrGatewayNoGuard is what a Gateway without the protection of shared apps gives: a 404 on /v1/gateway/guard.
ErrGatewayNoShare is what the gateway of a box without public sharing gives.
var ErrGuardPush = errors.New("the change is saved, but the Gateway has not taken it yet: it will be sent again")
ErrGuardPush is a change that is saved but that the Gateway has not taken: the resync pushes it again.
var ErrRelayPush = errors.New("the change is saved, but the relay has not taken it yet: it will be sent again")
ErrRelayPush is a relay setting that is saved but that the relay has not taken: the resync sends it again.
Functions ¶
func CodeHash ¶ added in v0.4.114
CodeHash is the lowercase hex HMAC-SHA256 of the normalised code under secret.
func NewCode ¶ added in v0.4.114
func NewCode() string
NewCode is 12 random symbols shown as XXXX-XXXX-XXXX. Bytes of 248 and up are redrawn (248 = 8 x 31), so the modulo is unbiased.
func NormalizeCode ¶ added in v0.4.114
NormalizeCode uppercases ASCII letters and drops every character outside the code alphabet, like the Gateway.
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 ¶
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 ¶
Needs is true when there is no certificate for wildcard, or it expires within renewBefore.
func (*ACME) Obtain ¶
Obtain gets *.<host>: it writes KeyFile, then CertFile, only once the CA has issued.
func (*ACME) Status ¶
func (a *ACME) Status() CertStatus
type Apps ¶
type Apps struct{ RuntimePath func() string }
Apps is AppManagement, reached through the runtime directory.
type BanReport ¶ added in v0.4.115
type BanReport struct {
Key string `json:"key"`
Reason string `json:"reason"`
App string `json:"app"`
Since time.Time `json:"since"`
}
BanReport is one ban of this box's Gateway, as sent to the relay.
type BanRules ¶ added in v0.4.114
type BanRules struct {
WAFHits int `json:"waf_hits"`
WAFWindowS int `json:"waf_window_s"`
Scan404 int `json:"scan_404"`
ScanWindowS int `json:"scan_window_s"`
RateStrikes int `json:"rate_strikes"`
RateWindowS int `json:"rate_window_s"`
CodeFails int `json:"code_fails"`
CodeWindowS int `json:"code_window_s"`
}
type CertStatus ¶
CertStatus is what the certificate on disk and the last attempt say.
type DialError ¶
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.
type ErrGuard ¶ added in v0.4.114
type ErrGuard struct{ Code string }
ErrGuard is a refusal the contract names: gateway_unsupported, bad_name, not_shared, not_found, unknown_app:<name>, bad_policy:<field>. Its message is the code.
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 ¶
DeleteHost stops the gateway sending requests for host anywhere.
func (Gateway) GuardEvents ¶ added in v0.4.114
func (g Gateway) GuardEvents(ctx context.Context, app, reason string, limit int) (json.RawMessage, error)
GuardEvents is the Gateway's journal, newest first; empty filters and limit 0 are left out.
func (Gateway) GuardStatus ¶ added in v0.4.114
GuardStatus is the Gateway's view of the protection; supported is false only when it has no guard route.
func (Gateway) SetGuard ¶ added in v0.4.114
func (g Gateway) SetGuard(ctx context.Context, cfg GatewayGuardConfig) error
SetGuard replaces the Gateway's whole protection config.
type GatewayGuardApp ¶ added in v0.4.114
type GatewayGuardConfig ¶ added in v0.4.114
type GatewayGuardConfig struct {
Secret []byte `json:"secret"` // base64 std in JSON
Protection bool `json:"protection"`
Apps map[string]GatewayGuardApp `json:"apps"`
People []GatewayPerson `json:"people"`
}
GatewayGuardConfig is the body of PUT /v1/gateway/guard; apps are keyed by the host registered for them.
type GatewayPerson ¶ added in v0.4.114
type GatewayPerson struct {
ID string `json:"id"`
Gen int `json:"gen"`
CodeHash string `json:"code_hash"`
Apps []string `json:"apps"`
}
GatewayPerson is what the Gateway needs of a person: no name, no dates.
type GeoFetcher ¶ added in v0.4.114
type GeoFetcher struct {
Dir string
BaseURL string
Client *http.Client
Now func() time.Time
// contains filtered or unexported fields
}
GeoFetcher keeps DB-IP Lite's country and network files current. The Gateway memory-maps them, so a file is only ever replaced by renaming a finished one over it.
func (*GeoFetcher) Stale ¶ added in v0.4.114
func (f *GeoFetcher) Stale() bool
Stale is whether a file is missing or older than 32 days.
func (*GeoFetcher) Status ¶ added in v0.4.114
func (f *GeoFetcher) Status() GeoStatus
Status reads the files' state; a missing file is false.
type GeoStatus ¶ added in v0.4.114
type GeoStatus struct {
Country, ASN bool
UpdatedAt time.Time // the older of the two files; zero when none
Error string
}
GeoStatus is what the files on disk are, and why the last update failed ("" when it did not).
type GuardApp ¶ added in v0.4.114
type GuardApp struct {
Access string `json:"access"` // "open" or "code"
Override *PolicyOverride `json:"override"`
}
type GuardAppView ¶ added in v0.4.114
type GuardAppView struct {
Access string `json:"access"`
Override *PolicyOverride `json:"override"`
Effective Policy `json:"effective"`
}
type GuardGatewayStatus ¶ added in v0.4.114
type GuardGatewayStatus struct {
Version int `json:"version"`
Configured bool `json:"configured"`
Geo struct {
Country bool `json:"country"`
ASN bool `json:"asn"`
} `json:"geo"`
PeopleLastUsed map[string]time.Time `json:"people_last_used"`
Waves []GuardWave `json:"waves"`
}
GuardGatewayStatus is the body of GET /v1/gateway/guard.
type GuardRelay ¶ added in v0.4.115
type GuardRelay struct {
Supported bool `json:"supported"`
Filter bool `json:"filter"`
CommunitySize int `json:"community_size"`
ContributedMonth int `json:"contributed_month"`
}
GuardRelay is the relay's part of the protection: the settings are the owner's (the file), the numbers the relay's last answer, zero while it has none or has no such routes.
type GuardState ¶ added in v0.4.114
type GuardState struct {
Secret []byte `json:"secret"`
Protection bool `json:"protection"`
Box Policy `json:"box"`
Apps map[string]GuardApp `json:"apps"`
People []Person `json:"people"`
ResetAt time.Time `json:"reset_at,omitzero"` // the file was found unusable and reset; cleared by the owner's next write the Gateway takes
RelayFilter *bool `json:"relay_filter,omitempty"` // the relay refuses known-bad addresses at the door; nil (an old file) is on
}
type GuardView ¶ added in v0.4.114
type GuardView struct {
Supported bool `json:"supported"`
Reason string `json:"reason"`
Reset bool `json:"reset"`
Protection bool `json:"protection"`
Box Policy `json:"box"`
Apps map[string]GuardAppView `json:"apps"` // the shared apps only
People []PersonView `json:"people"`
Geo GuardGeo `json:"geo"`
Waves []GuardWave `json:"waves"`
Relay GuardRelay `json:"relay"`
}
GuardView is the body of GET /v1/share/guard.
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 Person ¶ added in v0.4.114
type Person struct {
ID string `json:"id"`
Name string `json:"name"`
Apps []string `json:"apps"`
CodeHash string `json:"code_hash"`
Gen int `json:"gen"`
CreatedAt time.Time `json:"created_at"`
}
Person holds a hash of the access code, never the code.
type PersonView ¶ added in v0.4.114
type PersonView struct {
ID string `json:"id"`
Name string `json:"name"`
Apps []string `json:"apps"`
CreatedAt time.Time `json:"created_at"`
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
}
PersonView is a person without the hash of the code.
type Policy ¶ added in v0.4.114
type Policy struct {
WAF string `json:"waf"` // "off", "observe", "block"
RulesOff []int `json:"rules_off"`
Allow []string `json:"allow"`
Block []string `json:"block"`
Countries Countries `json:"countries"`
RatePerMin int `json:"rate_per_min"`
Ban BanRules `json:"ban"`
}
Policy, Countries and BanRules have the JSON shape of the Gateway's pkg/guard config.
func DefaultPolicy ¶ added in v0.4.114
func DefaultPolicy() Policy
func Effective ¶ added in v0.4.114
func Effective(box Policy, o *PolicyOverride) Policy
Effective is the policy an app runs under: the box's, with the override's lists added and its other set fields replacing.
type PolicyOverride ¶ added in v0.4.114
type PolicyOverride struct {
WAF *string `json:"waf"`
RulesOff []int `json:"rules_off"`
Allow []string `json:"allow"`
Block []string `json:"block"`
Countries *Countries `json:"countries"`
RatePerMin *int `json:"rate_per_min"`
Ban *BanRules `json:"ban"`
}
PolicyOverride is an app's own settings: nil follows the box; the lists add to the box's, the rest replace.
type Relay ¶
type Relay struct {
// Signed sends a request signed with this box's key (wired by main to the fleet's relay client), keeping at most
// maxReply bytes of the answer (0: the client's 64 KiB).
Signed func(ctx context.Context, method, path string, body []byte, maxReply int) (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
// contains filtered or unexported fields
}
Relay talks to the relay as this box.
func (*Relay) Refusals ¶ added in v0.4.115
Refusals returns the relay's refusals of this box's name over the last 7 days, as the relay wrote them.
func (*Relay) ReleaseName ¶
ReleaseName gives the public name back (also fine when there was none).
func (*Relay) ReportBans ¶ added in v0.4.115
ReportBans sends the bans this box's Gateway started; ErrRelayRejected means drop the batch.
func (*Relay) SetProtection ¶ added in v0.4.115
SetProtection pushes the two protection settings to the relay.
type RelayProtection ¶ added in v0.4.115
type RelayProtection struct {
Filter bool `json:"relay_filter"`
CommunitySize int `json:"community_size"`
ContributedMonth int `json:"contributed_month"`
}
RelayProtection is what the relay holds of this box's protection settings.
type Service ¶
type Service struct {
Root string
RuntimePath func() string
Relay *Relay
Gateway Gateway
Apps Apps
ACME *ACME
Geo *GeoFetcher
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
Alert func(key, sentence string, problem bool) // nil: no alerts; pkg/alerts' ShareGuardProblem
// 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 (*Service) AddPerson ¶ added in v0.4.114
func (s *Service) AddPerson(ctx context.Context, name string, apps []string) (PersonView, string, error)
AddPerson adds a person with a first code, which this answer is the only place to show.
func (*Service) DeletePerson ¶ added in v0.4.114
func (*Service) GuardEvents ¶ added in v0.4.114
func (s *Service) GuardEvents(ctx context.Context, app, reason string, limit int) (json.RawMessage, error)
GuardEvents is the Gateway's journal, newest first, filtered by compose name and reason.
func (*Service) GuardReset ¶ added in v0.4.114
GuardReset is whether share-guard.json was found unusable and reset, until the owner's next write the Gateway takes.
func (*Service) GuardView ¶ added in v0.4.114
GuardView is the protection as the dashboard shows it. It takes no ops lock; a Gateway that cannot be asked gives the view without its last uses and waves. A guard file that cannot be read is an error, not a view of the placeholder.
func (*Service) NewPersonCode ¶ added in v0.4.114
NewPersonCode replaces a person's code: the old one stops working, and the new one is shown here only.
func (*Service) OnAppEvent ¶
OnAppEvent follows AppManagement (Hub.OnAppEvent). It returns at once.
func (*Service) RelayRefusals ¶ added in v0.4.115
RelayRefusals is what the relay refused at its door for this box's name over a week; a relay without the route gives [].
func (*Service) Reset ¶
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. Ban sharing ends with the relay it was agreed for. It runs while the fleet holds its relay lock: it only calls the relay's Signed and the Gateway. It does not wait for the operation under way, which may register a host after these deletes: the resync that clear wakes deletes what is left (sweep).
func (*Service) Run ¶
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) SetAppGuard ¶ added in v0.4.114
func (s *Service) SetAppGuard(ctx context.Context, name, access string, o *PolicyOverride) (GuardView, error)
SetAppGuard sets who may enter a shared app ("open" or "code") and its own settings (nil: follow the box).
func (*Service) SetBoxPolicy ¶ added in v0.4.114
SetBoxPolicy replaces the box's policy; absent lists are empty ones.
func (*Service) SetName ¶
SetName claims name at the relay and saves it. The tunnel waits for an app to be shared.
func (*Service) SetProtection ¶ added in v0.4.114
func (*Service) SetRelay ¶ added in v0.4.115
func (s *Service) SetRelay(ctx context.Context, filter, shareBans *bool, consent bool) (GuardView, error)
SetRelay changes the relay's two settings: filtering, and sharing this box's bans (which needs the owner's consent). A nil one is left as it is. The change is saved, then sent to the relay at once; a relay without the routes (too old) undoes it, and one that cannot take it yet (failing, or waiting for the master) leaves it for the resync to send.
func (*Service) Unban ¶ added in v0.4.114
Unban lifts the ban on key, as the router gave it (unescaped once): the Gateway call escapes it again.
func (*Service) UpdatePerson ¶ added in v0.4.114
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.
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"`
Revoked bool `json:"revoked,omitempty"` // the relay closed the tunnel with 4403: no dial until a Share
}
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).