This commit is contained in:
Alex Emmet 2026-04-05 00:27:04 +02:00
commit c8a6d4104e
3 changed files with 44 additions and 37 deletions

View file

@ -19,11 +19,26 @@ impl ConfigUtil {
} }
pub fn clear(&mut self) { pub fn clear(&mut self) {
self.config = JsonValue::new_object(); self.config = JsonValue::new_object();
self.unique = false;
} }
pub fn load(&mut self) { pub fn load(&mut self) {
let s = load_file("", "config.json"); let s = load_file("", "config.json");
if !s.is_empty() { if s.is_empty() {
self.config = json::parse(&s).unwrap_or(JsonValue::new_object()); // File might be missing or empty/being written.
// We don't want to wipe the current config if it already has data.
// But if it's the first load, it will stay empty.
return;
}
match json::parse(&s) {
Ok(parsed) => {
self.config = parsed;
self.unique = false;
}
Err(e) => {
eprintln!("Failed to parse config.json: {}. Content: '{}'", e, s);
// Keep the current config rather than wiping it.
}
} }
} }
@ -55,6 +70,7 @@ impl ConfigUtil {
pub fn update(&mut self) { pub fn update(&mut self) {
if self.unique { if self.unique {
save_file("", "config.json", &self.config.to_string()); save_file("", "config.json", &self.config.to_string());
self.unique = false;
} }
} }
} }

View file

@ -9,8 +9,6 @@ use uuid::Uuid;
use walkdir::WalkDir; use walkdir::WalkDir;
use zip::ZipArchive; use zip::ZipArchive;
#[allow(dead_code)] #[allow(dead_code)]
pub fn delete_directory(path: &str) -> bool { pub fn delete_directory(path: &str) -> bool {
let dir = Path::new(&get_directory()).join(path); let dir = Path::new(&get_directory()).join(path);
@ -96,17 +94,10 @@ pub fn load_file(path: &str, name: &str) -> String {
let file_path = dir.join(name); let file_path = dir.join(name);
if !dir.exists() { if !dir.exists() {
if let Err(e) = fs::create_dir_all(&dir) {
println!("[IMPORTANT] Couldn't create directories: {}", e);
return String::new();
}
return String::new(); return String::new();
} }
if !file_path.exists() { if !file_path.exists() {
if let Err(e) = File::create(&file_path) {
println!("[IMPORTANT] Couldn't create file: {}", e);
}
return String::new(); return String::new();
} }

View file

@ -1,15 +1,15 @@
use dashmap::DashMap;
use iota_logger::{log, log_cv_in, log_cv_out, log_t};
use iota_state::{ACTIVE_TASKS, SHUTDOWN};
use iota_storage::users::contact::Contact; use iota_storage::users::contact::Contact;
use iota_storage::util::chat_files::{MessageState, change_message_state}; use iota_storage::util::chat_files::{MessageState, change_message_state};
use iota_storage::util::chats_util::{get_user, mod_user}; use iota_storage::util::chats_util::{get_user, mod_user};
use iota_storage::util::communities_util::CommunitiesUtil; use iota_storage::util::communities_util::CommunitiesUtil;
use iota_storage::util::config_util::CONFIG;
use iota_storage::util::{chat_files, chats_util};
use iota_util::crypto_helper;
use iota_util::crypto_util::{DataFormat, SecurePayload}; use iota_util::crypto_util::{DataFormat, SecurePayload};
use iota_util::file_util::{get_children, load_file, save_file}; use iota_util::file_util::{get_children, load_file, save_file};
use iota_storage::util::{chat_files, chats_util};
use iota_storage::util::config_util::CONFIG;
use iota_util::crypto_helper;
use iota_state::{ACTIVE_TASKS, SHUTDOWN};
use iota_logger::{log, log_cv_in, log_cv_out, log_t};
use dashmap::DashMap;
use json::JsonValue; use json::JsonValue;
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::{Arc, LazyLock}; use std::sync::{Arc, LazyLock};
@ -225,15 +225,15 @@ impl OmikronConnection {
*self.sender.write().await = Some(sender_arc.clone()); *self.sender.write().await = Some(sender_arc.clone());
*self.state.write().await = ConnectionState::Connected { identified: false }; *self.state.write().await = ConnectionState::Connected { identified: false };
// Handle registration/identification
self.handle_authentication().await;
// Start read loop // Start read loop
let read_self = self.clone(); let read_self = self.clone();
let read_handle = tokio::spawn(async move { let read_handle = tokio::spawn(async move {
read_self.read_loop(&mut receiver).await; read_self.read_loop(&mut receiver).await;
}); });
// Handle registration/identification
self.handle_authentication().await;
// Start heartbeat // Start heartbeat
let heartbeat_self = self.clone(); let heartbeat_self = self.clone();
let heartbeat_handle = tokio::spawn(async move { let heartbeat_handle = tokio::spawn(async move {
@ -280,26 +280,26 @@ impl OmikronConnection {
let private_key = conf.get_private_key(); let private_key = conf.get_private_key();
drop(conf); drop(conf);
if iota_id == 0 || public_key.is_none() || private_key.is_none() { if iota_id == 0 {
log_t!("iota_register_new"); log_t!("iota_register_new");
let (pub_k, priv_k) = if let (Some(pk), Some(sk)) = (public_key, private_key) {
(pk, sk)
} else {
let key_pair = crypto_helper::generate_keypair(); let key_pair = crypto_helper::generate_keypair();
let public_key_base64 = crypto_helper::public_key_to_base64(&key_pair.public); let public_key_base64 = crypto_helper::public_key_to_base64(&key_pair.public);
let _private_key_base64 = crypto_helper::secret_key_to_base64(&key_pair.secret); let private_key_base64 = crypto_helper::secret_key_to_base64(&key_pair.secret);
let mut conf_write = CONFIG.write().await; let mut conf_write = CONFIG.write().await;
// NOTE: conf_write.change("public_key", JsonValue::from(public_key_base64.clone()));
// Intentionally not storing the generated private/public keys directly into the conf_write.change("private_key", JsonValue::from(private_key_base64.clone()));
// config file here to avoid persisting sensitive material in plaintext. If you
// want to persist them, uncomment the two lines below and accept the security
// implications (they will be saved by `conf_write.update()`).
// conf_write.change("public_key", DataValue::Str(public_key_base64.clone()));
// conf_write.change("private_key", DataValue::Str(private_key_base64));
conf_write.update(); conf_write.update();
drop(conf_write); drop(conf_write);
(public_key_base64, private_key_base64)
};
let register_msg = CommunicationValue::new(CommunicationType::register_iota) let register_msg = CommunicationValue::new(CommunicationType::register_iota)
.add_data(DataTypes::public_key, DataValue::Str(public_key_base64)); .add_data(DataTypes::public_key, DataValue::Str(pub_k));
let msg_id = register_msg.get_id(); let msg_id = register_msg.get_id();
@ -311,7 +311,7 @@ impl OmikronConnection {
return false; return false;
} }
let iota_value = cv.get_data(DataTypes::register_id); let iota_value = cv.get_data(DataTypes::iota_id);
let iota_id = iota_value.as_number().unwrap_or(0); let iota_id = iota_value.as_number().unwrap_or(0);
if iota_id != 0 { if iota_id != 0 {