tsnet

package
v0.9.14 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 25 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 tsnet gives each service its own Tailscale identity (§7).

One tsnet.Server per service, named by the manifest's tailscale_name, with node state under <stateDir>/<service>/ so identities survive an Outpost restart instead of re-registering. Each node listens on the tailnet and reverse-proxies to the service on loopback, so the supervised process itself never binds anything but 127.0.0.1 — nothing is reachable except through Tailscale, which is the point.

Bringing a node up is slow: it does network I/O and, on a first run without an auth key, waits for someone to visit a login URL. Attach is called from the supervisor's reconcile loop, which must not stall, so Attach only records intent — a per-service goroutine does the work and retries until it succeeds or the service is detached.

Each node runs in its own child process (see process.go), so a Tailscale panic costs one service its tailnet exposure rather than taking down the daemon. A node that dies is brought back up the same way it first came up.

Index

Constants

View Source
const (
	// AccessPath is where the gateway answers it, on loopback only.
	AccessPath = "/.outpost/tailnet-access"
	// AccessSecretHeader carries the secret that shows the question comes
	// from one of the daemon's own nodes rather than any process on the
	// machine, which could otherwise learn whose logins are linked.
	AccessSecretHeader = "Outpost-Node-Secret"
	// AccessSecretEnv carries that secret to the node, for the same reason
	// AuthKeyEnv carries the auth key: arguments are visible in ps.
	AccessSecretEnv = "OUTPOST_TSNET_ACCESS_SECRET"
)

A tcp forward can't pass through the gateway's login: there is no request to send it, only a byte stream. So with the gateway on, a node asks it instead, once per connection, whether the caller's tailnet login belongs to an Outpost user who may open the service. These are the two ends of that question.

View Source
const (
	FunnelReadHeaderTimeout = 10 * time.Second
	FunnelReadTimeout       = 60 * time.Second
	FunnelWriteTimeout      = 75 * time.Second
)

FunnelReadHeaderTimeout, FunnelReadTimeout and FunnelWriteTimeout bound a request from the internet. A webhook sender gives up well before a minute.

View Source
const (
	FunnelOn          = "on"
	FunnelUnavailable = "unavailable"
)

What follows FunnelLinePrefix.

View Source
const (
	NodeStarting   = "starting"
	NodeNeedsLogin = "needs-login"
	NodeOn         = "on"
)

Node states, as NodeStatus reports them.

View Source
const (
	NodeFunnelStarting    = "starting"
	NodeFunnelOn          = FunnelOn
	NodeFunnelUnavailable = FunnelUnavailable
)

Funnel states, as NodeStatus reports them.

View Source
const AuthKeyEnv = "OUTPOST_TSNET_AUTHKEY"

AuthKeyEnv carries the auth key to the node. An argument would put the key in the process table for any local user to read.

View Source
const FunnelLinePrefix = "outpost-tsnet-node: funnel "

FunnelLinePrefix starts a line a node with Funnel prints on stdout, after ReadyLine, each time its Funnel listener comes up or fails to: FunnelOn, or FunnelUnavailable and the reason Tailscale gave. The node serves the tailnet either way.

View Source
const HostLinePrefix = "outpost-tsnet-node: host "

HostLinePrefix starts the line a node prints on stdout, before ReadyLine, naming the DNS name it was given. That can differ from the hostname asked for -- Tailscale appends -1 when the name is taken -- so the gateway learns it from the node rather than assuming it.

View Source
const LoginLinePrefix = "outpost-tsnet-node: login "

LoginLinePrefix starts a line a node prints on stdout, before ReadyLine, each time Tailscale gives it a URL to authorise it at. The daemon keeps the latest, so the operator finds it in `outpost status` rather than the log.

View Source
const NodeCommand = "__tsnet-node"

NodeCommand is the hidden subcommand that runs one node. The daemon re-executes its own binary with it, so there is nothing extra to install.

View Source
const PortalNode = manifest.PortalNodeName

PortalNode is the router's name for the portal's own node, which keeps its state in <StateDir>/_portal. Manifest validation reserves it, so no service can share the node or its directory.

View Source
const ReadyLine = "outpost-tsnet-node: ready"

ReadyLine is what a node prints on stdout once it is serving on the tailnet. Everything it prints on stderr is operator-facing log output, including the one-time login URL.

Variables

View Source
var ErrAccessRefused = errors.New("refused")

ErrAccessRefused is a definite no from the gateway, as opposed to a failure to ask.

Functions

func NewFunnelHandler added in v0.4.0

func NewFunnelHandler(ownName string, target *url.URL, serverName string) http.Handler

NewFunnelHandler is what a node serves on its Funnel listener: requests for its own name go to target, the gateway's public listener, with the client's address as the only X-Forwarded-For. Upgrades are refused, since webhooks don't need them and a held-open connection from the internet is all cost.

func NewProxy

func NewProxy(port int) *httputil.ReverseProxy

NewProxy is the reverse proxy a node serves, exported for the node process.

func NewUpstreamProxy

func NewUpstreamProxy(target *url.URL, serverName string) *httputil.ReverseProxy

NewUpstreamProxy is the reverse proxy a node serves in front of the login gateway, exported for the node process.

func WithFunnelClient added in v0.4.0

func WithFunnelClient(ctx context.Context, addr string) context.Context

WithFunnelClient records the address of the client a Funnel connection came from, which the node reads from Tailscale's own record of it; the connection's peer is the Funnel relay.

Types

type AccessChecker added in v0.4.0

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

AccessChecker asks the gateway about one caller.

func NewAccessChecker added in v0.4.0

func NewAccessChecker(upstream, serverName, secret string) (*AccessChecker, error)

NewAccessChecker asks the gateway at upstream, the URL a node already proxies to, verifying an https one's certificate for serverName.

func (*AccessChecker) Check added in v0.4.0

func (c *AccessChecker) Check(ctx context.Context, service, login string) (string, error)

Check asks whether login may reach service. It returns the Outpost user on yes, an error wrapping ErrAccessRefused with the gateway's reason on no, and any other error when it couldn't get an answer, which the caller must also treat as no.

type AccessDecision added in v0.4.0

type AccessDecision struct {
	// User is the Outpost user the login belongs to, when allowed.
	User string `json:"user,omitempty"`
	// Reason says why not, when refused.
	Reason string `json:"reason,omitempty"`
}

AccessDecision is the gateway's answer.

type Caller added in v0.4.0

type Caller struct {
	// Login is the Tailscale login the device is signed in to.
	Login string
	// Tagged is a device that belongs to tags rather than a person, whose
	// login names nobody.
	Tagged bool
	// Device is its name on the tailnet, for the log.
	Device string
}

Caller is who is on the other end of a tailnet connection, as Tailscale says.

type Forwarder added in v0.4.0

type Forwarder struct {
	// Service is the app the gateway is asked about. Log lines leave it
	// out: the node's output is already prefixed with it.
	Service string
	// Port is the tailnet port, for the log.
	Port int
	// Target is the service's address, 127.0.0.1:<port>.
	Target string
	// Identify says who a remote address is. Needed only with Check.
	Identify func(ctx context.Context, remote string) (Caller, error)
	// Check, when set, is asked about every connection before it reaches
	// the service, and anything but a user name refuses it. Nil when the
	// gateway is off, which leaves the tailnet itself as the boundary, as it
	// is for HTTP then.
	Check func(ctx context.Context, service, login string) (string, error)
	Logf  func(format string, v ...any)
}

Forwarder carries connections from one tailnet port to the service on loopback, byte for byte.

func (*Forwarder) Serve added in v0.4.0

func (f *Forwarder) Serve(ctx context.Context, ln net.Listener) error

Serve accepts on ln until ctx ends, which closes ln, and reports why it stopped when it wasn't that.

type NodeStatus

type NodeStatus struct {
	State string
	// Funnel is how a node with funnel is doing on the internet, and
	// FunnelError why it's unavailable; "" for a node without funnel.
	Funnel      string
	FunnelError string
	// Host is the node's DNS name, once it is on.
	Host string
	// LoginURL is where to authorise it, while it needs a login.
	LoginURL string
	// Error is why the last attempt failed, while it is retrying.
	Error string
}

NodeStatus is what the router knows of one service's node.

type Options

type Options struct {
	// StateDir holds per-service node state (~/.outpost/tsnet).
	StateDir string
	// AuthKey registers new nodes unattended. Empty is valid: tsnet then logs a
	// one-time login URL, which is the normal first-run flow.
	AuthKey string
	// Logf receives operator-facing lines.
	Logf func(format string, args ...any)
	// Logger is where the router logs, at levels and with fields, along with
	// what each node process says. It supersedes Logf.
	Logger *log.Logger
	// Verbose passes tsnet's own (noisy) logging through to Logf.
	Verbose bool
	// Upstream, when set, is where nodes send tailnet traffic instead of the
	// service: the login gateway, as a URL. Nodes then serve HTTPS, so that
	// every request on the tailnet passes the gateway's login.
	Upstream string
	// UpstreamServerName verifies an https Upstream's certificate.
	UpstreamServerName string
	// AccessSecret is what a node shows the gateway when it asks whether a
	// tcp forward's caller may connect; the gateway is given the same one.
	// Only used with Upstream.
	AccessSecret string
	// PublicUpstream is the gateway's public listener, where a node with
	// funnel sends what arrives from the internet. It serves only public
	// paths. Funnel needs both it and Upstream.
	PublicUpstream string
}

Options configures a Router.

type Router

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

Router owns one tsnet node per attached service.

func New

func New(opts Options) (*Router, error)

New builds a Router. It does not contact Tailscale until Attach is called.

func (*Router) Attach

func (r *Router) Attach(ctx context.Context, svc *manifest.Service) error

Attach brings a service onto the tailnet, proxying tailnet traffic to 127.0.0.1:<port>.

It returns as soon as the intent is recorded — the node comes up in the background and is retried until it succeeds or the service is detached. Attaching an already-attached service with the same configuration is a no-op, so the reconcile loop may call it on every tick.

func (*Router) AttachPortal added in v0.4.0

func (r *Router) AttachPortal(ctx context.Context, hostname, wantHost string) error

AttachPortal puts the portal on the tailnet as its own node, named hostname, which serves HTTPS and forwards everything to the gateway, like a service's node. wantHost is public_url's host: the name the node should be given. If Tailscale gives it another, because the name is taken, that is logged rather than followed, since the portal's address was fixed when the gateway started.

Like Attach it only records intent and returns; Close takes the node down with the rest. Calling it again with the same names is a no-op.

func (*Router) Attached

func (r *Router) Attached() []string

Attached reports the services currently live on the tailnet, sorted. A service still being brought up is not yet attached.

func (*Router) Close

func (r *Router) Close() error

Close detaches every service.

func (*Router) Detach

func (r *Router) Detach(ctx context.Context, name string) error

Detach removes a service from the tailnet, including one whose attach is still in flight. Node state is left on disk so the same identity is reused when the service comes back.

func (*Router) Hosts

func (r *Router) Hosts() map[string]string

Hosts maps each attached service to the DNS name its node was given, for the services whose node reported one.

func (*Router) NodeStatus

func (r *Router) NodeStatus(name string) (NodeStatus, bool)

NodeStatus reports name's node, and false if the router has none: the service isn't running, or isn't meant to be on the tailnet.

func (*Router) StateDirFor

func (r *Router) StateDirFor(service string) string

StateDirFor is where one service's node identity is persisted.

Directories

Path Synopsis
Package node runs one Tailscale node: the body of the hidden subcommand the daemon starts for each service on the tailnet (see internal/tsnet).
Package node runs one Tailscale node: the body of the hidden subcommand the daemon starts for each service on the tailnet (see internal/tsnet).

Jump to

Keyboard shortcuts

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