fix some other protocol errors

This commit is contained in:
PoliEcho 2025-07-28 22:02:50 +02:00
parent 752541c9f6
commit b1335bef08
3 changed files with 62 additions and 16 deletions

View File

@ -119,7 +119,9 @@ fn main() -> std::io::Result<()> {
&server_SocketAddr,
&socket,
&n,
&public_sock_addr,
&shared::crypto::encrypt(&n.key, &iv, public_sock_addr_raw.as_bytes())
.unwrap()
.into_boxed_slice(),
&iv,
);
n

View File

@ -81,6 +81,8 @@ pub fn query_request(
dst: &SocketAddr,
socket: &UdpSocket,
) -> Result<String, ServerErrorResponses> {
#[cfg(debug_assertions)]
println!("QUERY method");
match send_and_recv_with_retry(
buf,
&[ServerMethods::QUERY as u8],
@ -109,6 +111,8 @@ pub fn register_request(
public_sock_addr: &Box<[u8]>,
iv: &[u8; SALT_AND_IV_SIZE as usize],
) -> Result<usize, ServerErrorResponses> {
#[cfg(debug_assertions)]
println!("REGISTER method");
let mut send_buf: Box<[u8]> = vec![
0u8;
RegisterRequestDataPositions::DATA as usize
@ -169,6 +173,8 @@ pub fn get_request(
network_id: &String,
password: &Option<String>,
) -> Result<types::Network, ServerErrorResponses> {
#[cfg(debug_assertions)]
println!("GET method");
let mut send_buf: Box<[u8]> =
vec![0u8; GetRequestDataPositions::ID as usize + network_id.len()].into_boxed_slice();
send_buf[0] = ServerMethods::GET as u8;
@ -234,14 +240,7 @@ pub fn get_request(
+ sock_addr_len as usize]
.to_vec()
.into_boxed_slice();
#[cfg(debug_assertions)]
eprintln!(
"sock_addr_raw: {}",
sock_addr_raw
.iter()
.map(|x| format!("{:02X} ", x))
.collect::<String>()
);
loop {
// loop used to easily skip peer
let peer: SocketAddr = if encrypted {
@ -252,12 +251,21 @@ pub fn get_request(
+ offset
+ SALT_AND_IV_SIZE as usize],
);
#[cfg(debug_assertions)]
eprintln!(
"IV: {}\nSockAddr: {}",
iv.iter().map(|x| format!("{:02X} ", x)).collect::<String>(),
sock_addr_raw
.iter()
.map(|x| format!("{:02X} ", x))
.collect::<String>(),
);
match SocketAddr::from_str(&{
// sacrificed a goat to borrow checker to make this work
let decrypted = match shared::crypto::decrypt(&key, &iv, &sock_addr_raw) {
Ok(v) => v,
Err(_) => {
eprintln!("Warning peer ignored due to invalid data");
Err(e) => {
eprintln!("Warning peer ignored due to invalid data\nError: {}", e);
break;
}
};
@ -272,8 +280,8 @@ pub fn get_request(
}
}) {
Ok(s) => s,
Err(_) => {
eprintln!("Warning peer ignored due to invalid data");
Err(e) => {
eprintln!("Warning peer ignored due to invalid data\nError: {}", e);
break;
}
}
@ -287,8 +295,8 @@ pub fn get_request(
}
}) {
Ok(s) => s,
Err(_) => {
eprintln!("Warning peer ignored due to invalid data");
Err(e) => {
eprintln!("Warning peer ignored due to invalid data\nError: {}", e);
break;
}
}
@ -297,7 +305,7 @@ pub fn get_request(
peers.push(peer);
break;
}
offset += SALT_AND_IV_SIZE as usize + sock_addr_len as usize;
offset += SALT_AND_IV_SIZE as usize + sock_addr_len as usize + 1 /*for size byte */;
num_of_clients -= 1;
}
@ -318,6 +326,8 @@ pub fn send_heartbeat(
my_public_sock_addr: &Box<[u8]>,
iv: &[u8; SALT_AND_IV_SIZE as usize],
) -> Result<usize, ServerErrorResponses> {
#[cfg(debug_assertions)]
println!("HEARTBEAT method");
let mut send_buf: Box<[u8]> = vec![
0u8;
HeartBeatRequestDataPositions::IV as usize
@ -346,6 +356,16 @@ pub fn send_heartbeat(
+ my_public_sock_addr.len()]
.copy_from_slice(&my_public_sock_addr);
#[cfg(debug_assertions)]
eprintln!(
"IV: {}\nSockAddr: {}",
iv.iter().map(|x| format!("{:02X} ", x)).collect::<String>(),
my_public_sock_addr
.iter()
.map(|x| format!("{:02X} ", x))
.collect::<String>(),
);
match send_and_recv_with_retry(buf, &send_buf, dst, socket, STANDARD_RETRY_MAX) {
Ok((data_lenght, _)) => return Ok(data_lenght),
Err(e) => return Err(e),

View File

@ -92,6 +92,20 @@ pub async fn handle_request(
eprintln!("Found {} clients", registration.clients.len());
registration.clients.iter().for_each(|client| {
#[cfg(debug_assertions)]
eprintln!(
"Client:\nIV: {}\nSockAddr: {}",
client
.iv
.iter()
.map(|x| format!("{:02X} ", x))
.collect::<String>(),
client
.client_sock_addr
.iter()
.map(|x| format!("{:02X} ", x))
.collect::<String>(),
);
let sock_addr_len: u8 = client.client_sock_addr.len() as u8;
send_vec.push(sock_addr_len);
@ -296,6 +310,16 @@ pub async fn handle_request(
+ sock_addr_len as usize]
.to_vec();
#[cfg(debug_assertions)]
eprintln!(
"IV: {}\nSockAddr: {}",
iv.iter().map(|x| format!("{:02X} ", x)).collect::<String>(),
sock_addr
.iter()
.map(|x| format!("{:02X} ", x))
.collect::<String>(),
);
match registration_vector
.iter()
.find(|elem| elem.map(|s| &s.net_id == &net_id)) // find if id exists