149 lines
4.6 KiB
Rust
149 lines
4.6 KiB
Rust
use crate::NetworkEvent;
|
|
use crate::P2PSharedData;
|
|
use crate::cryptographic_signature::CryptographicSignature;
|
|
use crate::get_server_address;
|
|
use crate::message_handling::EventType;
|
|
use crate::messages_structure::construct_message;
|
|
use crate::server_communication::generate_id;
|
|
use crossbeam_channel::Sender;
|
|
use std::net::SocketAddr;
|
|
use std::str::FromStr;
|
|
use std::sync::Arc;
|
|
|
|
///
|
|
/// sends the cryptographic signature to the server using a PUT request over the HTTP API.
|
|
///
|
|
pub async fn register_with_the_server(
|
|
crypto_pair: &Arc<CryptographicSignature>,
|
|
server_uri: &String,
|
|
) -> Result<(), reqwest::Error> {
|
|
let client = reqwest::Client::new();
|
|
let uri = format!("{0}/peers/{1}/key", server_uri, 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();
|
|
let res = client.put(uri).body(pubkey_bytes_minus).send().await?;
|
|
let res = res.error_for_status()?;
|
|
match res.error_for_status() {
|
|
Ok(_) => {
|
|
println!("register ip adresses");
|
|
Ok(())
|
|
}
|
|
Err(e) => Err(e),
|
|
}
|
|
}
|
|
|
|
pub fn parse_addresses(input: &String) -> Vec<SocketAddr> {
|
|
let mut addrs = Vec::new();
|
|
for line in input.lines() {
|
|
let s = line.trim();
|
|
if s.is_empty() {
|
|
continue;
|
|
}
|
|
if let Ok(sock) = SocketAddr::from_str(s) {
|
|
addrs.push(sock);
|
|
}
|
|
}
|
|
addrs
|
|
}
|
|
|
|
///
|
|
/// registers the IP addresses by sending a Hello request to the server.
|
|
///
|
|
pub async fn perform_handshake(
|
|
sd: &P2PSharedData,
|
|
username: String,
|
|
ip: String,
|
|
event_tx: Sender<NetworkEvent>,
|
|
is_server_handshake: bool,
|
|
) {
|
|
println!("username: {}, ip: {}", username.clone(), ip.clone());
|
|
let crypto_pair = sd.cryptopair_ref();
|
|
let senders = sd.senders_ref();
|
|
|
|
let id = generate_id();
|
|
let server_addr_query = get_server_address(username.clone(), ip.clone());
|
|
match server_addr_query.await {
|
|
Some(sockaddr_bytes) => {
|
|
sd.set_servername(username);
|
|
sd.set_serveraddress(sockaddr_bytes.to_string());
|
|
// first: &SocketAddr
|
|
let mut payload = Vec::new();
|
|
payload.extend_from_slice(&0u32.to_be_bytes());
|
|
payload.extend_from_slice(&crypto_pair.username.clone().as_bytes());
|
|
let hello_handshake = construct_message(1, payload, id, crypto_pair);
|
|
if is_server_handshake {
|
|
sd.add_message(id, EventType::Hello);
|
|
} else {
|
|
sd.add_message(id, EventType::HelloThenRootRequest);
|
|
}
|
|
|
|
match hello_handshake {
|
|
Some(handshake_message) => {
|
|
senders.send_dispatch(
|
|
handshake_message,
|
|
sockaddr_bytes.to_string(),
|
|
is_server_handshake,
|
|
sd.messages_list(),
|
|
);
|
|
}
|
|
None => {}
|
|
}
|
|
}
|
|
None => {
|
|
let err_msg = format!("failed to retreive socket address:").to_string();
|
|
let res = event_tx.send(NetworkEvent::Error(err_msg));
|
|
}
|
|
}
|
|
|
|
/*let mut list = messages_list.lock().expect("Failed to lock messages_list");
|
|
match list.get(&id) {
|
|
Some(_) => {
|
|
list.remove(&id);
|
|
}
|
|
None => {
|
|
list.insert(id, EventType::ServerHelloReply);
|
|
}
|
|
}
|
|
println!("message sent: {}", &id);*/
|
|
// 3. Perform the insertion
|
|
/*let mut buf = [0u8; 1024];
|
|
socket.recv_from(&mut buf).expect("receive failed");
|
|
let hello_handshake_received = UDPMessage::parse(buf.to_vec());
|
|
hello_handshake_received.display();*/
|
|
//TODO
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
|
|
/*///
|
|
/// does the procedure to register with the server
|
|
///
|
|
#[tokio::test]
|
|
async fn registering_with_server() {
|
|
let username = String::from("gameixtreize");
|
|
let server_uri = String::from("https://jch.irif.fr:8443");
|
|
let crypto_pair = CryptographicSignature::new(username);
|
|
if let Err(e) = register_with_the_server(crypto_pair, server_uri).await {
|
|
eprintln!("Error during registration: {}", e);
|
|
}
|
|
}*/
|
|
|
|
/*///
|
|
/// retreives the socket address of a given peer
|
|
///
|
|
#[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);
|
|
}
|
|
}
|
|
}*/
|
|
}
|