some modifications

This commit is contained in:
Tiago Batista Cardoso
2026-01-16 12:33:21 +01:00
parent 14fa256f9c
commit 6b3cbbe557
2 changed files with 42 additions and 17 deletions

View File

@@ -7,6 +7,7 @@ use p256::ecdsa::{
signature::{Signer, Verifier}, signature::{Signer, Verifier},
}; };
use rand_core::OsRng; use rand_core::OsRng;
use reqwest::Error;
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
/// ///
@@ -50,20 +51,25 @@ pub async fn get_peer_key(username: &String) -> Result<VerifyingKey, reqwest::Er
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let uri = format!("https://jch.irif.fr:8443/peers/{}/key", username); let uri = format!("https://jch.irif.fr:8443/peers/{}/key", username);
let res = client.get(uri).send().await?; let res = client.get(uri).send().await?;
if res.status().is_success() {
match res.error_for_status_ref() {
Ok(_) => {
println!("Successfully retreived the peers key."); println!("Successfully retreived the peers key.");
} else {
eprintln!(
"Failed to get the peers key from the server. Status: {}",
res.status()
);
}
let body: Bytes = res.bytes().await?; let body: Bytes = res.bytes().await?;
let slice: &[u8] = body.as_ref(); let slice: &[u8] = body.as_ref();
let body_bytes: &[u8; 64] = slice.try_into().expect("size error"); let body_bytes: &[u8; 64] = slice.try_into().expect("size error");
let received_key = convert_verifyingkey(body_bytes); let received_key = convert_verifyingkey(body_bytes);
Ok(received_key) Ok(received_key)
} }
Err(e) => {
eprintln!(
"Failed to get the peers key from the server. Status: {}",
res.status()
);
Err(e)
}
}
}
fn convert_verifyingkey(raw_xy: &[u8; 64]) -> VerifyingKey { fn convert_verifyingkey(raw_xy: &[u8; 64]) -> VerifyingKey {
let mut sec1 = [0u8; 65]; let mut sec1 = [0u8; 65];

View File

@@ -461,12 +461,31 @@ pub async fn get_socket_address(username: String, ip: String) -> Option<SocketAd
match String::from_utf8(body.to_vec()) { match String::from_utf8(body.to_vec()) {
Ok(s) => { Ok(s) => {
let addresses = parse_addresses(&s); let addresses = parse_addresses(&s);
if let Some(first) = addresses.first() { addresses.iter().copied().find(|a| a.is_ipv4())
Some(first.clone())
} else {
None
}
} }
Err(_) => None, Err(_) => None,
} }
} }
pub async fn get_possible_socket_address(username: String, ip: String) -> Vec<SocketAddr> {
let client = reqwest::Client::new();
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.");
} else {
eprintln!(
"Failed to get the peers addresses from the server. Status: {}",
res.status()
);
}
let body: Bytes = res.bytes().await.expect("couldnt get bytes");
match String::from_utf8(body.to_vec()) {
Ok(s) => {
let addresses = parse_addresses(&s);
addresses.iter().copied().filter(|a| a.is_ipv4()).collect()
}
Err(_) => Vec::new(),
}
}