few crypto additions

This commit is contained in:
2025-07-25 16:04:48 +02:00
parent 3e736aaf45
commit 3e4f722b12
7 changed files with 416 additions and 7 deletions
+8 -3
View File
@@ -3,7 +3,12 @@ pub const BUFFER_SIZE: usize = 65535;
pub const DEFAULT_TIMEOUT: u64 = 30;
pub const VERSION: &str = "v0.1";
pub enum ServerMetods {
REGISTER,
GET,
#[repr(u8)]
pub enum ServerMethods {
QUERY = 0,
REGISTER = 1,
GET = 2,
HEARTBEAT = 3,
}
pub mod shared;
+11 -2
View File
@@ -13,13 +13,22 @@ fn main() -> std::io::Result<()> {
.expect("Failed to bind to any available port"),
);
let server_key_pear: pea_2_pea::shared::crypto::KeyPair =
pea_2_pea::shared::crypto::generate_rsa_key_pair();
let mut buf: [u8; pea_2_pea::BUFFER_SIZE] = [0; pea_2_pea::BUFFER_SIZE];
smol::block_on(async {
loop {
match socket.recv_from(&mut buf) {
Ok((data_length, src)) => {
smol::spawn(net::handle_request(buf, socket.clone(), src, data_length))
.detach();
smol::spawn(net::handle_request(
buf,
socket.clone(),
src,
data_length,
server_key_pear.clone(),
))
.detach();
}
Err(e) => {
eprintln!("Error receiving data: {}", e);
+25
View File
@@ -3,7 +3,32 @@ pub async fn handle_request(
socket: std::sync::Arc<std::net::UdpSocket>,
src: core::net::SocketAddr,
data_len: usize,
server_key_pair: pea_2_pea::shared::crypto::KeyPair,
) {
#[cfg(target_endian = "little")]
buf.reverse();
match buf[0] {
x if x == pea_2_pea::ServerMethods::QUERY as u8 => {
#[cfg(debug_assertions)]
println!("QUERY method");
}
x if x == pea_2_pea::ServerMethods::GET as u8 => {
#[cfg(debug_assertions)]
println!("GET method");
}
x if x == pea_2_pea::ServerMethods::REGISTER as u8 => {
#[cfg(debug_assertions)]
println!("REGISTER method");
}
x if x == pea_2_pea::ServerMethods::HEARTBEAT as u8 => {
#[cfg(debug_assertions)]
println!("HEARTBEAT method");
}
_ => {
#[cfg(debug_assertions)]
println!("Unknown method");
return;
}
}
}
+21
View File
@@ -0,0 +1,21 @@
#[derive(Clone)]
pub struct KeyPair {
private_key: rsa::RsaPrivateKey,
public_key: rsa::RsaPublicKey,
}
pub fn generate_rsa_private_key() -> Result<rsa::RsaPrivateKey, rsa::Error> {
let mut rng: rand::prelude::ThreadRng = rand::thread_rng();
let bits: usize = 2048;
return rsa::RsaPrivateKey::new(&mut rng, bits);
}
pub fn generate_rsa_key_pair() -> KeyPair {
let private_key = generate_rsa_private_key().unwrap();
let public_key = rsa::RsaPublicKey::from(&private_key);
KeyPair {
private_key,
public_key,
}
}
+1
View File
@@ -0,0 +1 @@
pub mod crypto;