more progress on friends
This commit is contained in:
parent
1b802ff33f
commit
7918e54487
19 changed files with 320 additions and 205 deletions
|
|
@ -16,4 +16,5 @@ hmac = "0.12.1"
|
|||
md-5 = "^0.10.6"
|
||||
|
||||
[features]
|
||||
friends = []
|
||||
prudpv0 = []
|
||||
friends = ["prudpv0"]
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ impl CryptoInstance for InsecureInstance {
|
|||
fn generate_signature(&self, types_flags: TypesFlags, data: &[u8]) -> [u8; 4] {
|
||||
if types_flags.get_types() == DATA {
|
||||
if data.len() == 0 {
|
||||
[0x12, 0x34, 0x56, 0x78]
|
||||
[0x78, 0x56, 0x34, 0x12]
|
||||
} else {
|
||||
let mut hmac = <HmacMd5 as Mac>::new_from_slice(ACCESS_KEY.as_bytes())
|
||||
.expect("unable to create hmac md5");
|
||||
|
|
|
|||
|
|
@ -1,6 +1,9 @@
|
|||
use hmac::Mac;
|
||||
use rc4::Rc4;
|
||||
use rnex_core::prudp::{encryption::EncryptionPair, types_flags::TypesFlags};
|
||||
use rc4::{Rc4, StreamCipher};
|
||||
use rnex_core::prudp::{
|
||||
encryption::EncryptionPair,
|
||||
types_flags::{TypesFlags, types::DATA},
|
||||
};
|
||||
use typenum::U32;
|
||||
|
||||
use crate::crypto::{
|
||||
|
|
@ -11,23 +14,34 @@ use crate::crypto::{
|
|||
|
||||
pub struct SecureInstance {
|
||||
pair: EncryptionPair<Rc4<U32>>,
|
||||
uid: u32,
|
||||
self_signat: [u8; 4],
|
||||
remote_signat: [u8; 4],
|
||||
}
|
||||
|
||||
impl CryptoInstance for SecureInstance {
|
||||
fn decrypt_incoming(&mut self, data: &mut [u8]) {
|
||||
todo!()
|
||||
self.pair.recv.apply_keystream(data);
|
||||
}
|
||||
fn encrypt_outgoing(&mut self, data: &mut [u8]) {
|
||||
todo!()
|
||||
self.pair.send.apply_keystream(data);
|
||||
}
|
||||
fn get_user_id(&self) -> u32 {
|
||||
todo!()
|
||||
self.uid
|
||||
}
|
||||
fn generate_signature(&self, types_flags: TypesFlags, data: &[u8]) -> [u8; 4] {
|
||||
let mut hmac = <HmacMd5 as Mac>::new_from_slice(ACCESS_KEY.as_bytes())
|
||||
.expect("unable to create hmac md5");
|
||||
hmac.update(data);
|
||||
hmac.finalize().into_bytes()[0..4].try_into().unwrap()
|
||||
if types_flags.get_types() == DATA {
|
||||
if data.len() == 0 {
|
||||
[0x78, 0x56, 0x34, 0x12]
|
||||
} else {
|
||||
let mut hmac = <HmacMd5 as Mac>::new_from_slice(ACCESS_KEY.as_bytes())
|
||||
.expect("unable to create hmac md5");
|
||||
hmac.update(data);
|
||||
hmac.finalize().into_bytes()[0..4].try_into().unwrap()
|
||||
}
|
||||
} else {
|
||||
self.self_signat
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,62 +1,67 @@
|
|||
use bytemuck::{Pod, Zeroable};
|
||||
use log::{error, info, warn};
|
||||
use proxy_common::{ProxyStartupParam, setup_edge_node_connection};
|
||||
use rnex_core::executables::common::{OWN_IP_PRIVATE, OWN_IP_PUBLIC, SERVER_PORT};
|
||||
use rnex_core::prudp::types_flags::TypesFlags;
|
||||
use rnex_core::prudp::types_flags::types::SYN;
|
||||
use rnex_core::prudp::virtual_port::VirtualPort;
|
||||
use rnex_core::reggie::EdgeNodeHolderConnectOption::Register;
|
||||
use rnex_core::reggie::RemoteEdgeNodeHolder;
|
||||
use rnex_core::rmc::protocols::{OnlyRemote, new_rmc_gateway_connection};
|
||||
use rnex_core::rmc::structures::RmcSerialize;
|
||||
use rnex_core::util::SplittableBufferConnection;
|
||||
use std::env;
|
||||
use std::net::SocketAddrV4;
|
||||
use std::process::abort;
|
||||
use std::sync::{Arc, LazyLock};
|
||||
use tokio::net::UdpSocket;
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "prudpv0")] {
|
||||
use bytemuck::{Pod, Zeroable};
|
||||
use cfg_if::cfg_if;
|
||||
use log::{error, info, warn};
|
||||
use proxy_common::{ProxyStartupParam, setup_edge_node_connection};
|
||||
use rnex_core::executables::common::{OWN_IP_PRIVATE, OWN_IP_PUBLIC, SERVER_PORT};
|
||||
use rnex_core::prudp::types_flags::TypesFlags;
|
||||
use rnex_core::prudp::types_flags::types::SYN;
|
||||
use rnex_core::prudp::virtual_port::VirtualPort;
|
||||
use rnex_core::reggie::EdgeNodeHolderConnectOption::Register;
|
||||
use rnex_core::reggie::RemoteEdgeNodeHolder;
|
||||
use rnex_core::rmc::protocols::{OnlyRemote, new_rmc_gateway_connection};
|
||||
use rnex_core::rmc::structures::RmcSerialize;
|
||||
use rnex_core::util::SplittableBufferConnection;
|
||||
use std::env;
|
||||
use std::net::SocketAddrV4;
|
||||
use std::process::abort;
|
||||
use std::sync::{Arc, LazyLock};
|
||||
use tokio::net::UdpSocket;
|
||||
|
||||
use crate::crypto::{Crypto, Insecure, Secure};
|
||||
use crate::packet::PRUDPV0Packet;
|
||||
use crate::server::Server;
|
||||
use crate::crypto::{Crypto, Insecure, Secure};
|
||||
use crate::packet::PRUDPV0Packet;
|
||||
use crate::server::Server;
|
||||
|
||||
mod crypto;
|
||||
mod packet;
|
||||
mod server;
|
||||
mod crypto;
|
||||
mod packet;
|
||||
mod server;
|
||||
|
||||
pub static EDGE_NODE_HOLDER: LazyLock<SocketAddrV4> = LazyLock::new(|| {
|
||||
env::var("EDGE_NODE_HOLDER")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.expect("EDGE_NODE_HOLDER not set")
|
||||
});
|
||||
pub static EDGE_NODE_HOLDER: LazyLock<SocketAddrV4> = LazyLock::new(|| {
|
||||
env::var("EDGE_NODE_HOLDER")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.expect("EDGE_NODE_HOLDER not set")
|
||||
});
|
||||
|
||||
pub static FORWARD_DESTINATION: LazyLock<SocketAddrV4> = LazyLock::new(|| {
|
||||
env::var("FORWARD_DESTINATION")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.expect("FORWARD_DESTINATION not set")
|
||||
});
|
||||
//same as with prudpv1 this is responsible for handeling the different cryptography
|
||||
//implementations, e.g. secure and insecure(this also includes special cases like friends)
|
||||
pub static FORWARD_DESTINATION: LazyLock<SocketAddrV4> = LazyLock::new(|| {
|
||||
env::var("FORWARD_DESTINATION")
|
||||
.ok()
|
||||
.and_then(|s| s.parse().ok())
|
||||
.expect("FORWARD_DESTINATION not set")
|
||||
});
|
||||
//same as with prudpv1 this is responsible for handeling the different cryptography
|
||||
//implementations, e.g. secure and insecure(this also includes special cases like friends)
|
||||
|
||||
async fn start_proxy<T: Crypto>(param: ProxyStartupParam) {
|
||||
setup_edge_node_connection(¶m, || abort());
|
||||
async fn start_proxy<T: Crypto>(param: ProxyStartupParam) {
|
||||
setup_edge_node_connection(¶m, || abort());
|
||||
|
||||
info!("creating cryptography instance");
|
||||
let mut crypto = Arc::new(T::new());
|
||||
info!("binding to socket");
|
||||
info!("creating cryptography instance");
|
||||
let mut crypto = Arc::new(T::new());
|
||||
info!("binding to socket");
|
||||
|
||||
let server: Arc<Server<T>> = Arc::new(Server::new(param).await);
|
||||
let server: Arc<Server<T>> = Arc::new(Server::new(param).await);
|
||||
|
||||
info!("waiting on packets");
|
||||
server.run_task().await;
|
||||
}
|
||||
|
||||
pub async fn start_secure(param: ProxyStartupParam) {
|
||||
start_proxy::<Secure>(param).await;
|
||||
}
|
||||
|
||||
pub async fn start_insecure(param: ProxyStartupParam) {
|
||||
start_proxy::<Insecure>(param).await;
|
||||
info!("waiting on packets");
|
||||
server.run_task().await;
|
||||
}
|
||||
|
||||
pub async fn start_secure(param: ProxyStartupParam) {
|
||||
start_proxy::<Secure>(param).await;
|
||||
}
|
||||
|
||||
pub async fn start_insecure(param: ProxyStartupParam) {
|
||||
start_proxy::<Insecure>(param).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ impl<T: AsRef<[u8]>> PRUDPV0Packet<T> {
|
|||
)
|
||||
}
|
||||
#[inline(always)]
|
||||
pub fn size_mut(&mut self) -> Option<&mut u16>
|
||||
pub fn size_mut(&mut self) -> Option<&mut [u8]>
|
||||
where
|
||||
T: AsMut<[u8]>,
|
||||
{
|
||||
|
|
@ -74,9 +74,7 @@ impl<T: AsRef<[u8]>> PRUDPV0Packet<T> {
|
|||
return None;
|
||||
}
|
||||
let offset = size_of::<PRUDPV0Header>() + get_type_specific_size(self.header()?.type_flags);
|
||||
Some(bytemuck::from_bytes_mut(
|
||||
self.0.as_mut().get_mut(offset..offset + 2)?,
|
||||
))
|
||||
Some(self.0.as_mut().get_mut(offset..offset + 2)?)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
|
@ -224,7 +222,10 @@ pub fn new_connect_packet(
|
|||
flags: u16,
|
||||
source: VirtualPort,
|
||||
destination: VirtualPort,
|
||||
signat: [u8; 4],
|
||||
self_signat: [u8; 4],
|
||||
remote_signat: [u8; 4],
|
||||
session_id: u8,
|
||||
data: &[u8],
|
||||
crypto: &impl Crypto,
|
||||
) -> Vec<u8> {
|
||||
let type_flags = TypesFlags::default().types(CONNECT).flags(flags);
|
||||
|
|
@ -236,15 +237,17 @@ pub fn new_connect_packet(
|
|||
*header = PRUDPV0Header {
|
||||
destination,
|
||||
source,
|
||||
packet_signature: DEFAULT_SIGNAT,
|
||||
sequence_id: 0,
|
||||
session_id: 0,
|
||||
packet_signature: self_signat,
|
||||
sequence_id: 1,
|
||||
session_id,
|
||||
type_flags,
|
||||
};
|
||||
*packet
|
||||
.connection_signature_mut()
|
||||
.expect("packet malformed in creation") = signat;
|
||||
|
||||
.expect("packet malformed in creation") = remote_signat;
|
||||
if let Some(size) = packet.size_mut() {
|
||||
size.copy_from_slice(&(data.len() as u16).to_le_bytes());
|
||||
}
|
||||
*packet.checksum_mut().expect("packet malformed in creation") = crypto.calculate_checksum(
|
||||
packet
|
||||
.checksummed_data()
|
||||
|
|
@ -279,7 +282,7 @@ pub fn new_data_packet(
|
|||
.copy_from_slice(data);
|
||||
|
||||
if let Some(size) = packet.size_mut() {
|
||||
*size = data.len() as u16;
|
||||
size.copy_from_slice(&(data.len() as u16).to_le_bytes());
|
||||
}
|
||||
*packet
|
||||
.fragment_id_mut()
|
||||
|
|
|
|||
|
|
@ -244,10 +244,13 @@ impl<C: Crypto> Server<C> {
|
|||
});
|
||||
|
||||
let packet = new_connect_packet(
|
||||
ACK,
|
||||
ACK | HAS_SIZE,
|
||||
header.destination,
|
||||
header.source,
|
||||
self_signat,
|
||||
remote_signat,
|
||||
packet.header().unwrap().session_id,
|
||||
&[],
|
||||
&self.crypto,
|
||||
);
|
||||
|
||||
|
|
@ -274,7 +277,7 @@ impl<C: Crypto> Server<C> {
|
|||
info!("frag: {}", frag_id);
|
||||
let mut conn = res.inner.lock().await;
|
||||
let ack = new_data_packet(
|
||||
ACK | HAS_SIZE,
|
||||
ACK,
|
||||
self.param.virtual_port,
|
||||
res.addr.virtual_port,
|
||||
&[],
|
||||
|
|
@ -324,6 +327,12 @@ impl<C: Crypto> Server<C> {
|
|||
};
|
||||
|
||||
let addr = PRUDPSockAddr::new(addr, header.source);
|
||||
|
||||
if header.type_flags.get_flags() & ACK != 0 {
|
||||
info!("got ack(acks are ignored for now)");
|
||||
return;
|
||||
}
|
||||
|
||||
println!("{:?}", header);
|
||||
match header.type_flags.get_types() {
|
||||
SYN => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue