conf server reestructure

This commit is contained in:
Guillermo Roche 2022-11-30 21:06:19 +01:00
parent ca43b2d75c
commit d992981f22
4 changed files with 112 additions and 61 deletions

16
Cargo.lock generated
View File

@ -30,19 +30,12 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "minecraft_proxy"
version = "0.1.0"
dependencies = [
"serde",
"serde_yaml",
"yaml-rust",
]
[[package]]
@ -124,12 +117,3 @@ name = "unsafe-libyaml"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68"
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]

View File

@ -4,6 +4,5 @@ version = "0.1.0"
edition = "2021"
[dependencies]
yaml-rust = "*"
serde = { version = "*", features = ["derive"] }
serde_yaml = "*"

View File

@ -1,4 +1,3 @@
use yaml_rust::yaml;
use serde::{Serialize, Deserialize};
use std::fs::File;
use std::str::FromStr;
@ -8,9 +7,21 @@ pub mod server_conf;
static FILE: &str = "mrprox.conf";
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct ConfFile{
port: String,
port_conf: String,
servers: Vec<ServerData>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub struct ServerData{
domain: String,
ip: String,
}
pub struct Config{
l_servers : HashMap<String, String>,
file: Vec<yaml::Yaml>,
port: String,
port_conf: String,
}
@ -20,40 +31,40 @@ impl Config {
let mut file = File::open(&FILE).unwrap();
let mut s = String::new();
file.read_to_string(&mut s).unwrap();
let yam = yaml::YamlLoader::load_from_str(&s).unwrap();
let yam: ConfFile = serde_yaml::from_str(&s).unwrap();
Self{
l_servers: Self::get_servers(&yam),
port: Self::get_port_f(&yam),
port_conf: Self::get_conf_port_f(&yam),
file: yam,
l_servers: Self::get_servers(&yam.servers),
port: yam.port,
port_conf: yam.port_conf,
}
}
fn get_port_f(file: &Vec<yaml::Yaml>) -> String {
match file[0]["port"].as_str() {
Some(h) => String::from(h),
_ => String::from("25565"),
pub fn add(&mut self, server: ServerData){
self.l_servers.insert(server.domain, server.ip);
}
pub fn del(&mut self, domain: String) -> bool {
match self.l_servers.remove(&domain) {
Some(_s) => true,
None => false,
}
}
fn get_conf_port_f(file: &Vec<yaml::Yaml>) -> String {
match file[0]["port_conf"].as_str() {
Some(h) => String::from(h),
_ => String::from("25565"),
}
}
fn get_servers(file: &Vec<yaml::Yaml>) -> HashMap<String, String> {
let mut ret = HashMap::new();
let docs2 = match file[0]["servers"] {
yaml::Yaml::Hash(ref h) => h,
_ => return ret,
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()})),
};
println!("{}", serde_yaml::to_string(&conf).unwrap());
}
for (k, v) in docs2{
ret.insert(String::from(k.as_str().unwrap()),
String::from(v.as_str().unwrap()));
fn get_servers(file: &Vec<ServerData>) -> HashMap<String, String> {
let mut ret = HashMap::new();
for j in file{
ret.insert(j.domain.clone(),j.ip.clone());
}
ret
}
@ -88,3 +99,4 @@ impl Config {
&self.port_conf
}
}

View File

@ -1,36 +1,92 @@
use std::net::{TcpListener, TcpStream};
use std::os::unix::net::{UnixListener, UnixStream};
use std::io::prelude::*;
use std::result;
use crate::conf;
use std::sync::{Arc, RwLock};
use std::path::{Path, PathBuf};
pub struct ConfSer{
path: String,
listener: UnixListener,
}
impl ConfSer {
fn new(path: String) -> ConfSer{
ConfSer{
path: path.clone(),
listener: UnixListener::bind(path).unwrap(),
}
}
}
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);
}
}
pub fn server_conf(conf: Arc<RwLock<conf::Config>>){
let listener = UnixListener::bind("mineproxy").unwrap();
//let listener = UnixListener::bind("mineproxy").unwrap();
let ser = ConfSer::new(String::from("mineproxy"));
for stream in listener.incoming() {
for stream in ser.listener.incoming() {
match stream{
Ok(s) => process_reques(s, conf.clone()),
Ok(s) => {
ConfCon{
conf: conf.clone(),
stream: s,
buf: [1; 256],
}.process_reques();
},
Err(_e) => println!("{}",_e),
}
}
}
fn process_reques(mut stream: UnixStream, conf: Arc<RwLock<conf::Config>>) {
let mut buf: [u8; 256] = [1; 256];
match stream.read(&mut buf){
Ok(len) => check_state(&mut buf, len, conf),
pub struct ConfCon {
conf: Arc<RwLock<conf::Config>>,
stream: UnixStream,
buf:[u8; 256],
}
impl ConfCon {
fn process_reques(&mut self) {
//let mut buf: [u8; 256] = [1; 256];
match self.stream.read(&mut self.buf){
Ok(len) => self.check_state(len),
Err(e) => println!("Fatal: {}", e),
}
}
fn check_state(buf: &mut [u8; 256], len: usize, conf: Arc<RwLock<conf::Config>>) {
let var = String::from_utf8(buf[0 .. len].to_vec()).unwrap();
fn read_domain_info(&mut self) ->
Result<conf::ServerData, std::io::Error>{
let mut len = self.stream.read(&mut self.buf)?;
let domain = String::from_utf8(self.buf[0 .. len].to_vec()).unwrap();
len = self.stream.read(&mut self.buf)?;
let ip = String::from_utf8(self.buf[0 .. len].to_vec()).unwrap();
Ok(conf::ServerData{
domain: domain,
ip: ip,
})
}
fn check_state(&mut self, len: usize) {
let var = String::from_utf8(self.buf[0 .. len].to_vec()).unwrap();
match var.as_str(){
"add" => println!("entra"),
"add" => {
match self.read_domain_info() {
Ok(sd) => println!("domain {}",sd.ip),
Err(e) => println!("e: {}",e),
}
},
"dell" => println!("del"),
"mod" => println!("mod"),
_=> println!("no recognice option: {}", var),
}
}
}