简单搞了下logger
This commit is contained in:
@@ -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<Stream> {
|
||||
|
||||
impl<T: StreamType> Bot<T> {
|
||||
pub fn new(username: String, proxy_addr: String, server_addr: String, config: Arc<Configuration>, var: Arc<BotVariable>) -> Result<Bot<T>, 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<T: StreamType> Bot<T> {
|
||||
// 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<T: StreamType> Bot<T> {
|
||||
// }
|
||||
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<T: StreamType> Bot<T> {
|
||||
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<T: StreamType> Bot<T> {
|
||||
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<T: StreamType> Bot<T> {
|
||||
}
|
||||
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<T: StreamType> Bot<T> {
|
||||
};
|
||||
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<T: StreamType> Bot<T> {
|
||||
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<T: StreamType> Bot<T> {
|
||||
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<T: StreamType> Bot<T> {
|
||||
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<BotMessage>) -> 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(())
|
||||
|
||||
Reference in New Issue
Block a user