use crate::error::FetchiumError;
pub const TOR_SOCKS5: &str = "socks5://127.0.0.1:9050";
pub fn is_tor_available() -> bool {
use std::net::TcpStream;
use std::time::Duration;
TcpStream::connect_timeout(&"127.0.0.1:9050".parse().unwrap(), Duration::from_secs(2)).is_ok()
}
pub fn tor_user_agent() -> &'static str {
"Mozilla/5.0 (Windows NT 10.0; rv:128.0) Gecko/20100101 Firefox/128.0"
}
pub fn install_hint() -> &'static str {
if cfg!(target_os = "macos") {
"Install Tor: `brew install tor && brew services start tor`"
} else if cfg!(target_os = "linux") {
"Install Tor: `sudo apt-get install tor && sudo systemctl start tor`"
} else {
"Download Tor Browser Bundle from https://www.torproject.org/"
}
}
pub fn require_tor() -> Result<(), FetchiumError> {
if is_tor_available() {
Ok(())
} else {
Err(FetchiumError::Internal(format!(
"Tor SOCKS5 proxy not reachable at 127.0.0.1:9050. {}",
install_hint()
)))
}
}