Async-first • Type-safe • Tokio runtime

Tor control protocol
for Rust

A complete Rust implementation of the Tor control protocol. Connect to Tor, authenticate with any method, query network information, and subscribe to real-time events.

tor_monitor.rs
use stem_rs::{Controller, Signal, EventType};

#[tokio::main]
async fn main() -> Result<(), stem_rs::Error> {
    // Connect to Tor control port
    let mut ctrl = Controller::from_port(
        "127.0.0.1:9051".parse()?
    ).await?;

    // Authenticate automatically
    ctrl.authenticate(None).await?;

    // Get Tor version
    let version = ctrl.get_version().await?;
    println!("Tor {}", version);

    // Subscribe to events
    ctrl.set_events(&[
        EventType::Bw,
        EventType::Circ,
    ]).await?;

    Ok(())
}

Up and running in minutes

Clear, well-documented examples to get you started with the most common use cases.

1

Connect & Authenticate

Establish connection to Tor control port

Rust
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 authentication method
    ctrl.authenticate(None).await?;
    
    println!("Connected to Tor!");
    Ok(())
}
2

Query Information

Get version, status, and network metrics

Rust
// Get Tor version
let version = ctrl.get_version().await?;
println!("Tor version: {}", version);

// Query traffic statistics
let bytes_read = ctrl.get_info("traffic/read").await?;
let bytes_written = ctrl.get_info("traffic/written").await?;

// Get active circuit status
let circuits = ctrl.get_info("circuit-status").await?;
3

Subscribe to Events

Real-time bandwidth and circuit monitoring

Rust
use stem_rs::EventType;

// Subscribe to bandwidth and circuit events
ctrl.set_events(&[
    EventType::Bw,
    EventType::Circ,
]).await?;

// Process events as they arrive
loop {
    let event = ctrl.recv_event().await?;
    println!("Event: {:?}", event);
}
📦

Download Source

Get the latest source code archive