From 1b175b81e9704227cf9b714aa06cc37a4fb5d738 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Tue, 18 May 2021 18:46:23 +0200 Subject: [PATCH] Add config.yml & Set AIRA_VERSION in build.rs --- Cargo.lock | 12 +++++++++++- Cargo.toml | 2 ++ build.rs | 33 ++++++++++++++++++++++++--------- config.yml | 2 ++ src/frontend/commons/style.css | 2 +- src/frontend/index.css | 18 +++++++++--------- src/frontend/index.js | 11 +++++++---- src/main.rs | 20 ++++++++++++++++---- src/session_manager.rs | 16 ++++++---------- 9 files changed, 78 insertions(+), 38 deletions(-) create mode 100644 config.yml diff --git a/Cargo.lock b/Cargo.lock index 811597a..2b73c23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -389,13 +389,14 @@ dependencies = [ "tungstenite", "uuid", "webbrowser", + "yaml-rust", "zeroize", ] [[package]] name = "async-psec" version = "0.1.0" -source = "git+https://forge.chapril.org/hardcoresushi/async-psec#272e7867e7a385b34d3d955aa3e5076e9833d3f1" +source = "git+https://forge.chapril.org/hardcoresushi/async-psec#9d2713aea1e982d7c0f10a466268f623a87ec5d9" dependencies = [ "aes-gcm", "async-trait", @@ -2732,6 +2733,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] + [[package]] name = "zeroize" version = "1.3.0" diff --git a/Cargo.toml b/Cargo.toml index 887db23..83a0e11 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,8 @@ if-addrs = "0.6" base64 = "0.13" scrypt = "0.7" zeroize = "1.2" +yaml-rust = "0.4" #only in debug mode [build-dependencies] html-minifier = "3.0" +yaml-rust = "0.4" \ No newline at end of file diff --git a/build.rs b/build.rs index 0d7f63f..194a168 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,7 @@ +#[cfg(not(debug_assertions))] use std::{env, fs::{File, read_to_string, create_dir}, path::Path, io::{Write, ErrorKind}}; -#[allow(dead_code)] +#[cfg(not(debug_assertions))] fn minify_content(content: &str, language: &str) -> Option { match language { "html" => Some(html_minifier::minify(content).unwrap()), @@ -10,8 +11,10 @@ fn minify_content(content: &str, language: &str) -> Option { } } -#[allow(dead_code)] -fn minify_web_files() { +#[cfg(not(debug_assertions))] +fn generate_web_files() { + use yaml_rust::YamlLoader; + let out_dir = env::var("OUT_DIR").unwrap(); let out_dir = Path::new(&out_dir); let src_dir = Path::new("src/frontend"); @@ -22,23 +25,35 @@ fn minify_web_files() { } } + let config = &YamlLoader::load_from_str(&read_to_string("config.yml").unwrap()).unwrap()[0]; + let css_values = config["css"].as_hash().unwrap(); + [ "login.html", "index.html", "index.css", "index.js", "commons/style.css", - "commons/script.js" + "commons/script.js", ].iter().for_each(|file_name| { - let file_name = Path::new(file_name); - let content = read_to_string(src_dir.join(file_name)).unwrap(); - let minified_content = minify_content(&content, file_name.extension().unwrap().to_str().unwrap()).unwrap(); - let mut dst = File::create(out_dir.join(file_name)).unwrap(); + let path = Path::new(file_name); + let extension = path.extension().unwrap().to_str().unwrap(); + let mut content = read_to_string(src_dir.join(path)).unwrap(); + if extension == "css" { + css_values.into_iter().for_each(|entry| { + content = content.replace(entry.0.as_str().unwrap(), entry.1.as_str().unwrap()); + }); + } + if file_name == &"index.html" { + content = content.replace("AIRA_VERSION", env!("CARGO_PKG_VERSION")); + } + let minified_content = minify_content(&content, extension).unwrap(); + let mut dst = File::create(out_dir.join(path)).unwrap(); dst.write(minified_content.as_bytes()).unwrap(); }); } fn main() { #[cfg(not(debug_assertions))] - minify_web_files(); + generate_web_files(); } \ No newline at end of file diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..67a14f4 --- /dev/null +++ b/config.yml @@ -0,0 +1,2 @@ +css: + ACCENT_COLOR: "19a52c" \ No newline at end of file diff --git a/src/frontend/commons/style.css b/src/frontend/commons/style.css index 8dc7fbb..14bf268 100644 --- a/src/frontend/commons/style.css +++ b/src/frontend/commons/style.css @@ -4,7 +4,7 @@ } :root { - --accent: #19a52c; + --accent: #ACCENT_COLOR; --transparent: #00000000; } diff --git a/src/frontend/index.css b/src/frontend/index.css index 22da76c..8b6e584 100644 --- a/src/frontend/index.css +++ b/src/frontend/index.css @@ -57,7 +57,7 @@ input[type="file"] { cursor: pointer; } .file_picker::after { - content: url("/static/imgs/icons/attach/19a52c"); + content: url("/static/imgs/icons/attach/ACCENT_COLOR"); width: 2em; } .popup { @@ -126,7 +126,7 @@ input[type="file"] { font-size: 0.9em; } .popup h2.warning::before { - content: url("/static/imgs/icons/warning/19a52c"); + content: url("/static/imgs/icons/warning/ACCENT_COLOR"); width: 9%; display: inline-block; vertical-align: middle; @@ -272,7 +272,7 @@ label { background-color: #333940; } #left_panel ul li p::after { - content: url("/static/imgs/icons/warning/19a52c"); + content: url("/static/imgs/icons/warning/ACCENT_COLOR"); display: inline-block; width: 1em; margin-left: 5px; @@ -282,7 +282,7 @@ label { content: unset; } #left_panel ul li.is_verified p::after { - content: url("/static/imgs/icons/verified/19a52c"); + content: url("/static/imgs/icons/verified/ACCENT_COLOR"); } #left_panel ul li .not_seen_marker { width: 12px; @@ -316,7 +316,7 @@ label { } #show_local_ips:hover::after { background-color: unset; - content: url("/static/imgs/icons/info/19a52c"); + content: url("/static/imgs/icons/info/ACCENT_COLOR"); } .popup ul { list-style-type: unset; @@ -341,7 +341,7 @@ label { margin: 0; } #chat_header p::after { - content: url("/static/imgs/icons/warning/19a52c"); + content: url("/static/imgs/icons/warning/ACCENT_COLOR"); display: inline-block; width: 1.2em; vertical-align: middle; @@ -351,7 +351,7 @@ label { content: unset; } #chat_header.is_verified p::after { - content: url("/static/imgs/icons/verified/19a52c"); + content: url("/static/imgs/icons/verified/ACCENT_COLOR"); } #chat_header.is_contact #delete_conversation::after { content: url("/static/imgs/icons/delete_conversation"); @@ -403,7 +403,7 @@ label { #file_cancel::after { background-color: unset; width: 20px; - content: url("/static/imgs/icons/cancel/19a52c"); + content: url("/static/imgs/icons/cancel/ACCENT_COLOR"); } #file_progress { display: none; @@ -477,7 +477,7 @@ label { color: var(--accent); } #msg_log .file a::after { - content: url("/static/imgs/icons/download/19a52c"); + content: url("/static/imgs/icons/download/ACCENT_COLOR"); display: block; width: 2em; margin-left: 15px; diff --git a/src/frontend/index.js b/src/frontend/index.js index 4b1a010..d9d4754 100644 --- a/src/frontend/index.js +++ b/src/frontend/index.js @@ -1071,6 +1071,7 @@ function displayChatBottom(speed = undefined) { function displayHistory(scrollToBottom = true) { msg_log.style.display = "block"; msg_log.innerHTML = ""; + let session = sessionsData.get(currentSessionId); let previousOutgoing = undefined; msgHistory.get(currentSessionId).forEach(entry => { let name = undefined; @@ -1079,7 +1080,7 @@ function displayHistory(scrollToBottom = true) { if (entry[0]) { //outgoing msg name = identityName; } else { - name = sessionsData.get(currentSessionId).name; + name = session.name; } } if (entry[1]) { //is file @@ -1091,7 +1092,9 @@ function displayHistory(scrollToBottom = true) { if (scrollToBottom) { msg_log.scrollTop = msg_log.scrollHeight; } - if (msg_log.scrollHeight <= msg_log.clientHeight) { - socket.send("load_msgs "+currentSessionId); + if (typeof session !== "undefined") { + if (msg_log.scrollHeight <= msg_log.clientHeight && session.isContact) { + socket.send("load_msgs "+currentSessionId); + } } -} +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ab1b27a..249f265 100644 --- a/src/main.rs +++ b/src/main.rs @@ -550,14 +550,14 @@ async fn handle_index(req: HttpRequest) -> HttpResponse { if is_authenticated(&req) { let global_vars_read = global_vars.read().unwrap(); #[cfg(debug_assertions)] - let html = fs::read_to_string("src/frontend/index.html").unwrap(); + let html = fs::read_to_string("src/frontend/index.html").unwrap() + .replace("AIRA_VERSION", env!("CARGO_PKG_VERSION")); #[cfg(not(debug_assertions))] let html = include_str!(concat!(env!("OUT_DIR"), "/index.html")); let public_key = global_vars_read.session_manager.identity.read().unwrap().as_ref().unwrap().get_public_key(); let use_padding = global_vars_read.session_manager.identity.read().unwrap().as_ref().unwrap().use_padding.to_string(); HttpResponse::Ok().body( html - .replace("AIRA_VERSION", env!("CARGO_PKG_VERSION")) .replace("IDENTITY_FINGERPRINT", &crypto::generate_fingerprint(&public_key)) .replace("WEBSOCKET_PORT", &global_vars_read.websocket_port.to_string()) .replace("IS_IDENTITY_PROTECTED", &Identity::is_protected().unwrap().to_string()) @@ -570,6 +570,18 @@ async fn handle_index(req: HttpRequest) -> HttpResponse { const JS_CONTENT_TYPE: &str = "text/javascript"; +#[cfg(debug_assertions)] +fn replace_css(file_path: &str) -> String { + use yaml_rust::YamlLoader; + let mut content = fs::read_to_string(file_path).unwrap(); + let config = &YamlLoader::load_from_str(&fs::read_to_string("config.yml").unwrap()).unwrap()[0]; + let css_values = config["css"].as_hash().unwrap(); + css_values.into_iter().for_each(|entry| { + content = content.replace(entry.0.as_str().unwrap(), entry.1.as_str().unwrap()); + }); + content +} + fn handle_static(req: HttpRequest) -> HttpResponse { let splits: Vec<&str> = req.path()[1..].split("/").collect(); if splits[0] == "static" { @@ -584,7 +596,7 @@ fn handle_static(req: HttpRequest) -> HttpResponse { } "index.css" => { #[cfg(debug_assertions)] - return response_builder.body(fs::read_to_string("src/frontend/index.css").unwrap()); + return response_builder.body(replace_css("src/frontend/index.css")); #[cfg(not(debug_assertions))] return response_builder.body(include_str!(concat!(env!("OUT_DIR"), "/index.css"))); } @@ -643,7 +655,7 @@ fn handle_static(req: HttpRequest) -> HttpResponse { } "style.css" => { #[cfg(debug_assertions)] - return response_builder.body(fs::read_to_string("src/frontend/commons/style.css").unwrap()); + return response_builder.body(replace_css("src/frontend/commons/style.css")); #[cfg(not(debug_assertions))] return response_builder.body(include_str!(concat!(env!("OUT_DIR"), "/commons/style.css"))); } diff --git a/src/session_manager.rs b/src/session_manager.rs index 29976d8..4cc21b3 100644 --- a/src/session_manager.rs +++ b/src/session_manager.rs @@ -85,9 +85,9 @@ impl SessionManager { pub fn store_msg(&self, session_id: &usize, outgoing: bool, buffer: Vec) { let mut msg_saved = false; - if self.is_contact(session_id) { - let mut offsets = self.last_loaded_msg_offsets.write().unwrap(); //locking mutex before modifying the DB to prevent race conditions - match self.identity.read().unwrap().as_ref().unwrap().store_msg(&self.loaded_contacts.read().unwrap().get(session_id).unwrap().uuid, outgoing, &buffer) { + if let Some(contact) = self.loaded_contacts.read().unwrap().get(session_id) { + let mut offsets = self.last_loaded_msg_offsets.write().unwrap(); //locking mutex before modifying the DB to prevent race conditions with load_msgs() + match self.identity.read().unwrap().as_ref().unwrap().store_msg(&contact.uuid, outgoing, &buffer) { Ok(_) => { *offsets.get_mut(session_id).unwrap() += 1; msg_saved = true; @@ -607,10 +607,6 @@ impl SessionManager { result } - pub fn is_contact(&self, session_id: &usize) -> bool { - self.loaded_contacts.read().unwrap().contains_key(session_id) - } - pub fn store_file(&self, session_id: &usize, data: &[u8]) -> Result { self.identity.read().unwrap().as_ref().unwrap().store_file(match self.loaded_contacts.read().unwrap().get(session_id) { Some(contact) => Some(contact.uuid), @@ -621,12 +617,12 @@ impl SessionManager { pub fn load_msgs(&self, session_id: &usize, count: usize) -> Option)>> { let mut offsets = self.last_loaded_msg_offsets.write().unwrap(); let msgs = self.identity.read().unwrap().as_ref().unwrap().load_msgs( - &self.loaded_contacts.read().unwrap().get(session_id).unwrap().uuid, - *offsets.get(session_id).unwrap(), + &self.loaded_contacts.read().unwrap().get(session_id)?.uuid, + *offsets.get(session_id)?, count ); if let Some(msgs) = msgs.as_ref() { - *offsets.get_mut(session_id).unwrap() += msgs.len(); + *offsets.get_mut(session_id)? += msgs.len(); } msgs }