stem-rs
The complete Rust library for Tor control protocol. Build privacy-focused applications with type-safe, async-first APIs.
cargo add stem-rs
Everything you need
A complete toolkit for building Tor-integrated applications with idiomatic Rust patterns.
Control Socket
Connect to Tor via TCP or Unix domain sockets. Async-first design with full tokio integration for high-performance applications.
Authentication
All authentication methods with automatic detection. Secure handling of credentials with constant-time comparison.
Controller API
High-level interface for Tor interaction. Query configuration, send signals, manage circuits, and control hidden services.
Descriptor Parsing
Complete parsing for all Tor descriptor types. Server, micro, consensus, extra-info, and hidden service descriptors.
Event Handling
Subscribe to real-time Tor events. Monitor bandwidth, circuit status, stream events, and log messages as they happen.
Exit Policy
Parse and evaluate exit policies. Determine what traffic a relay allows and make informed routing decisions.
Simple, powerful APIs
Get up and running in minutes with clear, well-documented code examples.
Connect to Tor
Establish connection and authenticate
use stem_rs::{Controller, Error};
#[tokio::main]
async fn main() -> Result<(), Error> {
// Connect to control port
let mut ctrl = Controller::from_port(
"127.0.0.1:9051".parse()?
).await?;
// Auto-detect auth method
ctrl.authenticate(None).await?;
println!("Connected!");
Ok(())
}
Query Information
Get version, status, and metrics
// Get Tor version
let version = ctrl
.get_version().await?;
println!("Tor {}", version);
// Query traffic stats
let read = ctrl
.get_info("traffic/read")
.await?;
// Get circuit status
let circuits = ctrl
.get_info("circuit-status")
.await?;
Event Subscription
Real-time bandwidth monitoring
use stem_rs::EventType;
// Subscribe to events
ctrl.set_events(&[
EventType::Bw,
EventType::Circ,
]).await?;
// Process events
loop {
let event = ctrl
.recv_event().await?;
println!("{:?}", event);
}
Explore the modules
Comprehensive documentation with examples for every module.