From d1e7d33c35d27268988389123716a6592ce37689 Mon Sep 17 00:00:00 2001 From: Will Dillon Date: Wed, 12 Nov 2025 09:06:32 -0800 Subject: Added more packets --- src/packet.rs | 20 +++++++++++---- src/packet_content.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 78 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/packet.rs b/src/packet.rs index ef10fe9..73de4b5 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -3,7 +3,7 @@ use std::{fmt::{Debug, Display}, str::FromStr}; use hex::decode; use tokio_util::bytes::{Buf, Bytes}; use structdiff::{Difference, StructDiff}; -use crate::{identity::Keystore, packet_content::{ClearText, PacketContent}}; +use crate::{identity::Keystore, packet_content::{ClearRequest, ClearText, PacketContent}}; #[derive(PartialEq, Debug, Clone, Difference)] #[difference(expose)] @@ -107,7 +107,8 @@ impl From for Packet { let f = i as f32; f / 4.0 }).collect(); - trace.temp_path = vec![]; + // DON'T delete the temp path, so Trace has access to the path + // when it prints the SNR results in its Display function. packet.content = PacketContent::Trace(trace); } @@ -171,7 +172,16 @@ impl Packet { path.cipher.try_decrypt(keystore) }, PacketContent::Request(ref mut request) => { - request.cipher.try_decrypt(keystore) + let result = request.cipher.try_decrypt(keystore); + + if let Some(cleartext) = &request.cipher.cleartext { + let cleartext = ClearRequest::from(cleartext.clone()); + request.cleartext = Some(cleartext); + + result + } else { + false + } }, PacketContent::Response(ref mut response) => { response.cipher.try_decrypt(keystore) @@ -188,14 +198,14 @@ impl Packet { }, PacketContent::AnonReq(ref mut anon_req) => { - let decrypt_reault = keystore.decrypt_anon( + let decrypt_result = keystore.decrypt_anon( anon_req.dest, &anon_req.public_key, anon_req.mac, &anon_req.ciphertext ); - if let Some(_cleartext) = decrypt_reault { + if let Some(_cleartext) = decrypt_result { true } else { false diff --git a/src/packet_content.rs b/src/packet_content.rs index cd5e778..cdad0ff 100644 --- a/src/packet_content.rs +++ b/src/packet_content.rs @@ -1,5 +1,6 @@ use std::{fmt::{Debug, Display}, rc::Rc}; +use aes::cipher; use chrono::{DateTime, Local, Utc}; use hex::encode; use tokio_util::bytes::{Buf, Bytes}; @@ -163,14 +164,62 @@ impl PeerToPeerCipher { #[derive(PartialEq, Debug, Clone)] pub struct Request { - pub(crate) cipher: PeerToPeerCipher, + pub cipher: PeerToPeerCipher, + pub cleartext: Option, +} + +#[derive(PartialEq, Debug, Clone)] +pub struct ClearRequest { + pub timestamp: DateTime, + pub request_type: RequestType, + pub request_data: Bytes +} + +impl From for ClearRequest { + fn from(value: Bytes) -> Self { + let mut bytes = value; + + let mut clear_request = ClearRequest { + timestamp: DateTime::from_timestamp(0, 0).unwrap(), + request_type: RequestType::Invalid, + request_data: Bytes::new() + }; + + // Just check for the whole fixed-size part at once + if bytes.len() < 5 { return clear_request } + if let Some(timestamp) = DateTime::from_timestamp(bytes.get_u32_le() as i64, 0) { + clear_request.timestamp = timestamp; + } + + clear_request.request_type = RequestType::from(bytes.get_u8()); + + clear_request.request_data = bytes; + + clear_request + } +} + +#[derive(PartialEq, Debug, Clone)] +pub enum RequestType { + Stats, + Keepalive, + Telemetry, + MinMaxAvg, + ACL, + Invalid +} + +impl From for RequestType { + fn from(value: u8) -> Self { + todo!() + } } impl From for Request { fn from(value: Bytes) -> Self { - Request { - cipher: PeerToPeerCipher::from(value) + cipher: PeerToPeerCipher::from(value), + cleartext: None } } } @@ -183,8 +232,9 @@ impl Display for Request { self.cipher.mac ))?; - if let Some(_cleartext) = &self.cipher.cleartext { - f.write_fmt(format_args!("TODO")) + if let Some(cleartext) = &self.cipher.cleartext { + f.write_str("TODO") + // f.write_fmt(format_args!("{}", cleartext)) } else { f.write_str("ENCRYPTED") } @@ -702,7 +752,11 @@ impl From for Trace { impl Display for Trace { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("Todo")) + for (snr, hash) in self.path_snr.iter().zip(self.temp_path.clone()) { + f.write_fmt(format_args!("({:2x?}): {}dB ", hash, snr))?; + } + + Ok(()) } } @@ -719,7 +773,7 @@ impl From for MultiPart { impl Display for MultiPart { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_fmt(format_args!("Todo")) + f.write_fmt(format_args!("Multipart isn't defined.")) } } @@ -870,7 +924,8 @@ mod tests { mac: 0x1d87, ciphertext: Bytes::copy_from_slice(&decode("ccaac89563cbb39d2333b725e407a1a6").unwrap()), cleartext: None - }}), + }, + cleartext: None, }), incomplete: false, }; -- cgit v1.2.3