Files
ServerCrasherRust/src/configuration.rs
2025-01-25 20:28:46 +08:00

114 lines
3.7 KiB
Rust

use std::fs::File;
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
}
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);
Self::default()
}
}
}
fn default() -> Self {
Configuration {
server_ip: "49.70.3.146".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
}
}
// 下面是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,
// }
// }
// }