agent

package
v0.0.0-...-fe767e9 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 18 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 agent is the service side of the relay: it dials out to a relay, keeps that connection alive across failures, and dispatches incoming streams (plaintext or end-to-end encrypted) to handlers.

Index

Constants

View Source
const Version = "0.2.0"

Version is reported to relays in the hello.

Variables

This section is empty.

Functions

func LocalURLs

func LocalURLs(port int) []string

LocalURLs lists ws://<ip>:<port> for each non-loopback unicast address, private ranges first.

func Pipe

func Pipe(st *mux.Stream, conn net.Conn)

Pipe copies both ways between a stream and a net.Conn until both sides are done, propagating half-closes.

Types

type Agent

type Agent struct {
	// Relay is the relay's base URL (http, https, ws or wss).
	Relay string
	// Code is the connect code. Clients need it (via the connection string)
	// to reach this agent.
	Code e2e.Code
	// Name is shown in relay logs. Optional.
	Name string
	// Auth is the registration key or JWT, if the relay requires one.
	Auth string
	// Public also serves plaintext streams opened by the relay, reachable
	// at <relay>/c/<routing token>/. Off by default: E2E only.
	Public bool
	// Handler serves streams. Usually a *Router.
	Handler Handler
	// OnSession is called for each new E2E mux session (one per connected
	// client), e.g. to open streams back to that client. Optional.
	OnSession func(ctx context.Context, s *mux.Session, hello *e2e.ClientHello)
	// Protos adds application protocols besides the built-in mux. A client
	// offering one of these names gets the raw encrypted message channel
	// instead of a mux session. Optional.
	Protos map[string]func(ctx context.Context, conn mux.Conn, hello *e2e.ClientHello)
	// Direct lists URLs where clients can reach this agent without the
	// relay (see ListenDirect). They are sent inside the encrypted
	// handshake, never to the relay.
	Direct []string
	// OnStatus observes connection changes. Optional.
	OnStatus func(Status)
	// Logger, MinBackoff, MaxBackoff and HTTPClient have sane defaults.
	Logger     *slog.Logger
	MinBackoff time.Duration
	MaxBackoff time.Duration
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

Agent connects a service to a relay. Fill the fields, then call Run.

func (*Agent) ConnString

func (a *Agent) ConnString() (string, error)

ConnString returns the connection string clients use to reach this agent.

func (*Agent) DirectHandler

func (a *Agent) DirectHandler(ctx context.Context) http.Handler

DirectHandler serves the same E2E entry point the relay forwards to, so clients on the same network (LAN, VPN, Tailscale…) can skip the relay. The handshake is identical, so a direct connection is exactly as private and authenticated as a relayed one; it is just shorter.

func (*Agent) ListenDirect

func (a *Agent) ListenDirect(ctx context.Context, addr string) (net.Addr, error)

ListenDirect serves DirectHandler on addr (e.g. ":7777") until ctx ends, and adds a ws:// URL for every local interface address to a.Direct so connected clients learn where to find it. Call it before Run.

func (*Agent) PublicURL

func (a *Agent) PublicURL() string

PublicURL is where plaintext traffic reaches this agent when Public is set.

func (*Agent) Run

func (a *Agent) Run(ctx context.Context) error

Run connects and serves until ctx is cancelled or a permanent error occurs. Transient failures are retried with exponential backoff and full jitter.

func (*Agent) ServeSecure

func (a *Agent) ServeSecure(ctx context.Context, conn mux.Conn)

ServeSecure runs the agent side of an E2E handshake on conn (a WebSocket from the relay or a direct one) and serves the session until it ends. It is exported so you can accept E2E connections from any transport.

type Handler

type Handler interface {
	ServeStream(ctx context.Context, st *mux.Stream, info Info)
}

Handler serves one inbound stream. It owns the stream and must close or reset it.

type HandlerFunc

type HandlerFunc func(ctx context.Context, st *mux.Stream, info Info)

HandlerFunc adapts a function to Handler.

func (HandlerFunc) ServeStream

func (f HandlerFunc) ServeStream(ctx context.Context, st *mux.Stream, info Info)

ServeStream calls f.

type Info

type Info struct {
	// Encrypted is true for streams opened inside an E2E session.
	Encrypted bool
	// Session is the session the stream arrived on. Inside an E2E session
	// the agent can use it to open streams back to the client.
	Session *mux.Session
}

Info describes where a stream came from.

type PermanentError

type PermanentError struct{ Err error }

PermanentError means retrying will not help (bad credentials, bad code).

func (*PermanentError) Error

func (e *PermanentError) Error() string

func (*PermanentError) Unwrap

func (e *PermanentError) Unwrap() error

type Router

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

Router dispatches streams by kind. It is the usual Agent.Handler:

r := agent.NewRouter()
r.HTTP(myHandler)                         // kind "http"
r.TCP(map[string]string{"ssh": "127.0.0.1:22"}) // kind "tcp"
r.Handle("com.example.chat", chatHandler) // your own kind

func NewRouter

func NewRouter() *Router

NewRouter returns an empty router.

func (*Router) HTTP

func (r *Router) HTTP(h http.Handler) *Router

HTTP serves `http` streams with an ordinary http.Handler.

func (*Router) Handle

func (r *Router) Handle(kind string, h Handler) *Router

Handle registers h for streams of the given kind.

func (*Router) HandleFunc

func (r *Router) HandleFunc(kind string, f func(ctx context.Context, st *mux.Stream, info Info)) *Router

HandleFunc registers a function for a kind.

func (*Router) ServeStream

func (r *Router) ServeStream(ctx context.Context, st *mux.Stream, info Info)

ServeStream implements Handler.

func (*Router) TCP

func (r *Router) TCP(targets map[string]string) *Router

TCP serves `tcp` streams by dialing one of the named targets. The empty name is the default target. Unknown names are refused: the agent never dials an address chosen by the client.

func (*Router) WebSocket

func (r *Router) WebSocket(fn func(ctx context.Context, c *wskind.Conn, meta wskind.OpenMeta, info Info)) *Router

WebSocket serves `ws` streams with fn, which receives the message connection already accepted.

type Status

type Status struct {
	Connected bool
	Attempt   int           // consecutive failed attempts
	Retry     time.Duration // delay before the next attempt (when !Connected)
	Err       error
}

Status is reported to Agent.OnStatus on every connection change.

type TCPMeta

type TCPMeta struct {
	Target string `json:"target"`
}

TCPMeta is the OPEN meta of a `tcp` stream.

Jump to

Keyboard shortcuts

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