forked from TeamPiped/piped-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Invidious image proxy endpoints
closes TeamPiped#111
- Loading branch information
1 parent
d6039c2
commit 7693c8b
Showing
7 changed files
with
260 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea/ | ||
.vscode/ | ||
/target | ||
*.sock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# piped-proxy | ||
|
||
A proxy for Piped written in Rust, meant to superseed [http3-ytproxy](https://github.com/TeamPiped/http3-ytproxy). | ||
A proxy for Piped written in Rust, meant to supersede [http3-ytproxy](https://github.com/TeamPiped/http3-ytproxy). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use actix_web::HttpRequest; | ||
use once_cell::sync::Lazy; | ||
use reqwest::{Client, Method, Request, Url}; | ||
use std::env; | ||
use std::net::{IpAddr, Ipv4Addr}; | ||
|
||
use crate::headers::is_header_allowed; | ||
|
||
pub static CLIENT: Lazy<Client> = Lazy::new(|| { | ||
let builder = Client::builder() | ||
.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") { | ||
reqwest::Proxy::all(proxy).ok() | ||
} else { | ||
None | ||
}; | ||
|
||
let builder = if let Some(proxy) = proxy { | ||
// proxy basic auth | ||
if let Ok(proxy_auth_user) = env::var("PROXY_USER") { | ||
let proxy_auth_pass = env::var("PROXY_PASS").unwrap_or_default(); | ||
builder.proxy(proxy.basic_auth(&proxy_auth_user, &proxy_auth_pass)) | ||
} else { | ||
builder.proxy(proxy) | ||
} | ||
} else { | ||
builder | ||
}; | ||
|
||
if crate::utils::get_env_bool("IPV4_ONLY") { | ||
builder.local_address(IpAddr::V4(Ipv4Addr::UNSPECIFIED)) | ||
} else { | ||
builder | ||
} | ||
.build() | ||
.unwrap() | ||
}); | ||
|
||
pub fn create_request(req: &HttpRequest, method: Method, url: Url) -> Request { | ||
let mut request = Request::new(method, url); | ||
let request_headers = request.headers_mut(); | ||
|
||
for (key, value) in req.headers() { | ||
if is_header_allowed(key.as_str()) { | ||
request_headers.insert(key, value.clone()); | ||
} | ||
} | ||
|
||
request | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use actix_web::HttpResponseBuilder; | ||
use reqwest::{header::HeaderMap, Response}; | ||
|
||
pub fn add_headers(response: &mut HttpResponseBuilder) { | ||
response | ||
.append_header(("Access-Control-Allow-Origin", "*")) | ||
.append_header(("Access-Control-Allow-Headers", "*")) | ||
.append_header(("Access-Control-Allow-Methods", "*")) | ||
.append_header(("Access-Control-Max-Age", "1728000")); | ||
} | ||
|
||
pub fn is_header_allowed(header: &str) -> bool { | ||
if header.starts_with("access-control") { | ||
return false; | ||
} | ||
|
||
!matches!( | ||
header, | ||
"host" | ||
| "content-length" | ||
| "set-cookie" | ||
| "alt-svc" | ||
| "accept-ch" | ||
| "report-to" | ||
| "strict-transport-security" | ||
| "user-agent" | ||
| "range" | ||
| "transfer-encoding" | ||
| "x-real-ip" | ||
| "origin" | ||
| "referer" | ||
// the 'x-title' header contains non-ascii characters which is not allowed on some HTTP clients | ||
| "x-title" | ||
) | ||
} | ||
|
||
pub fn get_content_length(headers: &HeaderMap) -> Option<u64> { | ||
headers | ||
.get("content-length") | ||
.and_then(|cl| cl.to_str().ok()) | ||
.and_then(|cl| str::parse::<u64>(cl).ok()) | ||
} | ||
|
||
pub fn copy_response_headers(req_resp: &Response, http_resp: &mut HttpResponseBuilder) { | ||
add_headers(http_resp); | ||
|
||
for (key, value) in req_resp.headers() { | ||
if is_header_allowed(key.as_str()) { | ||
http_resp.append_header((key.as_str(), value.as_bytes())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.