From d058ca8cddca160774272091e084976a8bf4c17e Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Fri, 9 Sep 2022 10:42:45 +0530 Subject: [PATCH 1/5] Cleanup code related to credential handling Signed-off-by: Hrishikesh Patil --- src-tauri/src/main.rs | 36 +++++++++++++++++++++++++++++------- ui/index.js | 4 ++-- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 40df495..b3e3182 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -13,7 +13,7 @@ use tauri::{ extern crate chrono; extern crate timer; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Clone)] struct Credentials { username: String, password: String, @@ -154,9 +154,8 @@ fn main() { tauri::Builder::default() .setup(|app: &mut tauri::App| { let save_dir = path::app_dir(&app.config()).unwrap(); - let file_creds = load_creds(&(save_dir.join("credentials.json"))); - if file_creds.is_ok() { - let _creds = file_creds.unwrap(); + let creds = load_creds(&(save_dir.join("credentials.json"))); + if creds.is_ok() { PROCEED_CAMPNET_ATTEMPT = true; } else { app.get_window("main").unwrap().show().unwrap(); @@ -164,8 +163,8 @@ fn main() { let write_save_file = save_dir.join("credentials.json"); let app_handle_save = app.app_handle(); app.listen_global("save", move |event: tauri::Event| { - let creds_save: Credentials = serde_json::from_str(event.payload().unwrap()).unwrap(); - save_creds(creds_save, &write_save_file); + let creds: Credentials = serde_json::from_str(event.payload().unwrap()).unwrap(); + save_creds(creds, &write_save_file); PROCEED_CAMPNET_ATTEMPT = true; std::thread::sleep(std::time::Duration::from_millis(3000)); app_handle_save.get_window("main").unwrap().hide().unwrap(); @@ -200,7 +199,19 @@ fn main() { .unwrap() .join("credentials.json"); let creds = load_creds(&save_file); - window.emit("credentials", &creds).unwrap(); + if creds.is_ok() { + window.emit("credentials", creds.unwrap()).unwrap(); + } else { + window + .emit( + "credentials", + Credentials { + username: "".into(), + password: "".into(), + }, + ) + .unwrap(); + } window.show().unwrap(); window.unminimize().unwrap(); } @@ -224,6 +235,17 @@ fn main() { } } "delete" => { + let window: tauri::Window = app.get_window("main").unwrap(); + window + .emit( + "credentials", + Credentials { + username: "".into(), + password: "".into(), + }, + ) + .unwrap(); + window.show().unwrap(); let save_file = path::app_dir(&app.config()) .unwrap() .join("credentials.json"); diff --git a/ui/index.js b/ui/index.js index c6b1799..50996b2 100644 --- a/ui/index.js +++ b/ui/index.js @@ -20,10 +20,10 @@ document.getElementById("save").addEventListener("click", (_) => { listen("credentials", (creds) => { document.getElementById("loginuserid").value = decodeURIComponent( - creds.payload.Ok.username + creds.payload.username ); document.getElementById("loginpassword").value = decodeURIComponent( - creds.payload.Ok.password + creds.payload.password ); }); From 7cca04c4952c78b530b0c1c5e7ef40373999d219 Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Sun, 11 Sep 2022 16:33:02 +0530 Subject: [PATCH 2/5] Replace GET requests with HEAD requests to minimise data transfer Signed-off-by: Hrishikesh Patil --- src-tauri/Cargo.lock | 12 ++++++------ src-tauri/Cargo.toml | 6 +++--- src-tauri/src/main.rs | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 5f581d5..936a9ab 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -269,9 +269,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.21" +version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f725f340c3854e3cb3ab736dc21f0cca183303acea3b3ffec30f141503ac8eb" +checksum = "bfd4d1b31faaa3a89d7934dbded3111da0d2ef28e3ebccdb4f0179f5929d1ef1" dependencies = [ "iana-time-zone", "js-sys", @@ -2469,18 +2469,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.143" +version = "1.0.144" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553" +checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.143" +version = "1.0.144" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3d8e8de557aee63c26b85b947f5e59b690d0454c753f3adeb5cd7835ab88391" +checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00" dependencies = [ "proc-macro2", "quote", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 0071fd0..38ef50b 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -16,11 +16,11 @@ tauri-build = { version = "1.1.1", features = [] } [dependencies] serde_json = "1.0" -serde = { version = "1.0", features = ["derive"] } +serde = { version = "1.0.144", features = ["derive"] } tauri = { version = "1.1.1", features = ["fs-create-dir", "fs-read-dir", "fs-read-file", "fs-remove-file", "fs-write-file", "notification", "notification-all", "path-all", "system-tray", "window-hide", "window-show", "window-start-dragging"] } timer = "0.2.0" -chrono = "0.4.19" -reqwest = { version = "0.11.10", features = ["blocking"] } +chrono = "0.4.22" +reqwest = { version = "0.11.11", features = ["blocking"] } [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index b3e3182..f1597bf 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -38,10 +38,11 @@ static mut PROCEED_CAMPNET_ATTEMPT: bool = false; static mut LOGOUT_CAMPNET: bool = false; unsafe fn connect_campnet(file_path: &std::path::PathBuf) { + let client = reqwest::blocking::Client::new(); if PROCEED_CAMPNET_ATTEMPT { - let campnet_status = reqwest::blocking::get("https://campnet.bits-goa.ac.in:8090/"); + let campnet_status = client.head("https://campnet.bits-goa.ac.in:8090/").send(); if campnet_status.is_ok() { - let login_status = reqwest::blocking::get("https://www.google.com"); + let login_status = client.head("https://www.google.com").send(); if login_status.is_err() { let helper_file = file_path.parent().unwrap().join("credentials.json"); let creds = load_creds(&helper_file); @@ -56,7 +57,6 @@ unsafe fn connect_campnet(file_path: &std::path::PathBuf) { .unwrap() .as_millis() ); - let client = reqwest::blocking::Client::new(); let res = client .post("https://campnet.bits-goa.ac.in:8090/login.xml") .header("Content-Type", "application/x-www-form-urlencoded") From 37012a73e962d6a991be39b85965d17beca07a6b Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Thu, 15 Sep 2022 19:16:54 +0530 Subject: [PATCH 3/5] Move logout and login to own functions, rewrite login loop to use app_handle Signed-off-by: Hrishikesh Patil --- src-tauri/src/main.rs | 91 ++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 40 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index f1597bf..3d5a166 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -34,10 +34,54 @@ fn load_creds(save_file: &std::path::Path) -> Result { } } +fn logout_campnet( + creds: Credentials, + client: reqwest::blocking::Client, +) -> Result { + let body: String = format!( + "mode=193&username={}&a={}&producttype=1", + creds.username, + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_millis() + ); + return client + .post("https://campnet.bits-goa.ac.in:8090/logout.xml") + .header("Content-Type", "application/x-www-form-urlencoded") + .header("Content-Length", body.chars().count()) + .body(body) + .send(); +} + +fn login_campnet( + creds: Credentials, + client: reqwest::blocking::Client, +) -> Result { + let body: String = format!( + "mode=191&username={}&password={}&a={}&producttype=1", + creds.username, + creds.password, + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_millis() + ); + return client + .post("https://campnet.bits-goa.ac.in:8090/login.xml") + .header("Content-Type", "application/x-www-form-urlencoded") + .header("Content-Length", body.chars().count()) + .body(body) + .send(); +} + static mut PROCEED_CAMPNET_ATTEMPT: bool = false; static mut LOGOUT_CAMPNET: bool = false; -unsafe fn connect_campnet(file_path: &std::path::PathBuf) { +unsafe fn connect_campnet(app_handle: tauri::AppHandle) { + let file_path = path::app_dir(&app_handle.config()) + .unwrap() + .join("credentials.json"); let client = reqwest::blocking::Client::new(); if PROCEED_CAMPNET_ATTEMPT { let campnet_status = client.head("https://campnet.bits-goa.ac.in:8090/").send(); @@ -47,22 +91,7 @@ unsafe fn connect_campnet(file_path: &std::path::PathBuf) { let helper_file = file_path.parent().unwrap().join("credentials.json"); let creds = load_creds(&helper_file); if creds.is_ok() { - let creds = creds.unwrap(); - let body: String = format!( - "mode=191&username={}&password={}&a={}&producttype=1", - creds.username, - creds.password, - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_millis() - ); - let res = client - .post("https://campnet.bits-goa.ac.in:8090/login.xml") - .header("Content-Type", "application/x-www-form-urlencoded") - .header("Content-Length", body.chars().count()) - .body(body) - .send(); + let res = login_campnet(creds.unwrap(), client); if res.is_ok() { let res_body: String = res.unwrap().text().unwrap(); if res_body.contains("LIVE") { @@ -97,27 +126,11 @@ unsafe fn connect_campnet(file_path: &std::path::PathBuf) { } } } - } - if LOGOUT_CAMPNET { + } else if LOGOUT_CAMPNET { let helper_file = file_path.parent().unwrap().join("credentials.json"); let creds = load_creds(&helper_file); if creds.is_ok() { - let creds = creds.unwrap(); - let body: String = format!( - "mode=193&username={}&a={}&producttype=1", - creds.username, - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap() - .as_millis() - ); - let client = reqwest::blocking::Client::new(); - let res = client - .post("https://campnet.bits-goa.ac.in:8090/logout.xml") - .header("Content-Type", "application/x-www-form-urlencoded") - .header("Content-Length", body.chars().count()) - .body(body) - .send(); + let res = logout_campnet(creds.unwrap(), client); if res.is_ok() { let res_body: String = res.unwrap().text().unwrap(); if res_body.contains("LOGIN") { @@ -132,10 +145,9 @@ unsafe fn connect_campnet(file_path: &std::path::PathBuf) { } let callback_timer = timer::Timer::new(); - let callback_path = file_path.parent().unwrap().join("credentials.json"); let _callback_gaurd = callback_timer.schedule_with_delay(chrono::Duration::milliseconds(2500), move || { - connect_campnet(&callback_path); + connect_campnet(app_handle.app_handle()); }); std::thread::sleep(std::time::Duration::from_millis(3000)); } @@ -182,8 +194,7 @@ fn main() { .hide() .unwrap(); }); - let read_save_file = save_dir.join("credentials.json"); - connect_campnet(&read_save_file); + connect_campnet(app.handle()); std::fs::create_dir_all(save_dir).unwrap(); Ok(()) }) @@ -226,7 +237,7 @@ fn main() { let creds = load_creds(&save_file); if creds.is_ok() { if PROCEED_CAMPNET_ATTEMPT { - connect_campnet(&save_file); + connect_campnet(app.app_handle()); } PROCEED_CAMPNET_ATTEMPT = true; } else { From e0f676c2d811130153aea390b09448f5e2dec32b Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Fri, 30 Sep 2022 15:00:29 +0530 Subject: [PATCH 4/5] Move login endpoint to constants at beginning of the file Signed-off-by: Hrishikesh Patil --- src-tauri/src/main.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 3d5a166..fc02e4d 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -4,6 +4,8 @@ windows_subsystem = "windows" )] +static LOGIN_ENDPOINT: &str = "https://campnet.bits-goa.ac.in:8090"; + use serde::{Deserialize, Serialize}; use std::io::Write; use tauri::{ @@ -47,7 +49,7 @@ fn logout_campnet( .as_millis() ); return client - .post("https://campnet.bits-goa.ac.in:8090/logout.xml") + .post(LOGIN_ENDPOINT.to_owned() + "/logout.xml") .header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Length", body.chars().count()) .body(body) @@ -68,7 +70,7 @@ fn login_campnet( .as_millis() ); return client - .post("https://campnet.bits-goa.ac.in:8090/login.xml") + .post(LOGIN_ENDPOINT.to_owned() + "/login.xml") .header("Content-Type", "application/x-www-form-urlencoded") .header("Content-Length", body.chars().count()) .body(body) @@ -84,7 +86,7 @@ unsafe fn connect_campnet(app_handle: tauri::AppHandle) { .join("credentials.json"); let client = reqwest::blocking::Client::new(); if PROCEED_CAMPNET_ATTEMPT { - let campnet_status = client.head("https://campnet.bits-goa.ac.in:8090/").send(); + let campnet_status = client.head(LOGIN_ENDPOINT.to_owned()).send(); if campnet_status.is_ok() { let login_status = client.head("https://www.google.com").send(); if login_status.is_err() { From 5dfea2b2d09ef76055e5f6dc0ec9037f50e8b3be Mon Sep 17 00:00:00 2001 From: Hrishikesh Patil Date: Fri, 30 Sep 2022 15:21:10 +0530 Subject: [PATCH 5/5] Bump version Signed-off-by: Hrishikesh Patil --- package.json | 2 +- src-tauri/Cargo.lock | 2 +- src-tauri/Cargo.toml | 2 +- src-tauri/tauri.conf.json | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 912dd35..54be411 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "auto-campnet-gui", - "version": "0.2.0", + "version": "0.2.1", "scripts": { "tauri": "tauri", "dev": "tauri dev", diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 936a9ab..f6a050d 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -64,7 +64,7 @@ checksum = "508b352bb5c066aac251f6daf6b36eccd03e8a88e8081cd44959ea277a3af9a8" [[package]] name = "app" -version = "0.2.0" +version = "0.2.1" dependencies = [ "chrono", "reqwest", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 38ef50b..3f470dd 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "app" -version = "0.2.0" +version = "0.2.1" description = "Connect to BITS Goa campus network automatically" authors = ["Hrishikesh Patil "] license = "MIT" diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index d2d460e..943c41d 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -1,7 +1,7 @@ { "package": { "productName": "Auto Campnet GUI", - "version": "0.2.0" + "version": "0.2.1" }, "build": { "distDir": "../ui",