From 276af0ba979bcc4cd43ea75e3877002b9d10ae01 Mon Sep 17 00:00:00 2001 From: groche97 Date: Tue, 23 May 2023 21:56:51 +0200 Subject: [PATCH] start to adding logs --- Cargo.lock | 190 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/client/mod.rs | 4 - src/conf/mod.rs | 12 ++- src/main.rs | 5 +- src/server_conf/conexion.rs | 9 +- src/server_conf/server.rs | 5 +- src/server_proxy.rs | 5 +- 8 files changed, 217 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c93413b..16c790e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,18 +2,55 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + [[package]] name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "indexmap" version = "1.9.1" @@ -30,12 +67,44 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + [[package]] name = "minecraft_proxy" version = "0.1.0" dependencies = [ + "log", "serde", "serde_yaml", + "simple_logger", +] + +[[package]] +name = "num_threads" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" +dependencies = [ + "libc", ] [[package]] @@ -95,6 +164,19 @@ dependencies = [ "unsafe-libyaml", ] +[[package]] +name = "simple_logger" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e78beb34673091ccf96a8816fce8bfd30d1292c7621ca2bcb5f2ba0fae4f558d" +dependencies = [ + "atty", + "colored", + "log", + "time", + "windows-sys", +] + [[package]] name = "syn" version = "1.0.103" @@ -106,6 +188,35 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "time" +version = "0.3.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f3403384eaacbca9923fa06940178ac13e4edb725486d70e8e15881d0c836cc" +dependencies = [ + "itoa", + "libc", + "num_threads", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7300fbefb4dadc1af235a9cef3737cea692a9d97e1b9cbcd4ebdae6f8868e6fb" + +[[package]] +name = "time-macros" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "372950940a5f07bf38dbe211d7283c9e6d7327df53794992d293e534c733d09b" +dependencies = [ + "time-core", +] + [[package]] name = "unicode-ident" version = "1.0.5" @@ -117,3 +228,82 @@ name = "unsafe-libyaml" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" diff --git a/Cargo.toml b/Cargo.toml index 2a22bc1..eb2b9cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" [dependencies] serde = { version = "*", features = ["derive"] } serde_yaml = "*" +log = "*" +simple_logger = "*" [profile.release] lto = true \ No newline at end of file diff --git a/src/client/mod.rs b/src/client/mod.rs index e01842c..268d964 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -24,10 +24,6 @@ impl<'a> Client<'a> { } } - pub fn to_string(&self){ - println!("len_pack {}", self.hs.get_host_name()); - } - fn join_conexions(mut origin: TcpStream, mut dest: TcpStream, run: Arc>){ diff --git a/src/conf/mod.rs b/src/conf/mod.rs index 7ae1c4f..39029e8 100644 --- a/src/conf/mod.rs +++ b/src/conf/mod.rs @@ -3,6 +3,8 @@ use std::fs::File; use std::str::FromStr; use std::io::prelude::*; use std::collections::HashMap; +use log::{error, warn}; + #[cfg(not(debug_assertions))] macro_rules! PATH { () => { "/etc/mrproxy/" } } #[cfg(not(debug_assertions))] @@ -45,8 +47,14 @@ 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 = serde_yaml::from_str(&s1).unwrap(); - let yam_ser: Vec = serde_yaml::from_str(&s2).unwrap(); + let yam_conf: ConfFile = match serde_yaml::from_str(&s1) { + Ok(result) => result, + Err(e) => {error!("Config file malformed"); 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()}, + }; Self{ l_servers: Self::get_servers(&yam_ser), port: yam_conf.port, diff --git a/src/main.rs b/src/main.rs index 1550ae3..af48b3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ use std::thread; //use server_conf::server_conf; use std::sync::{Arc, RwLock}; - +use log::info; +use simple_logger::SimpleLogger; mod client; mod conf; mod server_conf; @@ -9,6 +10,8 @@ mod protocol; mod server_proxy; fn main() { + SimpleLogger::new().init().unwrap(); + info!("start server"); let servers = Arc::new(RwLock::new(conf::Config::new())); let s1 = servers.clone(); let s2 = servers.clone(); diff --git a/src/server_conf/conexion.rs b/src/server_conf/conexion.rs index 767be08..73fa1d5 100644 --- a/src/server_conf/conexion.rs +++ b/src/server_conf/conexion.rs @@ -1,6 +1,7 @@ use std::io::prelude::*; use crate::conf; use std::sync::{Arc, RwLock}; +use log::{error, warn}; const OP_ADD: u8 = 0; const OP_DEL: u8 = 1; @@ -34,7 +35,7 @@ impl Conexion { while !self.exit { match self.stream.read_exact(&mut state){ Ok(len) => self.check_state(state[0]), - Err(e) => {self.exit=true; println!("Fatal: {}", e);}, + Err(e) => {self.exit=true; error!("{}", e);}, } } @@ -73,7 +74,7 @@ impl Conexion { }; self.conf.read().unwrap().flush(); }, - Err(e) => {self.exit=true; println!("e: {}", e);}, + Err(e) => {self.exit=true; warn!("{}", e);}, } }, @@ -87,10 +88,10 @@ impl Conexion { }; self.conf.read().unwrap().flush(); } - Err(e) => {self.exit=true; println!("e: {}", e);}, + Err(e) => {self.exit=true; error!("{}", e);}, }, OP_EXIT => self.exit=true, - _=> println!("no recognice option: {}", state), + _=> warn!("no recognice option: {}", state), } } } diff --git a/src/server_conf/server.rs b/src/server_conf/server.rs index baaef95..b9dacf7 100644 --- a/src/server_conf/server.rs +++ b/src/server_conf/server.rs @@ -3,6 +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}; pub struct ConfSer{ path: String, @@ -23,7 +24,7 @@ impl Drop for ConfSer { fn drop(&mut self) { // There's no way to return a useful error here let _ = std::fs::remove_file(self.path.clone()).unwrap(); - println!("> Dropping {}", self.path); + info!("> Dropping {}", self.path); } } @@ -37,7 +38,7 @@ pub fn start(conf: Arc>){ let c = conf.clone(); thread::spawn(|| Conexion::new(c, stream).process_reques()); } - Err(e) => println!("{}", e) + Err(e) => error!("{}", e) } } diff --git a/src/server_proxy.rs b/src/server_proxy.rs index 5678a6b..da7dfba 100644 --- a/src/server_proxy.rs +++ b/src/server_proxy.rs @@ -7,6 +7,7 @@ use std::thread; use std::time::Duration; use crate::protocol; use crate::conf; +use log::{error, info}; pub fn start(servers: Arc>){ let listener = TcpListener::bind(String::from("0.0.0.0:") + servers.read().unwrap().get_port()).unwrap(); @@ -20,7 +21,7 @@ pub fn start(servers: Arc>){ thread::spawn(|| read_connection(stream, s , g)); }, - Err(_e) => println!("{}",_e), + Err(_e) => error!("{}",_e), } } } @@ -55,7 +56,7 @@ fn conect_server(servers: Arc>, let c1 = client::Client::new(stream,sstream, hs); guard.write().unwrap().add_thread(c1.start_proxy()); }, - None => println!("No server found for {}", hs.get_host_name()) + None => info!("No server found for {}", hs.get_host_name()) } }