wip registering ip addresses
This commit is contained in:
@@ -1,45 +1,74 @@
|
||||
use bytes::Bytes;
|
||||
|
||||
use crate::cryptographic_signature::{CryptographicSignature, formatPubKey, sign_message};
|
||||
use crate::messages_structure::HandshakeMessage;
|
||||
use crate::messages_structure::{HandshakeMessage, UDPMessage};
|
||||
use std::net::UdpSocket;
|
||||
///
|
||||
/// Registration with the server happens in two steps: first, the client
|
||||
/// sends its cryptographic signature to the server using a PUT request over the HTTP API.
|
||||
async fn register_with_the_server(crypto_pair: CryptographicSignature) -> Result<(), reqwest::Error>{
|
||||
/// sends its cryptographic signature to the server using a PUT request over the HTTP API.
|
||||
async fn register_with_the_server(
|
||||
crypto_pair: CryptographicSignature,
|
||||
) -> Result<(), reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let uri = format!("https://jch.irif.fr:8443/peers/{}/key", crypto_pair.username);
|
||||
let uri = format!(
|
||||
"https://jch.irif.fr:8443/peers/{}/key",
|
||||
crypto_pair.username
|
||||
);
|
||||
let encoded_point = crypto_pair.pub_key.to_encoded_point(false);
|
||||
let pubkey_bytes = encoded_point.as_ref().to_vec();
|
||||
let pubkey_bytes_minus = pubkey_bytes[1..].to_vec();
|
||||
// In order to register with the server, a peer ϕ makes a PUT request to the URL /peers/ϕ/key with its 64-byte public key in the body
|
||||
let res = client.put(uri)
|
||||
.body(pubkey_bytes_minus)
|
||||
.send()
|
||||
.await?;
|
||||
let pubkey_bytes_minus = pubkey_bytes[1..].to_vec();
|
||||
// In order to register with the server, a peer ϕ makes a PUT request to the URL /peers/ϕ/key with its 64-byte public key in the body
|
||||
let res = client.put(uri).body(pubkey_bytes_minus).send().await?;
|
||||
if res.status().is_success() {
|
||||
println!("Successfully registered with the server.");
|
||||
} else {
|
||||
eprintln!("Failed to register with the server. Status: {}", res.status());
|
||||
eprintln!(
|
||||
"Failed to register with the server. Status: {}",
|
||||
res.status()
|
||||
);
|
||||
let str = hex::encode(res.bytes().await?);
|
||||
eprintln!("erreur : {}", str);
|
||||
}
|
||||
println!("register ip adresses");
|
||||
register_ip_addresses(crypto_pair);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_socket_address(username: String) -> Result<Bytes, reqwest::Error> {
|
||||
let client = reqwest::Client::new();
|
||||
let uri = format!("https://jch.irif.fr:8443/peers/{}/addresses", username);
|
||||
let res = client.get(uri).send().await?;
|
||||
if res.status().is_success() {
|
||||
println!("Successfully retreived the addresses.");
|
||||
} else {
|
||||
eprintln!(
|
||||
"Failed to register with the server. Status: {}",
|
||||
res.status()
|
||||
);
|
||||
}
|
||||
let body: Bytes = res.bytes().await?;
|
||||
Ok(body)
|
||||
}
|
||||
|
||||
/// It then
|
||||
/// registers each of its IP addresses by sending a Hello request to the server.
|
||||
/// After the client sends a Hello request to the server, the server will verify that the client is able
|
||||
/// to receive requests by sending a Hello request to the client. If the client doesn’t reply to the Hello
|
||||
/// request with a properly signed message, its address will not be published by the server.
|
||||
fn register_ip_addresses(crypto_pair: CryptographicSignature) {
|
||||
let socket = UdpSocket::bind("127.0.0.1:4242").expect("bind failed");
|
||||
let socket = UdpSocket::bind("0.0.0.0:0").expect("bind failed");
|
||||
let username_size = crypto_pair.username.len();
|
||||
let hello_handshake = HandshakeMessage::hello(0, username_size as u16, crypto_pair.username.clone());
|
||||
let hello_handshake =
|
||||
HandshakeMessage::hello(545, username_size as u16 + 4, crypto_pair.username.clone());
|
||||
//HandshakeMessage::display(&hello_handshake);
|
||||
let hello_handshake_serialized = hello_handshake.serialize();
|
||||
let message_signed = sign_message(crypto_pair, hello_handshake_serialized);
|
||||
socket.send_to(&message_signed, "jch.irif.fr:8443").expect("send failed");
|
||||
let message_signed = sign_message(crypto_pair, hello_handshake_serialized.to_vec());
|
||||
socket
|
||||
.send_to(&message_signed, "81.194.30.229:8443")
|
||||
.expect("send failed");
|
||||
let mut buf = [0u8; 1024];
|
||||
socket.recv_from(&mut buf).expect("receive failed");
|
||||
let hello_handshake_received = HandshakeMessage::parse(buf);
|
||||
let hello_handshake_received = UDPMessage::parse(buf);
|
||||
hello_handshake_received.display();
|
||||
}
|
||||
|
||||
@@ -48,12 +77,25 @@ mod tests {
|
||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||
use super::*;
|
||||
|
||||
/*#[tokio::test]
|
||||
async fn creating_cryptographic_signature() {
|
||||
let username = String::from("charlie_kirk");
|
||||
#[tokio::test]
|
||||
async fn registering_with_server() {
|
||||
let username = String::from("gamemixtreize");
|
||||
let crypto_pair = CryptographicSignature::new(username);
|
||||
if let Err(e) = register_with_the_server(crypto_pair).await {
|
||||
eprintln!("Error during registration: {}", e);
|
||||
if let Err(e) = register_with_the_server(crypto_pair).await {
|
||||
eprintln!("Error during registration: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
/*#[tokio::test]
|
||||
async fn retreive_socket_addr() {
|
||||
let username = String::from("ipjkndqfshjldfsjlbsdfjhhj");
|
||||
match get_socket_address(username).await {
|
||||
Ok(body) => {
|
||||
println!("{:?}",body);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Erreur HTTP: {}", e);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user