Spaces:
Build error
Build error
Download src/meta_search/proxy_manager.rs from LuckyMod/intentforge: direct link, hf CLI and curl.
- Browser
- Download file 5.12 kB
-
https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/meta_search/proxy_manager.rs
- Command line
-
hf download hf://spaces/LuckyMod/intentforge/src/meta_search/proxy_manager.rs
-
curl -L -o proxy_manager.rs https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/meta_search/proxy_manager.rs
5.12 kB
| use crate::meta_search::proxy::{Proxy, ProxyProtocol}; | |
| use anyhow::Result; | |
| use futures::future::join_all; | |
| use rand::seq::SliceRandom; | |
| use reqwest::Client; | |
| use std::sync::Arc; | |
| use std::time::Duration; | |
| use tokio::sync::RwLock; | |
| use tracing::{info, warn}; | |
| pub struct ProxyManager { | |
| proxies: Arc<RwLock<Vec<Proxy>>>, | |
| client: Client, | |
| } | |
| impl ProxyManager { | |
| pub fn new() -> Self { | |
| Self { | |
| proxies: Arc::new(RwLock::new(Vec::new())), | |
| client: Client::builder() | |
| .timeout(Duration::from_secs(5)) | |
| .build() | |
| .unwrap(), | |
| } | |
| } | |
| /// Scrape proxies from public lists and validate them. | |
| pub async fn scrape_proxies(&self) -> Result<()> { | |
| info!("Scraping for new proxies..."); | |
| let sources = vec![ | |
| "https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks5.txt", | |
| "https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/http.txt", | |
| "https://raw.githubusercontent.com/ShiftyTR/Proxy-List/master/socks5.txt", | |
| "https://raw.githubusercontent.com/ShiftyTR/Proxy-List/master/http.txt", | |
| "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/socks5.txt", | |
| "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt", | |
| "https://raw.githubusercontent.com/hookzof/socks5_list/master/proxy.txt", | |
| "https://raw.githubusercontent.com/roosterkid/openproxylist/main/SOCKS5_RAW.txt", | |
| "https://raw.githubusercontent.com/roosterkid/openproxylist/main/HTTPS_RAW.txt", | |
| "https://api.proxyscrape.com/v2/?request=displayproxies&protocol=socks5&timeout=10000&country=all&ssl=all&anonymity=all", | |
| "https://api.proxyscrape.com/v2/?request=displayproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all", | |
| ]; | |
| let mut new_proxies = Vec::new(); | |
| for source in sources { | |
| match self.client.get(source).send().await { | |
| Ok(resp) => { | |
| if let Ok(text) = resp.text().await { | |
| let proto = if source.contains("socks5") { | |
| ProxyProtocol::Socks5 | |
| } else { | |
| ProxyProtocol::Http | |
| }; | |
| for line in text.lines() { | |
| let parts: Vec<&str> = line.split(':').collect(); | |
| if parts.len() == 2 { | |
| if let Ok(port) = parts[1].parse::<u16>() { | |
| new_proxies.push(Proxy::new( | |
| parts[0].to_string(), | |
| port, | |
| proto.clone(), | |
| )); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| Err(e) => warn!("Failed to fetch proxies from {}: {}", source, e), | |
| } | |
| } | |
| info!( | |
| "Scraped {} potential proxies. Starting validation...", | |
| new_proxies.len() | |
| ); | |
| self.validate_and_add(new_proxies).await; | |
| Ok(()) | |
| } | |
| async fn validate_and_add(&self, proxies: Vec<Proxy>) { | |
| let to_check = proxies.into_iter().take(100).collect::<Vec<_>>(); | |
| let futures: Vec<_> = to_check | |
| .into_iter() | |
| .map(|mut proxy| async move { | |
| let start = std::time::Instant::now(); | |
| let test_url = "http://www.google.com/generate_204"; | |
| let proxy_url = proxy.to_url(); | |
| match Client::builder() | |
| .proxy(reqwest::Proxy::all(&proxy_url).ok()?) | |
| .timeout(Duration::from_secs(8)) | |
| .build() | |
| .ok()? | |
| .get(test_url) | |
| .send() | |
| .await | |
| { | |
| Ok(resp) if resp.status().is_success() => { | |
| proxy.is_working = true; | |
| proxy.latency = Some(start.elapsed()); | |
| proxy.last_checked = Some(std::time::Instant::now()); | |
| Some(proxy) | |
| } | |
| _ => None, | |
| } | |
| }) | |
| .collect(); | |
| let results = join_all(futures).await; | |
| let mut valid_proxies: Vec<Proxy> = results.into_iter().flatten().collect(); | |
| info!("Found {} working proxies", valid_proxies.len()); | |
| let mut current = self.proxies.write().await; | |
| current.append(&mut valid_proxies); | |
| } | |
| /// Get a random working proxy for use in requests. | |
| pub async fn get_random_proxy(&self) -> Option<Proxy> { | |
| let proxies = self.proxies.read().await; | |
| if proxies.is_empty() { | |
| return None; | |
| } | |
| proxies.as_slice().choose(&mut rand::thread_rng()).cloned() | |
| } | |
| } | |
| impl Default for ProxyManager { | |
| fn default() -> Self { | |
| Self::new() | |
| } | |
| } | |