use ntex::web; use regex::Regex; use url::Url; use crate::util::requester::Requester; #[derive(Debug, Clone)] pub struct HqpornerProxy {} impl HqpornerProxy { pub fn new() -> Self { Self {} } fn normalize_detail_url(endpoint: &str) -> Option { let endpoint = endpoint.trim().trim_start_matches('/'); if endpoint.is_empty() { return None; } let detail_url = if endpoint.starts_with("http://") || endpoint.starts_with("https://") { endpoint.to_string() } else { format!("https://{}", endpoint.trim_start_matches('/')) }; Self::is_allowed_detail_url(&detail_url).then_some(detail_url) } fn is_allowed_detail_url(url: &str) -> bool { let Some(url) = Url::parse(url).ok() else { return false; }; if url.scheme() != "https" { return false; } let Some(host) = url.host_str() else { return false; }; (host == "hqporner.com" || host == "www.hqporner.com") && url.path().starts_with("/hdporn/") } fn normalize_url(raw: &str) -> String { let value = raw.trim(); if value.is_empty() { return String::new(); } if value.starts_with("//") { return format!("https:{value}"); } if value.starts_with('/') { return format!("https://www.hqporner.com{value}"); } if value.starts_with("http://") { return value.replacen("http://", "https://", 1); } value.to_string() } fn regex(value: &str) -> Option { Regex::new(value).ok() } fn extract_player_url(detail_html: &str) -> Option { let path = detail_html .split("url: '/blocks/altplayer.php?i=") .nth(1) .and_then(|s| s.split('\'').next())?; Some(Self::normalize_url(&format!( "/blocks/altplayer.php?i={path}" ))) } fn extract_source_url(player_html: &str) -> Option { for source in player_html.split(") -> String { let Some(detail_url) = Self::normalize_detail_url(&url) else { return String::new(); }; let mut requester = requester.get_ref().clone(); let headers = vec![("Referer".to_string(), "https://hqporner.com/".to_string())]; let detail_html = requester .get_with_headers(&detail_url, headers.clone(), None) .await .unwrap_or_default(); if detail_html.is_empty() { return String::new(); } let Some(player_url) = Self::extract_player_url(&detail_html) else { return String::new(); }; let player_html = requester .get_with_headers(&player_url, headers, None) .await .unwrap_or_default(); if player_html.is_empty() { return String::new(); } Self::extract_source_url(&player_html).unwrap_or_default() } }