This commit is contained in:
TIBERGHIEN corentin
2026-01-21 02:45:48 +01:00
parent dacedd1ceb
commit 8b2ab4861b
10 changed files with 351 additions and 460 deletions

View File

@@ -1,5 +1,6 @@
mod cryptographic_signature;
mod data;
mod datum_generation;
mod datum_parsing;
mod message_handling;
mod messages_channels;
@@ -9,6 +10,7 @@ mod registration;
mod server_communication;
mod threads_handling;
use crate::peers_refresh::*;
use crate::{
cryptographic_signature::CryptographicSignature,
message_handling::EventType,
@@ -22,8 +24,10 @@ use crate::{
threads_handling::Worker,
};
use std::{
clone,
io::Error,
net::{IpAddr, Ipv4Addr, UdpSocket},
time::Duration,
};
use std::{
net::SocketAddr,
@@ -42,6 +46,7 @@ pub struct P2PSharedData {
use bytes::Bytes;
use p256::pkcs8::der::pem::Base64Encoder;
use reqwest::Client;
impl P2PSharedData {
pub fn new(
@@ -63,9 +68,6 @@ impl P2PSharedData {
let server_name = Arc::new(Mutex::new("".to_string()));
let handhsake_peers = Arc::new(HandshakeHistory::new());
let worker = handhsake_peers.update_handshake();
threads.push(worker);
Ok(P2PSharedData {
shared_socket: shared_socket,
shared_cryptopair: shared_cryptopair,
@@ -100,13 +102,12 @@ impl P2PSharedData {
pub fn socket_ref(&self) -> &UdpSocket {
&*self.shared_socket
}
pub fn handshakes(&self) -> Arc<HandshakeHistory> {
self.handshake_peers.clone()
}
pub fn cryptopair_ref(&self) -> &CryptographicSignature {
&*self.shared_cryptopair
}
pub fn handshake_ref(&self) -> &HandshakeHistory {
&*self.handshake_peers
}
pub fn messages_list_ref(&self) -> &Mutex<HashMap<i32, EventType>> {
&*self.shared_messageslist
@@ -195,8 +196,6 @@ pub fn start_p2p_executor(
// Use tokio to spawn the asynchronous networking logic
tokio::task::spawn(async move {
// P2P/Networking Setup goes here
let handshake_history = Arc::new(Mutex::new(HandshakeHistory::new()));
let handshake_clone = handshake_history.clone();
println!("Network executor started.");
@@ -208,13 +207,20 @@ pub fn start_p2p_executor(
NetworkCommand::ServerHandshake(username, ip) => {
println!("server handshake called");
if let Some(sd) = shared_data.as_mut() {
start_receving_thread(sd, event_tx.clone(), &handshake_clone);
start_receving_thread(sd, event_tx.clone(), sd.handshakes());
start_retry_thread(
sd.senders(),
4,
sd.messages_list(),
sd.threads().as_mut(),
);
update_handshake(
sd.senders(),
sd.cryptopair(),
sd.messages_list(),
sd.handshake_peers.username_k_peerinfo_v.clone(),
);
let res =
perform_handshake(&sd, username, ip, event_tx.clone(), true).await;
} else {
@@ -234,10 +240,7 @@ pub fn start_p2p_executor(
NetworkCommand::Discover(username, hash, ip) => {
// envoie un handshake au peer, puis un root request
if let Some(sd) = shared_data.as_ref() {
let res = {
let m = handshake_clone.lock().unwrap();
m.get_peer_info_username(username.clone()).cloned()
};
let res = sd.handshake_peers.get_peer_info_username(username.clone());
match res {
Some(peerinfo) => {
let id = generate_id();
@@ -392,7 +395,11 @@ pub fn start_p2p_executor(
);
}
}
None => {}
None => {
let err_msg =
format!("failed to retreive socket address:").to_string();
let res = event_tx.send(NetworkEvent::Error(err_msg));
}
}
}
println!("[Network] Ping() called");
@@ -428,13 +435,15 @@ pub fn start_p2p_executor(
print!("{:?}", payload.clone());
let id = generate_id();
let natreq = construct_message(
NATTRAVERSALREQUEST,
payload.clone(),
generate_id(),
id.clone(),
&sd.cryptopair(),
);
sd.add_message(id, EventType::NatTraversal);
sd.senders_ref().send_dispatch(
natreq.expect(
"couldnt construct message nattraversalrequest2",
@@ -502,11 +511,14 @@ fn parse_pack(s: &str) -> Option<[u8; 6]> {
///
pub async fn get_socket_address(username: String, ip: String) -> Option<SocketAddr> {
let client = reqwest::Client::new();
let client = Client::builder()
.timeout(Duration::from_secs(5))
.build()
.expect("cannot create client");
let uri = format!("{}/peers/{}/addresses", ip, username);
let res = client.get(uri).send().await.expect("couldnt get response");
if res.status().is_success() {
println!("Successfully retreived the addresses.");
println!("Successfully retreived the addresses. {}", res.status());
} else {
eprintln!(
"Failed to get the peers addresses from the server. Status: {}",