mirror of
https://github.com/TeamPiped/piped-proxy.git
synced 2024-08-14 23:50:45 +00:00
refactor: simplify error handling
This commit is contained in:
parent
f0a5da9dcb
commit
3f8a33b9d6
2 changed files with 17 additions and 33 deletions
46
src/main.rs
46
src/main.rs
|
@ -57,7 +57,7 @@ static CLIENT: Lazy<Client> = Lazy::new(|| {
|
||||||
.user_agent("Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0");
|
.user_agent("Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0");
|
||||||
|
|
||||||
let proxy = if let Ok(proxy) = env::var("PROXY") {
|
let proxy = if let Ok(proxy) = env::var("PROXY") {
|
||||||
Some(reqwest::Proxy::all(proxy).unwrap())
|
reqwest::Proxy::all(proxy).ok()
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
@ -75,13 +75,12 @@ static CLIENT: Lazy<Client> = Lazy::new(|| {
|
||||||
};
|
};
|
||||||
|
|
||||||
if get_env_bool("IPV4_ONLY") {
|
if get_env_bool("IPV4_ONLY") {
|
||||||
builder
|
builder.local_address("0.0.0.0".parse().ok())
|
||||||
.local_address(Some("0.0.0.0".parse().unwrap()))
|
|
||||||
.build()
|
|
||||||
.unwrap()
|
|
||||||
} else {
|
} else {
|
||||||
builder.build().unwrap()
|
builder
|
||||||
}
|
}
|
||||||
|
.build()
|
||||||
|
.unwrap()
|
||||||
});
|
});
|
||||||
|
|
||||||
const ANDROID_USER_AGENT: &str = "com.google.android.youtube/1537338816 (Linux; U; Android 13; en_US; ; Build/TQ2A.230505.002; Cronet/113.0.5672.24)";
|
const ANDROID_USER_AGENT: &str = "com.google.android.youtube/1537338816 (Linux; U; Android 13; en_US; ; Build/TQ2A.230505.002; Cronet/113.0.5672.24)";
|
||||||
|
@ -154,13 +153,9 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
|
|
||||||
let secret = env::var("HASH_SECRET");
|
let secret = env::var("HASH_SECRET");
|
||||||
if let Ok(secret) = secret {
|
if let Ok(secret) = secret {
|
||||||
let qhash = query.get("qhash");
|
let Some(qhash) = query.get("qhash") else {
|
||||||
|
|
||||||
if qhash.is_none() {
|
|
||||||
return Err("No qhash provided".into());
|
return Err("No qhash provided".into());
|
||||||
}
|
};
|
||||||
|
|
||||||
let qhash = qhash.unwrap();
|
|
||||||
|
|
||||||
if qhash.len() != 8 {
|
if qhash.len() != 8 {
|
||||||
return Err("Invalid qhash provided".into());
|
return Err("Invalid qhash provided".into());
|
||||||
|
@ -218,12 +213,9 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = query.get("host");
|
let Some(host) = query.get("host").map(|s| s.to_string()) else {
|
||||||
let res = res.map(|s| s.to_string());
|
|
||||||
|
|
||||||
if res.is_none() {
|
|
||||||
return Err("No host provided".into());
|
return Err("No host provided".into());
|
||||||
}
|
};
|
||||||
|
|
||||||
#[cfg(any(feature = "webp", feature = "avif"))]
|
#[cfg(any(feature = "webp", feature = "avif"))]
|
||||||
let disallow_image_transcoding = get_env_bool("DISALLOW_IMAGE_TRANSCODING");
|
let disallow_image_transcoding = get_env_bool("DISALLOW_IMAGE_TRANSCODING");
|
||||||
|
@ -233,14 +225,12 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
#[cfg(feature = "avif")]
|
#[cfg(feature = "avif")]
|
||||||
let avif = query.get("avif") == Some("true");
|
let avif = query.get("avif") == Some("true");
|
||||||
|
|
||||||
let host = res.unwrap();
|
let Some(domain) = RE_DOMAIN
|
||||||
let domain = RE_DOMAIN.captures(host.as_str());
|
.captures(host.as_str())
|
||||||
|
.map(|domain| domain.get(1).unwrap().as_str())
|
||||||
if domain.is_none() {
|
else {
|
||||||
return Err("Invalid host provided".into());
|
return Err("Invalid host provided".into());
|
||||||
}
|
};
|
||||||
|
|
||||||
let domain = domain.unwrap().get(1).unwrap().as_str();
|
|
||||||
|
|
||||||
if !ALLOWED_DOMAINS.contains(&domain) {
|
if !ALLOWED_DOMAINS.contains(&domain) {
|
||||||
return Err("Domain not allowed".into());
|
return Err("Domain not allowed".into());
|
||||||
|
@ -323,13 +313,7 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
||||||
request_headers.insert("User-Agent", ANDROID_USER_AGENT.parse().unwrap());
|
request_headers.insert("User-Agent", ANDROID_USER_AGENT.parse().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
let resp = CLIENT.execute(request).await;
|
let resp = CLIENT.execute(request).await?;
|
||||||
|
|
||||||
if resp.is_err() {
|
|
||||||
return Err(resp.err().unwrap().into());
|
|
||||||
}
|
|
||||||
|
|
||||||
let resp = resp?;
|
|
||||||
|
|
||||||
let mut response = HttpResponse::build(resp.status());
|
let mut response = HttpResponse::build(resp.status());
|
||||||
|
|
||||||
|
|
|
@ -43,9 +43,9 @@ fn finalize_url(path: &str, query: BTreeMap<String, String>) -> String {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if qhash.is_some() {
|
if let Some(qhash) = qhash {
|
||||||
let mut query = QString::new(query.into_iter().collect::<Vec<_>>());
|
let mut query = QString::new(query.into_iter().collect::<Vec<_>>());
|
||||||
query.add_pair(("qhash", qhash.unwrap()));
|
query.add_pair(("qhash", qhash));
|
||||||
return format!("{}?{}", path, query);
|
return format!("{}?{}", path, query);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue