Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support Invidious image proxy endpoints #257

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
.vscode/
/target
*.sock
27 changes: 24 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,24 @@ blake3 = { version = "1.5.1", optional = true }
bytes = "1.6.0"
futures-util = "0.3.30"
listenfd = "1.0.1"
thiserror = { version = "2.0.3", optional = true }

[features]
default = ["webp", "mimalloc", "reqwest-rustls", "qhash"]

reqwest-rustls = ["reqwest/rustls-tls"]
reqwest-native-tls = ["reqwest/default-tls"]

avif = ["dep:ravif", "dep:rgb", "dep:image"]
webp = ["dep:libwebp-sys", "dep:image"]
avif = ["dep:ravif", "dep:rgb", "dep:image", "dep:thiserror"]
webp = ["dep:libwebp-sys", "dep:image", "dep:thiserror"]

mimalloc = ["dep:mimalloc"]

optimized = ["libwebp-sys?/sse41", "libwebp-sys?/avx2", "libwebp-sys?/neon"]

qhash = ["blake3"]

invidious = []

[profile.release]
lto = true
2 changes: 1 addition & 1 deletion README.md
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).
51 changes: 51 additions & 0 deletions src/client.rs
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
}
52 changes: 52 additions & 0 deletions src/headers.rs
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()));
}
}
}
Loading