From 065c1bf4c9c9c7b43b8cb653f3c43b7992afcd4b Mon Sep 17 00:00:00 2001 From: code002lover Date: Mon, 19 Feb 2024 23:42:51 +0100 Subject: [PATCH] fix tauri error --- src-tauri/Cargo.toml | 8 +- src-tauri/src/lib.rs | 146 +++++++++++++++++++++++++++++++++++++ src-tauri/src/main.rs | 150 ++------------------------------------ src-tauri/tauri.conf.json | 92 ++++++++++++----------- src/main.js | 2 +- 5 files changed, 205 insertions(+), 193 deletions(-) create mode 100644 src-tauri/src/lib.rs diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 519624a..6db8738 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -6,15 +6,17 @@ license = "GNU GPLv3" description = "A Password manager" repository = "https://git.ipost.rocks/Code002Lover/IPass-GUI" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "ipass_gui_lib" +crate-type = ["staticlib", "cdylib", "rlib"] [build-dependencies] -tauri-build = { version = "2.0.0-beta.1", features = [] } +tauri-build = { version = "2.0.0-beta", features = [] } [dependencies] serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } -tauri = { version = "2.0.0-beta.2", features = [] } +tauri = { version = "2.0.0-beta", features = [] } ip_lib = { path = "../library/" } rand="0.8.5" open="5.0.0" diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs new file mode 100644 index 0000000..3671a84 --- /dev/null +++ b/src-tauri/src/lib.rs @@ -0,0 +1,146 @@ +#![cfg_attr( + all(not(debug_assertions), target_os = "windows"), + windows_subsystem = "windows" +)] + +use std::fs::File; +use std::io::Write; +use tauri::State; +use tokio::runtime::Runtime; + +extern crate ip_lib; +extern crate open; +extern crate rand; + +// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command + +#[tauri::command] +fn get_version() -> String { + option_env!("CARGO_PKG_VERSION") + .unwrap_or("x.x.x") + .to_string() +} + +#[tauri::command] +fn create_entry(name: &str, username: String, pw: &str, mpw: &str) -> bool { + let username_and_pw = username + ";" + pw; + let return_value = ip_lib::create_entry(name, &username_and_pw, mpw); + if return_value { + isync_save(); + } + return_value +} + +#[tauri::command] +fn random_password() -> String { + ip_lib::random_password() +} + +#[tauri::command] +fn get_entry(name: &str, mpw: &str) -> Result<(String, String), String> { + let binding = ip_lib::get_entry(name, mpw); + if let Ok(data) = binding { + let mut split = data.split(';'); + Ok(( + split.next().unwrap().to_string(), + split.next().unwrap().to_string(), + )) + } else { + Err(binding.expect_err("expected error")) + } +} + +#[tauri::command] +fn get_entries() -> Vec { + let mut vector: Vec = Vec::default(); + for entry in ip_lib::get_entries() { + let entry_filename = entry.unwrap().file_name(); + let ent = entry_filename.to_str().unwrap(); + if ent != "token.ipasst" && ent.len() > 6 { + vector.insert(0, ent[..ent.len() - 6].to_string()); + } + } + + vector.sort(); + + vector +} + +#[tauri::command] +fn remove_entry(name: &str) -> bool { + let filepath = &(ip_lib::get_ipass_folder() + name + ".ipass"); + if std::path::Path::new(filepath).exists() { + std::fs::remove_file(filepath).unwrap(); + isync_save(); + return true; + } + false +} + +#[tauri::command] +fn open_authorize() -> u32 { + let code: u32 = rand::random(); + open::that(format!("https://ipost.rocks/authorize?id=1&extra={}", code)).unwrap(); + //the IPass app id is 1 + code +} + +#[tauri::command] +fn store_token(token: String) { + let filepath = &(ip_lib::get_ipass_folder() + "token.ipasst"); + let mut file = File::create(filepath).unwrap(); + file.write_all(token.as_bytes()).unwrap(); +} + +#[tauri::command] +fn remove_token() { + let filepath = &(ip_lib::get_ipass_folder() + "token.ipasst"); + std::fs::remove_file(filepath).unwrap(); + //TODO: cleaner solution via IPass api call to unauthorize +} + +#[tauri::command] +fn get_isync_status() -> bool { + let filepath = &(ip_lib::get_ipass_folder() + "token.ipasst"); + std::path::Path::new(filepath).exists() +} + +fn isync_save() { + let isync_save_future = ip_lib::isync_save(); + + let rt = Runtime::new().unwrap(); + let _save_output = rt.block_on(isync_save_future); + //println!("isync_save: {}",save_output); +} + +#[tauri::command] +fn sync_isync(rt: State) { + //println!("going to sync"); + rt.spawn(async { + let _get_output = ip_lib::isync_get().await; + //println!("isync_get: {}",_get_output); + }); +} + +#[cfg_attr(mobile, tauri::mobile_entry_point)] +pub fn run() { + let rt = Runtime::new().unwrap(); + + tauri::Builder::default() + .invoke_handler(tauri::generate_handler![ + get_version, + create_entry, + random_password, + get_entry, + get_entries, + remove_entry, + open_authorize, + store_token, + get_isync_status, + sync_isync, + remove_token + ]) + .manage(rt) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 82394fe..128c5c8 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,145 +1,5 @@ -#![cfg_attr( - all(not(debug_assertions), target_os = "windows"), - windows_subsystem = "windows" -)] - -use std::fs::File; -use std::io::Write; -use tauri::State; -use tokio::runtime::Runtime; - -extern crate ip_lib; -extern crate open; -extern crate rand; - -// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command - -#[tauri::command] -fn get_version() -> String { - option_env!("CARGO_PKG_VERSION") - .unwrap_or("x.x.x") - .to_string() -} - -#[tauri::command] -fn create_entry(name: &str, username: String, pw: &str, mpw: &str) -> bool { - let username_and_pw = username + ";" + pw; - let return_value = ip_lib::create_entry(name, &username_and_pw, mpw); - if return_value { - isync_save(); - } - return_value -} - -#[tauri::command] -fn random_password() -> String { - ip_lib::random_password() -} - -#[tauri::command] -fn get_entry(name: &str, mpw: &str) -> Result<(String, String), String> { - let binding = ip_lib::get_entry(name, mpw); - if let Ok(data) = binding { - let mut split = data.split(';'); - Ok(( - split.next().unwrap().to_string(), - split.next().unwrap().to_string(), - )) - } else { - Err(binding.expect_err("expected error")) - } -} - -#[tauri::command] -fn get_entries() -> Vec { - let mut vector: Vec = Vec::default(); - for entry in ip_lib::get_entries() { - let entry_filename = entry.unwrap().file_name(); - let ent = entry_filename.to_str().unwrap(); - if ent != "token.ipasst" && ent.len() > 6 { - vector.insert(0, ent[..ent.len() - 6].to_string()); - } - } - - vector.sort(); - - vector -} - -#[tauri::command] -fn remove_entry(name: &str) -> bool { - let filepath = &(ip_lib::get_ipass_folder() + name + ".ipass"); - if std::path::Path::new(filepath).exists() { - std::fs::remove_file(filepath).unwrap(); - isync_save(); - return true; - } - false -} - -#[tauri::command] -fn open_authorize() -> u32 { - let code: u32 = rand::random(); - open::that(format!("https://ipost.rocks/authorize?id=1&extra={}", code)).unwrap(); - //the IPass app id is 1 - code -} - -#[tauri::command] -fn store_token(token: String) { - let filepath = &(ip_lib::get_ipass_folder() + "token.ipasst"); - let mut file = File::create(filepath).unwrap(); - file.write_all(token.as_bytes()).unwrap(); -} - -#[tauri::command] -fn remove_token() { - let filepath = &(ip_lib::get_ipass_folder() + "token.ipasst"); - std::fs::remove_file(filepath).unwrap(); - //TODO: cleaner solution via IPass api call to unauthorize -} - -#[tauri::command] -fn get_isync_status() -> bool { - let filepath = &(ip_lib::get_ipass_folder() + "token.ipasst"); - std::path::Path::new(filepath).exists() -} - -fn isync_save() { - let isync_save_future = ip_lib::isync_save(); - - let rt = Runtime::new().unwrap(); - let _save_output = rt.block_on(isync_save_future); - //println!("isync_save: {}",save_output); -} - -#[tauri::command] -fn sync_isync(rt: State) { - //println!("going to sync"); - rt.spawn(async { - let _get_output = ip_lib::isync_get().await; - //println!("isync_get: {}",_get_output); - }); -} - -fn main() { - let rt = Runtime::new().unwrap(); - - tauri::Builder::default() - .invoke_handler(tauri::generate_handler![ - get_version, - create_entry, - random_password, - get_entry, - get_entries, - remove_entry, - open_authorize, - store_token, - get_isync_status, - sync_isync, - remove_token - ]) - .manage(rt) - .run(tauri::generate_context!()) - .expect("error while running tauri application"); -} +#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] + +fn main() { + ipass_gui_lib::run(); +} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index e1c710e..6dd8a88 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,45 +1,49 @@ -{ - "build": { - "beforeDevCommand": "", - "beforeBuildCommand": "", - "frontendDist": "../src" - }, - "productName": "ipass", - "version": "1.0.0", - "bundle": { - "active": true, - "category": "DeveloperTool", - "copyright": "", - "externalBin": [], - "icon": [ - "icons/icon.ico", - "icons/icon.png" - ], - "longDescription": "", - "macOS": { - "entitlements": null, - "exceptionDomain": "", - "frameworks": [], - "providerShortName": null, - "signingIdentity": null - }, - "resources": [], - "shortDescription": "", - "targets": "all", - "windows": { - "certificateThumbprint": null, - "digestAlgorithm": "sha256", - "timestampUrl": "" - } - }, - "app": { - "windows": [{ - "fullscreen": false, - "height": 600, - "resizable": true, - "title": "IPass", - "width": 880, - "theme": "Dark" - }] - } +{ + "app": { + "windows": [ + { + "fullscreen": false, + "height": 600, + "resizable": true, + "theme": "Dark", + "title": "IPass", + "width": 880 + } + ], + "withGlobalTauri": true + }, + "build": { + "beforeBuildCommand": "", + "beforeDevCommand": "", + "frontendDist": "../src" + }, + "bundle": { + "active": true, + "category": "DeveloperTool", + "copyright": "", + "externalBin": [], + "icon": [ + "icons/icon.ico", + "icons/icon.png" + ], + "longDescription": "", + "macOS": { + "entitlements": null, + "exceptionDomain": "", + "frameworks": [], + "providerShortName": null, + "signingIdentity": null + }, + "resources": [], + "shortDescription": "", + "targets": "all", + "windows": { + "certificateThumbprint": null, + "digestAlgorithm": "sha256", + "timestampUrl": "" + } + }, + "plugins": {}, + "productName": "ipass", + "version": "1.0.0" } \ No newline at end of file diff --git a/src/main.js b/src/main.js index 8591b84..f51f108 100644 --- a/src/main.js +++ b/src/main.js @@ -1,4 +1,4 @@ -const { invoke } = window.__TAURI__.tauri; +const { invoke } = window.__TAURI__.core; let master_pw; let lock_status = true;