1 Commits

Author SHA1 Message Date
08518892f2 Merge pull request 'nat_transversal' (#3) from nat_transversal into master
Reviewed-on: #3
2026-01-16 10:20:01 +00:00
2 changed files with 17 additions and 42 deletions

View File

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

View File

@@ -461,31 +461,12 @@ pub async fn get_socket_address(username: String, ip: String) -> Option<SocketAd
match String::from_utf8(body.to_vec()) {
Ok(s) => {
let addresses = parse_addresses(&s);
addresses.iter().copied().find(|a| a.is_ipv4())
if let Some(first) = addresses.first() {
Some(first.clone())
} else {
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(),
}
}