Skip to content

Commit

Permalink
apply the proxy setting in wasmer config (#5091)
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed Sep 16, 2024
2 parents ec528e4 + fe1b72a commit 2a576c8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 52 deletions.
28 changes: 10 additions & 18 deletions lib/backend-api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ impl WasmerClient {
})
}

pub fn new(graphql_endpoint: Url, user_agent: &str) -> Result<Self, anyhow::Error> {
Self::new_with_proxy(graphql_endpoint, user_agent, None)
}

pub fn new_with_proxy(
graphql_endpoint: Url,
user_agent: &str,
proxy: reqwest::Proxy,
proxy: Option<reqwest::Proxy>,
) -> Result<Self, anyhow::Error> {
let builder = {
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
Expand All @@ -89,30 +93,18 @@ impl WasmerClient {
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(90));

builder.proxy(proxy)
if let Some(proxy) = proxy {
builder.proxy(proxy)
} else {
builder
}
};

let client = builder.build().context("failed to create reqwest client")?;

Self::new_with_client(client, graphql_endpoint, user_agent)
}

pub fn new(graphql_endpoint: Url, user_agent: &str) -> Result<Self, anyhow::Error> {
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
let client = reqwest::Client::builder()
.build()
.context("could not construct http client")?;

#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
let client = reqwest::Client::builder()
.connect_timeout(Duration::from_secs(10))
.timeout(Duration::from_secs(90))
.build()
.context("could not construct http client")?;

Self::new_with_client(client, graphql_endpoint, user_agent)
}

pub fn with_auth_token(mut self, auth_token: String) -> Self {
self.auth_token = Some(auth_token);
self
Expand Down
19 changes: 14 additions & 5 deletions lib/cli/src/commands/package/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub(super) async fn upload(
timeout: humantime::Duration,
package: &Package,
pb: ProgressBar,
proxy: Option<reqwest::Proxy>,
) -> anyhow::Result<String> {
let hash_str = hash.to_string();
let hash_str = hash_str.trim_start_matches("sha256:");
Expand All @@ -68,11 +69,19 @@ pub(super) async fn upload(

tracing::info!("signed url is: {session_uri}");

let client = reqwest::Client::builder()
.default_headers(reqwest::header::HeaderMap::default())
.timeout(timeout.into())
.build()
.unwrap();
let client = {
let builder = reqwest::Client::builder()
.default_headers(reqwest::header::HeaderMap::default())
.timeout(timeout.into());

let builder = if let Some(proxy) = proxy {
builder.proxy(proxy)
} else {
builder
};

builder.build().unwrap()
};

let res = client
.post(&session_uri)
Expand Down
16 changes: 2 additions & 14 deletions lib/cli/src/commands/package/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use dialoguer::console::{style, Emoji};
use indicatif::{ProgressBar, ProgressStyle};
use tempfile::NamedTempFile;
use wasmer_config::package::{PackageIdent, PackageSource};
use wasmer_wasix::http::reqwest::get_proxy;

use crate::config::WasmerEnv;

Expand All @@ -28,10 +27,6 @@ pub struct PackageDownload {
#[clap(long)]
pub quiet: bool,

/// proxy to use for downloading
#[clap(long)]
pub proxy: Option<String>,

/// The package to download.
package: PackageSource,
}
Expand Down Expand Up @@ -101,13 +96,7 @@ impl PackageDownload {
// caveat: client_unauthennticated will use a token if provided, it
// just won't fail if none is present. So, _unauthenticated() can actually
// produce an authenticated client.
let client = if let Some(proxy) = &self.proxy {
let proxy = reqwest::Proxy::all(proxy)?;

self.env.client_unauthennticated_with_proxy(proxy)?
} else {
self.env.client_unauthennticated()?
};
let client = self.env.client_unauthennticated()?;

let version = id.version_or_default().to_string();
let version = if version == "*" {
Expand Down Expand Up @@ -171,7 +160,7 @@ impl PackageDownload {

let builder = {
let mut builder = reqwest::blocking::ClientBuilder::new();
if let Some(proxy) = get_proxy()? {
if let Some(proxy) = self.env.proxy()? {
builder = builder.proxy(proxy);
}
builder
Expand Down Expand Up @@ -309,7 +298,6 @@ mod tests {
out_path: Some(out_path.clone()),
package: "wasmer/hello@0.1.0".parse().unwrap(),
quiet: true,
proxy: None,
};

cmd.execute().unwrap();
Expand Down
10 changes: 9 additions & 1 deletion lib/cli/src/commands/package/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,15 @@ impl PackagePush {
) -> anyhow::Result<()> {
let pb = make_spinner!(self.quiet, "Uploading the package..");

let signed_url = upload(client, package_hash, self.timeout, package, pb.clone()).await?;
let signed_url = upload(
client,
package_hash,
self.timeout,
package,
pb.clone(),
self.env.proxy()?,
)
.await?;
spinner_ok!(pb, "Package correctly uploaded");

let pb = make_spinner!(self.quiet, "Waiting for package to become available...");
Expand Down
26 changes: 12 additions & 14 deletions lib/cli/src/config/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,17 @@ impl WasmerEnv {
})
}

/// Returns the proxy specified in wasmer config if present
pub fn proxy(&self) -> Result<Option<reqwest::Proxy>, Error> {
self.config()?
.proxy
.url
.as_ref()
.map(reqwest::Proxy::all)
.transpose()
.map_err(Into::into)
}

/// The directory all Wasmer artifacts are stored in.
pub fn dir(&self) -> &Path {
&self.wasmer_dir
Expand Down Expand Up @@ -127,22 +138,9 @@ impl WasmerEnv {

pub fn client_unauthennticated(&self) -> Result<WasmerClient, anyhow::Error> {
let registry_url = self.registry_endpoint()?;
let client = wasmer_api::WasmerClient::new(registry_url, &DEFAULT_WASMER_CLI_USER_AGENT)?;

let client = if let Some(token) = self.token() {
client.with_auth_token(token)
} else {
client
};
let proxy = self.proxy()?;

Ok(client)
}

pub fn client_unauthennticated_with_proxy(
&self,
proxy: reqwest::Proxy,
) -> Result<WasmerClient, anyhow::Error> {
let registry_url = self.registry_endpoint()?;
let client = wasmer_api::WasmerClient::new_with_proxy(
registry_url,
&DEFAULT_WASMER_CLI_USER_AGENT,
Expand Down

0 comments on commit 2a576c8

Please sign in to comment.