How the-sarge/turn works
Purpose and Public Surface
the-sarge/turn is an owned Go UDP-only TURN client library for GridSwarm consumers such as wiremux. Its public lifecycle is: construct a client over a caller-owned net.PacketConn, allocate a relay, prepare peers, exchange relayed datagrams, and close. Primary API and exported error contracts reside in client.go, allocation.go, and errors.go.
Client and Allocation Lifecycle
Client.Allocate(ctx) initiates TURN allocation against a canonical netip.AddrPort server and returns the single stateful *Allocation object. Allocation owns relay lifetime, refresh behavior, prepared-peer state, relay reads/writes, and teardown. Core public orchestration is in client.go and allocation.go; lifecycle implementation is concentrated in internal/client/allocation.go.
TURN Transaction and Authentication Boundary
The internal client sends STUN/TURN requests, tracks request/response transactions, and handles protocol errors/authentication retries before exposing outcomes to the public API. internal/client/transaction.go is the request registry/control boundary; internal/client/client.go coordinates client operations; internal/client/errors.go maps internal failures into library behavior.
Peer Preparation and Data Plane
Allocation.PreparePeer(ctx, peer) establishes permission followed by channel binding. This is a hard gate: WriteTo emits ChannelData only for a confirmed prepared binding, with no Send-indication fallback. Permission and binding state machines are implemented in internal/client/permission.go and internal/client/binding.go; prepared-only write/read transport handling is in internal/client/udp_conn.go.
Refresh, Timers, and Failure Semantics
Allocation maintenance uses periodic timers to refresh allocations and likely related TURN resources. Refresh failure is material: it transitions the allocation into a typed failure condition (ErrAllocationRefreshFailed), while failed channel setup has distinct typed errors. The timing/control mechanics live in internal/client/periodic_timer.go and internal/client/allocation.go; public error identities are defined in errors.go.
Protocol Codec Boundary
TURN/STUN wire representation is isolated under internal/proto. It provides typed attributes and packet forms for allocation requests, channel data, addresses, lifetimes, and requested transport/family rather than leaking wire parsing into lifecycle code. Key paths are internal/proto/proto.go, internal/proto/stun_conn.go, and internal/proto/chandata.go. The library interoperates with a TURN server over caller-supplied UDP transport and uses pion/stun in its protocol/test ecosystem.
Verification Fixture and Delivery
turntest is an exported but test-only-in-practice in-process scripted TURN responder for this client and downstream integration tests; it is not a production TURN server. Its responder boundary is turntest/turntest.go and turntest/respond.go. CI/release automation is GitHub Actions-based, with ordinary validation in .github/workflows/ci.yml, deeper certification in .github/workflows/deep-checks.yml, and release packaging in .github/workflows/release.yml.
Main entry points: allocation.go Allocation Lifecycle · client.go Client API · transaction.go Transaction Registry · turntest.go Scripted TURN Responder
Components 21
Public Library
- Client API
client.go - Allocation API · Allocation, peer preparation, relay I/O, and close surface.
allocation.go - Public Errors · Includes typed allocation refresh and channel setup failures.
errors.go
Client Lifecycle
- Client Coordinator · Coordinates allocation and TURN operations.
- Allocation Lifecycle · Owns relay lifetime, refresh, peers, and teardown.
internal/client/allocation.go - Transaction Registry · Tracks TURN requests, responses, protocol errors, and auth retries.
internal/client/transaction.go - Permission State
internal/client/permission.go - Channel Binding State · Confirmed bindings gate relay writes.
internal/client/binding.go - Prepared Relay I/O · Reads and writes ChannelData only for prepared peers.
internal/client/udp_conn.go - Periodic Refresh Timer
internal/client/periodic_timer.go - Internal Error Mapping
internal/client/errors.go
Protocol Codec
- STUN/TURN Types · Typed requests, attributes, addresses, lifetimes, and transport settings.
internal/proto/proto.go - STUN Connection
internal/proto/stun_conn.go - ChannelData Codec
internal/proto/chandata.go
Verification & Delivery
- Scripted TURN Responder · In-process responder for client and downstream integration tests; not a production server.
turntest/turntest.go - Responder Logic
turntest/respond.go - CI Validation
.github/workflows/ci.yml - Deep Certification
.github/workflows/deep-checks.yml - Release Packaging
.github/workflows/release.yml
Other components
- Caller UDP PacketConn · Caller-owned UDP transport.
- TURN Server
Connections 22
- Caller UDP PacketConn → Client API: constructs over
- Client API → Allocation API: Allocate returns
- Client API → Client Coordinator: delegates
- Allocation API → Allocation Lifecycle: controls
- Client Coordinator → Transaction Registry: issues requests
- Allocation Lifecycle → Transaction Registry: allocation requests
- Allocation Lifecycle → Permission State: PreparePeer: permission
- Permission State → Channel Binding State: then bind channel
- Channel Binding State → Prepared Relay I/O: confirmed binding enables
- Allocation Lifecycle → Periodic Refresh Timer: schedules refresh
- Periodic Refresh Timer → Allocation Lifecycle: refreshes allocation
- Allocation Lifecycle → Internal Error Mapping: maps failures
- Internal Error Mapping → Public Errors: exposes typed errors
- Transaction Registry → STUN/TURN Types: encodes and decodes
- STUN/TURN Types → STUN Connection: uses
- Prepared Relay I/O → ChannelData Codec: uses
- STUN Connection → Caller UDP PacketConn: sends via
- Caller UDP PacketConn → TURN Server: UDP
- Scripted TURN Responder → Responder Logic: implements
- CI Validation → Scripted TURN Responder: validates with
- Deep Certification → Scripted TURN Responder: certifies with
- Release Packaging → CI Validation: release validation