initial commit

This commit is contained in:
c0repwn3r 2023-01-25 18:24:51 -05:00
commit 7d9739244d
Signed by: core
GPG Key ID: FDBF740DADDCEECF
9 changed files with 210 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,8 @@
use blake2::Blake2s256;
use blake2::Digest;
pub fn HASH(data: &(impl AsRef<[u8]> + ?Sized)) -> [u8; 32] {
let mut hasher = Blake2s256::new();
hasher.update(data);
hasher.finalize().try_into().unwrap()
}

View File

@ -0,0 +1,53 @@
use crate::packets::{PacketParseError, SptprpPacket};
#[derive(Debug, PartialEq)]
pub struct ChannelNotReadyPacket {
pub channel: [u8; 32]
}
impl SptprpPacket for ChannelNotReadyPacket {
const PACKET_ID: u8 = 0x02;
fn to_bytes(&self) -> Vec<u8> {
let mut bytes = vec![0x02];
bytes.extend_from_slice(&self.channel);
bytes
}
fn from_bytes(bytes: &Vec<u8>) -> Result<Self, PacketParseError> {
if bytes.len() != 33 {
return Err(PacketParseError::IncorrectPacketLength)
}
if bytes[0] != 0x02u8 {
return Err(PacketParseError::IncorrectPacketType)
}
Ok(Self {
channel: bytes[1..].try_into().unwrap()
})
}
}
#[derive(Debug, PartialEq)]
pub struct ChannelReadyPacket {
pub channel: [u8; 32]
}
impl SptprpPacket for ChannelReadyPacket {
const PACKET_ID: u8 = 0x03;
fn to_bytes(&self) -> Vec<u8> {
let mut bytes = vec![0x03];
bytes.extend_from_slice(&self.channel);
bytes
}
fn from_bytes(bytes: &Vec<u8>) -> Result<Self, PacketParseError> {
if bytes.len() != 33 {
return Err(PacketParseError::IncorrectPacketLength)
}
if bytes[0] != 0x03u8 {
return Err(PacketParseError::IncorrectPacketType)
}
Ok(Self {
channel: bytes[1..].try_into().unwrap()
})
}
}

View File

View File

@ -0,0 +1,17 @@
pub mod clientbound;
pub mod initbound;
pub mod recvbound;
pub mod relaybound;
#[derive(Debug)]
pub enum PacketParseError {
IncorrectPacketType,
IncorrectPacketLength
}
pub trait SptprpPacket {
const PACKET_ID: u8;
fn to_bytes(&self) -> Vec<u8>;
fn from_bytes(bytes: &Vec<u8>) -> Result<Self, PacketParseError> where Self: Sized;
}

View File

View File

@ -0,0 +1,71 @@
use crate::packets::{PacketParseError, SptprpPacket};
#[derive(Debug, PartialEq)]
pub struct ChannelWaitPacket {
pub channel: [u8; 32]
}
impl SptprpPacket for ChannelWaitPacket {
const PACKET_ID: u8 = 0x01;
fn to_bytes(&self) -> Vec<u8> {
let mut bytes = vec![0x01];
bytes.extend_from_slice(&self.channel);
bytes
}
fn from_bytes(bytes: &Vec<u8>) -> Result<Self, PacketParseError> {
if bytes.len() != 33 {
return Err(PacketParseError::IncorrectPacketLength)
}
if bytes[0] != 0x01u8 {
return Err(PacketParseError::IncorrectPacketType)
}
Ok(Self {
channel: bytes[1..].try_into().unwrap()
})
}
}
#[derive(Debug, PartialEq)]
pub struct RelayDataPacket {
pub channel: [u8; 32],
pub data_len: u64,
pub data: Vec<u8>
}
impl SptprpPacket for RelayDataPacket {
const PACKET_ID: u8 = 0x04;
fn to_bytes(&self) -> Vec<u8> {
let mut bytes = vec![4u8];
bytes.extend_from_slice(&self.channel);
bytes.extend_from_slice(&self.data_len.to_le_bytes());
bytes.extend_from_slice(&self.data[..]);
bytes.push(0xfe);
bytes
}
fn from_bytes(bytes: &Vec<u8>) -> Result<Self, PacketParseError> where Self: Sized {
if bytes.len() < 42 {
return Err(PacketParseError::IncorrectPacketLength)
}
if bytes[0] != 0x04 && bytes[bytes.len()-1] != 0xfe {
return Err(PacketParseError::IncorrectPacketType)
}
let channel = &bytes[1..33];
let data_len = u64::from_le_bytes(bytes[33..41].try_into().unwrap());
let data = &bytes[41..41 + data_len as usize];
if bytes[40 + data_len as usize + 1] != 0xfe {
return Err(PacketParseError::IncorrectPacketType)
}
Ok(Self {
channel: channel.try_into().unwrap(),
data_len,
data: data.to_vec(),
})
}
}

52
libsptprp/src/tests.rs Normal file
View File

@ -0,0 +1,52 @@
use crate::cryptography::HASH;
use crate::packets::clientbound::{ChannelNotReadyPacket, ChannelReadyPacket};
use crate::packets::relaybound::{ChannelWaitPacket, RelayDataPacket};
use crate::packets::SptprpPacket;
#[test]
pub fn pkt_channel_wait() {
assert_eq!(ChannelWaitPacket {
channel: [0u8; 32],
}.to_bytes(), [1u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8]);
assert_eq!(ChannelWaitPacket {
channel: [0u8; 32],
}, ChannelWaitPacket::from_bytes(&vec![1u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8]).unwrap())
}
#[test]
pub fn pkt_channel_not_ready() {
assert_eq!(ChannelNotReadyPacket {
channel: [0u8; 32],
}.to_bytes(), [2u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8]);
assert_eq!(ChannelNotReadyPacket {
channel: [0u8; 32],
}, ChannelNotReadyPacket::from_bytes(&vec![2u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8]).unwrap())
}
#[test]
pub fn pkt_channel_ready() {
assert_eq!(ChannelReadyPacket {
channel: [0u8; 32],
}.to_bytes(), [3u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8]);
assert_eq!(ChannelReadyPacket {
channel: [0u8; 32],
}, ChannelReadyPacket::from_bytes(&vec![3u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8]).unwrap())
}
#[test]
pub fn hash() {
assert_eq!(HASH("hello"), [25u8, 33, 59, 172, 197, 141, 238, 109, 189, 227, 206, 185, 164, 124, 187, 51, 11, 61, 134, 248, 204, 168, 153, 126, 176, 11, 228, 86, 241, 64, 202, 37])
}
#[test]
pub fn pkt_relay() {
assert_eq!(RelayDataPacket {
channel: [0u8; 32],
data_len: 1,
data: vec![42u8],
}.to_bytes(), [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 42, 254]);
assert_eq!(RelayDataPacket {
channel: [0u8; 32],
data_len: 1,
data: vec![42u8]
}, RelayDataPacket::from_bytes(&vec![4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 42, 254]).unwrap())
}