root request

This commit is contained in:
TIBERGHIEN corentin
2026-01-13 17:13:35 +01:00
parent 98fcc1a0b2
commit c852c5bb4a
6 changed files with 210 additions and 78 deletions

View File

@@ -1,5 +1,5 @@
use crate::{
NetworkEvent,
NetworkEvent, NodeHash,
cryptographic_signature::{
CryptographicSignature, get_peer_key, sign_message, verify_signature,
},
@@ -47,7 +47,7 @@ pub fn handle_recevied_message(
server_name: &String,
cmd_tx: crossbeam_channel::Sender<NetworkEvent>,
ip: SocketAddr,
handshake_history: HandshakeHistory,
handhsake_history: &Arc<Mutex<HandshakeHistory>>,
) {
if recevied_message.len() < 4 {
return;
@@ -78,7 +78,7 @@ pub fn handle_recevied_message(
cmd_tx,
ip,
messages_list,
handshake_history,
handhsake_history,
);
match resp {
@@ -168,8 +168,9 @@ pub fn parse_message(
cmd_tx: crossbeam_channel::Sender<NetworkEvent>,
ip: SocketAddr,
messages_list: &Arc<Mutex<HashMap<i32, EventType>>>,
handhsake_history: HandshakeHistory,
handhsake_history_mutex: &Arc<Mutex<HandshakeHistory>>,
) -> Option<Vec<u8>> {
let mut handhsake_history = handhsake_history_mutex.lock().unwrap();
let cmd_tx_clone = cmd_tx.clone();
let id_bytes: [u8; 4] = received_message[0..ID]
@@ -185,20 +186,27 @@ pub fn parse_message(
let msg_length = u16::from_be_bytes(length_bytes) as usize;
// verify signature
match msgtype {
HELLO | HELLOREPLY | ROOTREPLY | NODATUM | NATTRAVERSALREQUEST | NATTRAVERSALREQUEST2 => {
HELLO | HELLOREPLY | NODATUM | NATTRAVERSALREQUEST | NATTRAVERSALREQUEST2 => {
let ilength = u16::from_be_bytes(length_bytes);
println!("name received length: {}", ilength);
let received_name = &received_message[LENGTH + EXTENSIONS..LENGTH + ilength as usize];
let received_username = String::from_utf8(received_name.to_vec());
match received_username {
Ok(username) => {
let peer_pubkey = match handhsake_history.get_peer_info_username(username) {
Some(peerinfo) => peerinfo.pubkey,
_ => tokio::runtime::Runtime::new()
.unwrap()
.block_on(get_peer_key(&username))
.expect("failed to retrieve public key"),
};
let peer_pubkey =
match handhsake_history.get_peer_info_username(username.clone()) {
Some(peerinfo) => peerinfo.pubkey,
_ => tokio::runtime::Runtime::new()
.unwrap()
.block_on(get_peer_key(&username))
.expect("failed to retrieve public key"),
};
match msgtype {
HELLOREPLY => {
handhsake_history.add_new_handshake(peer_pubkey, "".to_string(), ip);
}
_ => {}
}
let signature: [u8; SIGNATURE] = received_message
[LENGTH + msg_length..LENGTH + msg_length + SIGNATURE]
.try_into()
@@ -217,6 +225,22 @@ pub fn parse_message(
}
}
}
ROOTREPLY => {
let ilength = u16::from_be_bytes(length_bytes);
println!("name received length: {}", ilength);
if let Some(peerinfo) = handhsake_history.get_peer_info_ip(ip.to_string()) {
if !verify_signature(peerinfo.pubkey, &received_message) {
println!(
"incorrect signature from given peer: {}, ignoring message of type {} with id {}",
&peerinfo.username, received_message[ID], id
);
return None;
} else {
println!("signature verified");
}
}
}
_ => {}
}
@@ -268,8 +292,18 @@ pub fn parse_message(
//
// ajoute a la liste des peers handshake
HELLOREPLY => {
// ajoute a la liste des peers handshake
handhsake_history.add_new_handshake(hash, username, ip);
// ajoute l'username a la liste des peers handshake
let received_length = u16::from_be_bytes(
received_message[TYPE..LENGTH]
.try_into()
.expect("incorrect size"),
);
let received_username =
&received_message[LENGTH + EXTENSIONS..LENGTH + received_length as usize];
handhsake_history.update_peer_info(
ip.to_string(),
String::from_utf8(received_username.to_vec()).expect("invalid conversion"),
);
// verifie s'il faut renvoyer un root request
let guard = messages_list.lock().expect("Échec du verrouillage");
let res = guard.get(&id);
@@ -300,15 +334,21 @@ pub fn parse_message(
//
ROOTREPLY => {
// recuperer le pseudo du peers ayant repondu
// envoyer le hash a la gui
let received_hash = String::from_utf8(received_message[LENGTH..(32 + LENGTH)].to_vec());
match received_hash {
Ok(hash) => {
cmd_tx_clone.send(NetworkEvent::FileTreeRootReceived());
let peers_exist = handhsake_history.get_peer_info_ip(ip.to_string());
match peers_exist {
Some(peerinfo) => {
// envoyer le hash a la gui
let received_hash: NodeHash = received_message[LENGTH..(32 + LENGTH)]
.try_into()
.expect("incorrect size");
let res = cmd_tx_clone.send(NetworkEvent::FileTreeRootReceived(
peerinfo.username.clone(),
received_hash,
));
println!("file tree sent")
}
Err(e) => {
println!("{}", e);
None => {
eprintln!("no peers found");
}
}
}