server

package
v0.4.0-alpha.2 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 47 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 server implements portholed: it accepts client sessions over WebSocket+yamux (docs/protocol.md), keeps the registry of tunnels and routes public HTTP and TCP traffic into them (DESIGN.md §3.5).

Every client session owns a context. Closing a session releases its tunnels, closes its listeners, streams and transports and waits for every goroutine it started. Server.Close does the same for all sessions.

Index

Constants

View Source
const (
	// HostKeyFile is the name of the gateway host key inside the data directory.
	HostKeyFile = "ssh_host_ed25519_key"
)

Variables

View Source
var (
	// ErrRequestNotFound means the request is not (or no longer) in the journal.
	ErrRequestNotFound = errors.New("server: request not found in the traffic log")
	// ErrReplayRefused means the request exists but cannot be replayed; the wrapped text says why.
	ErrReplayRefused = errors.New("server: request cannot be replayed")
)

Errors of ReplayRequest.

Functions

func HostKeyFingerprint

func HostKeyFingerprint(dir string) (string, error)

HostKeyFingerprint returns the SHA256 fingerprint of the existing gateway host key in dir.

func LoadHostKey

func LoadHostKey(dir string) (ssh.Signer, error)

LoadHostKey reads the gateway host key from dir. It does not create one.

Types

type Options

type Options struct {
	Config  *config.Config
	Store   store.Store
	Logger  *slog.Logger
	Version string
	// Now returns the current time; nil means time.Now. Tests use it to move token expiry.
	Now func() time.Time

	// HeartbeatInterval is the ping period (default proto.DefaultHBMilli ms).
	HeartbeatInterval time.Duration
	// RevalidateInterval is how often tokens of live sessions are re-read from the store (default 30 s).
	RevalidateInterval time.Duration
	// HandshakeTimeout bounds session establishment (default 10 s).
	HandshakeTimeout time.Duration
	// RemoteOpenTimeout bounds the wait for a client's answer to a remote tunnel request (default 15 s).
	RemoteOpenTimeout time.Duration

	// SSHIdleTimeout closes SSH gateway connections without open channels after this long (default 60 s).
	SSHIdleTimeout time.Duration

	// Metrics receives the Prometheus instrumentation. Nil turns it off, unless cfg.MetricsListen is set: then
	// the server creates its own.
	Metrics *metrics.Metrics
}

Options configures a Server.

type Server

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

Server is the portholed core. Create it with New and always release it with Close (Run and Serve do so).

func New

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

New validates opts and returns a Server. It starts nothing.

func (*Server) Close

func (s *Server) Close() error

Close ends every session (clients receive a shutting_down error), closes all TCP listeners and transports and waits for all goroutines started by the server. It is safe to call more than once.

func (*Server) Handler

func (s *Server) Handler() http.Handler

Handler returns the HTTP handler for the public listener: the control endpoint, /healthz and the virtual hosts of HTTP tunnels.

func (*Server) ReloadTLS

func (s *Server) ReloadTLS() error

ReloadTLS re-reads the TLS certificate and key files immediately instead of waiting for the periodic modification-time check; portholed calls it on SIGHUP. A failed reload keeps the previous certificate. The outcome is logged here; the error is returned for callers that want to react to it. It fails when TLS is not configured or Serve has not loaded the certificate yet.

func (*Server) ReplayRequest

func (s *Server) ReplayRequest(ctx context.Context, id uint64) (traffic.Request, error)

ReplayRequest sends the request with the given journal id again through the handler of its tunnel (not over the network) and returns the new journal entry, whose ReplayOf is id. The request must have been inspected with its detail still kept and its body must not be truncated. Headers that were stored as REDACTED (Authorization, Cookie, ...) are not sent, so a replay of an authenticated request reaches the service without credentials.

func (*Server) Run

func (s *Server) Run(ctx context.Context) error

Run listens on cfg.Listen (TLS when configured) and serves until ctx is done, then shuts down gracefully within cfg.ShutdownGrace and closes every session and TCP listener.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, ln net.Listener) error

Serve is Run on an existing listener. It takes ownership of ln.

func (*Server) StartAdminSocket

func (s *Server) StartAdminSocket(ln net.Listener) error

StartAdminSocket serves the admin API on ln in the background until the server closes. It takes ownership of ln, which must be reachable only by trusted local users: every caller on it is a full administrator.

func (*Server) StartMetricsListener

func (s *Server) StartMetricsListener(ln net.Listener) error

StartMetricsListener serves /metrics and /debug/pprof/ on ln in the background until the server closes. It takes ownership of ln. The endpoints are unauthenticated, so a listener that is not on a loopback address is logged as a warning (it is the operator's call, for example behind a firewall or a private network).

func (*Server) StartSSHGateway

func (s *Server) StartSSHGateway(ln net.Listener) error

StartSSHGateway serves the SSH gateway on ln in the background until the server closes. It takes ownership of ln and returns once the host key is loaded.

func (*Server) Traffic

func (s *Server) Traffic() *traffic.Log

Traffic returns the journal of proxied requests and tunnel connections (ADR 0005). The result is never nil; a journal sized 0 in the configuration records nothing.

Jump to

Keyboard shortcuts

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