From 06816a16ed41ea39f0aaedc75bedc3ce01a57639 Mon Sep 17 00:00:00 2001
From: Guillermo Roche <groche97@gmail.com>
Date: Fri, 23 Dec 2022 20:55:28 +0100
Subject: [PATCH] end server config parametrization

---
 src/conf/mod.rs             | 41 ++++++++++++++++++++++---------------
 src/server_conf/listener.rs | 15 +++++++++++---
 src/server_conf/server.rs   |  8 +++++---
 3 files changed, 41 insertions(+), 23 deletions(-)

diff --git a/src/conf/mod.rs b/src/conf/mod.rs
index 00f9d33..3076e47 100644
--- a/src/conf/mod.rs
+++ b/src/conf/mod.rs
@@ -4,14 +4,15 @@ use std::str::FromStr;
 use std::io::prelude::*;
 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;
 
 #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
 pub struct ConfFile{
     port: String,
     port_conf: String,
-    servers: Vec<ServerData>,
+    conf_type: String,
 }
 
 #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
@@ -24,18 +25,24 @@ pub struct Config{
     l_servers : HashMap<String, String>,
     port: String,
     port_conf: String,
+    conf_type: String,
 }
 
 impl Config {
     pub fn new() -> Self {
-        let mut file = File::open(&FILE).unwrap();
-        let mut s = String::new();
-        file.read_to_string(&mut s).unwrap();
-        let yam: ConfFile = serde_yaml::from_str(&s).unwrap();
+        let mut conf_file = File::open(&FILE_CONF).unwrap();
+        let mut servers_file = File::open(&FILE_SERVERS).unwrap();
+        let mut s1 = String::new();
+        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{
-            l_servers: Self::get_servers(&yam.servers),
-            port: yam.port,
-            port_conf: yam.port_conf,
+            l_servers: Self::get_servers(&yam_ser),
+            port: yam_conf.port,
+            port_conf: yam_conf.port_conf,
+            conf_type: yam_conf.conf_type,
         }
     }
 
@@ -54,14 +61,10 @@ impl Config {
     }
 
     pub fn flush(&self){
-        let conf = ConfFile {
-            port: self.port.clone(),
-            port_conf: self.port_conf.clone(),
-            servers: Vec::from_iter(self.l_servers.iter()
-                    .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());
+        let servers = Vec::from_iter(self.l_servers.iter()
+                    .map(|(x, y)| ServerData{domain: (*x).clone(), ip: (*y).clone()}));
+        let mut file = File::create(FILE_SERVERS).unwrap();
+        file.write(&serde_yaml::to_string(&servers).unwrap().into_bytes());
         file.flush();
 
     }
@@ -104,5 +107,9 @@ impl Config {
     pub fn get_port_conf(&self) -> &String{
         &self.port_conf
     }
+
+    pub fn get_conf_type(&self) -> &String{
+        &self.conf_type
+    }
 }
 
diff --git a/src/server_conf/listener.rs b/src/server_conf/listener.rs
index 56798ff..bbc18cf 100644
--- a/src/server_conf/listener.rs
+++ b/src/server_conf/listener.rs
@@ -4,8 +4,9 @@ use std::os::unix::net::UnixListener;
 use std::os::unix::net::UnixStream;
 use std::io::{Error, ErrorKind};
 
-pub const TCP_LIS : u8 = 0;
-pub const UNIX_LIS : u8 = 1;
+pub const TCP_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 {}
 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 {
             TCP_LIS => Self::new_tcp(TcpListener::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,
+        }
+    }
+
 }
 
diff --git a/src/server_conf/server.rs b/src/server_conf/server.rs
index 4f0f4b2..baaef95 100644
--- a/src/server_conf/server.rs
+++ b/src/server_conf/server.rs
@@ -10,10 +10,11 @@ pub struct ConfSer{
 }
 
 impl ConfSer {
-    fn new(path: String) -> ConfSer{
+    fn new(path: &String, conf_type: &String) -> ConfSer{
         ConfSer{
             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>>){
-    let ser = ConfSer::new(String::from("mineproxy"));
+    let ser = ConfSer::new(conf.read().unwrap().get_port_conf()
+                                        ,conf.read().unwrap().get_conf_type());
 
     loop{
         match ser.listener.accept() {