Expand description
§stem-rs
A Rust implementation of the Stem library for Tor control protocol interaction.
§Overview
stem-rs provides idiomatic Rust APIs for interacting with Tor’s control protocol, maintaining functional parity with Python Stem. The library enables:
- Control socket communication (TCP and Unix domain sockets)
- All authentication methods (NONE, PASSWORD, COOKIE, SAFECOOKIE)
- High-level Controller API for Tor interaction
- Complete descriptor parsing (server, micro, consensus, extra-info, hidden service)
- Event subscription and handling
- Exit policy parsing and evaluation
- ORPort relay communication
- Version parsing and comparison
§Architecture
The library is organized into these primary modules:
socket: Low-level control socket communicationauth: Authentication methods and protocol infocontroller: High-level Controller APIdescriptor: Tor descriptor parsingevents: Event types and handlingexit_policy: Exit policy evaluationclient: Direct ORPort relay communicationresponse: Control protocol response parsinginterpreter: Interactive Tor control interpreterversion: Tor version parsing and comparisonutil: Validation utilities for fingerprints, nicknames, etc.
§Quick Start
use stem_rs::{controller::Controller, Error};
#[tokio::main]
async fn main() -> Result<(), Error> {
// Connect to Tor's control port
let mut controller = Controller::from_port("127.0.0.1:9051".parse().unwrap()).await?;
// Authenticate (auto-detects method)
controller.authenticate(None).await?;
// Query Tor version
let version = controller.get_version().await?;
println!("Connected to Tor {}", version);
Ok(())
}§Thread Safety
The controller::Controller type is Send but not Sync. For concurrent access,
wrap it in Arc<Mutex<Controller>> or use separate connections.
§Security Considerations
- Authentication tokens are cleared from memory after use
- Constant-time comparison is used for sensitive data (see
util::secure_compare) - Input validation prevents protocol injection attacks
§Error Handling
All fallible operations return Result<T, Error>. The [Error] enum provides
specific error variants for different failure modes:
Error::Socket- I/O and connection failuresError::Authentication- Authentication failures (seeAuthError)Error::OperationFailed- Tor rejected the operationError::Parse- Descriptor parsing failures
See the [Error] documentation for recovery guidance.
Re-exports§
pub use controller::Controller;pub use socket::ControlSocket;pub use version::Version;
Modules§
- auth
- Authentication methods for Tor control protocol.
- client
- ORPort client module for direct relay communication.
- controller
- High-level controller API for Tor control protocol interaction.
- descriptor
- Descriptor parsing for Tor network documents.
- events
- Event types and handling for Tor control protocol async notifications.
- exit_
policy - Exit policy parsing and evaluation for Tor relays.
- interpreter
- Interactive interpreter for Tor control protocol.
- protocol
- Control protocol message parsing for Tor control protocol.
- response
- Response parsing for Tor control protocol messages.
- socket
- Control socket communication with Tor’s control interface.
- util
- Validation and helper functions for Tor-related data.
- version
- Tor version parsing and comparison.
Enums§
- Auth
Descriptor Action - Actions that directory authorities take with relay descriptors.
- Auth
Error - Authentication-specific errors.
- Bridge
Distribution - Bridge distribution methods.
- Circ
Build Flag - Attributes about how a circuit is built.
- Circ
Closure Reason - Reason that a circuit is being closed or failed to be established.
- Circ
Event - Type of change reflected in a circuit by a CIRC_MINOR event.
- Circ
Purpose - Purpose of a circuit.
- Circ
Status - Status of a circuit in the Tor network.
- Connection
Type - Purpose for a Tor connection.
- Error
- Errors that can occur during stem-rs operations.
- Event
Type - Types of events that can be subscribed to via the control protocol.
- Flag
- Flags assigned to Tor relays by directory authorities.
- Guard
Status - Status of a guard relay.
- Guard
Type - Type of guard relay usage.
- Hidden
Service State - State of a hidden service circuit.
- HsAuth
- Type of authentication for a HS_DESC event.
- HsDesc
Action - Action being taken in a HS_DESC event.
- HsDesc
Reason - Reason for a hidden service descriptor fetch to fail.
- OrClosure
Reason - Reason that an OR connection is being closed or failed.
- OrStatus
- Status of an OR (Onion Router) connection.
- Runlevel
- Logging severity levels for Tor events.
- Signal
- Signals that can be sent to the Tor process.
- Status
Type - Source of a status event.
- Stream
Closure Reason - Reason that a stream is being closed or failed to be established.
- Stream
Purpose - Purpose of a stream.
- Stream
Source - Cause of a stream being remapped to another address.
- Stream
Status - Status of a stream going through Tor.
- Timeout
SetType - Way in which the timeout value of a circuit is changing.
- Token
Bucket - Bucket categories for TB_EMPTY events.