end server config parametrization
This commit is contained in:
parent
930a1a3692
commit
06816a16ed
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user