tunnel

package
v0.1.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: Apache-2.0 Imports: 15 Imported by: ? 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 tunnel connects an agent that runs anywhere (a laptop, a home server, a GPU box) to a Lagrange server, like ngrok but for agents: the agent dials out, so it needs no public address, open port or deploy.

The agent side (Run) opens a WebSocket to the server's /tunnel endpoint with an address token and serves HTTP on a yamux session over it. The server side (Server) turns each connection into a Session whose Transport sends HTTP requests to the agent through the session. One connection carries any number of concurrent requests, streaming (SSE) responses included. Several connections for the same address are several workers of one agent.

Index

Constants

View Source
const DefaultCapacity = 4

DefaultCapacity is how many tasks a session runs at once unless it says otherwise.

View Source
const HeaderUnreachable = "Lagrange-Agent-Unreachable"

HeaderUnreachable is set on the 502 a tunnel client answers with when nothing answers at its target: the tunnel is up but the agent behind it isn't.

View Source
const MaxCapacity = 64

MaxCapacity caps what a session may ask for.

View Source
const Path = "/tunnel"

Path is where the server accepts tunnels.

Variables

View Source
var ErrUnauthorized = errors.New("tunnel: the server rejected the token")

ErrUnauthorized means the server rejected the token; retrying won't help.

Functions

func Probe

func Probe(ctx context.Context, target *url.URL) error

Probe reports whether something answers HTTP at target, for a warning at startup.

func Proxy

func Proxy(target *url.URL, logger *slog.Logger) http.Handler

Proxy returns a handler that forwards every request to target (for example http://localhost:9001), streaming responses as they come. When nothing answers at target, it says so in HeaderUnreachable, and logs to logger (nil for slog.Default).

func Run

func Run(ctx context.Context, cfg ClientConfig) error

Run keeps a tunnel open until ctx ends, reconnecting with backoff when it drops. It returns ErrUnauthorized if the server rejects the token.

Types

type ClientConfig

type ClientConfig struct {
	// Server is the Lagrange server's base URL, for example https://lagrange.example.com.
	Server string
	// Token is the address token.
	Token string
	// Capacity is how many tasks this copy of the agent runs at once.
	Capacity int
	// Handler serves the requests that come through the tunnel: the agent itself, or a
	// reverse proxy to it (see Proxy).
	Handler http.Handler
	// Expose lets the server forward raw HTTP requests for /t/<handle>/... to Handler,
	// for example push notifications the agent subscribed to.
	Expose bool
	// UserAgent describes the client, for the server's logs and UI.
	UserAgent string
	// OnConnect is called after each successful connection.
	OnConnect func()
	// Logger defaults to slog.Default().
	Logger *slog.Logger
}

ClientConfig configures Run.

type Server

type Server struct {
	// Authenticate maps a bearer token to the address it may serve.
	Authenticate func(ctx context.Context, token string) (handle string, err error)
	// OnSession is called for each new session, in its own goroutine, and should
	// register it (with a mailbox.Mailbox). The session is closed when the
	// connection drops.
	OnSession func(s *Session)
	// Logger defaults to slog.Default().
	Logger *slog.Logger
	// contains filtered or unexported fields
}

Server accepts tunnels.

func (*Server) ServeHTTP

func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler for Path.

type Session

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

Session is a connected agent on the server side.

func (*Session) Agent

func (s *Session) Agent() string

Agent is the user agent string the client sent (its name and version).

func (*Session) Capacity

func (s *Session) Capacity() int

Capacity implements mailbox.Session.

func (*Session) Close

func (s *Session) Close() error

Close ends the session.

func (*Session) Connected

func (s *Session) Connected() time.Time

Connected is when the session started.

func (*Session) Done

func (s *Session) Done() <-chan struct{}

Done implements mailbox.Session.

func (*Session) Expose

func (s *Session) Expose() bool

Expose reports whether the agent lets the server forward raw HTTP (webhooks) to it.

func (*Session) Handle

func (s *Session) Handle() string

Handle is the address the session serves.

func (*Session) ID

func (s *Session) ID() string

ID implements mailbox.Session.

func (*Session) Transport

func (s *Session) Transport() http.RoundTripper

Transport implements mailbox.Session. Requests go to the agent whatever their host and scheme: the tunnel is the secure channel, so https URLs from the agent's card are sent as plain HTTP through it.

type UnreachableError

type UnreachableError struct{ Reason string }

UnreachableError is what a session's Transport returns when the tunnel is connected but the agent behind it doesn't answer.

func (*UnreachableError) Error

func (e *UnreachableError) Error() string

Jump to

Keyboard shortcuts

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