From a8f49c7fb12855627ff9f634487be1ddf9793baa Mon Sep 17 00:00:00 2001 From: MeexReay Date: Sun, 9 Feb 2025 12:05:36 +0300 Subject: [PATCH] fix 1.99.2 --- .gitignore | 3 +- Cargo.lock | 7 +++++ Cargo.toml | 10 +++++++ README.md | 2 ++ main.rs | 88 ++++++++++++++++++++++++++++++++++++++---------------- 5 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 Cargo.lock create mode 100644 Cargo.toml diff --git a/.gitignore b/.gitignore index e3a7f03..e58a696 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ main -main.exe \ No newline at end of file +main.exe +/target \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..58dd46e --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bRAC" +version = "1.99.2" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..b6b4855 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "bRAC" +version = "1.99.2" +edition = "2021" + +[dependencies] + +[[bin]] +name = "bRAC" +path = "main.rs" diff --git a/README.md b/README.md index ff7f425..876810b 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,6 @@ better RAC client ```bash rustc main.rs -o main # build ./main # run +cargo build # build with cargo +cargo run # run with cargo ``` \ No newline at end of file diff --git a/main.rs b/main.rs index e162ea3..cab610b 100644 --- a/main.rs +++ b/main.rs @@ -1,4 +1,4 @@ -use std::{error::Error, io::{stdin, stdout, BufRead, Read, Write}, net::TcpStream, thread}; +use std::{error::Error, io::{stdin, stdout, BufRead, Read, Write}, net::TcpStream, thread, usize::MAX}; const MAX_MESSAGES: usize = 100; const DEFAULT_HOST: &str = "meex.lol:11234"; @@ -7,35 +7,71 @@ fn send_message(host: &str, message: &str) -> Result<(), Box> { let mut stream = TcpStream::connect(host)?; stream.write_all(&[0x01])?; stream.write_all(message.as_bytes())?; + stream.write_all("\0".repeat(1023 - message.len()).as_bytes())?; Ok(()) } -fn read_messages(host: &str) -> Result, Box> { +fn read_messages(host: &str, skip: usize) -> Result> { let mut stream = TcpStream::connect(host)?; + stream.write_all(&[0x00])?; + let packet_size = { - let mut buf= vec![0; 10]; - stream.read(&mut buf)?; - String::from_utf8(buf)?.trim_matches(char::from(0)).parse()? + let mut data = Vec::new(); + + loop { + let mut buf = vec![0; 1]; + stream.read_exact(&mut buf)?; + let ch = buf[0]; + if ch == 0 { + break + } + data.push(ch); + } + + String::from_utf8(data)? + .trim_matches(char::from(0)) + .parse()? }; - stream.write_all(&[0x01])?; + + // println!("{} {}", skip, packet_size); + + if packet_size <= skip { + return Ok(String::new()) + } + + let to_read = if skip == 0 { + stream.write_all(&[0x01])?; + packet_size + } else { + stream.write_all(&[0x02])?; + stream.write_all(skip.to_string().as_bytes())?; + packet_size - skip + }; + let packet_data = { - let mut buf = vec![0; packet_size]; - stream.read_exact(&mut buf)?; - let buf_str = String::from_utf8_lossy(&buf).to_string(); - let start_null = buf_str.len() - buf_str.trim_start_matches(char::from(0)).len(); - let mut buf = vec![0; start_null]; - stream.read_exact(&mut buf)?; - format!("{}{}", &buf_str, String::from_utf8_lossy(&buf).to_string()) + let mut data = vec![0; to_read]; + stream.read_exact(&mut data)?; + data.retain(|x| *x != 0); + while data.len() != to_read { + let mut buf = vec![0; to_read - data.len()]; + stream.read_exact(&mut buf)?; + data.append(&mut buf); + data.retain(|x| *x != 0); + } + String::from_utf8_lossy(&data).to_string() }; - let mut lines: Vec = packet_data.split("\n").map(|o| o.to_string()).collect(); - lines.reverse(); - lines.truncate(MAX_MESSAGES); - lines.reverse(); - Ok(lines) + + // println!("{}", packet_data); + + Ok(packet_data) } -fn print_console(messages: Vec) -> Result<(), Box> { +fn print_console(messages: Vec<&str>) -> Result<(), Box> { + let mut messages = messages.clone(); + messages.reverse(); + messages.truncate(MAX_MESSAGES); + messages.reverse(); let mut out = stdout().lock(); let text = format!("{}{}\n> ", "\n".repeat(MAX_MESSAGES - messages.len()), messages.join("\n")); out.write_all(text.as_bytes())?; @@ -44,11 +80,11 @@ fn print_console(messages: Vec) -> Result<(), Box> { } fn recv_loop(host: &str) -> Result<(), Box> { - let mut cache = Vec::new(); - while let Ok(messages) = read_messages(host) { - if cache == messages { continue } - print_console(messages.clone())?; - cache = messages; + let mut cache = String::new(); + while let Ok(messages) = read_messages(host, cache.len()) { + if messages.len() == 0 { continue } + cache.push_str(&messages); + print_console(cache.split("\n").collect())?; } Ok(()) } @@ -76,7 +112,7 @@ fn get_input(prompt: &str, default: &str) -> String { fn main() { let host = get_input("Host (default: meex.lol:11234) > ", DEFAULT_HOST); - let prefix = get_input("Prefix (default: none) > ", ""); + let name = get_input("Name (default: Anon) > ", "Anon"); thread::spawn({ let host = host.clone(); @@ -89,6 +125,6 @@ fn main() { let mut lines = stdin().lock().lines(); while let Some(Ok(message)) = lines.next() { - send_message(&host, &format!("{}{}", &prefix, &message)).expect("Error sending message"); + send_message(&host, &format!("<{}> {}", &name, &message)).expect("Error sending message"); } }