pub async fn control_loop(state: &mut AppState) -> StringExpand description
Main control loop for event processing.
Connects to Tor, authenticates, initializes state, and processes events in a continuous loop until the connection is lost or an error occurs.
§Flow
- Connect to Tor’s control port (socket or TCP)
- Authenticate using available methods
- Get Tor version for feature detection
- Initialize vanguard state from consensus
- Initialize optional components (logguard, pathverify)
- Subscribe to configured event types
- Process events until connection closes
§Arguments
state- The application state containing all protection components
§Returns
Returns a status string:
"closed"- Connection was closed normally"failed: <reason>"- Connection or operation failed
§Event Processing
The loop dispatches events to appropriate handlers:
| Event Type | Handlers |
|---|---|
| CIRC | RendGuard, BandGuards, CBTVerify, PathVerify, LogGuard |
| CIRC_BW | BandGuards |
| CIRC_MINOR | BandGuards, PathVerify |
| ORCONN | BandGuards, PathVerify |
| BW | BandGuards (connectivity check) |
| NEWCONSENSUS | VanguardState update |
| SIGNAL | Configuration reload (SIGHUP) |
§Example
use vanguards_rs::control::{AppState, control_loop};
use vanguards_rs::vanguards::VanguardState;
use vanguards_rs::config::Config;
let state = VanguardState::new("/tmp/vanguards.state");
let config = Config::default();
let mut app_state = AppState::new(state, config);
let result = control_loop(&mut app_state).await;
println!("Control loop exited: {}", result);§See Also
run_main- Higher-level entry point with reconnection supportauthenticate_any- Authentication implementationnew_consensus_event- Consensus processing