diff --git a/src/common/mod.rs b/src/common/mod.rs index 7d9b9a6..a6bb89e 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -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 for PacketNetworkError { - fn from(err: CipherError) -> PacketNetworkError { - PacketNetworkError::CipherError(err) - } -} - -impl From for PacketNetworkError { - fn from(err: std::io::Error) -> PacketNetworkError { - PacketNetworkError::IOError(err) - } -} - -impl From for PacketNetworkError { - fn from(err: PacketParseError) -> PacketNetworkError { - PacketNetworkError::PacketParseError(err) - } -} - -pub fn recv_packet(socket: &mut T, cipher: &mut dyn PSOCipher) -> Result, 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(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)?) -} diff --git a/src/common/network.rs b/src/common/network.rs new file mode 100644 index 0000000..c1976f0 --- /dev/null +++ b/src/common/network.rs @@ -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 for PacketNetworkError { + fn from(err: CipherError) -> PacketNetworkError { + PacketNetworkError::CipherError(err) + } +} + +impl From for PacketNetworkError { + fn from(err: std::io::Error) -> PacketNetworkError { + PacketNetworkError::IOError(err) + } +} + +impl From for PacketNetworkError { + fn from(err: PacketParseError) -> PacketNetworkError { + PacketNetworkError::PacketParseError(err) + } +} + +pub fn recv_packet(socket: &mut T, cipher: &mut dyn PSOCipher) -> Result, 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(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)?) +} diff --git a/src/login/main.rs b/src/login/main.rs index 9aab57e..bc12c61 100644 --- a/src/login/main.rs +++ b/src/login/main.rs @@ -10,7 +10,7 @@ use libpso::packet::login::*; use libpso::crypto::{CipherError, PSOCipher, NullCipher}; use libpso::crypto::bb::PSOBBCipher; -use elseware::common::{send_packet, recv_packet, PacketNetworkError}; +use elseware::common::network::{send_packet, recv_packet, PacketNetworkError}; use elseware::common::cipherkeys::{ELSEWHERE_PRIVATE_KEY, ELSEWHERE_PARRAY}; const LOGIN_PORT: u16 = 12000; diff --git a/src/patch/main.rs b/src/patch/main.rs index 9eabac1..8760a98 100644 --- a/src/patch/main.rs +++ b/src/patch/main.rs @@ -14,7 +14,7 @@ use libpso::{PacketParseError, PSOPacket}; use libpso::packet::patch::*; use libpso::crypto::{CipherError, PSOCipher, NullCipher}; use libpso::crypto::pc::PSOPCCipher; -use elseware::common::{send_packet, recv_packet, PacketNetworkError}; +use elseware::common::network::{send_packet, recv_packet, PacketNetworkError}; const PATCH_PORT: u16 = 11000;