Fixes reqwest client setup, #87 (#107)

This commit is contained in:
DecDuck
2025-08-04 15:01:44 +10:00
committed by GitHub
parent dbe8c8df4d
commit 776dc8fe7a
6 changed files with 185 additions and 22 deletions

View File

@ -8,7 +8,11 @@ use tauri::{AppHandle, Emitter, Manager};
use url::Url;
use crate::{
database::db::{borrow_db_checked, borrow_db_mut_checked}, error::remote_access_error::RemoteAccessError, remote::{auth::generate_authorization_header, requests::make_request, utils::DROP_CLIENT_SYNC}, AppState, AppStatus
database::db::{borrow_db_checked, borrow_db_mut_checked}, error::remote_access_error::RemoteAccessError, remote::{
auth::generate_authorization_header,
requests::make_request,
utils::{DROP_CLIENT_SYNC, DROP_CLIENT_WS_CLIENT},
}, AppState, AppStatus
};
use super::{
@ -18,11 +22,11 @@ use super::{
};
#[tauri::command]
pub fn use_remote(
pub async fn use_remote(
url: String,
state: tauri::State<'_, Mutex<AppState<'_>>>,
) -> Result<(), RemoteAccessError> {
use_remote_logic(url, state)
use_remote_logic(url, state).await
}
#[tauri::command]
@ -120,10 +124,12 @@ pub fn auth_initiate_code(app: AppHandle) -> Result<String, RemoteAccessError> {
let code = auth_initiate_logic("code".to_string())?;
let header_code = code.clone();
println!("using code: {} to sign in", code);
tauri::async_runtime::spawn(async move {
let load = async || -> Result<(), RemoteAccessError> {
let ws_url = base_url.join("/api/v1/client/auth/code/ws")?;
let response = reqwest::Client::default()
let response = DROP_CLIENT_WS_CLIENT
.get(ws_url)
.header("Authorization", header_code)
.upgrade()

View File

@ -2,6 +2,7 @@ use std::{
fs::{self, File},
io::Read,
sync::{LazyLock, Mutex},
time::Duration,
};
use log::{debug, info, warn};
@ -21,8 +22,10 @@ struct DropHealthcheck {
app_name: String,
}
static DROP_CERT_BUNDLE: LazyLock<Vec<Certificate>> = LazyLock::new(fetch_certificates);
pub static DROP_CLIENT_SYNC: LazyLock<reqwest::blocking::Client> = LazyLock::new(get_client_sync);
pub static DROP_CLIENT_ASYNC: LazyLock<reqwest::Client> = LazyLock::new(get_client_async);
pub static DROP_CLIENT_WS_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(get_client_ws);
fn fetch_certificates() -> Vec<Certificate> {
let certificate_dir = DATA_ROOT_DIR.join("certificates");
@ -58,24 +61,30 @@ fn fetch_certificates() -> Vec<Certificate> {
pub fn get_client_sync() -> reqwest::blocking::Client {
let mut client = reqwest::blocking::ClientBuilder::new();
let certs = fetch_certificates();
for cert in certs {
client = client.add_root_certificate(cert);
for cert in DROP_CERT_BUNDLE.iter() {
client = client.add_root_certificate(cert.clone());
}
client.build().unwrap()
client.use_rustls_tls().build().unwrap()
}
pub fn get_client_async() -> reqwest::Client {
let mut client = reqwest::ClientBuilder::new();
let certs = fetch_certificates();
for cert in certs {
client = client.add_root_certificate(cert);
for cert in DROP_CERT_BUNDLE.iter() {
client = client.add_root_certificate(cert.clone());
}
client.build().unwrap()
client.use_rustls_tls().build().unwrap()
}
pub fn get_client_ws() -> reqwest::Client {
let mut client = reqwest::ClientBuilder::new();
for cert in DROP_CERT_BUNDLE.iter() {
client = client.add_root_certificate(cert.clone());
}
client.use_rustls_tls().http1_only().build().unwrap()
}
pub fn use_remote_logic(
pub async fn use_remote_logic(
url: String,
state: tauri::State<'_, Mutex<AppState<'_>>>,
) -> Result<(), RemoteAccessError> {
@ -84,10 +93,14 @@ pub fn use_remote_logic(
// Test Drop url
let test_endpoint = base_url.join("/api/v1")?;
let client = DROP_CLIENT_SYNC.clone();
let response = client.get(test_endpoint.to_string()).send()?;
let client = DROP_CLIENT_ASYNC.clone();
let response = client
.get(test_endpoint.to_string())
.timeout(Duration::from_secs(3))
.send()
.await?;
let result: DropHealthcheck = response.json()?;
let result: DropHealthcheck = response.json().await?;
if result.app_name != "Drop" {
warn!("user entered drop endpoint that connected, but wasn't identified as Drop");