Skip to content

Commit

Permalink
Fix for the local memory
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardicus committed Oct 11, 2024
1 parent 91bba3b commit 04c0399
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 59 deletions.
114 changes: 106 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(dead_code)]
mod session;

use crate::session::messages::MessageData::{Chat, Encrypted};
use serde::{Deserialize, Serialize};
use session::crypto::{
ChaCha20Poly1305EnDeCrypt, Cryptical, CrypticalID, PGPEnCryptOwned, PGPEnDeCrypt,
Expand Down Expand Up @@ -55,6 +56,10 @@ struct Cli {
#[arg(default_value = "false")]
test_sender: bool,

#[clap(long)]
#[arg(default_value = "false")]
no_memory: bool,

#[clap(short, long)]
#[arg(default_value = "zenoh/config.json5")]
zenoh_config: String,
Expand Down Expand Up @@ -117,6 +122,10 @@ struct InitializeCommand {
pub entry: usize,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
struct RewindCommand {
pub entry: usize,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
struct ExitCommand {}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand All @@ -126,6 +135,7 @@ enum InputCommand {
Exit(ExitCommand),
Help(HelpCommand),
Remind(RemindCommand),
Rewind(RewindCommand),
}

impl InputCommand {
Expand Down Expand Up @@ -157,6 +167,14 @@ impl InputCommand {
let cmd = RemindCommand {};
Some(InputCommand::Remind(cmd))
}
Some("rewind") => {
let entry = match parts.next() {
Some(entry) => entry.parse::<usize>().unwrap(),
None => 0,
};
let cmd = RewindCommand { entry };
Some(InputCommand::Rewind(cmd))
}
_ => None,
}
}
Expand All @@ -166,19 +184,22 @@ impl InputCommand {
println_message_str(1, "Available commands:").await;
println_message_str(1, " list").await;
println_message_str(1, " - List and enumerate all discovered peers.").await;
println_message_str(1, " remind").await;
println_message_str(1, " - List and enumerate encrypted and stored sessions.").await;
println_message_str(1, " init [entry]").await;
println_message_str(1, " - Initialize a chat session with a peer").await;
println_message_str(1, " enumerated as per !list.").await;
println_message_str(1, " enumerated as per 'list'").await;
println_message_str(1, " remind").await;
println_message_str(1, " - List and enumerate encrypted and stored sessions.").await;
println_message_str(1, " rewind [entry]").await;
println_message_str(1, " - Decrypts and displays previous chat sessions").await;
println_message_str(1, " enumerated as per 'remind'.").await;
println_message_str(1, " exit").await;
println_message_str(1, " - Exit the program.").await;
}
async fn print_small_help() {
println_message(1, InputCommand::get_small_help()).await;
}
fn get_small_help() -> String {
"Welcome to Chat-PGP. Type help for help.".to_string()
"Welcome to Chat-PGP. Type 'help' for help.".to_string()
}
async fn read_yes_or_no(
window: usize,
Expand Down Expand Up @@ -701,6 +722,77 @@ async fn launch_terminal_program(
}
}
}
Some(InputCommand::Rewind(cmd)) => {
let entry = cmd.entry;
let ids = session.get_reminded_session_ids().await;
if ids.len() == 0 || entry > ids.len() || entry <= 0 {
println_message_str(
1,
"There is no memory of that session ¯\\_(ツ)_/¯... {} ",
)
.await;
} else {
let session_log =
session.get_reminded_session_log(&ids[entry - 1]).await;
if session_log.is_err() {
println_message_str(1, "Sorry. This did not work ¯\\_(ツ)_/¯... ")
.await;
} else {
let (encrypted_sym_key, logs) = session_log.unwrap();
println_message_str(
1,
"Decrypting stored encrypted session key...",
)
.await;
let sym_key =
session.decrypt_encrypted_str(encrypted_sym_key).await;

if sym_key.is_ok() {
println_message_str(1, "Restored session key.").await;
println_message_str(1, "Decrypting memory.").await;
let sym_key = sym_key.unwrap();
for msg in logs {
match msg.message {
Encrypted(msg) => {
let hidden_msg = session
.decrypt_sym_encrypted_msg(
sym_key.clone(),
msg.data.clone(),
)
.await;
if hidden_msg.is_ok() {
let msg = hidden_msg.unwrap();
match msg.message {
Chat(msg) => {
println_message(
1,
format!(
"[{}] {} ({})- {}",
msg.date_time,
msg.sender_userid,
short_fingerprint(
&msg.sender_fingerprint
),
msg.message
),
)
.await;
}
_ => {}
}
}
}
_ => {}
}
}
} else {
println_message_str(1, "Sorry. Cannot read that memory.. Perhaps you were using a different PGP key?")
.await;
}
}
}
}

None => {
// Send this input to listeners
if input.len() > 0 && session.get_number_of_sessions().await > 0 {
Expand All @@ -720,7 +812,14 @@ async fn launch_terminal_program(
let fingerprint =
pub_encro.get_public_key_fingerprint();
let topic_out = Topic::messaging_topic_in(&fingerprint);
let msg = SessionMessage::new_chat(input.clone());
let fingerprint = session.get_fingerprint().await;
let userid = session.get_userid().await;

let msg = SessionMessage::new_chat(
input.clone(),
userid.clone(),
fingerprint.clone(),
);
let _ = session
.session_send_msg(
&id,
Expand All @@ -729,8 +828,6 @@ async fn launch_terminal_program(
&zenoh_handler,
)
.await;
let fingerprint = session.get_fingerprint().await;
let userid = session.get_userid().await;

let (_, chat_view) =
format_chat_msg_fmt(&input, &userid, &fingerprint);
Expand Down Expand Up @@ -772,6 +869,7 @@ async fn main() {
let test_sender = cli.test_sender;
let test_receiver = cli.test_receiver;
let zenoh_config = cli.zenoh_config;
let memory = !cli.no_memory;

let mut cert = None;

Expand Down Expand Up @@ -801,7 +899,7 @@ async fn main() {
let cert = Arc::new(cert.unwrap());

let pgp_handler = PGPEnDeCrypt::new(cert.clone(), &passphrase);
let mut session = Session::new(pgp_handler, zenoh_config.clone(), false);
let mut session = Session::new(pgp_handler, zenoh_config.clone(), false, memory);

if test_receiver {
session.set_discovery_interval_seconds(1);
Expand Down
2 changes: 1 addition & 1 deletion src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn main() {
let pgp_handler = PGPEnDeCrypt::new(cert.clone(), &passphrase);

// launching a relay session
let mut session = Session::new(pgp_handler, zenoh_config.clone(), true);
let mut session = Session::new(pgp_handler, zenoh_config.clone(), true, false);

let _ = match session.serve().await {
Ok(_) => {}
Expand Down
7 changes: 6 additions & 1 deletion src/session/memory/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ impl Memory {

/// Returns a Vec of Strings containing the session_ids in the session_log.
pub fn get_session_ids(&self) -> Vec<String> {
self.session_log.keys().cloned().collect()
let mut session_logs: Vec<&SessionLog> = self.session_log.values().collect();
session_logs.sort_by(|a, b| b.last_active.cmp(&a.last_active));
session_logs
.iter()
.map(|log| log.session_id.clone())
.collect()
}

pub fn get_others(&self, session_id: &str) -> Result<Vec<String>, ()> {
Expand Down
15 changes: 11 additions & 4 deletions src/session/messages/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::session::protocol::challenge_len;
use crate::util::generate_random_string;
use crate::util::{generate_random_string, get_current_datetime};
use serde::{Deserialize, Serialize};
use serde_cbor;
use tokio::sync::mpsc;
Expand All @@ -15,7 +15,6 @@ pub struct InitMsg {
pub signature: String,
pub challenge: String,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct InitOkMsg {
pub sym_key_encrypted: String,
Expand Down Expand Up @@ -48,6 +47,9 @@ pub struct CloseMsg {
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ChatMsg {
pub message: String,
pub sender_userid: String,
pub sender_fingerprint: String,
pub date_time: String,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -252,9 +254,14 @@ impl SessionMessage {
session_id: "".to_string(),
}
}
pub fn new_chat(message: String) -> Self {
pub fn new_chat(message: String, sender_userid: String, sender_fingerprint: String) -> Self {
SessionMessage {
message: MessageData::Chat(ChatMsg { message: message }),
message: MessageData::Chat(ChatMsg {
message: message,
sender_userid,
sender_fingerprint,
date_time: get_current_datetime(),
}),
session_id: "".to_string(),
}
}
Expand Down
Loading

0 comments on commit 04c0399

Please sign in to comment.