move network functions to their own file
This commit is contained in:
		
							parent
							
								
									b6a8e2f97e
								
							
						
					
					
						commit
						4cb5b665f3
					
				@ -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)?)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										84
									
								
								src/common/network.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								src/common/network.rs
									
									
									
									
									
										Normal file
									
								
							@ -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)?)
 | 
			
		||||
}
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user