Compare commits
No commits in common. "1a25e882f30a7f4a8af5c0942c82fcce4396121b" and "0235de28961d0e6d80a18557361c1246a42005ea" have entirely different histories.
1a25e882f3
...
0235de2896
@ -142,5 +142,3 @@ pub fn register_request(
|
|||||||
Err(e) => return Err(e),
|
Err(e) => return Err(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_request() -> Result<usize, ServerErrorResponses> {}
|
|
||||||
|
11
src/lib.rs
11
src/lib.rs
@ -12,7 +12,7 @@ pub enum ServerMethods {
|
|||||||
QUERY = 0, // return IP and port of the client
|
QUERY = 0, // return IP and port of the client
|
||||||
REGISTER = 1,
|
REGISTER = 1,
|
||||||
GET = 2,
|
GET = 2,
|
||||||
HEARTBEAT = 3, // this also registers addtional clients
|
HEARTBEAT = 3,
|
||||||
}
|
}
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub enum ServerResponse {
|
pub enum ServerResponse {
|
||||||
@ -24,7 +24,6 @@ pub enum ServerResponse {
|
|||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ServerErrorResponses {
|
pub enum ServerErrorResponses {
|
||||||
// success server returns id of method
|
|
||||||
GENERAL_ERROR(String),
|
GENERAL_ERROR(String),
|
||||||
ID_EXISTS,
|
ID_EXISTS,
|
||||||
ID_DOESNT_EXIST,
|
ID_DOESNT_EXIST,
|
||||||
@ -76,12 +75,4 @@ pub enum GetResponseDataPositions {
|
|||||||
// after this there will be blocks of this sturcture: one byte size of sockaddr than there will be IV that is SALT_AND_IV_SIZE long and after that there will be sockaddr this repeats until the end of packet
|
// after this there will be blocks of this sturcture: one byte size of sockaddr than there will be IV that is SALT_AND_IV_SIZE long and after that there will be sockaddr this repeats until the end of packet
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(non_camel_case_types)]
|
|
||||||
pub enum HeartBeatRequestDataPositions {
|
|
||||||
ID_LEN = 1,
|
|
||||||
SOCKADDR_LEN = 2,
|
|
||||||
IV = 3,
|
|
||||||
DATA = (HeartBeatRequestDataPositions::IV as u8 + SALT_AND_IV_SIZE) as isize, // first ID than sockaddr
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod shared;
|
pub mod shared;
|
||||||
|
@ -68,12 +68,9 @@ pub async fn handle_request(
|
|||||||
}
|
}
|
||||||
.cloned();
|
.cloned();
|
||||||
let mut send_vec: Vec<u8> = Vec::with_capacity(
|
let mut send_vec: Vec<u8> = Vec::with_capacity(
|
||||||
1/*initial status byte */ +
|
|
||||||
GetResponseDataPositions::SALT as usize + /*2 times one for SALT and other for first IV*/ 2*SALT_AND_IV_SIZE as usize + 20, /*magic number guess for how long is encrypted residencial ipv4 with port long */
|
GetResponseDataPositions::SALT as usize + /*2 times one for SALT and other for first IV*/ 2*SALT_AND_IV_SIZE as usize + 20, /*magic number guess for how long is encrypted residencial ipv4 with port long */
|
||||||
); // use vector to handle many clients
|
); // use vector to handle many clients
|
||||||
|
|
||||||
send_vec.push(ServerMethods::GET as u8); // this means success
|
|
||||||
|
|
||||||
// lets start serializing
|
// lets start serializing
|
||||||
send_vec.push(registration.encrypted as u8);
|
send_vec.push(registration.encrypted as u8);
|
||||||
send_vec.push(registration.net_id.len() as u8);
|
send_vec.push(registration.net_id.len() as u8);
|
||||||
@ -91,7 +88,7 @@ pub async fn handle_request(
|
|||||||
send_vec.extend_from_slice(&client.client_sock_addr);
|
send_vec.extend_from_slice(&client.client_sock_addr);
|
||||||
});
|
});
|
||||||
|
|
||||||
if send_vec.len() > BUFFER_SIZE {
|
if (send_vec.len() > BUFFER_SIZE) {
|
||||||
send_general_error_to_client(
|
send_general_error_to_client(
|
||||||
src,
|
src,
|
||||||
std::io::Error::new(
|
std::io::Error::new(
|
||||||
@ -216,98 +213,10 @@ pub async fn handle_request(
|
|||||||
x if x == ServerMethods::HEARTBEAT as u8 => {
|
x if x == ServerMethods::HEARTBEAT as u8 => {
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
println!("HEARTBEAT method");
|
println!("HEARTBEAT method");
|
||||||
|
|
||||||
let id_len: u8 = if buf[HeartBeatRequestDataPositions::ID_LEN as usize] != 0 {
|
|
||||||
buf[HeartBeatRequestDataPositions::ID_LEN as usize]
|
|
||||||
} else {
|
|
||||||
send_general_error_to_client(
|
|
||||||
src,
|
|
||||||
std::io::Error::new(std::io::ErrorKind::InvalidInput, "ID too short!"),
|
|
||||||
socket,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
let sock_addr_len: u8 = if buf[HeartBeatRequestDataPositions::SOCKADDR_LEN as usize]
|
|
||||||
!= 0
|
|
||||||
{
|
|
||||||
buf[HeartBeatRequestDataPositions::SOCKADDR_LEN as usize]
|
|
||||||
} else {
|
|
||||||
send_general_error_to_client(
|
|
||||||
src,
|
|
||||||
std::io::Error::new(std::io::ErrorKind::InvalidInput, "SockAddr too short!"),
|
|
||||||
socket,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
|
|
||||||
let net_id: String = match std::str::from_utf8(
|
|
||||||
&buf[(HeartBeatRequestDataPositions::DATA as usize)
|
|
||||||
..(id_len as usize) + (HeartBeatRequestDataPositions::DATA as usize)],
|
|
||||||
) {
|
|
||||||
Ok(s) => s.to_string(),
|
|
||||||
Err(e) => {
|
|
||||||
eprint!("id to utf-8 failed: {}", e);
|
|
||||||
utils::send_general_error_to_client(src, e, socket);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let iv: [u8; SALT_AND_IV_SIZE as usize] =
|
|
||||||
buf[HeartBeatRequestDataPositions::IV as usize
|
|
||||||
..HeartBeatRequestDataPositions::IV as usize + SALT_AND_IV_SIZE as usize]
|
|
||||||
.try_into()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let sock_addr: Vec<u8> = buf[HeartBeatRequestDataPositions::DATA as usize
|
|
||||||
+ id_len as usize
|
|
||||||
..HeartBeatRequestDataPositions::DATA as usize
|
|
||||||
+ id_len as usize
|
|
||||||
+ sock_addr_len as usize]
|
|
||||||
.to_vec();
|
|
||||||
|
|
||||||
match registration_vector
|
|
||||||
.iter()
|
|
||||||
.find(|elem| elem.map(|s| &s.net_id == &net_id)) // find if id exists
|
|
||||||
{
|
|
||||||
Some(reg) => {
|
|
||||||
let current_time = chrono::Utc::now().timestamp();
|
|
||||||
reg.update(|r| {r.last_heart_beat = current_time;
|
|
||||||
match r.clients.par_iter_mut().find_first(|c| *c.client_sock_addr == *sock_addr && c.iv == iv) {
|
|
||||||
Some(c) => c.last_heart_beat = current_time,
|
|
||||||
None => {// add new client if it isn't found
|
|
||||||
r.clients.push(types::Client::new(sock_addr.clone(), current_time, iv));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
None => {match socket.send_to(&[ServerResponse::ID_DOESNT_EXIST as u8], src) {
|
|
||||||
Ok(s) => {
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
eprintln!("send {} bytes", s);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Error sending data: {}", e);
|
|
||||||
}
|
|
||||||
} return;}
|
|
||||||
}
|
|
||||||
match socket.send_to(&[ServerMethods::HEARTBEAT as u8], src) {
|
|
||||||
// succes responce
|
|
||||||
Ok(s) => {
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
eprintln!("send {} bytes", s);
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
eprintln!("Error sending data: {}", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!(
|
#[cfg(debug_assertions)]
|
||||||
"Warning!: client: {} called Unknown method: 0x{:02x}",
|
println!("Unknown method");
|
||||||
src.to_string(),
|
|
||||||
buf[0]
|
|
||||||
);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
use pea_2_pea::*;
|
use pea_2_pea::*;
|
||||||
use std::ops::{Deref, DerefMut};
|
|
||||||
use std::sync::{Arc, atomic::Ordering};
|
use std::sync::{Arc, atomic::Ordering};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@ -26,6 +25,7 @@ impl Client {
|
|||||||
pub struct Registration {
|
pub struct Registration {
|
||||||
#[readonly]
|
#[readonly]
|
||||||
pub net_id: String,
|
pub net_id: String,
|
||||||
|
#[readonly]
|
||||||
pub clients: Vec<Client>,
|
pub clients: Vec<Client>,
|
||||||
|
|
||||||
pub last_heart_beat: i64,
|
pub last_heart_beat: i64,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user