| Crates.io | hightower-stun |
| lib.rs | hightower-stun |
| version | 0.1.1 |
| created_at | 2025-10-07 19:48:37.111377+00 |
| updated_at | 2025-10-07 20:50:17.324012+00 |
| description | A lightweight STUN (Session Traversal Utilities for NAT) server and client implementation conforming to RFC 8489 |
| homepage | |
| repository | https://github.com/chrishayen/hightower-stun |
| max_upload_size | |
| id | 1872736 |
| size | 35,336 |
A lightweight, RFC 8489-compliant STUN (Session Traversal Utilities for NAT) server and client implementation in Rust.
use hightower_stun::client::StunClient;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = StunClient::new()?;
// Query a STUN server to discover your public IP
let addr = client.get_public_address("stun.l.google.com:3478")?;
println!("Your public IP: {}", addr.ip());
println!("Your public port: {}", addr.port());
Ok(())
}
use hightower_stun::server::StunServer;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let server = StunServer::bind("0.0.0.0:3478")?;
println!("STUN server listening on {}", server.local_addr()?);
// Run the server (blocks indefinitely)
server.run()?;
Ok(())
}
The crate includes two binaries:
ht-stun-clientQuery a STUN server to discover your public IP address:
# Port defaults to 3478
ht-stun-client stun.l.google.com
ht-stun-client gateway.shotgun.dev
# Or specify port explicitly
ht-stun-client 192.168.1.1:3478
ht-stun-serverRun a STUN server:
# Listen on default port 3478
ht-stun-server
# Or specify custom address
ht-stun-server 0.0.0.0:3478
# Build library and binaries
cargo build --release
# Run tests
cargo test
# Run client
cargo run --bin ht-stun-client -- stun.l.google.com
# Run server
cargo run --bin ht-stun-server
To build for ARM64 (aarch64) servers:
# Install the target
rustup target add aarch64-unknown-linux-gnu
# Install the cross-compiler (Arch Linux)
sudo pacman -S aarch64-linux-gnu-gcc
# Build
cargo build --release --target aarch64-unknown-linux-gnu
The included Makefile provides a deploy target for building and deploying to a remote server.
STUN helps clients discover their public IP address when behind a NAT:
0x2112A4420x0001), Binding Response (0x0101)0x0020), MAPPED-ADDRESS (0x0001)See the examples/ directory for more:
get_public_ip.rs - Simple client examplerun_server.rs - Basic server example# Run all tests
cargo test
# Test against a public STUN server
cargo run --example get_public_ip stun.l.google.com
# Test client against your own server
cargo run --bin ht-stun-server &
cargo run --bin ht-stun-client localhost
This library implements a Basic STUN Server as defined in RFC 8489, Section 12. It focuses on the core functionality needed for NAT traversal and public IP discovery.
Per RFC 8489 Section 12, Basic STUN Servers SHOULD NOT implement:
Additional features not implemented:
See IMPLEMENTATION.md for a complete feature breakdown.
Contributions welcome! Please feel free to submit a Pull Request.