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");
|
||||
|
||||
let proxy = if let Ok(proxy) = env::var("PROXY") {
|
||||
Some(reqwest::Proxy::all(proxy).unwrap())
|
||||
reqwest::Proxy::all(proxy).ok()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
@ -75,13 +75,12 @@ static CLIENT: Lazy<Client> = Lazy::new(|| {
|
|||
};
|
||||
|
||||
if get_env_bool("IPV4_ONLY") {
|
||||
builder
|
||||
.local_address(Some("0.0.0.0".parse().unwrap()))
|
||||
.build()
|
||||
.unwrap()
|
||||
builder.local_address("0.0.0.0".parse().ok())
|
||||
} 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)";
|
||||
|
@ -154,13 +153,9 @@ async fn index(req: HttpRequest) -> Result<HttpResponse, Box<dyn Error>> {
|
|||
|
||||
let secret = env::var("HASH_SECRET");
|
||||
if let Ok(secret) = secret {
|
||||
let qhash = query.get("qhash");
|
||||
|
||||
if qhash.is_none() {
|
||||
let Some(qhash) = query.get("qhash") else {
|
||||
return Err("No qhash provided".into());
|
||||
}
|
||||
|
||||
let qhash = qhash.unwrap();
|
||||
};
|
||||
|
||||
if qhash.len() != 8 {
|
||||
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 res = res.map(|s| s.to_string());
|
||||
|
||||
if res.is_none() {
|
||||
let Some(host) = query.get("host").map(|s| s.to_string()) else {
|
||||
return Err("No host provided".into());
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(any(feature = "webp", feature = "avif"))]
|
||||
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")]
|
||||
let avif = query.get("avif") == Some("true");
|
||||
|
||||
let host = res.unwrap();
|
||||
let domain = RE_DOMAIN.captures(host.as_str());
|
||||
|
||||
if domain.is_none() {
|
||||
let Some(domain) = RE_DOMAIN
|
||||
.captures(host.as_str())
|
||||
.map(|domain| domain.get(1).unwrap().as_str())
|
||||
else {
|
||||
return Err("Invalid host provided".into());
|
||||
}
|
||||
|
||||
let domain = domain.unwrap().get(1).unwrap().as_str();
|
||||
};
|
||||
|
||||
if !ALLOWED_DOMAINS.contains(&domain) {
|
||||
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());
|
||||
}
|
||||
|
||||
let resp = CLIENT.execute(request).await;
|
||||
|
||||
if resp.is_err() {
|
||||
return Err(resp.err().unwrap().into());
|
||||
}
|
||||
|
||||
let resp = resp?;
|
||||
let resp = CLIENT.execute(request).await?;
|
||||
|
||||
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<_>>());
|
||||
query.add_pair(("qhash", qhash.unwrap()));
|
||||
query.add_pair(("qhash", qhash));
|
||||
return format!("{}?{}", path, query);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue