|
|
|
@ -9,8 +9,9 @@ use std::io::Cursor;
|
|
|
|
|
use serde::{Serialize, Deserialize};
|
|
|
|
|
|
|
|
|
|
use std::io::prelude::*;
|
|
|
|
|
use std::io::BufReader;
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
|
|
|
|
|
use sha2::Digest;
|
|
|
|
|
use sha2::Sha256;
|
|
|
|
|
|
|
|
|
|
use std::net::TcpStream;
|
|
|
|
|
use std::sync::Mutex;
|
|
|
|
@ -217,6 +218,17 @@ struct BackendPacket {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BackendPacket {
|
|
|
|
|
fn new(header: u32, timestamp: f64, msg_type: u32, seq_num: u64, data: Vec<u8>) -> BackendPacket {
|
|
|
|
|
return BackendPacket{
|
|
|
|
|
header,
|
|
|
|
|
timestamp,
|
|
|
|
|
msg_type,
|
|
|
|
|
seq_num,
|
|
|
|
|
size: data.len().try_into().unwrap(),
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_bytes(bytes: Vec<u8>) -> Option<BackendPacket> {
|
|
|
|
|
if bytes.len() < BACKEND_PACKET_HEADER_SIZE {
|
|
|
|
|
return None;
|
|
|
|
@ -232,7 +244,7 @@ impl BackendPacket {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_bytes(self: BackendPacket) -> Vec<u8> {
|
|
|
|
|
fn as_bytes(self: &BackendPacket) -> Vec<u8> {
|
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
|
|
|
|
|
|
bytes.extend(self.header.to_le_bytes());
|
|
|
|
@ -240,7 +252,7 @@ impl BackendPacket {
|
|
|
|
|
bytes.extend(self.msg_type.to_le_bytes());
|
|
|
|
|
bytes.extend(self.seq_num.to_le_bytes());
|
|
|
|
|
bytes.extend(self.size.to_le_bytes());
|
|
|
|
|
bytes.extend(self.data);
|
|
|
|
|
bytes.extend(self.data.clone());
|
|
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
|
}
|
|
|
|
@ -261,6 +273,25 @@ struct ConnectMsg {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ConnectMsg {
|
|
|
|
|
fn from_welcome(welcome: ConnectMsg, password: &str, uuid: [u8; 16], client_name: [u8; 32]) -> ConnectMsg {
|
|
|
|
|
let mut hasher = Sha256::new();
|
|
|
|
|
hasher.update(welcome.pass_hash);
|
|
|
|
|
hasher.update(password);
|
|
|
|
|
let result = hasher.finalize();
|
|
|
|
|
|
|
|
|
|
return ConnectMsg{
|
|
|
|
|
version: welcome.version,
|
|
|
|
|
uuid,
|
|
|
|
|
last_notice_id: welcome.last_notice_id,
|
|
|
|
|
username: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
|
pass_hash: result.try_into().unwrap(),
|
|
|
|
|
pw_valid: welcome.pw_valid,
|
|
|
|
|
state_valid: welcome.state_valid,
|
|
|
|
|
client_name,
|
|
|
|
|
server_time_zone: welcome.server_time_zone,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_bytes(bytes: Vec<u8>) -> Option<ConnectMsg> {
|
|
|
|
|
if bytes.len() < CONNECT_MSG_LEN {
|
|
|
|
|
return None;
|
|
|
|
@ -279,7 +310,7 @@ impl ConnectMsg {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn to_bytes(self: ConnectMsg) -> Vec<u8> {
|
|
|
|
|
fn as_bytes(self: &ConnectMsg) -> Vec<u8> {
|
|
|
|
|
let mut bytes = Vec::new();
|
|
|
|
|
|
|
|
|
|
bytes.extend(self.version.to_le_bytes());
|
|
|
|
@ -339,18 +370,33 @@ impl TMClient {
|
|
|
|
|
let data = incoming[0..read].to_vec();
|
|
|
|
|
match BackendPacket::from_bytes(data) {
|
|
|
|
|
Some(packet) => {
|
|
|
|
|
println!("Packet: {:?}", packet);
|
|
|
|
|
match packet.msg_type {
|
|
|
|
|
2 => {
|
|
|
|
|
match ConnectMsg::from_bytes(packet.data) {
|
|
|
|
|
Some(welcome_msg) => println!("Welcome msg: {:?}", welcome_msg),
|
|
|
|
|
None => println!("Failed to parse wlecome msg"),
|
|
|
|
|
Some(welcome_msg) => {
|
|
|
|
|
println!("Welcome msg: {:?}", welcome_msg);
|
|
|
|
|
if welcome_msg.pw_valid == 0 {
|
|
|
|
|
let uuid = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
|
|
|
|
|
let client_name = [116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
|
|
|
|
let connect_response = ConnectMsg::from_welcome(welcome_msg, "", uuid, client_name);
|
|
|
|
|
let response = BackendPacket::new(packet.header, packet.timestamp, packet.msg_type, packet.seq_num, connect_response.as_bytes());
|
|
|
|
|
match stream.write(&response.as_bytes()) {
|
|
|
|
|
Err(error) => println!("Send error: {:?}", error),
|
|
|
|
|
Ok(sent) => println!("Sent {} bytes", sent),
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
println!("Connected to TM backend!");
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => println!("Failed to parse welcome msg"),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
_ => println!("Unhandled message type: {}", packet.msg_type),
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
None => println!("Failed to parse BackendPacket"),
|
|
|
|
|
None => {
|
|
|
|
|
panic!("Failed to parse BackendPacket({}): {}", read, String::from_utf8_lossy(&incoming));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Err(error) => println!("Error: {}", error),
|
|
|
|
|