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
- Variables
- func Probe(ctx context.Context, target *url.URL) error
- func Proxy(target *url.URL, logger *slog.Logger) http.Handler
- func Run(ctx context.Context, cfg ClientConfig) error
- type ClientConfig
- type Server
- type Session
- func (s *Session) Agent() string
- func (s *Session) Capacity() int
- func (s *Session) Close() error
- func (s *Session) Connected() time.Time
- func (s *Session) Done() <-chan struct{}
- func (s *Session) Expose() bool
- func (s *Session) Handle() string
- func (s *Session) ID() string
- func (s *Session) Transport() http.RoundTripper
- type UnreachableError
Constants ¶
const DefaultCapacity = 4
DefaultCapacity is how many tasks a session runs at once unless it says otherwise.
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.
const MaxCapacity = 64
MaxCapacity caps what a session may ask for.
const Path = "/tunnel"
Path is where the server accepts tunnels.
Variables ¶
ErrUnauthorized means the server rejected the token; retrying won't help.
Functions ¶
func Proxy ¶
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).
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.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is a connected agent on the server side.
func (*Session) Expose ¶
Expose reports whether the agent lets the server forward raw HTTP (webhooks) to it.
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