From 80e0f84b6234a478eacffe2cbbbe00b6b8fb705f Mon Sep 17 00:00:00 2001 From: Josiah Glosson Date: Tue, 15 Jul 2025 20:10:24 -0500 Subject: [PATCH] Use single LAUNCHER_USER_AGENT constant for all user agents --- apps/app/src/main.rs | 12 +++++++++++- apps/app/src/updater_impl.rs | 9 ++++----- packages/app-lib/src/lib.rs | 6 ++++++ packages/app-lib/src/state/friends.rs | 7 ++----- packages/app-lib/src/util/fetch.rs | 8 +++----- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/apps/app/src/main.rs b/apps/app/src/main.rs index f3a177d2d..8c4ce69ba 100644 --- a/apps/app/src/main.rs +++ b/apps/app/src/main.rs @@ -116,7 +116,17 @@ fn main() { #[cfg(feature = "updater")] { - builder = builder.plugin(tauri_plugin_updater::Builder::new().build()); + use tauri_plugin_http::reqwest::header::{HeaderValue, USER_AGENT}; + use theseus::LAUNCHER_USER_AGENT; + builder = builder.plugin( + tauri_plugin_updater::Builder::new() + .header( + USER_AGENT, + HeaderValue::from_str(LAUNCHER_USER_AGENT).unwrap(), + ) + .unwrap() + .build(), + ); } builder = builder diff --git a/apps/app/src/updater_impl.rs b/apps/app/src/updater_impl.rs index 2f38b5e89..0a515b2c1 100644 --- a/apps/app/src/updater_impl.rs +++ b/apps/app/src/updater_impl.rs @@ -7,12 +7,11 @@ use tauri_plugin_http::reqwest; use tauri_plugin_http::reqwest::ClientBuilder; use tauri_plugin_updater::Error; use tauri_plugin_updater::Update; -use theseus::{LoadingBarType, emit_loading, init_loading}; +use theseus::{ + LAUNCHER_USER_AGENT, LoadingBarType, emit_loading, init_loading, +}; use tokio::time::Instant; -const UPDATER_USER_AGENT: &str = - concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); - #[derive(Default)] pub struct PendingUpdateData(pub Mutex, Vec)>>); @@ -32,7 +31,7 @@ pub async fn get_update_size( ); } - let mut request = ClientBuilder::new().user_agent(UPDATER_USER_AGENT); + let mut request = ClientBuilder::new().user_agent(LAUNCHER_USER_AGENT); if let Some(timeout) = update.timeout { request = request.timeout(timeout); } diff --git a/packages/app-lib/src/lib.rs b/packages/app-lib/src/lib.rs index 55f4459ac..c6c280635 100644 --- a/packages/app-lib/src/lib.rs +++ b/packages/app-lib/src/lib.rs @@ -26,3 +26,9 @@ pub use event::{ }; pub use logger::start_logger; pub use state::State; + +pub const LAUNCHER_USER_AGENT: &str = concat!( + "modrinth/theseus/", + env!("CARGO_PKG_VERSION"), + " (support@modrinth.com)" +); diff --git a/packages/app-lib/src/state/friends.rs b/packages/app-lib/src/state/friends.rs index 008660d9f..410f20076 100644 --- a/packages/app-lib/src/state/friends.rs +++ b/packages/app-lib/src/state/friends.rs @@ -1,3 +1,4 @@ +use crate::LAUNCHER_USER_AGENT; use crate::config::{MODRINTH_API_URL_V3, MODRINTH_SOCKET_URL}; use crate::data::ModrinthCredentials; use crate::event::FriendPayload; @@ -82,13 +83,9 @@ impl FriendsSocket { ) .into_client_request()?; - let user_agent = format!( - "modrinth/theseus/{} (support@modrinth.com)", - env!("CARGO_PKG_VERSION") - ); request.headers_mut().insert( "User-Agent", - HeaderValue::from_str(&user_agent).unwrap(), + HeaderValue::from_str(LAUNCHER_USER_AGENT).unwrap(), ); let res = connect_async(request).await; diff --git a/packages/app-lib/src/util/fetch.rs b/packages/app-lib/src/util/fetch.rs index fb62386aa..c4469e33c 100644 --- a/packages/app-lib/src/util/fetch.rs +++ b/packages/app-lib/src/util/fetch.rs @@ -1,5 +1,6 @@ //! Functions for fetching information from the Internet use super::io::{self, IOError}; +use crate::LAUNCHER_USER_AGENT; use crate::config::{MODRINTH_API_URL, MODRINTH_API_URL_V3}; use crate::event::LoadingBarId; use crate::event::emit::emit_loading; @@ -20,11 +21,8 @@ pub struct FetchSemaphore(pub Semaphore); pub static REQWEST_CLIENT: LazyLock = LazyLock::new(|| { let mut headers = reqwest::header::HeaderMap::new(); - let header = reqwest::header::HeaderValue::from_str(&format!( - "modrinth/theseus/{} (support@modrinth.com)", - env!("CARGO_PKG_VERSION") - )) - .unwrap(); + let header = + reqwest::header::HeaderValue::from_str(LAUNCHER_USER_AGENT).unwrap(); headers.insert(reqwest::header::USER_AGENT, header); reqwest::Client::builder() .tcp_keepalive(Some(time::Duration::from_secs(10)))