Documentation
¶
Overview ¶
Package directaccess is the home's side of direct remote access (Epic 50, story 50.15): it asks the router to open a port (UPnP-IGD, NAT-PMP, then PCP), notices a shared address (CGNAT), keeps the home's key and certificate for *.<home id>.<its domain>, serves the same web and API as the LAN port over TLS on the direct listener, and tells the cloud through the relay agent (POST /admin/cloud-link/direct). Its three loops are the cloud.direct component's: direct_listener, direct_portmap and direct_cert.
Index ¶
- Constants
- Variables
- func HomeID(serverID string) string
- func MapFirst(ctx context.Context, mappers []Mapper, internal, want int) (Mapping, Mapper, error)
- func ParsePort(s string) (int, error)
- func RenewDue(c *tls.Certificate, n Name, now time.Time) bool
- func Shared(ip netip.Addr) bool
- type Agent
- type AgentRequest
- type AgentStatus
- type Check
- type Endpoints
- type Files
- type Mapper
- type Mapping
- type NATPMP
- type Name
- type PCP
- type Routes
- type Service
- type Settings
- type Status
- type UPnP
Constants ¶
const ( Lease = time.Hour RenewEvery = 20 * time.Minute )
Lease is how long a mapping is asked for; it is renewed every RenewEvery.
const ( StateOn = "on" StateOff = "off" StateCGNAT = "cgnat" StateMappingFailed = "mapping_failed" )
States the home reports.
const ServerIDHeader = "X-Maktaba-Server-Id"
ServerIDHeader is how the direct listener says which home it is: /api/health answers it with the home's cloud server_id, and the cloud's outside check compares it.
const ViaHeader = "X-Maktaba-Via"
ViaHeader marks a request that came in on the direct listener (the internet), as the relay agent marks its own with "relay": the API refuses the machines' routes to it and never takes it for a program on this machine.
Variables ¶
var ErrNoGateway = errors.New("directaccess: no router answered (UPnP, NAT-PMP, PCP)")
ErrNoGateway: no router answered.
Functions ¶
func HomeID ¶
HomeID is the home's stable direct-name label: the first 32 hex characters of SHA-256("direct-v1" ‖ server_id) (the cloud's direct.HomeID).
func MapFirst ¶
MapFirst asks each mapper in order (UPnP-IGD, NAT-PMP, PCP) and answers the first mapping made, asking for want first (the port the home had) and the router's choice after.
func RenewDue ¶
RenewDue says whether c should be renewed now: at two thirds of its life, or once it no longer names n.
Types ¶
type Agent ¶
Agent is the relay agent's loopback admin port (MAKTABA_CLOUDLINK_ADMIN).
type AgentRequest ¶
type AgentRequest struct {
Endpoints *Endpoints `json:"endpoints,omitempty"`
AskName bool `json:"ask_name,omitempty"`
CSR []byte `json:"csr,omitempty"`
CheckNow bool `json:"check_now,omitempty"`
}
AgentRequest is what the home tells the cloud through the agent.
type AgentStatus ¶
type AgentStatus struct {
Online bool `json:"online"`
Domain string `json:"domain"`
HomeID string `json:"home_id"`
Chain string `json:"chain"`
Check *Check `json:"check"`
Pending bool `json:"pending"`
}
AgentStatus is what the relay agent last heard from the cloud (GET /admin/cloud-link/direct).
type Check ¶
type Check struct {
Result string `json:"result"` // ok, closed, timeout, wrong_home, cgnat
Port int `json:"port"`
At time.Time `json:"at"`
}
Check is the outside check's result as the cloud sent it.
type Endpoints ¶
type Endpoints struct {
Port int `json:"port"`
State string `json:"state"` // on, off, cgnat, mapping_failed
LAN []string `json:"lan,omitempty"`
External string `json:"external,omitempty"`
}
Endpoints is the home's direct listener as reported to the cloud (META_ENDPOINTS).
type Files ¶
type Files struct {
Dir string
// contains filtered or unexported fields
}
Files keeps the home's key, its name and its chain in a directory of its own (0700): key.pem (0600, never sent anywhere), name.json and chain.pem. The last good chain is kept when a renewal fails.
func (*Files) Cert ¶
func (f *Files) Cert() *tls.Certificate
Cert is the certificate the listener presents (nil: none yet).
type Mapper ¶
type Mapper interface {
Name() string
// Map opens external (0: the router picks) to internal on this machine for lifetime.
Map(ctx context.Context, internal, external int, lifetime time.Duration) (Mapping, error)
// Unmap closes it.
Unmap(ctx context.Context, m Mapping) error
}
Mapper is one way of asking the router.
type Mapping ¶
type Mapping struct {
By string // upnp, natpmp, pcp
Internal int
External int
ExternalIP netip.Addr // the router's external address, when it said
Lifetime time.Duration
}
Mapping is a port the router opened to this home.
type NATPMP ¶
type NATPMP struct {
Gateway netip.AddrPort // the router, port 5351
// Tries is how many times a request is sent (250 ms, doubling); default 4.
Tries int
}
NATPMP asks the gateway by NAT-PMP (RFC 6886) on UDP 5351.
func (*NATPMP) ExternalAddress ¶
ExternalAddress asks the router's external IPv4 address.
type PCP ¶
PCP asks the gateway by the Port Control Protocol (RFC 6887) on UDP 5351: a MAP request for TCP, from this machine's address on the way to the router.
type Routes ¶
type Routes struct {
Service *Service
}
Routes serves the settings page's direct access (admins only):
GET /api/direct Status: the state, the public port, the name, the outside check POST /api/direct/check ask the cloud to check from outside now (202)
The switch, the mapping and the manual port are rules.direct (PATCH /api/rules/direct).
type Service ¶
type Service struct {
Files *Files
Agent *Agent
Listen string // the direct listener (toml direct.listen, default :8444)
Upstream *url.URL // this machine's web port on loopback (the same pages and API as the LAN)
// ServerID is the home's cloud server id ("" not claimed yet).
ServerID func() string
Settings func(ctx context.Context) (Settings, error)
// Mappers are asked in order; nil: UPnP-IGD, then NAT-PMP and PCP at the default gateway.
Mappers []Mapper
// LAN lists the listener's LAN addresses (ip:port); nil: this machine's private IPv4s.
LAN func(port int) []string
Log *slog.Logger
Poll time.Duration // how often the settings are read again; default 30 s
Now func() time.Time
// contains filtered or unexported fields
}
Service is the home's direct access: three loops (RunListener, RunPortmap, RunCert) and what the settings page reads.
func (*Service) Handler ¶
Handler is the direct listener's handler: this machine's web port, as a request from the internet (ViaHeader direct, forwarding headers the client sent dropped), with /api/health naming the home.
func (*Service) RunCert ¶
RunCert keeps the home's certificate while direct access is on (loop direct_cert): it asks the cloud its name, sends a CSR for it (the key stays here) and keeps the chain the cloud returns, renewing at two thirds of its life; it keeps the outside check's result.
func (*Service) RunListener ¶
RunListener serves the direct listener while direct access is on (loop direct_listener). It presents the home's certificate; until there is one, handshakes fail.
func (*Service) RunPortmap ¶
RunPortmap keeps the port open on the router while direct access is on (loop direct_portmap): at start and every 20 minutes, sooner when the settings change, and tells the cloud the result. Stopping it removes the mapping and tells the cloud direct is off.
type Status ¶
type Status struct {
Enabled bool `json:"enabled"`
State string `json:"state"`
Port int `json:"port,omitempty"` // the public port (mapped or forwarded by hand)
Internal int `json:"internal,omitempty"` // the direct listener's port on this machine
Listening bool `json:"listening"`
By string `json:"by,omitempty"` // upnp, natpmp, pcp, manual
ExternalIP string `json:"external_ip,omitempty"`
Error string `json:"error,omitempty"`
Name string `json:"name,omitempty"` // *.<home id>.<domain>
NotAfter time.Time `json:"not_after,omitzero"`
Check *Check `json:"check,omitempty"`
// LANURLs are the listener's LAN names, https://<ip label>.<home id>.<domain>:<port>, the
// first entries apps on this network try (story 50.15 §3). The settings page tries them
// from the viewer's browser: a name that doesn't answer while the page itself came over
// the LAN is a router blocking DNS rebinding. Set once the listener has a certificate.
LANURLs []string `json:"lan_urls,omitempty"`
// Online: the relay agent has a tunnel to a cloud with direct access.
Online bool `json:"online"`
}
Status is what the home's settings show.
type UPnP ¶
type UPnP struct {
// SSDP is where the M-SEARCH goes; default the multicast group 239.255.255.250:1900.
SSDP string
// Wait is how long answers are awaited; default 2 s.
Wait time.Duration
Client *http.Client
// contains filtered or unexported fields
}
UPnP asks an Internet Gateway Device (UPnP-IGD v1 or v2) on the LAN: it finds the router by SSDP, reads its description, and calls WANIPConnection (or WANPPPConnection) by SOAP.