From 71519af2f3e4f1929783ab50971e7eff4a6b1723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=B4=80=E1=B4=8D=E1=B4=9B=E1=B4=8F=E1=B4=80=E1=B4=87?= =?UTF-8?q?=CA=80?= Date: Thu, 28 Aug 2025 18:51:23 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E7=A7=BB=E9=99=A4=E4=B8=8D=E5=BF=85?= =?UTF-8?q?=E8=A6=81=E7=9A=84=20image-proxy=20(#451)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/bili_sync/src/api/request.rs | 5 -- crates/bili_sync/src/api/routes/mod.rs | 53 ++----------------- web/src/app.html | 1 + .../lib/components/subscription-card.svelte | 2 +- web/vite.config.ts | 3 +- 5 files changed, 6 insertions(+), 58 deletions(-) diff --git a/crates/bili_sync/src/api/request.rs b/crates/bili_sync/src/api/request.rs index 7a8095a..d9da879 100644 --- a/crates/bili_sync/src/api/request.rs +++ b/crates/bili_sync/src/api/request.rs @@ -81,11 +81,6 @@ pub struct InsertSubmissionRequest { pub path: String, } -#[derive(Deserialize)] -pub struct ImageProxyParams { - pub url: String, -} - #[derive(Deserialize, Validate)] pub struct UpdateVideoSourceRequest { #[validate(custom(function = "crate::utils::validation::validate_path"))] diff --git a/crates/bili_sync/src/api/routes/mod.rs b/crates/bili_sync/src/api/routes/mod.rs index c3157e1..4706aff 100644 --- a/crates/bili_sync/src/api/routes/mod.rs +++ b/crates/bili_sync/src/api/routes/mod.rs @@ -1,20 +1,13 @@ -use std::collections::HashSet; -use std::sync::Arc; - -use axum::body::Body; -use axum::extract::{Extension, Query, Request}; +use axum::extract::Request; use axum::http::HeaderMap; use axum::middleware::Next; use axum::response::{IntoResponse, Response}; -use axum::routing::get; use axum::{Router, middleware}; use base64::Engine; use base64::prelude::BASE64_URL_SAFE_NO_PAD; -use reqwest::{Method, StatusCode, header}; +use reqwest::StatusCode; -use super::request::ImageProxyParams; use crate::api::wrapper::ApiResponse; -use crate::bilibili::BiliClient; use crate::config::VersionedConfig; mod config; @@ -27,7 +20,7 @@ mod ws; pub use ws::{LogHelper, MAX_HISTORY_LOGS}; pub fn router() -> Router { - Router::new().route("/image-proxy", get(image_proxy)).nest( + Router::new().nest( "/api", config::router() .merge(me::router()) @@ -64,43 +57,3 @@ pub async fn auth(mut headers: HeaderMap, request: Request, next: Next) -> Resul } Ok(ApiResponse::<()>::unauthorized("auth token does not match").into_response()) } - -/// B 站的图片会检查 referer,需要做个转发伪造一下,否则直接返回 403 -pub async fn image_proxy( - Extension(bili_client): Extension>, - Query(params): Query, -) -> Response { - let resp = bili_client.client.request(Method::GET, ¶ms.url, None).send().await; - let whitelist = [ - header::CONTENT_TYPE, - header::CONTENT_LENGTH, - header::CACHE_CONTROL, - header::EXPIRES, - header::LAST_MODIFIED, - header::ETAG, - header::CONTENT_DISPOSITION, - header::CONTENT_ENCODING, - header::ACCEPT_RANGES, - header::ACCESS_CONTROL_ALLOW_ORIGIN, - ] - .into_iter() - .collect::>(); - - let builder = Response::builder(); - - let response = match resp { - Err(e) => builder.status(StatusCode::BAD_GATEWAY).body(Body::new(e.to_string())), - Ok(res) => { - let mut response = builder.status(res.status()); - for (k, v) in res.headers() { - if whitelist.contains(k) { - response = response.header(k, v); - } - } - let streams = res.bytes_stream(); - response.body(Body::from_stream(streams)) - } - }; - //safety: all previously configured headers are taken from a valid response, ensuring the response is safe to use - response.unwrap() -} diff --git a/web/src/app.html b/web/src/app.html index 77a5ff5..5ed421c 100644 --- a/web/src/app.html +++ b/web/src/app.html @@ -4,6 +4,7 @@ + %sveltekit.head% diff --git a/web/src/lib/components/subscription-card.svelte b/web/src/lib/components/subscription-card.svelte index 924c68b..fd2e435 100644 --- a/web/src/lib/components/subscription-card.svelte +++ b/web/src/lib/components/subscription-card.svelte @@ -125,7 +125,7 @@ function getAvatarUrl(): string { switch (type) { case 'upper': - return `/image-proxy?url=${(item as UpperWithSubscriptionStatus).face}`; + return (item as UpperWithSubscriptionStatus).face; default: return ''; } diff --git a/web/vite.config.ts b/web/vite.config.ts index e4ecc78..0ecf9a0 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -11,8 +11,7 @@ export default defineConfig({ ws: true, rewriteWsOrigin: true }, - '/api': 'http://localhost:12345', - '/image-proxy': 'http://localhost:12345' + '/api': 'http://localhost:12345' }, host: true }