diff --git a/Cargo.lock b/Cargo.lock index 96156e2..18bac70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "damage-meter" version = "0.1.0" dependencies = [ + "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "packet_sniffer 0.1.0", "protocol16 0.1.0", ] diff --git a/Cargo.toml b/Cargo.toml index 38057d2..d7d1f92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,5 @@ edition = "2018" [dependencies] packet_sniffer = { path = "./lib/packet-sniffer" } -protocol16 = { path = "./lib/protocol16" } \ No newline at end of file +protocol16 = { path = "./lib/protocol16" } +bytes = "0.4" \ No newline at end of file diff --git a/lib/protocol16/Cargo.toml b/lib/protocol16/Cargo.toml index 4280506..3480f3a 100644 --- a/lib/protocol16/Cargo.toml +++ b/lib/protocol16/Cargo.toml @@ -7,4 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bytes = "0.4.12" \ No newline at end of file +bytes = "0.4" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9223aab..48c2044 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,29 @@ +extern crate bytes; + extern crate packet_sniffer; +extern crate protocol16; + use std::sync::mpsc::{Sender, Receiver}; use std::sync::mpsc; use packet_sniffer::UdpPacket; +mod protocol; + fn main() { let (tx, rx): (Sender, Receiver) = mpsc::channel(); packet_sniffer::receive(tx); loop { - println!("{:?}", rx.recv()); + if let Ok(packet) = rx.recv() { + if packet.destination_port != 5056 && packet.source_port != 5056 { + continue; + } + protocol::decode(&packet.payload); + } + + } -} +} \ No newline at end of file diff --git a/src/protocol.rs b/src/protocol.rs new file mode 100644 index 0000000..218e3de --- /dev/null +++ b/src/protocol.rs @@ -0,0 +1,63 @@ +use std::io::Cursor; +use bytes::Buf; + +static CMD_HEADER_LENGTH: u32 = 12; +static SIGNIFIER_BYTE_LENGTH: usize = 1; + +static LOG_OUT: u8 = 4; +static SEND_UNRELIABLE: u8 = 7; +static SEND_RELIABLE: u8 = 6; + +static MSG_TYPE_REQUEST: u8 = 2; +static MSG_TYPE_RESPONSE: u8 = 3; +static MSG_TYPE_EVENT: u8 = 4; + + +pub fn decode(payload: &[u8]) +{ + let mut cursor = Cursor::new(payload); + cursor.advance(3); + + let cmd_cnt = protocol16::deserialize_byte(&mut cursor).unwrap(); + cursor.advance(8); + + for _ in 0..cmd_cnt { + let cmd_type = protocol16::deserialize_byte(&mut cursor).unwrap(); + cursor.advance(3); + let cmd_length = protocol16::deserialize_integer(&mut cursor).unwrap(); + cursor.advance(4); + + if cmd_type == LOG_OUT { + break; + } else if cmd_type == SEND_UNRELIABLE { + cursor.advance(4); + on_message(&mut cursor, cmd_length - 4); + } else if cmd_type == SEND_RELIABLE { + on_message(&mut cursor, cmd_length); + } else { + cursor.advance((cmd_length - CMD_HEADER_LENGTH) as usize); + } + } +} + +fn on_message(cursor: &mut Cursor<&[u8]>, msg_len: u32) { + let init = cursor.bytes().len(); + cursor.advance(SIGNIFIER_BYTE_LENGTH); + + let msg_type = protocol16::deserialize_byte(cursor).unwrap(); + let operation_length = msg_len - CMD_HEADER_LENGTH - 2; + + let mut payload = Cursor::new(&cursor.bytes()[0..operation_length as usize]); + + if msg_type == MSG_TYPE_EVENT { + if let Some(event_data) = protocol16::deserialize_event_data(&mut payload) { + if event_data.code != 2 { + println!("{:?}", event_data); + } + } + } + + cursor.advance(operation_length as usize); + let last = cursor.bytes().len(); + assert!(init - last == msg_len as usize - CMD_HEADER_LENGTH as usize); +} \ No newline at end of file