Jake Probst
6 years ago
4 changed files with 87 additions and 85 deletions
@ -1,86 +1,4 @@ |
|||
pub mod cipherkeys;
|
|||
pub mod network;
|
|||
|
|||
use std::net::TcpStream;
|
|||
use std::io::{Read, Write};
|
|||
use libpso::crypto::{PSOCipher, CipherError};
|
|||
use libpso::{PSOPacket, PacketParseError};
|
|||
|
|||
#[derive(Debug)]
|
|||
pub enum PacketNetworkError {
|
|||
CouldNotSend,
|
|||
CipherError(CipherError),
|
|||
PacketParseError(PacketParseError),
|
|||
IOError(std::io::Error),
|
|||
DataNotReady,
|
|||
ClientDisconnected,
|
|||
}
|
|||
|
|||
impl From<CipherError> for PacketNetworkError {
|
|||
fn from(err: CipherError) -> PacketNetworkError {
|
|||
PacketNetworkError::CipherError(err)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<std::io::Error> for PacketNetworkError {
|
|||
fn from(err: std::io::Error) -> PacketNetworkError {
|
|||
PacketNetworkError::IOError(err)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<PacketParseError> for PacketNetworkError {
|
|||
fn from(err: PacketParseError) -> PacketNetworkError {
|
|||
PacketNetworkError::PacketParseError(err)
|
|||
}
|
|||
}
|
|||
|
|||
pub fn recv_packet<T: Read>(socket: &mut T, cipher: &mut dyn PSOCipher) -> Result<Vec<u8>, PacketNetworkError> {
|
|||
let mut size_buf = vec![0u8; cipher.header_size()];
|
|||
let mut offset = 0;
|
|||
while offset < cipher.header_size() {
|
|||
let diff = socket.read(&mut size_buf[offset..])?;
|
|||
println!("! {} {:?}", diff, size_buf);
|
|||
if diff == 0 {
|
|||
return Err(PacketNetworkError::ClientDisconnected);
|
|||
}
|
|||
offset += diff;
|
|||
}
|
|||
|
|||
let mut dec_size_buf = cipher.decrypt(&size_buf)?;
|
|||
let size = u16::from_le_bytes([dec_size_buf[0], dec_size_buf[1]]) as usize;
|
|||
|
|||
let mut data_buf = vec![0u8; size - cipher.header_size()];
|
|||
let mut offset = 0;
|
|||
while offset < size - cipher.header_size() {
|
|||
let diff = socket.read(&mut data_buf[offset..])?;
|
|||
if diff == 0 {
|
|||
return Err(PacketNetworkError::ClientDisconnected);
|
|||
}
|
|||
offset += diff;
|
|||
}
|
|||
|
|||
let mut null_data_count = 0;
|
|||
while data_buf.len() % cipher.header_size() != 0 {
|
|||
data_buf.push(0);
|
|||
null_data_count += 1;
|
|||
}
|
|||
|
|||
let mut dec_data_buf = cipher.decrypt(&data_buf.to_vec())?;
|
|||
|
|||
for _ in 0..null_data_count {
|
|||
dec_data_buf.pop();
|
|||
}
|
|||
|
|||
let mut full_buf = Vec::new();
|
|||
full_buf.append(&mut dec_size_buf);
|
|||
full_buf.append(&mut dec_data_buf);
|
|||
|
|||
//println!("[recv]: {:X?}", full_buf);
|
|||
Ok(full_buf)
|
|||
}
|
|||
|
|||
pub fn send_packet<T: Write>(socket: &mut T, cipher: &mut dyn PSOCipher, pkt: &dyn PSOPacket) -> Result<(), PacketNetworkError> {
|
|||
let buf = pkt.as_bytes();
|
|||
//println!("[send]: {:X?}", buf);
|
|||
let cbuf = cipher.encrypt(&buf)?;
|
|||
Ok(socket.write_all(&cbuf)?)
|
|||
}
|
@ -0,0 +1,84 @@ |
|||
use std::net::TcpStream;
|
|||
use std::io::{Read, Write};
|
|||
use libpso::crypto::{PSOCipher, CipherError};
|
|||
use libpso::{PSOPacket, PacketParseError};
|
|||
|
|||
#[derive(Debug)]
|
|||
pub enum PacketNetworkError {
|
|||
CouldNotSend,
|
|||
CipherError(CipherError),
|
|||
PacketParseError(PacketParseError),
|
|||
IOError(std::io::Error),
|
|||
DataNotReady,
|
|||
ClientDisconnected,
|
|||
}
|
|||
|
|||
impl From<CipherError> for PacketNetworkError {
|
|||
fn from(err: CipherError) -> PacketNetworkError {
|
|||
PacketNetworkError::CipherError(err)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<std::io::Error> for PacketNetworkError {
|
|||
fn from(err: std::io::Error) -> PacketNetworkError {
|
|||
PacketNetworkError::IOError(err)
|
|||
}
|
|||
}
|
|||
|
|||
impl From<PacketParseError> for PacketNetworkError {
|
|||
fn from(err: PacketParseError) -> PacketNetworkError {
|
|||
PacketNetworkError::PacketParseError(err)
|
|||
}
|
|||
}
|
|||
|
|||
pub fn recv_packet<T: Read>(socket: &mut T, cipher: &mut dyn PSOCipher) -> Result<Vec<u8>, PacketNetworkError> {
|
|||
let mut size_buf = vec![0u8; cipher.header_size()];
|
|||
let mut offset = 0;
|
|||
while offset < cipher.header_size() {
|
|||
let diff = socket.read(&mut size_buf[offset..])?;
|
|||
println!("! {} {:?}", diff, size_buf);
|
|||
if diff == 0 {
|
|||
return Err(PacketNetworkError::ClientDisconnected);
|
|||
}
|
|||
offset += diff;
|
|||
}
|
|||
|
|||
let mut dec_size_buf = cipher.decrypt(&size_buf)?;
|
|||
let size = u16::from_le_bytes([dec_size_buf[0], dec_size_buf[1]]) as usize;
|
|||
|
|||
let mut data_buf = vec![0u8; size - cipher.header_size()];
|
|||
let mut offset = 0;
|
|||
while offset < size - cipher.header_size() {
|
|||
let diff = socket.read(&mut data_buf[offset..])?;
|
|||
if diff == 0 {
|
|||
return Err(PacketNetworkError::ClientDisconnected);
|
|||
}
|
|||
offset += diff;
|
|||
}
|
|||
|
|||
let mut null_data_count = 0;
|
|||
while data_buf.len() % cipher.header_size() != 0 {
|
|||
data_buf.push(0);
|
|||
null_data_count += 1;
|
|||
}
|
|||
|
|||
let mut dec_data_buf = cipher.decrypt(&data_buf.to_vec())?;
|
|||
|
|||
for _ in 0..null_data_count {
|
|||
dec_data_buf.pop();
|
|||
}
|
|||
|
|||
let mut full_buf = Vec::new();
|
|||
full_buf.append(&mut dec_size_buf);
|
|||
full_buf.append(&mut dec_data_buf);
|
|||
|
|||
//println!("[recv]: {:X?}", full_buf);
|
|||
Ok(full_buf)
|
|||
}
|
|||
|
|||
pub fn send_packet<T: Write>(socket: &mut T, cipher: &mut dyn PSOCipher, pkt: &dyn PSOPacket) -> Result<(), PacketNetworkError> {
|
|||
let buf = pkt.as_bytes();
|
|||
//println!("[send]: {:X?}", buf);
|
|||
let cbuf = cipher.encrypt(&buf)?;
|
|||
Ok(socket.write_all(&cbuf)?)
|
|||
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue