feat: add proxy for avatar fetching

This commit is contained in:
MeexReay 2025-09-01 17:31:52 +03:00
parent e26cf0ffa7
commit dba2b73e56
2 changed files with 18 additions and 4 deletions

View File

@ -20,7 +20,7 @@ notify-rust = { version = "4.11.7", optional = true }
gdk-pixbuf = { version = "0.3.0", optional = true } # DO NOT UPDATE gdk-pixbuf = { version = "0.3.0", optional = true } # DO NOT UPDATE
winapi = { version = "0.3.9", optional = true, features = ["wincon", "winuser"] } winapi = { version = "0.3.9", optional = true, features = ["wincon", "winuser"] }
tungstenite = "0.27.0" tungstenite = "0.27.0"
reqwest = { version = "0.12.20", features = ["blocking"] } reqwest = { version = "0.12.20", features = ["blocking", "socks"] }
libadwaita = { version = "0.8.0", optional = true, features = ["v1_6"] } libadwaita = { version = "0.8.0", optional = true, features = ["v1_6"] }
[build-dependencies] [build-dependencies]

View File

@ -971,7 +971,7 @@ fn setup(_: &Application, ctx: Arc<Context>, ui: UiModel) {
let Some(avatar_url) = grab_avatar(message) else { continue }; let Some(avatar_url) = grab_avatar(message) else { continue };
let avatar_id = get_avatar_id(&avatar_url); let avatar_id = get_avatar_id(&avatar_url);
let Some(avatar) = load_avatar(&avatar_url, ctx.config(|o| o.max_avatar_size as usize)) else { println!("cant load avatar: {avatar_url} request error"); continue }; let Some(avatar) = load_avatar(&avatar_url, ctx.config(|o| o.proxy.clone()), ctx.config(|o| o.max_avatar_size as usize)) else { println!("cant load avatar: {avatar_url} request error"); continue };
let Ok(pixbuf) = load_pixbuf(&avatar) else { println!("cant load avatar: {avatar_url} pixbuf error"); continue; }; let Ok(pixbuf) = load_pixbuf(&avatar) else { println!("cant load avatar: {avatar_url} pixbuf error"); continue; };
let Some(pixbuf) = pixbuf.scale_simple(32, 32, InterpType::Bilinear) else { println!("cant load avatar: {avatar_url} scale image error"); continue }; let Some(pixbuf) = pixbuf.scale_simple(32, 32, InterpType::Bilinear) else { println!("cant load avatar: {avatar_url} scale image error"); continue };
let texture = Texture::for_pixbuf(&pixbuf); let texture = Texture::for_pixbuf(&pixbuf);
@ -1178,8 +1178,22 @@ fn get_avatar_id(url: &str) -> u64 {
hasher.finish() hasher.finish()
} }
fn load_avatar(url: &str, response_limit: usize) -> Option<Vec<u8>> { fn load_avatar(url: &str, proxy: Option<String>, response_limit: usize) -> Option<Vec<u8>> {
reqwest::blocking::get(url).ok() let client = if let Some(proxy) = proxy {
let proxy = if proxy.starts_with("socks5://") {
proxy
} else {
format!("socks5://{proxy}")
};
reqwest::blocking::Client::builder()
.proxy(reqwest::Proxy::all(&proxy).ok()?)
.build().ok()?
} else {
reqwest::blocking::Client::new()
};
client.get(url).send().ok()
.and_then(|mut resp| { .and_then(|mut resp| {
let mut data = Vec::new(); let mut data = Vec::new();
let mut length = 0; let mut length = 0;