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

feat(upload): added ssl cert options to upload and download function #2165

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion plugins/upload/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 additions & 4 deletions plugins/upload/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ async function upload(
url: string,
filePath: string,
progressHandler?: ProgressHandler,
headers?: Map<string, string>
headers?: Map<string, string>,
options?: {
skipSslCertCheck?: boolean
trustSslCert?: string
}
): Promise<string> {
const ids = new Uint32Array(1)
window.crypto.getRandomValues(ids)
Expand All @@ -33,7 +37,9 @@ async function upload(
url,
filePath,
headers: headers ?? {},
onProgress
onProgress,
skipSslCertCheck: options?.skipSslCertCheck,
trustSslCert: options?.trustSslCert
})
}

Expand All @@ -46,7 +52,11 @@ async function download(
filePath: string,
progressHandler?: ProgressHandler,
headers?: Map<string, string>,
body?: string
body?: string,
options?: {
skipSslCertCheck?: boolean
trustSslCert?: string
}
): Promise<void> {
const ids = new Uint32Array(1)
window.crypto.getRandomValues(ids)
Expand All @@ -63,7 +73,9 @@ async function download(
filePath,
headers: headers ?? {},
onProgress,
body
body,
skipSslCertCheck: options?.skipSslCertCheck,
trustSslCert: options?.trustSslCert
})
}

Expand Down
29 changes: 24 additions & 5 deletions plugins/upload/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,34 @@ struct ProgressPayload {
transfer_speed: u64,
}

fn build_client(
skip_ssl_cert_check: Option<bool>,
trust_ssl_cert: Option<String>,
) -> Result<reqwest::Client> {
let mut client_builder = reqwest::Client::builder();
if skip_ssl_cert_check.unwrap_or(false) {
client_builder = client_builder.danger_accept_invalid_certs(true);
}
if let Some(cert) = match trust_ssl_cert {
Some(cert) => reqwest::tls::Certificate::from_pem(cert.as_bytes()).ok(),
None => None,
} {
client_builder = client_builder.add_root_certificate(cert);
}
client_builder.build().map_err(Into::into)
}

#[command]
async fn download(
url: &str,
file_path: &str,
headers: HashMap<String, String>,
body: Option<String>,
on_progress: Channel<ProgressPayload>,
skip_ssl_cert_check: Option<bool>,
trust_ssl_cert: Option<String>,
) -> Result<()> {
let client = reqwest::Client::new();
let client = build_client(skip_ssl_cert_check, trust_ssl_cert)?;
let mut request = if let Some(body) = body {
client.post(url).body(body)
} else {
Expand Down Expand Up @@ -118,13 +137,13 @@ async fn upload(
file_path: &str,
headers: HashMap<String, String>,
on_progress: Channel<ProgressPayload>,
skip_ssl_cert_check: Option<bool>,
trust_ssl_cert: Option<String>,
) -> Result<String> {
// Read the file
let file = File::open(file_path).await?;
let file_len = file.metadata().await.unwrap().len();

// Create the request and attach the file to the body
let client = reqwest::Client::new();
let client = build_client(skip_ssl_cert_check, trust_ssl_cert)?;
let mut request = client
.post(url)
.header(reqwest::header::CONTENT_LENGTH, file_len)
Expand Down Expand Up @@ -210,7 +229,7 @@ mod tests {
let _ = msg;
Ok(())
});
download(url, file_path, headers, None, sender).await
download(url, file_path, headers, None, sender, None, None).await
}

async fn spawn_server_mocked(return_status: usize) -> MockedServer {
Expand Down
Loading