🦀 Built with Rust

stem-rs

The complete Rust library for Tor control protocol. Build privacy-focused applications with type-safe, async-first APIs.

$ cargo add stem-rs
100%
Safe Rust
Async
Tokio Runtime
Full
Stem Parity

Simple, powerful APIs

Get up and running in minutes with clear, well-documented code examples.

1

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(())
}
2

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?;
3

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);
}