From 91624eec0fc24758ce55396b7c4f5a6f32f5078e Mon Sep 17 00:00:00 2001 From: Zihaoxu2008 Date: Sun, 26 Jan 2025 11:46:37 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E6=90=9E=E4=BA=86=E4=B8=8Blo?= =?UTF-8?q?gger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 137 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/bot_socks5.rs | 23 ++++---- src/configuration.rs | 17 +++--- src/main.rs | 16 +++-- 5 files changed, 172 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25a4ff5..51d783f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,8 @@ version = 4 name = "ServerCrasherRust" version = "0.1.0" dependencies = [ + "env_logger", + "log", "rand", "reqwest", "rust_mc_proto", @@ -31,6 +33,65 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8acc5369981196006228e28809f761875c0327210a891e941f4c683b3a99529b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3534e77181a9cc07539ad51f2141fe32f6c3ffd4df76db8ad92346b003ae4e" +dependencies = [ + "anstyle", + "once_cell", + "windows-sys 0.59.0", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -112,6 +173,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "core-foundation" version = "0.9.4" @@ -157,6 +224,29 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_filter" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "186e05a59d4c50738528153b83b0b0194d3a29507dfec16eccd4b342903397d0" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcaee3d8e3cfc3fd92428d477bc97fc29ec8716d180c0d74c643bb26166660e0" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -340,6 +430,12 @@ version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "1.5.2" @@ -567,6 +663,12 @@ version = "2.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130" +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itoa" version = "1.0.14" @@ -838,6 +940,35 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + [[package]] name = "reqwest" version = "0.12.12" @@ -1354,6 +1485,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.12.1" diff --git a/Cargo.toml b/Cargo.toml index 8a0b356..ec3ef65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,5 @@ uuid = "1.12.1" rand = "0.8.5" # 添加rand库依赖 reqwest = "0.12.12" tokio = { version = "1.43.0", features = ["full", "rt", "rt-multi-thread"] } +log = "0.4.25" +env_logger = "0.11.6" diff --git a/src/bot_socks5.rs b/src/bot_socks5.rs index 1632c6f..69ae0d5 100644 --- a/src/bot_socks5.rs +++ b/src/bot_socks5.rs @@ -2,6 +2,7 @@ use std::{io::{Error, Read, Write}, net::TcpStream, sync::Arc, thread::{self, sleep}, time::{self, Duration}}; +use log::{info, warn}; use rust_mc_proto::{DataBufferReader, DataBufferWriter, MinecraftConnection, Packet, ProtocolError}; use socks::Socks5Stream; use tokio::{io::{AsyncBufReadExt, AsyncSeekExt}, sync::Mutex}; @@ -117,7 +118,7 @@ impl Clone for Bot { impl Bot { pub fn new(username: String, proxy_addr: String, server_addr: String, config: Arc, var: Arc) -> Result, Error> { - println!("[{}] Creating bot on {} with proxy {}", username, server_addr, proxy_addr); + info!("[{}] Creating bot on {} with proxy {}", username, server_addr, proxy_addr); let stream = T::connect(server_addr.as_str(), proxy_addr.as_str()); let should_restart = false; match stream { @@ -206,7 +207,7 @@ impl Bot { // disconnect let message = packet.read_string()?; let text = utils::parse_json_component(message.as_str()); - println!("[{}] Server disconnected: {}", self.username, text); + info!("[{}] Server disconnected: {}", self.username, text); // if text.contains("验证程序已启用") { // TODO 把这个搞进配置文件里面 // println!("[{}] Restart flag setted, will restart after 1.5 min", self.username); // sleep(Duration::from_secs(90)); @@ -214,7 +215,7 @@ impl Bot { // } for i in self.config.reco_words(){ if text.contains(i.as_str()) { - println!("[{}] Restart flag setted, will restart after 1.5 min", self.username); + info!("[{}] Restart flag setted, will restart after 1.5 min", self.username); sleep(Duration::from_secs(90)); self.should_restart = true; } @@ -233,7 +234,7 @@ impl Bot { match packet.id() { 0x02 => { // 成功登录 // login success - println!("[{}] Successfully logged in!",self.username); + info!("[{}] Successfully logged in!",self.username); self.status = 1; message.state = 1; message.last_keepalive_sec = time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_secs(); @@ -263,7 +264,7 @@ impl Bot { 0x00 => {// 版本不匹配啥的就会走这 if self.status == 0{ let text = packet.read_string()?; - println!("[{}] Failed to login: {}", self.username, utils::parse_json_component(text.as_str())); + warn!("[{}] Failed to login: {}", self.username, utils::parse_json_component(text.as_str())); message.state = 2; status_tx.send(message).unwrap(); break; @@ -271,7 +272,7 @@ impl Bot { } 0x38 => {// 去世包(自动复活) // player died - println!("[{}] Player died,respawning...",self.username); + info!("[{}] Player died,respawning...",self.username); self.conn.lock().await.write_packet(&Packet::build(0x07, |packet| { packet.write_u8_varint(0) })?)? @@ -294,7 +295,7 @@ impl Bot { }; let mut iter = 0; let mut flag = true; - println!("[{}/WORKER] Worker started!", self.username); + info!("[{}/WORKER] Worker started!", self.username); while flag { if !status_rx.is_empty() { message = status_rx.recv().await.unwrap(); @@ -309,7 +310,7 @@ impl Bot { self.send_command(i.to_string()).await.unwrap(); thread::sleep(Duration::from_secs(1)); } - println!("[{}/WORKER] Login commands executed!",self.username); + info!("[{}/WORKER] Login commands executed!",self.username); login_command_flag = true; } } @@ -345,7 +346,7 @@ impl Bot { let timestamp = time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_secs(); if message.last_keepalive_sec + 60 < timestamp { // 连接超时 (*self.alive.lock().await) = false; - println!("[{}/WORKER] Bot timed out", self.username); + warn!("[{}/WORKER] Bot timed out", self.username); break; } iter += 1; @@ -353,12 +354,12 @@ impl Bot { sleep(Duration::from_millis(1)); flag = self.conn.lock().await.is_alive() && message.state!=2; } - println!("[{}/WORKER] Worker stopped.", self.username); + warn!("[{}/WORKER] Worker stopped.", self.username); } // 运行bot pub async fn run(&mut self, status_tx: tokio::sync::mpsc::UnboundedSender) -> Result<(), ProtocolError> { - println!("[{}] Connecting to {} with proxy {}", self.username, self.server_addr, self.proxy_addr); + info!("[{}] Connecting to {} with proxy {}", self.username, self.server_addr, self.proxy_addr); self.login().await.unwrap(); self.handle_packets(status_tx).await.unwrap_or(()); Ok(()) diff --git a/src/configuration.rs b/src/configuration.rs index 8145054..61906ce 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -1,4 +1,5 @@ use std::{fs::File, io::BufWriter}; +use log::{info, warn, error}; use serde::{Deserialize, Serialize}; use std::io::BufReader; @@ -27,26 +28,26 @@ impl Configuration { let reader = BufReader::new(file); match serde_json::from_reader::<_,Configuration>(reader) { // serde可以直接解析出实现了Deserialize trait的类型 就很不错 Ok(config) => { - println!("[CONFIG] Server IP: {}", config.server_ip); - println!("[CONFIG] Server Port: {}", config.server_port); - println!("[CONFIG] Username Prefix: {}", config.username_prefix); - println!("[CONFIG] Bot Count: {}", config.count); - println!("[CONFIG] Proxy URL: {}", config.proxy_url); + info!("Server IP: {}", config.server_ip); + info!("Server Port: {}", config.server_port); + info!("Username Prefix: {}", config.username_prefix); + info!("Bot Count: {}", config.count); + info!("Proxy URL: {}", config.proxy_url); config } Err(error) => { - eprintln!("[MAIN/CONF] Error parsing config file: {}", error); + error!("Error parsing config file: {}", error); Self::default() } } } Err(error) => { - eprintln!("[MAIN/CONF] Unable to read config file, will use default values: {}", error); + warn!("Unable to read config file, will use default values: {}", error); let file = File::create_new(filename).unwrap(); let writer = BufWriter::new(file); let mut serializer = serde_json::Serializer::pretty(writer); Self::default().serialize(&mut serializer).unwrap(); - eprintln!("[Main/CONF] A configure file with default values is created."); + warn!("A configure file with default values is created."); Self::default() } } diff --git a/src/main.rs b/src/main.rs index 57d197e..a283195 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod utils; mod configuration; use bot_socks5::BotVariable; +use log::error; use reqwest; use serde_json::Value; use tokio::runtime::Runtime; @@ -29,6 +30,13 @@ use crate::configuration::Configuration; #[tokio::main] async fn main() { + let mut logger_builder = env_logger::Builder::from_default_env(); + logger_builder.filter_level(log::LevelFilter::Info); + // logger_builder.format(|buf,record| { + // buf.write(format!("[{}/{}] {}\n", record.target(),record.level(),record.args()).as_bytes()).unwrap(); + // Ok(()) + // }); + logger_builder.init(); // 一个互斥锁,防止有多个线程同时请求uuproxy let thread_lock = Arc::new(Mutex::new(())); @@ -39,7 +47,7 @@ async fn main() { /* 在傻逼地球村中这个可以忽略了 if configuration_rc.username_prefix().len() > 14 { - eprintln!("[CONFIG] Error: Username prefix should not longer than 14"); + error!("Error: Username prefix should not longer than 14"); return; } @@ -133,7 +141,7 @@ async fn attack_thread(lock: Arc>, config: Arc, variabl }; if !proxy.is_object() { flag = true; - eprintln!("[{}] Unable to get proxy,retrying...",username) + error!("[{}] Unable to get proxy,retrying...",username) } } } @@ -173,11 +181,11 @@ async fn attack_thread(lock: Arc>, config: Arc, variabl } } Err(e) => { - eprintln!("[{}] Error running bot: {}", username.clone(), e); + error!("[{}] Error running bot: {}", username.clone(), e); } } }else if let Err(e) = bot_result { - eprintln!("[{}] Error creating bot: {}", username.clone(), e); + error!("[{}] Error creating bot: {}", username.clone(), e); continue; }