feat: refactor prudp code and start working on refactoring rmc

This commit is contained in:
DJMrTV 2025-02-18 22:55:33 +01:00
commit 3ea7c7e671
37 changed files with 2029 additions and 456 deletions

View file

@ -2,6 +2,6 @@ pub mod packet;
pub mod router;
pub mod socket;
mod auth_module;
mod sockaddr;
pub mod secure;
pub mod sockaddr;
//pub mod secure;
pub mod station_url;

View file

@ -96,10 +96,11 @@ impl Debug for TypesFlags {
}
#[repr(transparent)]
#[derive(PartialEq, Eq, Copy, Clone, Pod, Zeroable, SwapEndian, Hash)]
#[derive(PartialEq, Eq, Ord, PartialOrd, Copy, Clone, Pod, Zeroable, SwapEndian, Hash)]
pub struct VirtualPort(pub(crate) u8);
impl VirtualPort {
#[inline]
pub const fn get_stream_type(self) -> u8 {
(self.0 & 0xF0) >> 4
@ -236,7 +237,7 @@ impl PacketOption{
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct PRUDPPacket {
pub header: PRUDPHeader,
pub packet_signature: [u8; 16],
@ -375,6 +376,7 @@ impl PRUDPPacket {
types_and_flags: flags,
sequence_id: self.header.sequence_id,
substream_id: self.header.substream_id,
session_id: self.header.session_id,
..base.header
},
options,

View file

@ -11,7 +11,7 @@ use once_cell::sync::Lazy;
use log::{error, info, trace};
use thiserror::Error;
use tokio::sync::RwLock;
use crate::prudp::socket::SocketData;
use crate::prudp::socket::{new_socket_pair, AnyInternalSocket, CryptoHandler, ExternalSocket};
use crate::prudp::packet::{PRUDPPacket, VirtualPort};
use crate::prudp::router::Error::VirtualPortTaken;
@ -22,10 +22,9 @@ static SERVER_DATAGRAMS: Lazy<u8> = Lazy::new(||{
});
pub struct Router {
endpoints: RwLock<[Option<Arc<SocketData>>; 16]>,
endpoints: RwLock<[Option<Arc<dyn AnyInternalSocket>>; 16]>,
running: AtomicBool,
socket: Arc<UdpSocket>,
//pub auth_module: Arc<dyn AuthModule>
_no_outside_construction: PhantomData<()>
}
#[derive(Debug, Error)]
@ -36,9 +35,6 @@ pub enum Error{
impl Router {
fn process_prudp_packet(&self, _packet: &PRUDPPacket){
}
async fn process_prudp_packets<'a>(self: Arc<Self>, _socket: Arc<UdpSocket>, addr: SocketAddrV4, udp_message: Vec<u8>){
let mut stream = Cursor::new(&udp_message);
@ -69,7 +65,9 @@ impl Router {
trace!("sending packet to endpoint");
endpoint.process_packet(connection, &packet).await;
tokio::spawn(async move {
endpoint.recieve_packet(connection, packet).await
});
}
}
@ -88,6 +86,7 @@ impl Router {
continue;
};
let current_msg = &msg_buffer[0..len];
tokio::spawn(self.clone().process_prudp_packets(socket.clone(), addr, current_msg.to_vec()));
@ -144,18 +143,22 @@ impl Router {
}
// returns Some(()) i
pub(crate) async fn add_socket(&self, socket: Arc<SocketData>) -> Result<(), Error>{
pub(crate) async fn add_socket<E: CryptoHandler>(&self, virtual_port: VirtualPort, encryption: E)
-> Result<ExternalSocket, Error>{
let mut endpoints = self.endpoints.write().await;
let idx = socket.get_virual_port().get_port_number() as usize;
let idx = virtual_port.get_port_number() as usize;
if endpoints[idx].is_none() {
endpoints[idx] = Some(socket);
} else {
// dont create the socket if we dont need to
if !endpoints[idx].is_none(){
return Err(VirtualPortTaken(idx as u8));
}
Ok(())
let (internal, external) = new_socket_pair(virtual_port, encryption, self.socket.clone());
endpoints[idx] = Some(internal);
Ok(external)
}
pub fn get_own_address(&self) -> SocketAddrV4{

View file

@ -73,12 +73,12 @@ pub fn read_secure_connection_data(data: &[u8], act: &Account) -> Option<([u8; 3
type Rc4U32 = StreamCipherCoreWrapper<Rc4Core<U32>>;
pub fn generate_secure_encryption_pairs(mut session_key: [u8; 32], count: u8) -> Vec<EncryptionPair>{
pub fn generate_secure_encryption_pairs(mut session_key: [u8; 32], count: u8) -> Vec<EncryptionPair<Rc4<U32>>>{
let mut vec = Vec::with_capacity(count as usize);
vec.push(EncryptionPair{
send: Box::new(Rc4U32::new_from_slice(&session_key).expect("unable to create rc4")),
recv: Box::new(Rc4U32::new_from_slice(&session_key).expect("unable to create rc4"))
send: Rc4U32::new_from_slice(&session_key).expect("unable to create rc4"),
recv: Rc4U32::new_from_slice(&session_key).expect("unable to create rc4")
});
for _ in 1..=count{
@ -91,8 +91,8 @@ pub fn generate_secure_encryption_pairs(mut session_key: [u8; 32], count: u8) ->
}
vec.push(EncryptionPair{
send: Box::new(Rc4U32::new_from_slice(&session_key).expect("unable to create rc4")),
recv: Box::new(Rc4U32::new_from_slice(&session_key).expect("unable to create rc4"))
send: Rc4U32::new_from_slice(&session_key).expect("unable to create rc4"),
recv: Rc4U32::new_from_slice(&session_key).expect("unable to create rc4")
});
}

View file

@ -5,14 +5,24 @@ use crate::prudp::packet::VirtualPort;
type Md5Hmac = Hmac<md5::Md5>;
#[derive(Eq, PartialEq, Hash, Debug, Copy, Clone)]
#[derive(Eq, PartialEq, Hash, Debug, Copy, Clone, Ord, PartialOrd)]
pub struct PRUDPSockAddr{
pub regular_socket_addr: SocketAddrV4,
pub virtual_port: VirtualPort
}
impl PRUDPSockAddr{
pub fn calculate_connection_signature(&self) -> [u8; 16] {
pub fn new(regular_socket_addr: SocketAddrV4, virtual_port: VirtualPort) -> Self{
Self{
regular_socket_addr,
virtual_port
}
}
pub(super) fn calculate_connection_signature(&self) -> [u8; 16] {
let mut hmac = Md5Hmac::new_from_slice(&[0; 16]).expect("fuck");
let mut data = self.regular_socket_addr.ip().octets().to_vec();

File diff suppressed because it is too large Load diff