reorganisation, cleanup and new nonce protocol

This commit is contained in:
DecDuck
2024-10-12 17:34:47 +11:00
parent ac66b20591
commit e828bca2a5
19 changed files with 429 additions and 60 deletions
+180 -13
View File
@@ -62,6 +62,27 @@ version = "1.0.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86fdf8605db99b54d3cd748a44c6d04df638eb5dafb219b135d0149bd0db01f6"
[[package]]
name = "ashpd"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d43c03d9e36dd40cab48435be0b09646da362c278223ca535493877b2c1dee9"
dependencies = [
"enumflags2",
"futures-channel",
"futures-util",
"rand 0.8.5",
"raw-window-handle",
"serde",
"serde_repr",
"tokio",
"url",
"wayland-backend",
"wayland-client",
"wayland-protocols",
"zbus",
]
[[package]]
name = "async-broadcast"
version = "0.7.1"
@@ -882,6 +903,15 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd0c93bb4b0c6d9b77f4435b0ae98c24d17f1c45b2ff844c6151a07256ca923b"
[[package]]
name = "dlib"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412"
dependencies = [
"libloading",
]
[[package]]
name = "dlopen2"
version = "0.7.0"
@@ -914,6 +944,12 @@ dependencies = [
"const-random",
]
[[package]]
name = "downcast-rs"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2"
[[package]]
name = "dpi"
version = "0.1.1"
@@ -932,7 +968,6 @@ dependencies = [
"hex",
"log",
"openssl",
"os_info",
"rayon",
"reqwest",
"rustbreak",
@@ -942,6 +977,7 @@ dependencies = [
"tauri",
"tauri-build",
"tauri-plugin-deep-link",
"tauri-plugin-dialog",
"tauri-plugin-shell",
"tauri-plugin-single-instance",
"url",
@@ -2382,6 +2418,7 @@ checksum = "0ee638a5da3799329310ad4cfa62fbf045d5f56e3ef5ba4149e7452dcf89d5a8"
dependencies = [
"bitflags 2.6.0",
"block2",
"dispatch",
"libc",
"objc2",
]
@@ -2528,17 +2565,6 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "os_info"
version = "3.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae99c7fa6dd38c7cafe1ec085e804f8f555a2f8659b0dbe03f1f9963a9b51092"
dependencies = [
"log",
"serde",
"windows-sys 0.52.0",
]
[[package]]
name = "os_pipe"
version = "1.2.1"
@@ -2786,7 +2812,7 @@ checksum = "42cf17e9a1800f5f396bc67d193dc9411b59012a5876445ef450d449881e1016"
dependencies = [
"base64 0.22.1",
"indexmap 2.6.0",
"quick-xml",
"quick-xml 0.32.0",
"serde",
"time",
]
@@ -2914,6 +2940,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "quick-xml"
version = "0.36.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7649a7b4df05aed9ea7ec6f628c67c9953a43869b8bc50929569b2999d443fe"
dependencies = [
"memchr",
]
[[package]]
name = "quote"
version = "1.0.37"
@@ -3125,6 +3160,29 @@ dependencies = [
"windows-registry 0.2.0",
]
[[package]]
name = "rfd"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8af382a047821a08aa6bfc09ab0d80ff48d45d8726f7cd8e44891f7cb4a4278e"
dependencies = [
"ashpd",
"block2",
"glib-sys",
"gobject-sys",
"gtk-sys",
"js-sys",
"log",
"objc2",
"objc2-app-kit",
"objc2-foundation",
"raw-window-handle",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"windows-sys 0.48.0",
]
[[package]]
name = "ring"
version = "0.17.8"
@@ -3282,6 +3340,12 @@ dependencies = [
"syn 2.0.79",
]
[[package]]
name = "scoped-tls"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294"
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -4058,6 +4122,45 @@ dependencies = [
"windows-result",
]
[[package]]
name = "tauri-plugin-dialog"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddb2fe88b602461c118722c574e2775ab26a4e68886680583874b2f6520608b7"
dependencies = [
"log",
"raw-window-handle",
"rfd",
"serde",
"serde_json",
"tauri",
"tauri-plugin",
"tauri-plugin-fs",
"thiserror",
"url",
]
[[package]]
name = "tauri-plugin-fs"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab300488ebec3487ca5f56289692e7e45feb07eea8d5e1dba497f7dc9dd9c407"
dependencies = [
"anyhow",
"dunce",
"glob",
"percent-encoding",
"schemars",
"serde",
"serde_json",
"serde_repr",
"tauri",
"tauri-plugin",
"thiserror",
"url",
"uuid",
]
[[package]]
name = "tauri-plugin-shell"
version = "2.0.1"
@@ -4303,7 +4406,9 @@ dependencies = [
"mio",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tracing",
"windows-sys 0.52.0",
]
@@ -4805,6 +4910,66 @@ dependencies = [
"web-sys",
]
[[package]]
name = "wayland-backend"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "056535ced7a150d45159d3a8dc30f91a2e2d588ca0b23f70e56033622b8016f6"
dependencies = [
"cc",
"downcast-rs",
"rustix",
"scoped-tls",
"smallvec",
"wayland-sys",
]
[[package]]
name = "wayland-client"
version = "0.31.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3f45d1222915ef1fd2057220c1d9d9624b7654443ea35c3877f7a52bd0a5a2d"
dependencies = [
"bitflags 2.6.0",
"rustix",
"wayland-backend",
"wayland-scanner",
]
[[package]]
name = "wayland-protocols"
version = "0.32.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b5755d77ae9040bb872a25026555ce4cb0ae75fd923e90d25fba07d81057de0"
dependencies = [
"bitflags 2.6.0",
"wayland-backend",
"wayland-client",
"wayland-scanner",
]
[[package]]
name = "wayland-scanner"
version = "0.31.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "597f2001b2e5fc1121e3d5b9791d3e78f05ba6bfa4641053846248e3a13661c3"
dependencies = [
"proc-macro2",
"quick-xml 0.36.2",
"quote",
]
[[package]]
name = "wayland-sys"
version = "0.31.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efa8ac0d8e8ed3e3b5c9fc92c7881406a268e11555abe36493efabe649a29e09"
dependencies = [
"dlib",
"log",
"pkg-config",
]
[[package]]
name = "web-sys"
version = "0.3.70"
@@ -5405,6 +5570,7 @@ dependencies = [
"serde_repr",
"sha1",
"static_assertions",
"tokio",
"tracing",
"uds_windows",
"windows-sys 0.52.0",
@@ -5476,6 +5642,7 @@ dependencies = [
"enumflags2",
"serde",
"static_assertions",
"url",
"zvariant_derive",
]
+1 -1
View File
@@ -30,11 +30,11 @@ rayon = "1.10.0"
directories = "5.0.1"
webbrowser = "1.0.2"
url = "2.5.2"
os_info = "3.8.2"
tauri-plugin-deep-link = "2"
log = "0.4.22"
structured-logger = "1.0.3"
hex = "0.4.3"
tauri-plugin-dialog = "2"
[dependencies.uuid]
version = "1.10.0"
+2 -1
View File
@@ -12,6 +12,7 @@
"core:window:allow-minimize",
"core:window:allow-maximize",
"core:window:allow-close",
"deep-link:default"
"deep-link:default",
"dialog:default"
]
}
+19 -12
View File
@@ -1,10 +1,11 @@
use std::{
borrow::{Borrow, BorrowMut},
env,
fmt::format,
sync::Mutex,
};
use log::info;
use log::{info, warn};
use openssl::{
ec::EcKey,
hash::MessageDigest,
@@ -12,11 +13,11 @@ use openssl::{
sign::{self, Signer},
};
use serde::{Deserialize, Serialize};
use tauri::{App, AppHandle, Emitter, Error, EventLoopMessage, Manager, Wry};
use tauri::{http::response, App, AppHandle, Emitter, EventLoopMessage, Manager, Wry};
use url::Url;
use uuid::Uuid;
use crate::{data::DatabaseAuth, AppState, AppStatus, User, DB};
use crate::{db::DatabaseAuth, AppState, AppStatus, User, DB};
#[derive(Serialize)]
struct InitiateRequestBody {
@@ -49,7 +50,7 @@ macro_rules! unwrap_or_return {
};
}
pub fn sign_nonce(private_key: String, nonce: String) -> Result<String, Error> {
pub fn sign_nonce(private_key: String, nonce: String) -> Result<String, ()> {
let client_private_key = EcKey::private_key_from_pem(private_key.as_bytes()).unwrap();
let pkey_private_key = PKey::from_ec_key(client_private_key).unwrap();
@@ -74,7 +75,7 @@ pub fn generate_authorization_header() -> String {
return format!("Nonce {} {} {}", certs.clientId, nonce, signature);
}
pub fn fetch_user() -> Result<User, Error> {
pub fn fetch_user() -> Result<User, ()> {
let base_url = {
let handle = DB.borrow_data().unwrap();
Url::parse(&handle.base_url).unwrap()
@@ -90,6 +91,11 @@ pub fn fetch_user() -> Result<User, Error> {
.send()
.unwrap();
if response.status() != 200 {
warn!("Failed to fetch user: {}", response.status());
return Err(());
}
let user = response.json::<User>().unwrap();
return Ok(user);
@@ -138,11 +144,10 @@ pub fn recieve_handshake(app: AppHandle, path: String) {
let app_state = app.state::<Mutex<AppState>>();
let mut app_state_handle = app_state.lock().unwrap();
app_state_handle.status = AppStatus::SignedIn;
app_state_handle.user = Some(fetch_user().unwrap());
}
app.emit("auth/finished", ()).unwrap();
fetch_user().unwrap();
}
#[tauri::command]
@@ -152,12 +157,10 @@ pub async fn auth_initiate<'a>() -> Result<(), String> {
Url::parse(&db_lock.base_url.clone()).unwrap()
};
let current_os_info = os_info::get();
let endpoint = base_url.join("/api/v1/client/auth/initiate").unwrap();
let body = InitiateRequestBody {
name: format!("Drop Desktop Client"),
platform: current_os_info.os_type().to_string(),
platform: env::consts::OS.to_string(),
};
let client = reqwest::Client::new();
@@ -168,6 +171,10 @@ pub async fn auth_initiate<'a>() -> Result<(), String> {
.await
.unwrap();
if response.status() != 200 {
return Err("Failed to create redirect URL. Please try again later.".to_string());
}
let redir_url = response.text().await.unwrap();
let complete_redir_url = base_url.join(&redir_url).unwrap();
@@ -177,7 +184,7 @@ pub async fn auth_initiate<'a>() -> Result<(), String> {
return Ok(());
}
pub fn setup() -> Result<(AppStatus, Option<User>), Error> {
pub fn setup() -> Result<(AppStatus, Option<User>), ()> {
let data = DB.borrow_data().unwrap();
// If we have certs, exit for now
@@ -186,7 +193,7 @@ pub fn setup() -> Result<(AppStatus, Option<User>), Error> {
if user_result.is_err() {
return Ok((AppStatus::SignedInNeedsReauth, None));
}
return Ok((AppStatus::SignedIn, Some(user_result.unwrap())))
return Ok((AppStatus::SignedIn, Some(user_result.unwrap())));
}
drop(data);
+23 -2
View File
@@ -1,4 +1,8 @@
use std::fs;
use std::{
fs::{self, create_dir_all},
path::PathBuf,
sync::LazyLock,
};
use directories::BaseDirs;
use rustbreak::{deser::Bincode, PathDatabase};
@@ -13,20 +17,37 @@ pub struct DatabaseAuth {
pub clientId: String,
}
#[derive(serde::Serialize, Clone, Deserialize)]
pub struct DatabaseApps {
pub apps_base_dir: String,
}
#[derive(serde::Serialize, Clone, Deserialize)]
pub struct Database {
pub auth: Option<DatabaseAuth>,
pub base_url: String,
pub downloads: DatabaseApps,
}
pub type DatabaseInterface =
rustbreak::Database<Database, rustbreak::backend::PathBackend, Bincode>;
pub static DATA_ROOT_DIR: LazyLock<PathBuf> =
LazyLock::new(|| BaseDirs::new().unwrap().data_dir().join("drop"));
pub fn setup() -> DatabaseInterface {
let db_path = BaseDirs::new().unwrap().data_dir().join("drop");
let db_path = DATA_ROOT_DIR.join("drop.db");
let apps_base_dir = DATA_ROOT_DIR.join("apps");
create_dir_all(DATA_ROOT_DIR.clone()).unwrap();
create_dir_all(apps_base_dir.clone()).unwrap();
let default = Database {
auth: None,
base_url: "".to_string(),
downloads: DatabaseApps {
apps_base_dir: apps_base_dir.to_str().unwrap().to_string(),
},
};
let db = match fs::exists(db_path.clone()).unwrap() {
true => PathDatabase::load_from_path(db_path).expect("Database loading failed"),
+19 -5
View File
@@ -1,5 +1,5 @@
mod auth;
mod data;
mod db;
mod remote;
mod unpacker;
@@ -10,7 +10,7 @@ use std::{
};
use auth::{auth_initiate, recieve_handshake};
use data::DatabaseInterface;
use db::{DatabaseInterface, DATA_ROOT_DIR};
use log::info;
use remote::{gen_drop_url, use_remote};
use serde::{Deserialize, Serialize};
@@ -52,7 +52,7 @@ fn setup<'a>() -> AppState {
.with_target_writer("*", new_writer(io::stdout()))
.init();
let is_set_up = data::is_set_up();
let is_set_up = db::is_set_up();
if !is_set_up {
return AppState {
status: AppStatus::NotConfigured,
@@ -67,14 +67,14 @@ fn setup<'a>() -> AppState {
};
}
pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(|| data::setup());
pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(|| db::setup());
#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
let state = setup();
info!("Initialized drop client");
let mut builder = tauri::Builder::default();
let mut builder = tauri::Builder::default().plugin(tauri_plugin_dialog::init());
#[cfg(desktop)]
{
@@ -103,6 +103,19 @@ pub fn run() {
let handle = app.handle().clone();
let main_window = tauri::WebviewWindowBuilder::new(
&handle,
"main", // BTW this is not the name of the window, just the label. Keep this 'main', there are permissions & configs that depend on it
tauri::WebviewUrl::App("index.html".into()),
)
.title("Drop Desktop App")
.min_inner_size(900.0, 900.0)
.inner_size(1536.0, 864.0)
.decorations(false)
.data_directory(DATA_ROOT_DIR.join(".webview"))
.build()
.unwrap();
app.deep_link().on_open_url(move |event| {
info!("handling drop:// url");
let binding = event.urls();
@@ -112,6 +125,7 @@ pub fn run() {
_ => (),
}
});
Ok(())
})
.run(tauri::generate_context!())
-10
View File
@@ -10,16 +10,6 @@
"frontendDist": "../.output/public"
},
"app": {
"windows": [
{
"title": "Drop",
"width": 1536,
"height": 864,
"minWidth": 820,
"minHeight": 600,
"decorations": false
}
],
"security": {
"csp": null
}