sxyprn fix
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
use std::error::Error;
|
||||
use std::process::Command;
|
||||
|
||||
pub mod cache;
|
||||
pub mod discord;
|
||||
pub mod flaresolverr;
|
||||
@@ -50,3 +53,37 @@ pub fn interleave<T: Clone>(lists: &[Vec<T>]) -> Vec<T> {
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
pub fn get_redirect_location(url: &str) -> Result<Option<String>, Box<dyn Error>> {
|
||||
// 1. Execute curl:
|
||||
// -s: Silent (no progress bar)
|
||||
// -I: Fetch headers only (HEAD request)
|
||||
let output = Command::new("curl")
|
||||
.arg("-sI")
|
||||
.arg(url)
|
||||
.output()?;
|
||||
|
||||
// Check if the command executed successfully
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(format!("curl command failed: {}", stderr).into());
|
||||
}
|
||||
|
||||
// 2. Parse the stdout
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
|
||||
// HTTP headers are separated by \r\n or \n
|
||||
for line in stdout.lines() {
|
||||
// Case-insensitive check for "Location:"
|
||||
if line.to_lowercase().starts_with("location:") {
|
||||
// Split "Location: https://example.com" into ["Location", " https://example.com"]
|
||||
let parts: Vec<&str> = line.splitn(2, ':').collect();
|
||||
if parts.len() == 2 {
|
||||
// Trim whitespace and potential carriage returns (\r)
|
||||
return Ok(Some(parts[1].trim().to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
Reference in New Issue
Block a user