fix 1.99.2

This commit is contained in:
MeexReay 2025-02-09 12:05:36 +03:00
parent 3ba2b30008
commit a8f49c7fb1
5 changed files with 83 additions and 27 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
main main
main.exe main.exe
/target

7
Cargo.lock generated Normal file
View File

@ -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"

10
Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "bRAC"
version = "1.99.2"
edition = "2021"
[dependencies]
[[bin]]
name = "bRAC"
path = "main.rs"

View File

@ -6,4 +6,6 @@ better RAC client
```bash ```bash
rustc main.rs -o main # build rustc main.rs -o main # build
./main # run ./main # run
cargo build # build with cargo
cargo run # run with cargo
``` ```

88
main.rs
View File

@ -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 MAX_MESSAGES: usize = 100;
const DEFAULT_HOST: &str = "meex.lol:11234"; const DEFAULT_HOST: &str = "meex.lol:11234";
@ -7,35 +7,71 @@ fn send_message(host: &str, message: &str) -> Result<(), Box<dyn Error>> {
let mut stream = TcpStream::connect(host)?; let mut stream = TcpStream::connect(host)?;
stream.write_all(&[0x01])?; stream.write_all(&[0x01])?;
stream.write_all(message.as_bytes())?; stream.write_all(message.as_bytes())?;
stream.write_all("\0".repeat(1023 - message.len()).as_bytes())?;
Ok(()) Ok(())
} }
fn read_messages(host: &str) -> Result<Vec<String>, Box<dyn Error>> { fn read_messages(host: &str, skip: usize) -> Result<String, Box<dyn Error>> {
let mut stream = TcpStream::connect(host)?; let mut stream = TcpStream::connect(host)?;
stream.write_all(&[0x00])?; stream.write_all(&[0x00])?;
let packet_size = { let packet_size = {
let mut buf= vec![0; 10]; let mut data = Vec::new();
stream.read(&mut buf)?;
String::from_utf8(buf)?.trim_matches(char::from(0)).parse()? 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 packet_data = {
let mut buf = vec![0; packet_size]; let mut data = vec![0; to_read];
stream.read_exact(&mut buf)?; stream.read_exact(&mut data)?;
let buf_str = String::from_utf8_lossy(&buf).to_string(); data.retain(|x| *x != 0);
let start_null = buf_str.len() - buf_str.trim_start_matches(char::from(0)).len(); while data.len() != to_read {
let mut buf = vec![0; start_null]; let mut buf = vec![0; to_read - data.len()];
stream.read_exact(&mut buf)?; stream.read_exact(&mut buf)?;
format!("{}{}", &buf_str, String::from_utf8_lossy(&buf).to_string()) data.append(&mut buf);
data.retain(|x| *x != 0);
}
String::from_utf8_lossy(&data).to_string()
}; };
let mut lines: Vec<String> = packet_data.split("\n").map(|o| o.to_string()).collect();
lines.reverse(); // println!("{}", packet_data);
lines.truncate(MAX_MESSAGES);
lines.reverse(); Ok(packet_data)
Ok(lines)
} }
fn print_console(messages: Vec<String>) -> Result<(), Box<dyn Error>> { fn print_console(messages: Vec<&str>) -> Result<(), Box<dyn Error>> {
let mut messages = messages.clone();
messages.reverse();
messages.truncate(MAX_MESSAGES);
messages.reverse();
let mut out = stdout().lock(); let mut out = stdout().lock();
let text = format!("{}{}\n> ", "\n".repeat(MAX_MESSAGES - messages.len()), messages.join("\n")); let text = format!("{}{}\n> ", "\n".repeat(MAX_MESSAGES - messages.len()), messages.join("\n"));
out.write_all(text.as_bytes())?; out.write_all(text.as_bytes())?;
@ -44,11 +80,11 @@ fn print_console(messages: Vec<String>) -> Result<(), Box<dyn Error>> {
} }
fn recv_loop(host: &str) -> Result<(), Box<dyn Error>> { fn recv_loop(host: &str) -> Result<(), Box<dyn Error>> {
let mut cache = Vec::new(); let mut cache = String::new();
while let Ok(messages) = read_messages(host) { while let Ok(messages) = read_messages(host, cache.len()) {
if cache == messages { continue } if messages.len() == 0 { continue }
print_console(messages.clone())?; cache.push_str(&messages);
cache = messages; print_console(cache.split("\n").collect())?;
} }
Ok(()) Ok(())
} }
@ -76,7 +112,7 @@ fn get_input(prompt: &str, default: &str) -> String {
fn main() { fn main() {
let host = get_input("Host (default: meex.lol:11234) > ", DEFAULT_HOST); 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({ thread::spawn({
let host = host.clone(); let host = host.clone();
@ -89,6 +125,6 @@ fn main() {
let mut lines = stdin().lock().lines(); let mut lines = stdin().lock().lines();
while let Some(Ok(message)) = lines.next() { 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");
} }
} }