Spaces:
Build error
Build error
Likhith Sai Seemala
fix: resolve compilation errors in tor.rs by adding missing imports and correcting macro calls
6a81ad7 Download src/meta_search/tor.rs from LuckyMod/intentforge: direct link, hf CLI and curl.
- Browser
- Download file 7.68 kB
-
https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/meta_search/tor.rs
- Command line
-
hf download hf://spaces/LuckyMod/intentforge/src/meta_search/tor.rs
-
curl -L -o tor.rs https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/meta_search/tor.rs
7.68 kB
| use anyhow::Result; | |
| use std::process::Command; | |
| use std::time::Duration; | |
| use tracing::{info, warn}; | |
| /// Tor integration. | |
| /// | |
| /// When compiled with `--features tor`, starts a local Tor daemon and provides | |
| /// a reqwest::Client that routes all HTTP traffic through the Tor SOCKS proxy (127.0.0.1:9050). | |
| /// Without the feature, it's a no-op stub. | |
| pub struct TorManager { | |
| tor_client: Option<reqwest::Client>, | |
| available: bool, | |
| } | |
| fn start_tor_daemon() -> Result<()> { | |
| // Check if Tor is already running | |
| let check = Command::new("nc") | |
| .args(["-z", "127.0.0.1", "9050"]) | |
| .output(); | |
| if let Ok(out) = check { | |
| if out.status.success() { | |
| info!("Tor daemon already running on 127.0.0.1:9050"); | |
| return Ok(()); | |
| } | |
| } | |
| info!("Starting Tor daemon with Snowflake bridges (bypasses exit node blocking)..."); | |
| // Ensure directories exist | |
| std::fs::create_dir_all("/tmp/tor").ok(); | |
| std::fs::create_dir_all("/tmp/tor-data").ok(); | |
| // Tor configuration with Snowflake bridges | |
| // Snowflake routes traffic through volunteer browsers, making exits appear | |
| // as regular residential IPs instead of known Tor exit nodes. | |
| // This bypasses services that block Tor exit nodes (Google, Bing, etc.) | |
| let tor_config = r#" | |
| # SOCKS proxy for applications | |
| SOCKSPort 127.0.0.1:9050 | |
| DataDirectory /tmp/tor-data | |
| # Snowflake bridge configuration | |
| # Routes traffic through volunteer WebRTC peers | |
| # Exit IPs appear as regular residential/consumer IPs | |
| ClientTransportPlugin snowflake exec /usr/bin/snowflake-client -url https://snowflake-broker.torproject.net/ -front https://www.google.com/ | |
| # Use Snowflake as bridge | |
| Bridge snowflake 192.0.2.3:1 | |
| UseBridges 1 | |
| # Prefer circuits through Snowflake bridges | |
| # This gives us diverse, non-blocked exit nodes | |
| ExitNodes {us} {gb} {de} {ca} {nl} {fr} {jp} | |
| StrictNodes 0 | |
| # Circuit building settings for reliability | |
| CircuitBuildTimeout 30 | |
| LearnCircuitBuildTimeout 1 | |
| MaxCircuitDirtiness 600 | |
| # Logging (minimal) | |
| Log notice stdout | |
| "#; | |
| std::fs::write("/tmp/tor/torrc", tor_config) | |
| .map_err(|e| anyhow::anyhow!("Failed to write torrc: {}", e))?; | |
| // Check if snowflake-client is available | |
| let snowflake_available = Command::new("which") | |
| .arg("snowflake-client") | |
| .output() | |
| .map(|o| o.status.success()) | |
| .unwrap_or(false); | |
| if !snowflake_available { | |
| info!("Snowflake client not found, using standard Tor with exit node rotation"); | |
| // Fallback: standard Tor with diverse exit nodes | |
| let fallback_config = r#" | |
| SOCKSPort 127.0.0.1:9050 | |
| DataDirectory /tmp/tor-data | |
| ExitNodes {us} {gb} {de} {ca} {nl} {fr} {jp} {au} {se} {no} | |
| StrictNodes 0 | |
| CircuitBuildTimeout 30 | |
| MaxCircuitDirtiness 300 | |
| Log notice stdout | |
| "#; | |
| std::fs::write("/tmp/tor/torrc", fallback_config) | |
| .map_err(|e| anyhow::anyhow!("Failed to write torrc: {}", e))?; | |
| } | |
| Command::new("tor") | |
| .args(["-f", "/tmp/tor/torrc"]) | |
| .spawn() | |
| .map_err(|e| anyhow::anyhow!("Failed to start tor daemon: {}", e))?; | |
| // Wait for Tor to fully bootstrap (up to 90 seconds) | |
| // Tor needs to establish circuits through the network before it can route traffic | |
| for attempt in 0..90 { | |
| std::thread::sleep(Duration::from_secs(1)); | |
| // Try a simple TCP connection through Tor to verify it's working | |
| let check = Command::new("nc") | |
| .args(["-z", "-w", "2", "127.0.0.1", "9050"]) | |
| .output(); | |
| if let Ok(out) = check { | |
| if out.status.success() { | |
| // Port is open, but we need to wait for full bootstrap | |
| // Tor typically takes 20-40 seconds to fully bootstrap | |
| if attempt >= 20 { | |
| info!( | |
| "Tor daemon bootstrapped and ready (attempt {})", | |
| attempt + 1 | |
| ); | |
| return Ok(()); | |
| } | |
| } | |
| } | |
| if attempt % 10 == 9 { | |
| info!( | |
| "Waiting for Tor to bootstrap... (attempt {}/{})", | |
| attempt + 1, | |
| 90 | |
| ); | |
| } | |
| } | |
| Err(anyhow::anyhow!( | |
| "Tor daemon failed to start within 90 seconds" | |
| )) | |
| } | |
| impl TorManager { | |
| /// Attempt to initialize a Tor-routed HTTP client. | |
| /// Never blocks startup — returns a disabled manager if it fails. | |
| pub async fn new() -> Result<Self> { | |
| { | |
| info!("Initializing Tor-routed HTTP client (SOCKS5 127.0.0.1:9050)..."); | |
| // Start Tor daemon if not already running | |
| if let Err(e) = start_tor_daemon() { | |
| warn!( | |
| "Tor daemon failed to start: {}. Falling back to proxies.", | |
| e | |
| ); | |
| return Ok(Self { tor_client: None }); | |
| } | |
| match reqwest::Client::builder() | |
| .proxy(reqwest::Proxy::all("socks5://127.0.0.1:9050").map_err(|e| anyhow::anyhow!("Failed to create Tor proxy: {}", e))?) | |
| .user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 IntentForge/0.1") | |
| .timeout(Duration::from_secs(30)) | |
| .connect_timeout(Duration::from_secs(15)) | |
| .pool_idle_timeout(Duration::from_secs(120)) | |
| .build() | |
| { | |
| Ok(client) => { | |
| // Verify Tor is actually working | |
| match client.get("https://check.torproject.org/api/ip").send().await { | |
| Ok(resp) if resp.status().is_success() => { | |
| let body = resp.text().await.unwrap_or_default(); | |
| info!("Tor client verified successfully. Response: {}", body.chars().take(100).collect::<String>()); | |
| Ok(Self { tor_client: Some(client) }) | |
| } | |
| Ok(resp) => { | |
| warn!("Tor proxy responded with status {}. Tor may not be fully bootstrapped.", resp.status()); | |
| Ok(Self { tor_client: Some(client) }) | |
| } | |
| Err(e) => { | |
| warn!("Tor verification failed ({}). Tor may not be bootstrapped yet, but client is available.", e); | |
| Ok(Self { tor_client: Some(client) }) | |
| } | |
| } | |
| } | |
| Err(e) => { | |
| warn!("Tor HTTP client creation failed ({}). Tor disabled.", e); | |
| Ok(Self { tor_client: None }) | |
| } | |
| } | |
| } | |
| { | |
| info!( | |
| "Tor feature not enabled. Build with `--features tor` for Tor support. \ | |
| Continuing with public proxies only." | |
| ); | |
| Ok(Self { available: false }) | |
| } | |
| } | |
| pub fn is_available(&self) -> bool { | |
| { | |
| self.tor_client.is_some() | |
| } | |
| { | |
| self.available | |
| } | |
| } | |
| /// Get a Tor-routed HTTP client. Returns None if Tor is not available. | |
| pub fn get_client(&self) -> Option<&reqwest::Client> { | |
| { | |
| self.tor_client.as_ref() | |
| } | |
| { | |
| None | |
| } | |
| } | |
| } | |