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
- func LocalURLs(port int) []string
- func Pipe(st *mux.Stream, conn net.Conn)
- type Agent
- func (a *Agent) ConnString() (string, error)
- func (a *Agent) DirectHandler(ctx context.Context) http.Handler
- func (a *Agent) ListenDirect(ctx context.Context, addr string) (net.Addr, error)
- func (a *Agent) PublicURL() string
- func (a *Agent) Run(ctx context.Context) error
- func (a *Agent) ServeSecure(ctx context.Context, conn mux.Conn)
- type Handler
- type HandlerFunc
- type Info
- type PermanentError
- type Router
- func (r *Router) HTTP(h http.Handler) *Router
- func (r *Router) Handle(kind string, h Handler) *Router
- func (r *Router) HandleFunc(kind string, f func(ctx context.Context, st *mux.Stream, info Info)) *Router
- func (r *Router) ServeStream(ctx context.Context, st *mux.Stream, info Info)
- func (r *Router) TCP(targets map[string]string) *Router
- func (r *Router) WebSocket(fn func(ctx context.Context, c *wskind.Conn, meta wskind.OpenMeta, info Info)) *Router
- type Status
- type TCPMeta
Constants ¶
const Version = "0.2.0"
Version is reported to relays in the hello.
Variables ¶
This section is empty.
Functions ¶
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 ¶
ConnString returns the connection string clients use to reach this agent.
func (*Agent) DirectHandler ¶
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 ¶
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 ¶
PublicURL is where plaintext traffic reaches this agent when Public is set.
func (*Agent) Run ¶
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 ¶
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 HandlerFunc ¶
HandlerFunc adapts a function to Handler.
func (HandlerFunc) ServeStream ¶
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 (*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 ¶
ServeStream implements Handler.