Fix unix config and refactor configs
This commit is contained in:
parent
276af0ba97
commit
bfb550e5a6
@ -19,11 +19,22 @@ static FILE_SERVERS: &str = "mrprox_servers.conf";
|
||||
|
||||
const DEF_PORT: u16 = 25565;
|
||||
|
||||
const TCP_TYPE: &str = "TCP";
|
||||
const UNIX_TYPE: &str = "UNIX";
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct ConfFile{
|
||||
ip: String,
|
||||
port: String,
|
||||
port_conf: String,
|
||||
conf_type: String,
|
||||
conf: ConfServer,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
pub struct ConfServer{
|
||||
sock_type: String,
|
||||
path: Option<String>,
|
||||
ip: Option<String>,
|
||||
port: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||
@ -35,8 +46,8 @@ pub struct ServerData{
|
||||
pub struct Config{
|
||||
l_servers : HashMap<String, String>,
|
||||
port: String,
|
||||
port_conf: String,
|
||||
conf_type: String,
|
||||
ip: String,
|
||||
conf: ConfServer,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@ -47,9 +58,9 @@ impl Config {
|
||||
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 = match serde_yaml::from_str(&s1) {
|
||||
let yam_conf:ConfFile = match serde_yaml::from_str(&s1) {
|
||||
Ok(result) => result,
|
||||
Err(e) => {error!("Config file malformed"); panic!("{}", e);},
|
||||
Err(e) => {error!("Config file malformed: {}",e); panic!("{}", e);},
|
||||
};
|
||||
let yam_ser: Vec<ServerData> = match serde_yaml::from_str(&s2) {
|
||||
Ok(result) => result,
|
||||
@ -58,8 +69,8 @@ impl Config {
|
||||
Self{
|
||||
l_servers: Self::get_servers(&yam_ser),
|
||||
port: yam_conf.port,
|
||||
port_conf: yam_conf.port_conf,
|
||||
conf_type: yam_conf.conf_type,
|
||||
ip: yam_conf.ip,
|
||||
conf: yam_conf.conf,
|
||||
}
|
||||
}
|
||||
|
||||
@ -121,12 +132,45 @@ impl Config {
|
||||
&self.port
|
||||
}
|
||||
|
||||
pub fn get_ip(&self) -> &String{
|
||||
&self.ip
|
||||
}
|
||||
|
||||
pub fn get_port_conf(&self) -> &String{
|
||||
&self.port_conf
|
||||
match self.conf.sock_type.as_str() {
|
||||
TCP_TYPE => &self.conf.port.as_ref().unwrap(),
|
||||
_=> {error!("Only tcp types have port"); panic!("Only tcp types have port")},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_conf_ip(&self) -> &String{
|
||||
match self.conf.sock_type.as_str() {
|
||||
TCP_TYPE => &self.conf.ip.as_ref().unwrap(),
|
||||
_=> {error!("Only tcp types have IP"); panic!("Only tcp types have IP")},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_bindeable_ip(&self) -> String {
|
||||
build_bindeable_ip(&self.ip,&self.port)
|
||||
}
|
||||
|
||||
pub fn get_bindeable_conf(&self) -> String {
|
||||
match self.conf.sock_type.as_str() {
|
||||
TCP_TYPE => build_bindeable_ip(
|
||||
self.conf.ip.as_ref().unwrap(),
|
||||
self.conf.port.as_ref().unwrap()
|
||||
),
|
||||
UNIX_TYPE => self.conf.path.as_ref().unwrap().to_string(),
|
||||
_=> {error!("Invalid type"); panic!("Invalid type")},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_conf_type(&self) -> &String{
|
||||
&self.conf_type
|
||||
&self.conf.sock_type
|
||||
}
|
||||
}
|
||||
|
||||
fn build_bindeable_ip(ip: &String, port: &String) -> String{
|
||||
format!("{}:{}",ip,port)
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ use std::net::TcpStream;
|
||||
use std::os::unix::net::UnixListener;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use std::io::{Error, ErrorKind};
|
||||
use log::{warn};
|
||||
|
||||
pub const TCP_LIS : u8 = 1;
|
||||
pub const UNIX_LIS : u8 = 2;
|
||||
@ -35,10 +36,18 @@ impl GenericListener {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bind(port: &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(String::from("0.0.0.0:") + port).unwrap()),
|
||||
UNIX_LIS => Self::new_unix(UnixListener::bind(port).unwrap()),
|
||||
TCP_LIS => Self::new_tcp(
|
||||
match TcpListener::bind(address) {
|
||||
Ok(s) => s,
|
||||
Err(e) => return Err(e.to_string()),
|
||||
}),
|
||||
UNIX_LIS => Self::new_unix(
|
||||
match UnixListener::bind(address){
|
||||
Ok(s) => s,
|
||||
Err(e) => return Err(e.to_string()),
|
||||
}),
|
||||
_ => return Err("No valid option".to_string()),
|
||||
};
|
||||
Ok(ret)
|
||||
@ -57,8 +66,8 @@ impl GenericListener {
|
||||
|
||||
pub fn string_top_type(s: &String) -> u8 {
|
||||
match (*s).as_str(){
|
||||
"tcp" => TCP_LIS,
|
||||
"unix" => UNIX_LIS,
|
||||
"TCP" => TCP_LIS,
|
||||
"UNIX" => UNIX_LIS,
|
||||
&_ => ERROR_LIS,
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ use crate::server_conf::conexion::Conexion;
|
||||
use crate::server_conf::listener::GenericListener;
|
||||
use std::thread;
|
||||
use std::sync::{Arc, RwLock};
|
||||
use log::{error,info};
|
||||
use log::{error,warn,info};
|
||||
|
||||
pub struct ConfSer{
|
||||
path: String,
|
||||
@ -11,11 +11,17 @@ pub struct ConfSer{
|
||||
}
|
||||
|
||||
impl ConfSer {
|
||||
fn new(path: &String, conf_type: &String) -> ConfSer{
|
||||
fn new(path: String, conf_type: &String) -> ConfSer{
|
||||
ConfSer{
|
||||
path: path.clone(),
|
||||
listener: GenericListener::bind(path,
|
||||
GenericListener::string_top_type(conf_type)).unwrap(),
|
||||
listener: match GenericListener::bind(path,
|
||||
GenericListener::string_top_type(conf_type)){
|
||||
Ok(l) => l,
|
||||
Err(e) => {
|
||||
warn!("Error al levantar el servidor de configuraciones:{}",e);
|
||||
panic!("Error al levantar el servidor de configuraciones:{}",e)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -29,8 +35,10 @@ impl Drop for ConfSer {
|
||||
}
|
||||
|
||||
pub fn start(conf: Arc<RwLock<conf::Config>>){
|
||||
let ser = ConfSer::new(conf.read().unwrap().get_port_conf()
|
||||
,conf.read().unwrap().get_conf_type());
|
||||
let ser = ConfSer::new(
|
||||
conf.read().unwrap().get_bindeable_conf(),
|
||||
conf.read().unwrap().get_conf_type(),
|
||||
);
|
||||
|
||||
loop{
|
||||
match ser.listener.accept() {
|
||||
|
@ -10,7 +10,9 @@ use crate::conf;
|
||||
use log::{error, info};
|
||||
|
||||
pub fn start(servers: Arc<RwLock<conf::Config>>){
|
||||
let listener = TcpListener::bind(String::from("0.0.0.0:") + servers.read().unwrap().get_port()).unwrap();
|
||||
let listener = TcpListener::bind(
|
||||
servers.read().unwrap().get_bindeable_ip()
|
||||
).unwrap();
|
||||
let guard = Arc::new(RwLock::new(guard::Guard::new()));
|
||||
for stream in listener.incoming() {
|
||||
if guard.read().unwrap().can_add(){
|
||||
@ -60,3 +62,4 @@ fn conect_server(servers: Arc<RwLock<conf::Config>>,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user