directaccess

package
v0.0.0-...-79b1474 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 38 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 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

View Source
const (
	Lease      = time.Hour
	RenewEvery = 20 * time.Minute
)

Lease is how long a mapping is asked for; it is renewed every RenewEvery.

View Source
const (
	StateOn            = "on"
	StateOff           = "off"
	StateCGNAT         = "cgnat"
	StateMappingFailed = "mapping_failed"
)

States the home reports.

View Source
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.

View Source
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

View Source
var ErrNoGateway = errors.New("directaccess: no router answered (UPnP, NAT-PMP, PCP)")

ErrNoGateway: no router answered.

Functions

func HomeID

func HomeID(serverID string) string

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

func MapFirst(ctx context.Context, mappers []Mapper, internal, want int) (Mapping, Mapper, error)

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 ParsePort

func ParsePort(s string) (int, error)

ParsePort reads a TCP port, 1–65535.

func RenewDue

func RenewDue(c *tls.Certificate, n Name, now time.Time) bool

RenewDue says whether c should be renewed now: at two thirds of its life, or once it no longer names n.

func Shared

func Shared(ip netip.Addr) bool

Shared says whether ip can't be reached from outside: carrier-grade NAT (100.64.0.0/10), a private, loopback or link-local address. A router reporting one as its external address means the provider shares it, so direct access can't work (the cloud's direct.Shared).

Types

type Agent

type Agent struct {
	Addr   string
	Client *http.Client
}

Agent is the relay agent's loopback admin port (MAKTABA_CLOUDLINK_ADMIN).

func (*Agent) Post

func (a *Agent) Post(ctx context.Context, r AgentRequest) error

Post tells the cloud r.

func (*Agent) Status

func (a *Agent) Status(ctx context.Context) (AgentStatus, error)

Status reads what the cloud last said.

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) CSR

func (f *Files) CSR(n Name) ([]byte, error)

CSR is a request for n's wildcard, signed by the home's key (DER).

func (*Files) Cert

func (f *Files) Cert() *tls.Certificate

Cert is the certificate the listener presents (nil: none yet).

func (*Files) Key

func (f *Files) Key() (crypto.Signer, error)

Key loads the home's key, making it on first use.

func (*Files) Name

func (f *Files) Name() Name

Name is the name the cloud assigned (zero: none yet).

func (*Files) SetChain

func (f *Files) SetChain(chainPEM []byte, n Name) error

SetChain keeps a chain the cloud delivered when it is for this home's key and n's name; anything else is refused and the last good one stays.

func (*Files) SetName

func (f *Files) SetName(n Name) error

SetName keeps the assigned name.

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

func (n *NATPMP) ExternalAddress(ctx context.Context) (netip.Addr, error)

ExternalAddress asks the router's external IPv4 address.

func (*NATPMP) Map

func (n *NATPMP) Map(ctx context.Context, internal, external int, lifetime time.Duration) (Mapping, error)

Map asks a TCP mapping (opcode 2).

func (*NATPMP) Name

func (n *NATPMP) Name() string

func (*NATPMP) Unmap

func (n *NATPMP) Unmap(ctx context.Context, m Mapping) error

Unmap asks the mapping with lifetime 0 (RFC 6886 §3.4).

type Name

type Name struct {
	Domain string `json:"domain"`
	HomeID string `json:"home_id"`
}

Name is the home's certificate name as the cloud assigned it.

func (Name) Wildcard

func (n Name) Wildcard() string

Wildcard is *.<home id>.<domain>, the only name the certificate carries.

type PCP

type PCP struct {
	Gateway netip.AddrPort
	Tries   int
	// contains filtered or unexported fields
}

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.

func (*PCP) Map

func (p *PCP) Map(ctx context.Context, internal, external int, lifetime time.Duration) (Mapping, error)

func (*PCP) Name

func (p *PCP) Name() string

func (*PCP) Unmap

func (p *PCP) Unmap(ctx context.Context, m Mapping) error

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).

func (*Routes) Mount

func (h *Routes) Mount(r chi.Router)

Mount wires the routes.

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) CheckNow

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

CheckNow asks the cloud to check the home from outside now.

func (*Service) Handler

func (s *Service) Handler() http.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

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

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

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

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

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

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.

func (*Service) Status

func (s *Service) Status() Status

Status is the settings page's view.

type Settings

type Settings struct {
	Enabled    bool
	MapPort    bool
	ManualPort int
}

Settings are rules.direct.

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.

func (*UPnP) Map

func (u *UPnP) Map(ctx context.Context, internal, external int, lifetime time.Duration) (Mapping, error)

Map adds a TCP mapping from external to this machine's internal port. With external 0 it asks for a random port in 30000–49999 (a few tries when one is taken).

func (*UPnP) Name

func (u *UPnP) Name() string

func (*UPnP) Unmap

func (u *UPnP) Unmap(ctx context.Context, m Mapping) error

Unmap deletes the mapping.

Jump to

Keyboard shortcuts

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