From 0be47b8b88bfa1c21937d197118241d989e6561a Mon Sep 17 00:00:00 2001 From: groche97 Date: Sat, 3 Jun 2023 13:20:56 +0200 Subject: [PATCH] Refactor config servers first read to fix docker deploy --- Dockerfile | 3 ++- config/mrprox.conf | 2 +- src/conf/mod.rs | 57 +++++++++++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/Dockerfile b/Dockerfile index ac9c72b..906145a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,4 +7,5 @@ COPY src /opt/mrproxy/src COPY config/* /etc/mrproxy WORKDIR /opt/mrproxy RUN apk add cargo -RUN cargo run --release +RUN cargo build --release +CMD /opt/mrproxy/target/release/minecraft_proxy diff --git a/config/mrprox.conf b/config/mrprox.conf index 6ea4521..200f8d2 100644 --- a/config/mrprox.conf +++ b/config/mrprox.conf @@ -5,4 +5,4 @@ conf: sock_type: 'TCP' #path: '/home/roche/portmrproxy' port: '25564' - ip: '127.0.0.1' + ip: '0.0.0.0' diff --git a/src/conf/mod.rs b/src/conf/mod.rs index d88519d..c8f1da9 100644 --- a/src/conf/mod.rs +++ b/src/conf/mod.rs @@ -50,27 +50,48 @@ pub struct Config{ conf: ConfServer, } +pub fn generate_conf_from_file() -> ConfFile{ + match File::open(&FILE_CONF) { + Ok(mut f) => { + let mut s = String::new(); + f.read_to_string(&mut s).unwrap(); + match serde_yaml::from_str(&s) { + Ok(result) => result, + Err(e) => {error!("Config file malformed: {}",e); panic!("{}", e);}, + } + } + Err(e) => {error!("Error open config file: {}",e); panic!("{}",e)} + } +} + +pub fn generate_slist_from_file() -> Vec{ + match File::open(&FILE_SERVERS){ + Ok(mut f) => { + let mut s = String::new(); + f.read_to_string(&mut s).unwrap(); + match serde_yaml::from_str(&s) { + Ok(result) => result, + Err(e) => { + warn!("IPs list file malformed; creating new one\nerror: {}",e); + Vec::new() + }, + } + } + Err(e) => { + warn!("IPs list file can't be open; creating new one \nerror: {}",e); + Vec::new() + }, + } +} + impl Config { pub fn new() -> Self { - 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 = match serde_yaml::from_str(&s1) { - Ok(result) => result, - Err(e) => {error!("Config file malformed: {}",e); panic!("{}", e);}, - }; - let yam_ser: Vec = match serde_yaml::from_str(&s2) { - Ok(result) => result, - Err(e) => {warn!("IPs list file malformed; creating new one"); Vec::new()}, - }; + let yaml_conf = generate_conf_from_file(); Self{ - l_servers: Self::get_servers(&yam_ser), - port: yam_conf.port, - ip: yam_conf.ip, - conf: yam_conf.conf, + l_servers: Self::get_servers(&generate_slist_from_file()), + port: yaml_conf.port, + ip: yaml_conf.ip, + conf: yaml_conf.conf, } }