chore: fix clippy warnings
This commit is contained in:
+1
-1
@@ -136,7 +136,7 @@ impl Client {
|
||||
let unlocked_context = context.lock().unwrap();
|
||||
|
||||
if let Some(sender) = &unlocked_context.sender {
|
||||
if unlocked_context.peers.get(&peer.username).is_none() {
|
||||
if !unlocked_context.peers.contains_key(&peer.username) {
|
||||
let peer_clone = peer.clone();
|
||||
let sender_clone = sender.clone();
|
||||
thread::spawn(move || {
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
use crate::message::{handlers::Handlers, message::Message};
|
||||
use crate::message::{handlers::Handlers, Message};
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
/// Add `<Op>` to make it generic over any operation type.
|
||||
|
||||
@@ -25,7 +25,7 @@ impl<Op> Handlers<Op> {
|
||||
self.handlers.insert(handler.get_code(), Box::new(handler));
|
||||
self
|
||||
}
|
||||
pub fn get_handler(&self, code: u8) -> Option<&Box<dyn MessageHandler<Op>>> {
|
||||
self.handlers.get(&code)
|
||||
pub fn get_handler(&self, code: u8) -> Option<&dyn MessageHandler<Op>> {
|
||||
self.handlers.get(&code).map(|v| &**v)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,317 +0,0 @@
|
||||
use std::str;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Message {
|
||||
data: Vec<u8>,
|
||||
pointer: usize,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
data: Vec::new(),
|
||||
pointer: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_hex(&self) {
|
||||
let data = &self.data;
|
||||
const BYTES_PER_LINE: usize = 16;
|
||||
|
||||
let chunks = data.chunks(BYTES_PER_LINE);
|
||||
for (i, chunk) in chunks.enumerate() {
|
||||
// Print the offset
|
||||
print!("{:04x} ", i * BYTES_PER_LINE);
|
||||
|
||||
// Print the hexadecimal part
|
||||
for j in 0..BYTES_PER_LINE {
|
||||
if j < chunk.len() {
|
||||
print!("{:02x} ", chunk[j]);
|
||||
} else {
|
||||
print!(" ");
|
||||
}
|
||||
|
||||
// Add extra space in the middle
|
||||
if j == 7 {
|
||||
print!(" ");
|
||||
}
|
||||
}
|
||||
|
||||
print!(" ");
|
||||
|
||||
// Print the ASCII part
|
||||
let mut i = 0;
|
||||
for &byte in chunk {
|
||||
i += 1;
|
||||
if byte.is_ascii_graphic() || byte.is_ascii_whitespace() {
|
||||
print!("{}", byte as char);
|
||||
} else {
|
||||
print!(".");
|
||||
}
|
||||
|
||||
if i == 8 {
|
||||
print!(" ");
|
||||
}
|
||||
}
|
||||
|
||||
println!();
|
||||
}
|
||||
}
|
||||
//
|
||||
// pub fn print_hex2(&self) -> String {
|
||||
// let data = &self.data;
|
||||
// let mut out = String::from("");
|
||||
// const BYTES_PER_LINE: usize = 16;
|
||||
//
|
||||
// let chunks = data.chunks(BYTES_PER_LINE);
|
||||
// for (i, chunk) in chunks.enumerate() {
|
||||
// // Print the offset
|
||||
// // out += &format!("{:04x} ", i * BYTES_PER_LINE);
|
||||
//
|
||||
// // Print the hexadecimal part
|
||||
// for j in 0..BYTES_PER_LINE {
|
||||
// if j < chunk.len() {
|
||||
// out += &format!("{:02x} ", chunk[j]);
|
||||
// } else {
|
||||
// // out += &format!(" ");
|
||||
// }
|
||||
//
|
||||
// // Add extra space in the middle
|
||||
// // if j == 7 {
|
||||
// // out += &format!(" ");
|
||||
// // }
|
||||
// }
|
||||
//
|
||||
// // // Print the ASCII part
|
||||
// // let mut i = 0;
|
||||
// // for &byte in chunk {
|
||||
// // i = i + 1;
|
||||
// // if byte.is_ascii_graphic() || byte.is_ascii_whitespace() {
|
||||
// // print!("{}", byte as char);
|
||||
// // } else {messa
|
||||
// // print!(".");
|
||||
// // }
|
||||
// //
|
||||
// // if i == 8 {
|
||||
// // print!(" ");
|
||||
// // }
|
||||
// // }
|
||||
// //
|
||||
// }
|
||||
// println!("{:?}", out.trim());
|
||||
// return out.trim().into();
|
||||
// }
|
||||
|
||||
pub fn get_message_code_u32(&self) -> u32 {
|
||||
u32::from_le_bytes(self.data[4..8].try_into().unwrap())
|
||||
}
|
||||
|
||||
pub fn get_message_code(&self) -> u8 {
|
||||
self.data[4]
|
||||
}
|
||||
|
||||
pub fn new_with_data(data: Vec<u8>) -> Self {
|
||||
Self { data, pointer: 0 }
|
||||
}
|
||||
// pub fn reset_pointer(&mut self) {
|
||||
// self.pointer = 0;
|
||||
// }
|
||||
pub fn set_pointer(&mut self, pointer: usize) {
|
||||
self.pointer = pointer;
|
||||
}
|
||||
|
||||
pub fn get_pointer(&mut self) -> usize {
|
||||
self.pointer
|
||||
}
|
||||
|
||||
pub fn get_size(&mut self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
|
||||
pub fn get_data(&self) -> Vec<u8> {
|
||||
self.data.clone()
|
||||
}
|
||||
|
||||
pub fn get_slice(&self, from: usize, to: usize) -> Vec<u8> {
|
||||
self.data[from..to].to_vec()
|
||||
}
|
||||
|
||||
/// gets buffer with the message length prepended
|
||||
pub fn get_buffer(&self) -> Vec<u8> {
|
||||
let mut b = vec![0u8; 4];
|
||||
let length = self.data.len() as u32;
|
||||
b[0..4].copy_from_slice(&length.to_le_bytes());
|
||||
let mut combined = b;
|
||||
combined.extend(&self.data);
|
||||
combined
|
||||
}
|
||||
|
||||
pub fn read_string(&mut self) -> String {
|
||||
let size = u32::from_le_bytes([
|
||||
self.data[self.pointer],
|
||||
self.data[self.pointer + 1],
|
||||
self.data[self.pointer + 2],
|
||||
self.data[self.pointer + 3],
|
||||
]) as usize;
|
||||
|
||||
self.pointer += 4;
|
||||
let data = &self.data[self.pointer..self.pointer + size];
|
||||
self.pointer += size;
|
||||
|
||||
String::from_utf8(data.to_vec()).expect("Failed to read string")
|
||||
}
|
||||
pub fn read_raw_hex_str(&mut self, size: usize) -> String {
|
||||
let hex_str = self.data[self.pointer..self.pointer + size]
|
||||
.iter()
|
||||
.map(|byte| format!("{:02x}", byte))
|
||||
.collect::<String>();
|
||||
self.pointer += size;
|
||||
hex_str
|
||||
}
|
||||
|
||||
pub fn read_int8(&mut self) -> i8 {
|
||||
let val = self.data[self.pointer] as i8;
|
||||
self.pointer += 1;
|
||||
val
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn read_int64(&mut self) -> i64 {
|
||||
let val = i64::from_le_bytes([
|
||||
self.data[self.pointer],
|
||||
self.data[self.pointer + 1],
|
||||
self.data[self.pointer + 2],
|
||||
self.data[self.pointer + 3],
|
||||
self.data[self.pointer + 4],
|
||||
self.data[self.pointer + 5],
|
||||
self.data[self.pointer + 6],
|
||||
self.data[self.pointer + 7],
|
||||
]);
|
||||
self.pointer += 8;
|
||||
val
|
||||
}
|
||||
|
||||
pub fn read_int32(&mut self) -> i32 {
|
||||
if self.pointer + 4 > self.data.len() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let val = i32::from_le_bytes([
|
||||
self.data[self.pointer],
|
||||
self.data[self.pointer + 1],
|
||||
self.data[self.pointer + 2],
|
||||
self.data[self.pointer + 3],
|
||||
]);
|
||||
self.pointer += 4;
|
||||
val
|
||||
}
|
||||
|
||||
pub fn read_bool(&mut self) -> bool {
|
||||
let val = self.data[self.pointer] == 1;
|
||||
self.pointer += 1;
|
||||
val
|
||||
}
|
||||
|
||||
pub fn write_string(&mut self, val: &str) -> &mut Self {
|
||||
let length = val.len() as u32;
|
||||
self.data.extend_from_slice(&length.to_le_bytes());
|
||||
self.data.extend_from_slice(val.as_bytes());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn write_int32(&mut self, value: i32) -> &mut Self {
|
||||
self.data.extend_from_slice(&value.to_le_bytes());
|
||||
self
|
||||
}
|
||||
pub fn write_raw_bytes(&mut self, value: Vec<u8>) -> &mut Self {
|
||||
self.data.extend_from_slice(&value);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn write_raw_hex_string(&mut self, val: &str) -> &mut Self {
|
||||
let mut b = Vec::new();
|
||||
for i in (0..val.len()).step_by(2) {
|
||||
let byte_str = &val[i..i + 2];
|
||||
let byte = u8::from_str_radix(byte_str, 16).expect("Invalid hex string");
|
||||
b.push(byte);
|
||||
}
|
||||
self.data.extend_from_slice(&b);
|
||||
self.pointer += b.len();
|
||||
self
|
||||
}
|
||||
// pub fn decode(&self) {
|
||||
// println!("{:?}", self.data);
|
||||
// }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_buffer() {
|
||||
let message = Message::new_with_data(
|
||||
[
|
||||
26, 0, 0, 0, 219, 178, 47, 28, 11, 0, 0, 0, 116, 104, 101, 32, 119, 101, 101, 107, 101,
|
||||
110, 100,
|
||||
]
|
||||
.to_vec(),
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_buffer(),
|
||||
[
|
||||
23, 0, 0, 0, 26, 0, 0, 0, 219, 178, 47, 28, 11, 0, 0, 0, 116, 104, 101, 32, 119, 101,
|
||||
101, 107, 101, 110, 100,
|
||||
]
|
||||
.to_vec()
|
||||
);
|
||||
|
||||
let message = Message::new_with_data(
|
||||
[
|
||||
1, 0, 0, 0, 20, 0, 0, 0, 105, 110, 115, 97, 110, 101, 95, 105, 110, 95, 116, 104, 101,
|
||||
95, 98, 114, 97, 105, 110, 50, 8, 0, 0, 0, 49, 51, 51, 55, 53, 49, 51, 55, 160, 0, 0,
|
||||
0, 32, 0, 0, 0, 50, 101, 100, 102, 53, 49, 100, 48, 51, 55, 57, 52, 51, 55, 56, 102,
|
||||
56, 98, 98, 54, 51, 49, 48, 100, 52, 54, 48, 99, 50, 50, 98, 49, 17, 0, 0, 0,
|
||||
]
|
||||
.to_vec(),
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_buffer(),
|
||||
[
|
||||
84, 0, 0, 0, 1, 0, 0, 0, 20, 0, 0, 0, 105, 110, 115, 97, 110, 101, 95, 105, 110, 95,
|
||||
116, 104, 101, 95, 98, 114, 97, 105, 110, 50, 8, 0, 0, 0, 49, 51, 51, 55, 53, 49, 51,
|
||||
55, 160, 0, 0, 0, 32, 0, 0, 0, 50, 101, 100, 102, 53, 49, 100, 48, 51, 55, 57, 52, 51,
|
||||
55, 56, 102, 56, 98, 98, 54, 51, 49, 48, 100, 52, 54, 48, 99, 50, 50, 98, 49, 17, 0, 0,
|
||||
0,
|
||||
]
|
||||
.to_vec()
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_read_string() {
|
||||
let data = vec![
|
||||
5, 0, 0, 0, // size = 5
|
||||
72, 101, 108, 108, 111, // "Hello"
|
||||
];
|
||||
let mut test_data = Message::new_with_data(data);
|
||||
assert_eq!(test_data.read_string(), "Hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_string_2() {
|
||||
let data = vec![
|
||||
50, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 81, 170, 162, 77, 32, 0, 0, 0, 101, 102, 99, 97,
|
||||
51, 52, 102, 99, 52, 99, 56, 98, 101, 56, 98, 55, 101, 102, 51, 56, 97, 102, 50, 54, 50,
|
||||
52, 100, 101, 53, 52, 54, 52, 0,
|
||||
];
|
||||
let mut test_data = Message::new_with_data(data);
|
||||
test_data.set_pointer(9);
|
||||
assert_eq!(test_data.read_string(), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Failed to read string")]
|
||||
fn test_read_string_invalid_utf8() {
|
||||
let data = vec![
|
||||
3, 0, 0, 0, // size = 3
|
||||
255, 255, 255, // invalid UTF-8 sequence
|
||||
];
|
||||
let mut test_data = Message::new_with_data(data);
|
||||
test_data.read_string();
|
||||
}
|
||||
|
||||
+322
-2
@@ -1,9 +1,329 @@
|
||||
pub mod handlers;
|
||||
pub mod message;
|
||||
pub mod message_reader;
|
||||
pub mod peer;
|
||||
pub mod server;
|
||||
|
||||
pub use handlers::*;
|
||||
pub use message::*;
|
||||
pub use message_reader::*;
|
||||
|
||||
use std::str;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub struct Message {
|
||||
data: Vec<u8>,
|
||||
pointer: usize,
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
data: Vec::new(),
|
||||
pointer: 0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_hex(&self) {
|
||||
let data = &self.data;
|
||||
const BYTES_PER_LINE: usize = 16;
|
||||
|
||||
let chunks = data.chunks(BYTES_PER_LINE);
|
||||
for (i, chunk) in chunks.enumerate() {
|
||||
// Print the offset
|
||||
print!("{:04x} ", i * BYTES_PER_LINE);
|
||||
|
||||
// Print the hexadecimal part
|
||||
for j in 0..BYTES_PER_LINE {
|
||||
if j < chunk.len() {
|
||||
print!("{:02x} ", chunk[j]);
|
||||
} else {
|
||||
print!(" ");
|
||||
}
|
||||
|
||||
// Add extra space in the middle
|
||||
if j == 7 {
|
||||
print!(" ");
|
||||
}
|
||||
}
|
||||
|
||||
print!(" ");
|
||||
|
||||
// Print the ASCII part
|
||||
let mut i = 0;
|
||||
for &byte in chunk {
|
||||
i += 1;
|
||||
if byte.is_ascii_graphic() || byte.is_ascii_whitespace() {
|
||||
print!("{}", byte as char);
|
||||
} else {
|
||||
print!(".");
|
||||
}
|
||||
|
||||
if i == 8 {
|
||||
print!(" ");
|
||||
}
|
||||
}
|
||||
|
||||
println!();
|
||||
}
|
||||
}
|
||||
//
|
||||
// pub fn print_hex2(&self) -> String {
|
||||
// let data = &self.data;
|
||||
// let mut out = String::from("");
|
||||
// const BYTES_PER_LINE: usize = 16;
|
||||
//
|
||||
// let chunks = data.chunks(BYTES_PER_LINE);
|
||||
// for (i, chunk) in chunks.enumerate() {
|
||||
// // Print the offset
|
||||
// // out += &format!("{:04x} ", i * BYTES_PER_LINE);
|
||||
//
|
||||
// // Print the hexadecimal part
|
||||
// for j in 0..BYTES_PER_LINE {
|
||||
// if j < chunk.len() {
|
||||
// out += &format!("{:02x} ", chunk[j]);
|
||||
// } else {
|
||||
// // out += &format!(" ");
|
||||
// }
|
||||
//
|
||||
// // Add extra space in the middle
|
||||
// // if j == 7 {
|
||||
// // out += &format!(" ");
|
||||
// // }
|
||||
// }
|
||||
//
|
||||
// // // Print the ASCII part
|
||||
// // let mut i = 0;
|
||||
// // for &byte in chunk {
|
||||
// // i = i + 1;
|
||||
// // if byte.is_ascii_graphic() || byte.is_ascii_whitespace() {
|
||||
// // print!("{}", byte as char);
|
||||
// // } else {messa
|
||||
// // print!(".");
|
||||
// // }
|
||||
// //
|
||||
// // if i == 8 {
|
||||
// // print!(" ");
|
||||
// // }
|
||||
// // }
|
||||
// //
|
||||
// }
|
||||
// println!("{:?}", out.trim());
|
||||
// return out.trim().into();
|
||||
// }
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn get_message_code_u32(&self) -> u32 {
|
||||
u32::from_le_bytes(self.data[4..8].try_into().unwrap())
|
||||
}
|
||||
|
||||
pub fn get_message_code(&self) -> u8 {
|
||||
self.data[4]
|
||||
}
|
||||
|
||||
pub fn new_with_data(data: Vec<u8>) -> Self {
|
||||
Self { data, pointer: 0 }
|
||||
}
|
||||
// pub fn reset_pointer(&mut self) {
|
||||
// self.pointer = 0;
|
||||
// }
|
||||
pub fn set_pointer(&mut self, pointer: usize) {
|
||||
self.pointer = pointer;
|
||||
}
|
||||
|
||||
pub fn get_pointer(&mut self) -> usize {
|
||||
self.pointer
|
||||
}
|
||||
|
||||
pub fn get_size(&mut self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
|
||||
pub fn get_data(&self) -> Vec<u8> {
|
||||
self.data.clone()
|
||||
}
|
||||
|
||||
pub fn get_slice(&self, from: usize, to: usize) -> Vec<u8> {
|
||||
self.data[from..to].to_vec()
|
||||
}
|
||||
|
||||
/// gets buffer with the message length prepended
|
||||
pub fn get_buffer(&self) -> Vec<u8> {
|
||||
let mut b = vec![0u8; 4];
|
||||
let length = self.data.len() as u32;
|
||||
b[0..4].copy_from_slice(&length.to_le_bytes());
|
||||
let mut combined = b;
|
||||
combined.extend(&self.data);
|
||||
combined
|
||||
}
|
||||
|
||||
pub fn read_string(&mut self) -> String {
|
||||
let size = u32::from_le_bytes([
|
||||
self.data[self.pointer],
|
||||
self.data[self.pointer + 1],
|
||||
self.data[self.pointer + 2],
|
||||
self.data[self.pointer + 3],
|
||||
]) as usize;
|
||||
|
||||
self.pointer += 4;
|
||||
let data = &self.data[self.pointer..self.pointer + size];
|
||||
self.pointer += size;
|
||||
|
||||
String::from_utf8(data.to_vec()).expect("Failed to read string")
|
||||
}
|
||||
pub fn read_raw_hex_str(&mut self, size: usize) -> String {
|
||||
let hex_str = self.data[self.pointer..self.pointer + size]
|
||||
.iter()
|
||||
.fold(String::new(), |mut acc, byte| {
|
||||
use std::fmt::Write;
|
||||
write!(acc, "{:02x}", byte).expect("Failed to write to string");
|
||||
acc
|
||||
});
|
||||
self.pointer += size;
|
||||
hex_str
|
||||
}
|
||||
|
||||
pub fn read_int8(&mut self) -> i8 {
|
||||
let val = self.data[self.pointer] as i8;
|
||||
self.pointer += 1;
|
||||
val
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn read_int64(&mut self) -> i64 {
|
||||
let val = i64::from_le_bytes([
|
||||
self.data[self.pointer],
|
||||
self.data[self.pointer + 1],
|
||||
self.data[self.pointer + 2],
|
||||
self.data[self.pointer + 3],
|
||||
self.data[self.pointer + 4],
|
||||
self.data[self.pointer + 5],
|
||||
self.data[self.pointer + 6],
|
||||
self.data[self.pointer + 7],
|
||||
]);
|
||||
self.pointer += 8;
|
||||
val
|
||||
}
|
||||
|
||||
pub fn read_int32(&mut self) -> i32 {
|
||||
if self.pointer + 4 > self.data.len() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let val = i32::from_le_bytes([
|
||||
self.data[self.pointer],
|
||||
self.data[self.pointer + 1],
|
||||
self.data[self.pointer + 2],
|
||||
self.data[self.pointer + 3],
|
||||
]);
|
||||
self.pointer += 4;
|
||||
val
|
||||
}
|
||||
|
||||
pub fn read_bool(&mut self) -> bool {
|
||||
let val = self.data[self.pointer] == 1;
|
||||
self.pointer += 1;
|
||||
val
|
||||
}
|
||||
|
||||
pub fn write_string(&mut self, val: &str) -> &mut Self {
|
||||
let length = val.len() as u32;
|
||||
self.data.extend_from_slice(&length.to_le_bytes());
|
||||
self.data.extend_from_slice(val.as_bytes());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn write_int32(&mut self, value: i32) -> &mut Self {
|
||||
self.data.extend_from_slice(&value.to_le_bytes());
|
||||
self
|
||||
}
|
||||
pub fn write_raw_bytes(&mut self, value: Vec<u8>) -> &mut Self {
|
||||
self.data.extend_from_slice(&value);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn write_raw_hex_string(&mut self, val: &str) -> &mut Self {
|
||||
let mut b = Vec::new();
|
||||
for i in (0..val.len()).step_by(2) {
|
||||
let byte_str = &val[i..i + 2];
|
||||
let byte = u8::from_str_radix(byte_str, 16).expect("Invalid hex string");
|
||||
b.push(byte);
|
||||
}
|
||||
self.data.extend_from_slice(&b);
|
||||
self.pointer += b.len();
|
||||
self
|
||||
}
|
||||
// pub fn decode(&self) {
|
||||
// println!("{:?}", self.data);
|
||||
// }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_buffer() {
|
||||
let message = Message::new_with_data(
|
||||
[
|
||||
26, 0, 0, 0, 219, 178, 47, 28, 11, 0, 0, 0, 116, 104, 101, 32, 119, 101, 101, 107, 101,
|
||||
110, 100,
|
||||
]
|
||||
.to_vec(),
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_buffer(),
|
||||
[
|
||||
23, 0, 0, 0, 26, 0, 0, 0, 219, 178, 47, 28, 11, 0, 0, 0, 116, 104, 101, 32, 119, 101,
|
||||
101, 107, 101, 110, 100,
|
||||
]
|
||||
.to_vec()
|
||||
);
|
||||
|
||||
let message = Message::new_with_data(
|
||||
[
|
||||
1, 0, 0, 0, 20, 0, 0, 0, 105, 110, 115, 97, 110, 101, 95, 105, 110, 95, 116, 104, 101,
|
||||
95, 98, 114, 97, 105, 110, 50, 8, 0, 0, 0, 49, 51, 51, 55, 53, 49, 51, 55, 160, 0, 0,
|
||||
0, 32, 0, 0, 0, 50, 101, 100, 102, 53, 49, 100, 48, 51, 55, 57, 52, 51, 55, 56, 102,
|
||||
56, 98, 98, 54, 51, 49, 48, 100, 52, 54, 48, 99, 50, 50, 98, 49, 17, 0, 0, 0,
|
||||
]
|
||||
.to_vec(),
|
||||
);
|
||||
assert_eq!(
|
||||
message.get_buffer(),
|
||||
[
|
||||
84, 0, 0, 0, 1, 0, 0, 0, 20, 0, 0, 0, 105, 110, 115, 97, 110, 101, 95, 105, 110, 95,
|
||||
116, 104, 101, 95, 98, 114, 97, 105, 110, 50, 8, 0, 0, 0, 49, 51, 51, 55, 53, 49, 51,
|
||||
55, 160, 0, 0, 0, 32, 0, 0, 0, 50, 101, 100, 102, 53, 49, 100, 48, 51, 55, 57, 52, 51,
|
||||
55, 56, 102, 56, 98, 98, 54, 51, 49, 48, 100, 52, 54, 48, 99, 50, 50, 98, 49, 17, 0, 0,
|
||||
0,
|
||||
]
|
||||
.to_vec()
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn test_read_string() {
|
||||
let data = vec![
|
||||
5, 0, 0, 0, // size = 5
|
||||
72, 101, 108, 108, 111, // "Hello"
|
||||
];
|
||||
let mut test_data = Message::new_with_data(data);
|
||||
assert_eq!(test_data.read_string(), "Hello");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_string_2() {
|
||||
let data = vec![
|
||||
50, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 81, 170, 162, 77, 32, 0, 0, 0, 101, 102, 99, 97,
|
||||
51, 52, 102, 99, 52, 99, 56, 98, 101, 56, 98, 55, 101, 102, 51, 56, 97, 102, 50, 54, 50,
|
||||
52, 100, 101, 53, 52, 54, 52, 0,
|
||||
];
|
||||
let mut test_data = Message::new_with_data(data);
|
||||
test_data.set_pointer(9);
|
||||
assert_eq!(test_data.read_string(), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "Failed to read string")]
|
||||
fn test_read_string_invalid_utf8() {
|
||||
let data = vec![
|
||||
3, 0, 0, 0, // size = 3
|
||||
255, 255, 255, // invalid UTF-8 sequence
|
||||
];
|
||||
let mut test_data = Message::new_with_data(data);
|
||||
test_data.read_string();
|
||||
}
|
||||
@@ -5,6 +5,7 @@ use std::collections::HashMap;
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub struct File {
|
||||
pub username: String,
|
||||
pub name: String,
|
||||
@@ -12,6 +13,7 @@ pub struct File {
|
||||
pub attribs: HashMap<i32, i32>,
|
||||
}
|
||||
#[derive(Debug)]
|
||||
#[allow(dead_code)]
|
||||
pub struct FileSearch {
|
||||
pub token: String,
|
||||
pub files: Vec<File>,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
use crate::{
|
||||
message::{handlers::MessageHandler, message::Message},
|
||||
use crate::{message::handlers::MessageHandler,
|
||||
server::ServerOperation,
|
||||
message::Message
|
||||
};
|
||||
|
||||
pub struct FileSearchHandler;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{message::message::Message, utils::md5};
|
||||
use crate::{message::Message, utils::md5};
|
||||
|
||||
pub struct MessageFactory;
|
||||
impl MessageFactory {
|
||||
|
||||
@@ -7,13 +7,14 @@ use std::sync::mpsc::{Receiver, Sender};
|
||||
use std::sync::{Arc, Barrier};
|
||||
use std::thread::{self};
|
||||
|
||||
use super::peer::Peer;
|
||||
use crate::peer::Peer;
|
||||
use crate::client::ClientOperation;
|
||||
use std::io::{self, Write};
|
||||
use std::net::TcpStream;
|
||||
use std::net::ToSocketAddrs;
|
||||
use std::time::Duration;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct DefaultPeer {
|
||||
peer: Peer,
|
||||
// client_channel: Sender<ClientOperation>,
|
||||
@@ -45,7 +46,7 @@ impl DefaultPeer {
|
||||
stream.set_write_timeout(Some(Duration::from_secs(5)))?;
|
||||
if let Some(token) = self.peer.token.clone() {
|
||||
stream
|
||||
.write_all(&MessageFactory::build_watch_user(&token).get_data())
|
||||
.write_all(&MessageFactory::build_watch_user(token.as_str()).get_data())
|
||||
.unwrap();
|
||||
}
|
||||
self.start_read_write_loops(stream).unwrap();
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ use crate::server::ServerOperation;
|
||||
pub struct Listen {}
|
||||
|
||||
impl Listen {
|
||||
pub fn new(port: u32, server_channel: Sender<ServerOperation>) {
|
||||
pub fn start(port: u32, _server_channel: Sender<ServerOperation>) {
|
||||
println!("starting listener on port {port}");
|
||||
let listener = TcpListener::bind(format!("0.0.0.0:{port}")).unwrap();
|
||||
for stream in listener.incoming() {
|
||||
|
||||
+155
-2
@@ -1,4 +1,157 @@
|
||||
pub mod default_peer;
|
||||
|
||||
pub use default_peer::*;
|
||||
pub mod listen;
|
||||
pub mod peer;
|
||||
pub use self::{default_peer::*, peer::*};
|
||||
|
||||
use crate::message::Message;
|
||||
use core::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ConnectionType {
|
||||
P,
|
||||
F,
|
||||
D,
|
||||
}
|
||||
|
||||
// Define the error type
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ParseConnectionTypeError;
|
||||
|
||||
impl fmt::Display for ParseConnectionTypeError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "invalid connection type")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ParseConnectionTypeError {}
|
||||
|
||||
// Implement FromStr for ConnectionType
|
||||
impl FromStr for ConnectionType {
|
||||
type Err = ParseConnectionTypeError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"P" => Ok(ConnectionType::P),
|
||||
"F" => Ok(ConnectionType::F),
|
||||
"D" => Ok(ConnectionType::D),
|
||||
_ => Err(ParseConnectionTypeError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub struct Peer {
|
||||
pub username: String,
|
||||
pub connection_type: ConnectionType,
|
||||
pub host: String,
|
||||
pub port: i32,
|
||||
pub token: Option<String>,
|
||||
pub privileged: i8,
|
||||
pub unknown: i8,
|
||||
pub obfuscated_port: i8,
|
||||
}
|
||||
impl Peer {
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn new(
|
||||
username: String,
|
||||
connection_type: ConnectionType,
|
||||
host: String,
|
||||
port: i32,
|
||||
token: String,
|
||||
privileged: i8,
|
||||
unknown: i8,
|
||||
obfuscated_port: i8,
|
||||
) -> Self {
|
||||
Self {
|
||||
username,
|
||||
connection_type,
|
||||
host,
|
||||
port,
|
||||
token: Some(token),
|
||||
privileged,
|
||||
unknown,
|
||||
obfuscated_port,
|
||||
}
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub fn new_from_message(message: &mut Message) -> Self {
|
||||
let username = message.read_string();
|
||||
let connection_type: ConnectionType = message.read_string().parse().unwrap();
|
||||
|
||||
let mut ip: Vec<i32> = vec![];
|
||||
for _ in 0..4 {
|
||||
ip.push(message.read_int8().into());
|
||||
}
|
||||
let host: String = format!(
|
||||
"{}.{}.{}.{}",
|
||||
ip[3].abs(),
|
||||
ip[2].abs(),
|
||||
ip[1].abs(),
|
||||
ip[0].abs()
|
||||
);
|
||||
|
||||
let (port, token, privileged, unknown, obfuscated_port) = (
|
||||
message.read_int32(),
|
||||
message.read_raw_hex_str(4),
|
||||
message.read_int8(),
|
||||
message.read_int8(),
|
||||
message.read_int8(),
|
||||
);
|
||||
|
||||
Self {
|
||||
username,
|
||||
connection_type,
|
||||
host,
|
||||
port,
|
||||
token: Some(token),
|
||||
privileged,
|
||||
unknown,
|
||||
obfuscated_port,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn test_new_from_message() {
|
||||
let data: Vec<u8> = [
|
||||
36, 0, 0, 0, 18, 0, 0, 0, 2, 0, 0, 0, 100, 112, 1, 0, 0, 0, 80, 27, 231, 37, 45, 186, 8, 0,
|
||||
0, 178, 78, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
]
|
||||
.to_vec();
|
||||
let mut message = Message::new_with_data(data);
|
||||
message.set_pointer(8);
|
||||
|
||||
let peer = Peer::new_from_message(&mut message);
|
||||
|
||||
assert_eq!(peer.username, "dp");
|
||||
assert!(matches!(peer.connection_type, ConnectionType::P));
|
||||
assert_eq!(peer.host, "45.37.25.27");
|
||||
assert_eq!(peer.port, 2234);
|
||||
assert_eq!(peer.token, Some("b24e1900".to_string()));
|
||||
assert_eq!(peer.privileged, 0);
|
||||
assert_eq!(peer.unknown, 0);
|
||||
assert_eq!(peer.obfuscated_port, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_from_message2() {
|
||||
let data: Vec<u8> = [
|
||||
42, 0, 0, 0, 18, 0, 0, 0, 8, 0, 0, 0, 103, 114, 97, 110, 100, 112, 97, 103, 1, 0, 0, 0, 80,
|
||||
137, 128, 193, 68, 187, 8, 0, 0, 58, 16, 0, 0, 0, 1, 0, 0, 0, 188, 8, 0, 0,
|
||||
]
|
||||
.to_vec();
|
||||
let mut message = Message::new_with_data(data);
|
||||
message.set_pointer(8);
|
||||
|
||||
let peer = Peer::new_from_message(&mut message);
|
||||
|
||||
assert_eq!(peer.username, "grandpag");
|
||||
assert!(matches!(peer.connection_type, ConnectionType::P));
|
||||
assert_eq!(peer.host, "68.63.128.119");
|
||||
assert_eq!(peer.port, 2235);
|
||||
assert_eq!(peer.token, Some("3a100000".to_string()));
|
||||
assert_eq!(peer.privileged, 0);
|
||||
assert_eq!(peer.unknown, 1);
|
||||
assert_eq!(peer.obfuscated_port, 0);
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
use core::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::message::Message;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ConnectionType {
|
||||
P,
|
||||
F,
|
||||
D,
|
||||
}
|
||||
|
||||
// Define the error type
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ParseConnectionTypeError;
|
||||
|
||||
impl fmt::Display for ParseConnectionTypeError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "invalid connection type")
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for ParseConnectionTypeError {}
|
||||
|
||||
// Implement FromStr for ConnectionType
|
||||
impl FromStr for ConnectionType {
|
||||
type Err = ParseConnectionTypeError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
match s {
|
||||
"P" => Ok(ConnectionType::P),
|
||||
"F" => Ok(ConnectionType::F),
|
||||
"D" => Ok(ConnectionType::D),
|
||||
_ => Err(ParseConnectionTypeError),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(dead_code)]
|
||||
pub struct Peer {
|
||||
pub username: String,
|
||||
pub connection_type: ConnectionType,
|
||||
pub host: String,
|
||||
pub port: i32,
|
||||
pub token: Option<String>,
|
||||
pub privileged: i8,
|
||||
pub unknown: i8,
|
||||
pub obfuscated_port: i8,
|
||||
}
|
||||
impl Peer {
|
||||
#[allow(dead_code)]
|
||||
pub fn new(
|
||||
username: String,
|
||||
connection_type: ConnectionType,
|
||||
host: String,
|
||||
port: i32,
|
||||
token: String,
|
||||
privileged: i8,
|
||||
unknown: i8,
|
||||
obfuscated_port: i8,
|
||||
) -> Self {
|
||||
Self {
|
||||
username,
|
||||
connection_type,
|
||||
host,
|
||||
port,
|
||||
token: Some(token),
|
||||
privileged,
|
||||
unknown,
|
||||
obfuscated_port,
|
||||
}
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub fn new_from_message(message: &mut Message) -> Self {
|
||||
let username = message.read_string();
|
||||
let connection_type: ConnectionType = message.read_string().parse().unwrap();
|
||||
|
||||
let mut ip: Vec<i32> = vec![];
|
||||
for _ in 0..4 {
|
||||
ip.push(message.read_int8().into());
|
||||
}
|
||||
let host: String = format!(
|
||||
"{}.{}.{}.{}",
|
||||
ip[3].abs(),
|
||||
ip[2].abs(),
|
||||
ip[1].abs(),
|
||||
ip[0].abs()
|
||||
);
|
||||
|
||||
let (port, token, privileged, unknown, obfuscated_port) = (
|
||||
message.read_int32(),
|
||||
message.read_raw_hex_str(4),
|
||||
message.read_int8(),
|
||||
message.read_int8(),
|
||||
message.read_int8(),
|
||||
);
|
||||
|
||||
Self {
|
||||
username,
|
||||
connection_type,
|
||||
host,
|
||||
port,
|
||||
token: Some(token),
|
||||
privileged,
|
||||
unknown,
|
||||
obfuscated_port,
|
||||
}
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn test_new_from_message() {
|
||||
let data: Vec<u8> = [
|
||||
36, 0, 0, 0, 18, 0, 0, 0, 2, 0, 0, 0, 100, 112, 1, 0, 0, 0, 80, 27, 231, 37, 45, 186, 8, 0,
|
||||
0, 178, 78, 25, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
]
|
||||
.to_vec();
|
||||
let mut message = Message::new_with_data(data);
|
||||
message.set_pointer(8);
|
||||
|
||||
let peer = Peer::new_from_message(&mut message);
|
||||
|
||||
assert_eq!(peer.username, "dp");
|
||||
assert!(matches!(peer.connection_type, ConnectionType::P));
|
||||
assert_eq!(peer.host, "45.37.25.27");
|
||||
assert_eq!(peer.port, 2234);
|
||||
assert_eq!(peer.token, Some("b24e1900".to_string()));
|
||||
assert_eq!(peer.privileged, 0);
|
||||
assert_eq!(peer.unknown, 0);
|
||||
assert_eq!(peer.obfuscated_port, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_new_from_message2() {
|
||||
let data: Vec<u8> = [
|
||||
42, 0, 0, 0, 18, 0, 0, 0, 8, 0, 0, 0, 103, 114, 97, 110, 100, 112, 97, 103, 1, 0, 0, 0, 80,
|
||||
137, 128, 193, 68, 187, 8, 0, 0, 58, 16, 0, 0, 0, 1, 0, 0, 0, 188, 8, 0, 0,
|
||||
]
|
||||
.to_vec();
|
||||
let mut message = Message::new_with_data(data);
|
||||
message.set_pointer(8);
|
||||
|
||||
let peer = Peer::new_from_message(&mut message);
|
||||
|
||||
assert_eq!(peer.username, "grandpag");
|
||||
assert!(matches!(peer.connection_type, ConnectionType::P));
|
||||
assert_eq!(peer.host, "68.63.128.119");
|
||||
assert_eq!(peer.port, 2235);
|
||||
assert_eq!(peer.token, Some("3a100000".to_string()));
|
||||
assert_eq!(peer.privileged, 0);
|
||||
assert_eq!(peer.unknown, 1);
|
||||
assert_eq!(peer.obfuscated_port, 0);
|
||||
}
|
||||
|
||||
+2
-2
@@ -15,7 +15,7 @@ use crate::message::Handlers;
|
||||
use crate::message::Message;
|
||||
use crate::message::MessageReader;
|
||||
use crate::peer::listen::Listen;
|
||||
use crate::peer::peer::Peer;
|
||||
use crate::peer::Peer;
|
||||
|
||||
use std::io::{self, Write};
|
||||
use std::net::{TcpStream, ToSocketAddrs};
|
||||
@@ -332,7 +332,7 @@ impl Server {
|
||||
}
|
||||
}
|
||||
fn start_listener(&self, server_channel: Sender<ServerOperation>) {
|
||||
thread::spawn(move || Listen::new(2234, server_channel));
|
||||
thread::spawn(move || Listen::start(2234, server_channel));
|
||||
}
|
||||
|
||||
pub fn login(&self, username: &str, password: &str) -> Result<bool, std::io::Error> {
|
||||
|
||||
+10
-10
@@ -71,8 +71,8 @@ fn round_one_operations(
|
||||
mut b: u32,
|
||||
mut c: u32,
|
||||
mut d: u32,
|
||||
table: &Vec<u32>,
|
||||
x: &Vec<u32>,
|
||||
table: &[u32],
|
||||
x: &[u32],
|
||||
) -> [u32; 4] {
|
||||
let _ = x;
|
||||
let _ = table;
|
||||
@@ -119,8 +119,8 @@ fn round_two_operations(
|
||||
mut b: u32,
|
||||
mut c: u32,
|
||||
mut d: u32,
|
||||
table: &Vec<u32>,
|
||||
x: &Vec<u32>,
|
||||
table: &[u32],
|
||||
x: &[u32],
|
||||
) -> [u32; 4] {
|
||||
macro_rules! round2 {
|
||||
( $a:ident, $b:ident, $c:ident, $d:ident, $k:expr, $s:expr, $i:expr) => {
|
||||
@@ -165,8 +165,8 @@ fn round_three_operations(
|
||||
mut b: u32,
|
||||
mut c: u32,
|
||||
mut d: u32,
|
||||
table: &Vec<u32>,
|
||||
x: &Vec<u32>,
|
||||
table: &[u32],
|
||||
x: &[u32],
|
||||
) -> [u32; 4] {
|
||||
macro_rules! round3 {
|
||||
( $a:ident, $b:ident, $c:ident, $d:ident, $k:expr, $s:expr, $i:expr ) => {
|
||||
@@ -211,8 +211,8 @@ fn round_four_operations(
|
||||
mut b: u32,
|
||||
mut c: u32,
|
||||
mut d: u32,
|
||||
table: &Vec<u32>,
|
||||
x: &Vec<u32>,
|
||||
table: &[u32],
|
||||
x: &[u32],
|
||||
) -> [u32; 4] {
|
||||
macro_rules! round4 {
|
||||
( $a:ident, $b:ident, $c:ident, $d:ident, $k:expr, $s:expr, $i:expr ) => {
|
||||
@@ -260,8 +260,8 @@ fn convert_u8_chunk_to_u32(chunk: &mut [u8]) -> Vec<u32> {
|
||||
// iterate over our block and take
|
||||
// our 8 bit ints and convert them to
|
||||
// 32 bit integers
|
||||
for i in 0..chunk.len() {
|
||||
temporary_vec.push(chunk[i]);
|
||||
for item in chunk {
|
||||
temporary_vec.push(*item);
|
||||
count += 1;
|
||||
if count == 4 {
|
||||
let temp_arr: [u8; 4] = vec_to_array(temporary_vec.clone());
|
||||
|
||||
+4
-8
@@ -291,15 +291,11 @@ fn decode_trees(r: &mut BitReader) -> Result<(HuffmanTree, HuffmanTree), String>
|
||||
} else if sym == 17 {
|
||||
// repeat code length 0 for 3..10 times. (3 bits of length)
|
||||
let repeat_length = r.read_bits(3)? + 3;
|
||||
for _ in 0..repeat_length {
|
||||
bl.push(0);
|
||||
}
|
||||
bl.resize(bl.len() + repeat_length as usize, 0);
|
||||
} else if sym == 18 {
|
||||
// repeat code length 0 for 11..138 times. (7 bits of length)
|
||||
let repeat_length = r.read_bits(7)? + 11;
|
||||
for _ in 0..repeat_length {
|
||||
bl.push(0);
|
||||
}
|
||||
bl.resize(bl.len() + repeat_length as usize, 0);
|
||||
} else {
|
||||
return Err("Invalid symbol".to_string());
|
||||
}
|
||||
@@ -362,8 +358,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_extract_header_success() {
|
||||
let data = vec![120, 156]; // Valid zlib header
|
||||
let result = deflate(&vec![120, 156, 3, 0, 0, 0, 0, 1]); // Minimal valid zlib stream
|
||||
let _data = [120, 156]; // Valid zlib header
|
||||
let result = deflate(&[120, 156, 3, 0, 0, 0, 0, 1]); // Minimal valid zlib stream
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user