$ show mycrl/turn-rs
turn-rs is a high-performance TURN/STUN server implemented in Rust, designed primarily for WebRTC NAT traversal and media relay. The codebase consists of a core library (`turn-server`) and a companion SDK crate (`turn-server-sdk`) that provides a gRPC client and hooks API. The server architecture is async I/O driven (Tokio) with optional transport modules for UDP and TCP (including TLS via rustls). Protocol handling is implemented from scratch with a STUN/TURN message codec, a connection-less switch mechanism for relaying packets between sessions, and a memory pool for zero-copy buffering. Management and observability are exposed through an optional gRPC API (sessions, statistics, destroy) and a Prometheus metrics endpoint. Authentication supports static credentials, TURN REST shared-secret, and an external gRPC hook service for dynamic password lookup and event callbacks. The project is well-tested with unit tests, property-based fuzzing (proptest), integration tests using coturn's test client, and benchmarks. It is actively maintained with recent releases and comprehensive documentation.
The LLM identified 5 files worth highlighting for reuse:
Implements a fast, lock-free port allocator using a bit set (u64 buckets) over a configurable port range. Provides O(1) allocate/deallocate with random starting offset to reduce collision predictability. The `PortAllocator` is a self-contained, reusable component for any service needing ephemeral port assignment in a fixed range.
Provides a global, self-shrinking `Buffer` type backed by a lock-free `crossbeam::ArrayQueue` of pre-allocated `BytesMut` buffers. A background task monitors the ratio of idle-to-acquired buffers and reduces the pool when it detects sustained excess, preventing memory leaks under burst workloads. The buffer is safely acquired/returned via RAII (Drop). This pattern is extraction-ready for any high-throughput network service.
Implements an in-memory switch that maps session `Identifier`s to `tokio::sync::mpsc::UnboundedSender` channels. Relay packets are published via `send()` which drops silently if the receiver is gone, providing backpressure-free forwarding. The switch is transport-agnostic and enables the cross-protocol (UDP↔TCP) relaying required by the TURN spec.
Defines a type-safe, extensible STUN/TURN attribute hierarchy using the `Attribute` trait. Each attribute (e.g., `UserName`, `XorMappedAddress`, `MessageIntegrity`) has a well-defined serialization/deserialization and is automatically discovered from the binary stream. The pattern supports both comprehension-required and optional attributes with zero-copy parsing, and can serve as a template for any TLV-based protocol codec.
Provides cryptographic primitives for STUN/TURN authentication: HMAC-SHA1 for MESSAGE-INTEGRITY, CRC32 fingerprint, and password derivation (MD5 and SHA256) per RFC 5389/8489. The functions are pure, side-effect-free, and come with verified test vectors; they can be extracted into a standalone crate for any STUN-based project.