2025-03-08 16:10:00 -05:00
|
|
|
use std::{
|
|
|
|
fs::write,
|
|
|
|
io::Cursor,
|
|
|
|
time::{SystemTime, UNIX_EPOCH},
|
|
|
|
};
|
2025-03-08 13:58:24 -05:00
|
|
|
|
2025-03-08 16:10:00 -05:00
|
|
|
use binrw::{BinRead, BinResult, BinWrite, binrw, helpers::until_eof};
|
2025-03-08 15:27:28 -05:00
|
|
|
use physis::blowfish::Blowfish;
|
2025-03-08 16:10:00 -05:00
|
|
|
use tokio::{
|
|
|
|
io::{AsyncWriteExt, WriteHalf},
|
|
|
|
net::TcpStream,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::encryption::{decrypt, generate_encryption_key};
|
2025-03-08 14:09:57 -05:00
|
|
|
|
|
|
|
pub(crate) fn read_bool_from<T: std::convert::From<u8> + std::cmp::PartialEq>(x: T) -> bool {
|
|
|
|
x == T::from(1u8)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn write_bool_as<T: std::convert::From<u8>>(x: &bool) -> T {
|
|
|
|
if *x { T::from(1u8) } else { T::from(0u8) }
|
|
|
|
}
|
|
|
|
|
2025-03-08 14:38:31 -05:00
|
|
|
pub(crate) fn read_string(byte_stream: Vec<u8>) -> String {
|
|
|
|
let str = String::from_utf8(byte_stream).unwrap();
|
|
|
|
str.trim_matches(char::from(0)).to_string() // trim \0 from the end of strings
|
|
|
|
}
|
|
|
|
|
2025-03-08 14:09:57 -05:00
|
|
|
#[binrw]
|
|
|
|
#[brw(repr = u16)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum ConnectionType {
|
2025-03-08 15:33:58 -05:00
|
|
|
None = 0x0,
|
|
|
|
Zone = 0x1,
|
|
|
|
Chat = 0x2,
|
2025-03-08 14:09:57 -05:00
|
|
|
Lobby = 0x3,
|
|
|
|
}
|
|
|
|
|
2025-03-08 14:38:31 -05:00
|
|
|
#[binrw]
|
2025-03-08 15:27:28 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2025-03-08 16:08:25 -05:00
|
|
|
struct IPCSegment {
|
2025-03-08 16:10:00 -05:00
|
|
|
unk: u32,
|
2025-03-08 16:08:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[binrw]
|
|
|
|
#[br(import(size: u32, encryption_key: Option<&[u8]>))]
|
|
|
|
#[derive(Debug, Clone)]
|
2025-03-08 14:38:31 -05:00
|
|
|
enum SegmentType {
|
2025-03-08 16:08:25 -05:00
|
|
|
// Client->Server Packets
|
2025-03-08 14:38:31 -05:00
|
|
|
#[brw(magic = 0x9u32)]
|
|
|
|
InitializeEncryption {
|
2025-03-08 15:27:28 -05:00
|
|
|
#[brw(pad_before = 36)] // empty
|
2025-03-08 14:38:31 -05:00
|
|
|
#[br(count = 64)]
|
|
|
|
#[br(map = read_string)]
|
|
|
|
#[bw(ignore)]
|
|
|
|
phrase: String,
|
|
|
|
|
2025-03-08 15:27:28 -05:00
|
|
|
#[brw(pad_after = 512)] // empty
|
|
|
|
key: [u8; 4],
|
2025-03-08 14:38:31 -05:00
|
|
|
},
|
2025-03-08 16:08:25 -05:00
|
|
|
#[brw(magic = 0x3u32)]
|
|
|
|
IPC {
|
|
|
|
#[br(parse_with = decrypt, args(size, encryption_key))]
|
|
|
|
#[bw(ignore)]
|
|
|
|
data: IPCSegment,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Server->Client Packets
|
2025-03-08 15:27:28 -05:00
|
|
|
#[brw(magic = 0x0Au32)]
|
|
|
|
InitializationEncryptionResponse {
|
|
|
|
#[br(count = 0x280)]
|
2025-03-08 16:10:00 -05:00
|
|
|
data: Vec<u8>,
|
2025-03-08 16:08:25 -05:00
|
|
|
},
|
2025-03-08 14:38:31 -05:00
|
|
|
}
|
|
|
|
|
2025-03-08 14:09:57 -05:00
|
|
|
#[binrw]
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct PacketHeader {
|
|
|
|
unk1: u64,
|
|
|
|
unk2: u64,
|
|
|
|
timestamp: u64,
|
|
|
|
size: u32,
|
|
|
|
connection_type: ConnectionType,
|
2025-03-08 14:38:31 -05:00
|
|
|
segment_count: u16,
|
2025-03-08 14:09:57 -05:00
|
|
|
unk3: u8,
|
|
|
|
#[br(map = read_bool_from::<u8>)]
|
|
|
|
#[bw(map = write_bool_as::<u8>)]
|
|
|
|
compressed: bool,
|
2025-03-08 14:38:31 -05:00
|
|
|
unk4: u16,
|
|
|
|
unk5: u32, // iolite says the size after oodle decompression
|
|
|
|
}
|
|
|
|
|
|
|
|
#[binrw]
|
2025-03-08 16:08:25 -05:00
|
|
|
#[br(import(encryption_key: Option<&[u8]>))]
|
2025-03-08 15:27:28 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2025-03-08 14:38:31 -05:00
|
|
|
struct PacketSegment {
|
2025-03-08 15:27:28 -05:00
|
|
|
#[bw(calc = self.calc_size())]
|
2025-03-08 14:38:31 -05:00
|
|
|
size: u32,
|
|
|
|
source_actor: u32,
|
|
|
|
target_actor: u32,
|
2025-03-08 16:08:25 -05:00
|
|
|
#[br(args(size, encryption_key))]
|
2025-03-08 14:38:31 -05:00
|
|
|
segment_type: SegmentType,
|
|
|
|
}
|
|
|
|
|
2025-03-08 15:27:28 -05:00
|
|
|
impl PacketSegment {
|
|
|
|
fn calc_size(&self) -> u32 {
|
|
|
|
let header = std::mem::size_of::<u32>() * 4;
|
2025-03-08 16:10:00 -05:00
|
|
|
return header as u32
|
|
|
|
+ match &self.segment_type {
|
|
|
|
SegmentType::InitializeEncryption { .. } => 616,
|
|
|
|
SegmentType::InitializationEncryptionResponse { .. } => 640,
|
|
|
|
SegmentType::IPC { .. } => todo!(),
|
|
|
|
};
|
2025-03-08 15:27:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-08 14:38:31 -05:00
|
|
|
#[binrw]
|
2025-03-08 16:08:25 -05:00
|
|
|
#[br(import(encryption_key: Option<&[u8]>))]
|
2025-03-08 14:38:31 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
struct Packet {
|
|
|
|
header: PacketHeader,
|
2025-03-08 16:08:25 -05:00
|
|
|
#[br(count = header.segment_count, args { inner: (encryption_key,) })]
|
2025-03-08 14:38:31 -05:00
|
|
|
segments: Vec<PacketSegment>,
|
2025-03-08 14:09:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn dump(msg: &str, data: &[u8]) {
|
2025-03-08 13:58:24 -05:00
|
|
|
write("packet.bin", data);
|
2025-03-08 14:09:57 -05:00
|
|
|
panic!("{msg} Dumped to packet.bin.");
|
|
|
|
}
|
|
|
|
|
2025-03-08 15:27:28 -05:00
|
|
|
async fn send_packet(socket: &mut WriteHalf<TcpStream>, segments: &[PacketSegment]) {
|
|
|
|
let timestamp: u64 = SystemTime::now()
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
.expect("Failed to get UNIX timestamp!")
|
|
|
|
.as_millis()
|
|
|
|
.try_into()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let mut total_segment_size = 0;
|
|
|
|
for segment in segments {
|
|
|
|
total_segment_size += segment.calc_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
let header = PacketHeader {
|
|
|
|
unk1: 0,
|
|
|
|
unk2: 0,
|
|
|
|
timestamp,
|
|
|
|
size: std::mem::size_of::<PacketHeader>() as u32 + total_segment_size,
|
|
|
|
connection_type: ConnectionType::Lobby,
|
|
|
|
segment_count: segments.len() as u16,
|
|
|
|
unk3: 0,
|
|
|
|
compressed: false,
|
|
|
|
unk4: 0,
|
|
|
|
unk5: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
let packet = Packet {
|
|
|
|
header,
|
|
|
|
segments: segments.to_vec(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut cursor = Cursor::new(Vec::new());
|
|
|
|
packet.write_le(&mut cursor);
|
|
|
|
|
|
|
|
let buffer = cursor.into_inner();
|
|
|
|
|
|
|
|
tracing::info!("Wrote response packet to outpacket.bin");
|
|
|
|
write("outpacket.bin", &buffer);
|
|
|
|
|
|
|
|
socket
|
|
|
|
.write(&buffer)
|
|
|
|
.await
|
|
|
|
.expect("Failed to write packet!");
|
|
|
|
}
|
|
|
|
|
2025-03-08 16:08:25 -05:00
|
|
|
// temporary
|
|
|
|
pub struct State {
|
2025-03-08 16:10:00 -05:00
|
|
|
pub client_key: Option<[u8; 16]>,
|
2025-03-08 16:08:25 -05:00
|
|
|
}
|
2025-03-08 14:09:57 -05:00
|
|
|
|
2025-03-08 16:08:25 -05:00
|
|
|
pub async fn parse_packet(socket: &mut WriteHalf<TcpStream>, data: &[u8], state: &mut State) {
|
|
|
|
let mut cursor = Cursor::new(data);
|
2025-03-08 15:29:38 -05:00
|
|
|
|
2025-03-08 16:10:00 -05:00
|
|
|
match Packet::read_le_args(
|
|
|
|
&mut cursor,
|
|
|
|
(state.client_key.as_ref().map(|s: &[u8; 16]| s.as_slice()),),
|
|
|
|
) {
|
2025-03-08 15:29:38 -05:00
|
|
|
Ok(packet) => {
|
|
|
|
println!("{:#?}", packet);
|
|
|
|
|
|
|
|
if packet.header.size as usize != data.len() {
|
|
|
|
dump(
|
|
|
|
"Packet size mismatch between what we're given and the header!",
|
2025-03-08 16:10:00 -05:00
|
|
|
data,
|
2025-03-08 15:29:38 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for segment in &packet.segments {
|
|
|
|
match &segment.segment_type {
|
|
|
|
SegmentType::InitializeEncryption { phrase, key } => {
|
|
|
|
// Generate an encryption key for this client
|
2025-03-08 16:08:25 -05:00
|
|
|
state.client_key = Some(generate_encryption_key(key, phrase));
|
2025-03-08 15:29:38 -05:00
|
|
|
|
2025-03-08 16:08:25 -05:00
|
|
|
let blowfish = Blowfish::new(&state.client_key.unwrap());
|
2025-03-08 15:29:38 -05:00
|
|
|
let mut data = blowfish.encrypt(&0xE0003C2Au32.to_le_bytes()).unwrap();
|
|
|
|
data.resize(0x280, 0);
|
|
|
|
|
|
|
|
let response_packet = PacketSegment {
|
|
|
|
source_actor: 0,
|
|
|
|
target_actor: 0,
|
2025-03-08 16:10:00 -05:00
|
|
|
segment_type: SegmentType::InitializationEncryptionResponse { data },
|
2025-03-08 15:29:38 -05:00
|
|
|
};
|
|
|
|
send_packet(socket, &[response_packet]).await;
|
2025-03-08 16:10:00 -05:00
|
|
|
}
|
|
|
|
SegmentType::InitializationEncryptionResponse { .. } => {
|
|
|
|
panic!("The server is recieving a response packet!")
|
|
|
|
}
|
2025-03-08 16:08:25 -05:00
|
|
|
SegmentType::IPC { .. } => {
|
|
|
|
// decrypt
|
2025-03-08 16:10:00 -05:00
|
|
|
}
|
2025-03-08 15:29:38 -05:00
|
|
|
}
|
2025-03-08 15:27:28 -05:00
|
|
|
}
|
2025-03-08 16:10:00 -05:00
|
|
|
}
|
2025-03-08 15:29:38 -05:00
|
|
|
Err(err) => {
|
|
|
|
println!("{err}");
|
|
|
|
dump("Failed to parse packet!", data);
|
2025-03-08 16:10:00 -05:00
|
|
|
}
|
2025-03-08 15:27:28 -05:00
|
|
|
}
|
|
|
|
}
|