end server config parametrization

This commit is contained in:
Guillermo Roche 2022-12-23 20:55:28 +01:00
parent 930a1a3692
commit 06816a16ed
3 changed files with 41 additions and 23 deletions

View File

@ -4,14 +4,15 @@ use std::str::FromStr;
use std::io::prelude::*; use std::io::prelude::*;
use std::collections::HashMap; use std::collections::HashMap;
static FILE: &str = "mrprox.conf"; static FILE_CONF: &str = "mrprox.conf";
static FILE_SERVERS: &str = "mrprox_servers.conf";
const DEF_PORT: u16 = 25565; const DEF_PORT: u16 = 25565;
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct ConfFile{ pub struct ConfFile{
port: String, port: String,
port_conf: String, port_conf: String,
servers: Vec<ServerData>, conf_type: String,
} }
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
@ -24,18 +25,24 @@ pub struct Config{
l_servers : HashMap<String, String>, l_servers : HashMap<String, String>,
port: String, port: String,
port_conf: String, port_conf: String,
conf_type: String,
} }
impl Config { impl Config {
pub fn new() -> Self { pub fn new() -> Self {
let mut file = File::open(&FILE).unwrap(); let mut conf_file = File::open(&FILE_CONF).unwrap();
let mut s = String::new(); let mut servers_file = File::open(&FILE_SERVERS).unwrap();
file.read_to_string(&mut s).unwrap(); let mut s1 = String::new();
let yam: ConfFile = serde_yaml::from_str(&s).unwrap(); let mut s2 = String::new();
conf_file.read_to_string(&mut s1).unwrap();
servers_file.read_to_string(&mut s2).unwrap();
let yam_conf: ConfFile = serde_yaml::from_str(&s1).unwrap();
let yam_ser: Vec<ServerData> = serde_yaml::from_str(&s2).unwrap();
Self{ Self{
l_servers: Self::get_servers(&yam.servers), l_servers: Self::get_servers(&yam_ser),
port: yam.port, port: yam_conf.port,
port_conf: yam.port_conf, port_conf: yam_conf.port_conf,
conf_type: yam_conf.conf_type,
} }
} }
@ -54,14 +61,10 @@ impl Config {
} }
pub fn flush(&self){ pub fn flush(&self){
let conf = ConfFile { let servers = Vec::from_iter(self.l_servers.iter()
port: self.port.clone(), .map(|(x, y)| ServerData{domain: (*x).clone(), ip: (*y).clone()}));
port_conf: self.port_conf.clone(), let mut file = File::create(FILE_SERVERS).unwrap();
servers: Vec::from_iter(self.l_servers.iter() file.write(&serde_yaml::to_string(&servers).unwrap().into_bytes());
.map(|(x, y)| ServerData{domain: (*x).clone(), ip: (*y).clone()})),
};
let mut file = File::create(FILE).unwrap();
file.write(&serde_yaml::to_string(&conf).unwrap().into_bytes());
file.flush(); file.flush();
} }
@ -104,5 +107,9 @@ impl Config {
pub fn get_port_conf(&self) -> &String{ pub fn get_port_conf(&self) -> &String{
&self.port_conf &self.port_conf
} }
pub fn get_conf_type(&self) -> &String{
&self.conf_type
}
} }

View File

@ -4,8 +4,9 @@ use std::os::unix::net::UnixListener;
use std::os::unix::net::UnixStream; use std::os::unix::net::UnixStream;
use std::io::{Error, ErrorKind}; use std::io::{Error, ErrorKind};
pub const TCP_LIS : u8 = 0; pub const TCP_LIS : u8 = 1;
pub const UNIX_LIS : u8 = 1; pub const UNIX_LIS : u8 = 2;
pub const ERROR_LIS : u8 = 0;
pub trait RWTrait: std::io::Read + std::io::Write + Send {} pub trait RWTrait: std::io::Read + std::io::Write + Send {}
impl RWTrait for TcpStream {} impl RWTrait for TcpStream {}
@ -34,7 +35,7 @@ impl GenericListener {
} }
} }
pub fn bind(address: String, type_lis: u8) -> Result<Self, String>{ pub fn bind(address: &String, type_lis: u8) -> Result<Self, String>{
let ret = match type_lis { let ret = match type_lis {
TCP_LIS => Self::new_tcp(TcpListener::bind(address).unwrap()), TCP_LIS => Self::new_tcp(TcpListener::bind(address).unwrap()),
UNIX_LIS => Self::new_unix(UnixListener::bind(address).unwrap()), UNIX_LIS => Self::new_unix(UnixListener::bind(address).unwrap()),
@ -54,5 +55,13 @@ impl GenericListener {
} }
pub fn string_top_type(s: &String) -> u8 {
match (*s).as_str(){
"tcp" => TCP_LIS,
"unix" => UNIX_LIS,
&_ => ERROR_LIS,
}
}
} }

View File

@ -10,10 +10,11 @@ pub struct ConfSer{
} }
impl ConfSer { impl ConfSer {
fn new(path: String) -> ConfSer{ fn new(path: &String, conf_type: &String) -> ConfSer{
ConfSer{ ConfSer{
path: path.clone(), path: path.clone(),
listener: GenericListener::bind(path, 1).unwrap(), listener: GenericListener::bind(path,
GenericListener::string_top_type(conf_type)).unwrap(),
} }
} }
} }
@ -27,7 +28,8 @@ impl Drop for ConfSer {
} }
pub fn start(conf: Arc<RwLock<conf::Config>>){ pub fn start(conf: Arc<RwLock<conf::Config>>){
let ser = ConfSer::new(String::from("mineproxy")); let ser = ConfSer::new(conf.read().unwrap().get_port_conf()
,conf.read().unwrap().get_conf_type());
loop{ loop{
match ser.listener.accept() { match ser.listener.accept() {