添加README.md,加了点注释
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
// bot的实现
|
||||
|
||||
use std::{io::Error, thread::sleep, time::Duration};
|
||||
|
||||
use rust_mc_proto::{DataBufferReader, DataBufferWriter, MinecraftConnection, Packet, ProtocolError};
|
||||
@@ -24,11 +26,13 @@ impl Socks5Bot {
|
||||
}
|
||||
// bot登录函数
|
||||
pub fn login(&mut self) -> Result<(), ProtocolError> {
|
||||
// 这里其实传的server host是什么都不要紧,但保险起见还是弄一下
|
||||
let mut server_host: String = self.server_addr.clone();
|
||||
let server_port: String = server_host.split_off(server_host.find(":").unwrap()+1);
|
||||
// println!("{}",server_host);
|
||||
server_host = server_host.strip_suffix(':').unwrap().to_string();
|
||||
let server_port_num: u16 = server_port.parse::<u16>().unwrap();
|
||||
// 发一个handshake包,设置next_state为2(Login)
|
||||
self.conn.write_packet(&Packet::build(0x00, |packet| {
|
||||
packet.write_u16_varint(763)?; // protocol_version
|
||||
packet.write_string(&server_host)?; // server_address
|
||||
@@ -36,6 +40,7 @@ impl Socks5Bot {
|
||||
packet.write_u8_varint(2) // next_state
|
||||
})?)?; // handshake packet
|
||||
|
||||
// 再发一个,把用户名发过去(不知道为什么,反正文档上写要发两个)
|
||||
self.conn.write_packet(&Packet::build(0x00, |packet| {
|
||||
packet.write_string(&self.username)?;
|
||||
packet.write_boolean(false)// ?;
|
||||
@@ -45,25 +50,30 @@ impl Socks5Bot {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 处理服务器发的keepalive
|
||||
pub fn handle_keepalive(&mut self,packet: &mut Packet) -> Result<(), ProtocolError> {
|
||||
// keep alive
|
||||
let id = packet.read_long()?;
|
||||
// keepalive原封不动丢回去就行
|
||||
// TODO 在20秒未进行Keepalive时断开连接
|
||||
self.conn.write_packet(&Packet::build(0x12, |packet| { // respond with a same keep alive packet
|
||||
packet.write_long(id)
|
||||
})?)?;
|
||||
// println!("Server keep alived");
|
||||
// 这个包是重生用的,因为我发现如果玩家上线的时候就是死的,那它就没法自动重生
|
||||
self.conn.write_packet(&Packet::build(0x07, |packet| {
|
||||
packet.write_u8_varint(0)
|
||||
})?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 处理断开连接事件(这个指的是建立连接后再断开连接)
|
||||
pub fn handle_disconnect(&mut self,packet: &mut Packet) -> Result<(), ProtocolError> {
|
||||
// disconnect
|
||||
let message = packet.read_string()?;
|
||||
let text = utils::parse_json_component(message.as_str());
|
||||
println!("[{}] Server disconnected: {}", self.username, text);
|
||||
if text.contains("验证程序已启用") {
|
||||
if text.contains("验证程序已启用") { // TODO 把这个搞进配置文件里面
|
||||
println!("[{}] Restart flag setted, will restart after 1.5 min", self.username);
|
||||
sleep(Duration::from_secs(90));
|
||||
self.should_restart = true;
|
||||
@@ -78,12 +88,12 @@ impl Socks5Bot {
|
||||
while self.conn.is_alive() {
|
||||
let mut packet = self.conn.read_packet()?;
|
||||
match packet.id() {
|
||||
0x02 => {
|
||||
0x02 => { // 成功登录
|
||||
// login success
|
||||
println!("[{}] Successfully logged in!",self.username);
|
||||
status = 1;
|
||||
}
|
||||
0x03 => {
|
||||
0x03 => {// 设置压缩CompressionThreshold
|
||||
// set compression
|
||||
let threshold = packet.read_i32_varint()?;
|
||||
if threshold >= 0 {
|
||||
@@ -91,22 +101,22 @@ impl Socks5Bot {
|
||||
// println!("[{}] Compression threshold set to {}", self.username, threshold)
|
||||
}
|
||||
}
|
||||
0x23 => {
|
||||
0x23 => { // KeepAlive包
|
||||
self.handle_keepalive(&mut packet)?;
|
||||
}
|
||||
0x1A => {
|
||||
0x1A => {// 断连包
|
||||
// disconnect
|
||||
self.handle_disconnect(&mut packet)?;
|
||||
break;
|
||||
}
|
||||
0x00 => {
|
||||
0x00 => {// 版本不匹配啥的就会走这
|
||||
if status == 0{
|
||||
let text = packet.read_string()?;
|
||||
println!("[{}] Failed to login: {}", self.username, utils::parse_json_component(text.as_str()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
0x38 => {
|
||||
0x38 => {// 去世包(自动复活)
|
||||
// player died
|
||||
println!("[{}] Player died,respawning...",self.username);
|
||||
self.conn.write_packet(&Packet::build(0x07, |packet| {
|
||||
@@ -121,6 +131,7 @@ impl Socks5Bot {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 运行bot
|
||||
pub fn run(&mut self) -> Result<(), ProtocolError> {
|
||||
println!("[{}] Connecting to {} with proxy {}", self.username, self.server_addr,self.proxy_addr);
|
||||
self.login().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user