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

fix: populate-user-agent-header #912

Closed
Closed
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
9 changes: 8 additions & 1 deletion src-tauri/src/binaries/adapter_github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ impl LatestVersionApiAdapter for GithubReleasesAdapter {
.join(format!("{}.sha256", asset.name));
let checksum_url = format!("{}.sha256", asset.url);

match download_file_with_retries(&checksum_url, &checksum_path, progress_tracker).await {
let user_agent = format!(
"universe {}({}) | {}",
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
"github adapter"
);

match download_file_with_retries(&checksum_url, &checksum_path, progress_tracker, user_agent.as_str()).await {
Ok(_) => Ok(checksum_path),
Err(e) => {
error!(target: LOG_TARGET, "Failed to download checksum file: {}", e);
Expand Down
9 changes: 8 additions & 1 deletion src-tauri/src/binaries/adapter_tor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ impl LatestVersionApiAdapter for TorReleaseAdapter {
.join(format!("{}.asc", asset.name));
let checksum_url = format!("{}.asc", asset.url);

match download_file_with_retries(&checksum_url, &checksum_path, progress_tracker).await {
let user_agent = format!(
"universe {}({}) | {}",
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
"github adapter"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this say "tor adapter" ?

Suggested change
"github adapter"
"tor adapter"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, only TU version and platform info I think is useful. Getting reports on TU version or platform usage from CDN provider is possible. Different adapter would not request resources from different services.

);

match download_file_with_retries(&checksum_url, &checksum_path, progress_tracker, user_agent.as_str()).await {
Ok(_) => Ok(checksum_path),
Err(e) => {
error!(target: LOG_TARGET, "Failed to download checksum file: {}", e);
Expand Down
7 changes: 7 additions & 0 deletions src-tauri/src/binaries/binaries_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,17 @@ impl BinaryManager {
.map_err(|e| anyhow!("Error creating in progress folder. Error: {:?}", e))?;
let in_progress_file_zip = in_progress_dir.join(asset.name.clone());

let user_agent = format!(
"universe {}({}) | {}",
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
self.binary_name
);
download_file_with_retries(
asset.url.as_str(),
&in_progress_file_zip,
progress_tracker.clone(),
user_agent.as_str(),
)
.await
.map_err(|e| anyhow!("Error downloading version: {:?}. Error: {:?}", version, e))?;
Expand Down
11 changes: 9 additions & 2 deletions src-tauri/src/download_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ pub async fn download_file_with_retries(
url: &str,
destination: &Path,
progress_tracker: ProgressTracker,
user_agent: &str,
) -> Result<(), Error> {
let mut retries = 0;
loop {
match download_file(url, destination, progress_tracker.clone()).await {
match download_file(url, destination, progress_tracker.clone(), user_agent).await {
Ok(_) => return Ok(()),
Err(err) => {
if retries >= 3 {
Expand All @@ -43,8 +44,14 @@ async fn download_file(
url: &str,
destination: &Path,
progress_tracker: ProgressTracker,
user_agent: &str,
) -> Result<(), anyhow::Error> {
let response = reqwest::get(url).await?;
let client = reqwest::Client::new();
let response = client
.get(url)
.header("User-Agent", user_agent)
.send()
.await?;

// Ensure the directory exists
if let Some(parent) = destination.parent() {
Expand Down
9 changes: 8 additions & 1 deletion src-tauri/src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,16 @@ async fn list_releases_from(
ReleaseSource::Mirror => get_mirror_url(repo_owner, repo_name),
};

let user_agent = format!(
"universe {}({}) | {}",
env!("CARGO_PKG_VERSION"),
std::env::consts::OS,
repo_name
);

let response = client
.get(&url)
.header("User-Agent", "request")
.header("User-Agent", user_agent)
.send()
.await?;
if response.status() != 200 {
Expand Down
Loading