conf server reestructure
This commit is contained in:
parent
ca43b2d75c
commit
d992981f22
16
Cargo.lock
generated
16
Cargo.lock
generated
@ -30,19 +30,12 @@ version = "1.0.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
|
checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "linked-hash-map"
|
|
||||||
version = "0.5.6"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "minecraft_proxy"
|
name = "minecraft_proxy"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
"serde_yaml",
|
"serde_yaml",
|
||||||
"yaml-rust",
|
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -124,12 +117,3 @@ name = "unsafe-libyaml"
|
|||||||
version = "0.2.4"
|
version = "0.2.4"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68"
|
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",
|
|
||||||
]
|
|
||||||
|
@ -4,6 +4,5 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
yaml-rust = "*"
|
|
||||||
serde = { version = "*", features = ["derive"] }
|
serde = { version = "*", features = ["derive"] }
|
||||||
serde_yaml = "*"
|
serde_yaml = "*"
|
@ -1,4 +1,3 @@
|
|||||||
use yaml_rust::yaml;
|
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
@ -8,9 +7,21 @@ pub mod server_conf;
|
|||||||
|
|
||||||
static FILE: &str = "mrprox.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{
|
pub struct Config{
|
||||||
l_servers : HashMap<String, String>,
|
l_servers : HashMap<String, String>,
|
||||||
file: Vec<yaml::Yaml>,
|
|
||||||
port: String,
|
port: String,
|
||||||
port_conf: String,
|
port_conf: String,
|
||||||
}
|
}
|
||||||
@ -20,40 +31,40 @@ impl Config {
|
|||||||
let mut file = File::open(&FILE).unwrap();
|
let mut file = File::open(&FILE).unwrap();
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
file.read_to_string(&mut s).unwrap();
|
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{
|
Self{
|
||||||
l_servers: Self::get_servers(&yam),
|
l_servers: Self::get_servers(&yam.servers),
|
||||||
port: Self::get_port_f(&yam),
|
port: yam.port,
|
||||||
port_conf: Self::get_conf_port_f(&yam),
|
port_conf: yam.port_conf,
|
||||||
file: yam,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_port_f(file: &Vec<yaml::Yaml>) -> String {
|
pub fn add(&mut self, server: ServerData){
|
||||||
match file[0]["port"].as_str() {
|
self.l_servers.insert(server.domain, server.ip);
|
||||||
Some(h) => String::from(h),
|
}
|
||||||
_ => String::from("25565"),
|
|
||||||
|
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 {
|
pub fn flush(&self){
|
||||||
match file[0]["port_conf"].as_str() {
|
let conf = ConfFile {
|
||||||
Some(h) => String::from(h),
|
port: self.port.clone(),
|
||||||
_ => String::from("25565"),
|
port_conf: self.port_conf.clone(),
|
||||||
}
|
servers: Vec::from_iter(self.l_servers.iter()
|
||||||
}
|
.map(|(x, y)| ServerData{domain: (*x).clone(), ip: (*y).clone()})),
|
||||||
|
|
||||||
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,
|
|
||||||
};
|
};
|
||||||
|
println!("{}", serde_yaml::to_string(&conf).unwrap());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
for (k, v) in docs2{
|
fn get_servers(file: &Vec<ServerData>) -> HashMap<String, String> {
|
||||||
ret.insert(String::from(k.as_str().unwrap()),
|
let mut ret = HashMap::new();
|
||||||
String::from(v.as_str().unwrap()));
|
for j in file{
|
||||||
|
ret.insert(j.domain.clone(),j.ip.clone());
|
||||||
}
|
}
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
@ -88,3 +99,4 @@ impl Config {
|
|||||||
&self.port_conf
|
&self.port_conf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,36 +1,92 @@
|
|||||||
use std::net::{TcpListener, TcpStream};
|
use std::net::{TcpListener, TcpStream};
|
||||||
use std::os::unix::net::{UnixListener, UnixStream};
|
use std::os::unix::net::{UnixListener, UnixStream};
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
use std::result;
|
||||||
use crate::conf;
|
use crate::conf;
|
||||||
use std::sync::{Arc, RwLock};
|
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>>){
|
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 ser.listener.incoming() {
|
||||||
for stream in listener.incoming() {
|
|
||||||
match stream{
|
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),
|
Err(_e) => println!("{}",_e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn process_reques(mut stream: UnixStream, conf: Arc<RwLock<conf::Config>>) {
|
pub struct ConfCon {
|
||||||
let mut buf: [u8; 256] = [1; 256];
|
conf: Arc<RwLock<conf::Config>>,
|
||||||
match stream.read(&mut buf){
|
stream: UnixStream,
|
||||||
Ok(len) => check_state(&mut buf, len, conf),
|
buf:[u8; 256],
|
||||||
Err(e) => println!("Fatal: {}", e),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_state(buf: &mut [u8; 256], len: usize, conf: Arc<RwLock<conf::Config>>) {
|
impl ConfCon {
|
||||||
let var = String::from_utf8(buf[0 .. len].to_vec()).unwrap();
|
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 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(){
|
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"),
|
"dell" => println!("del"),
|
||||||
"mod" => println!("mod"),
|
"mod" => println!("mod"),
|
||||||
_=> println!("no recognice option: {}", var),
|
_=> println!("no recognice option: {}", var),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user