functional server config
This commit is contained in:
parent
d992981f22
commit
8991a7fbf0
@ -3,9 +3,9 @@ use std::fs::File;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
pub mod server_conf;
|
|
||||||
|
|
||||||
static FILE: &str = "mrprox.conf";
|
static FILE: &str = "mrprox.conf";
|
||||||
|
const DEF_PORT: u16 = 25565;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||||
pub struct ConfFile{
|
pub struct ConfFile{
|
||||||
@ -16,8 +16,8 @@ pub struct ConfFile{
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
|
||||||
pub struct ServerData{
|
pub struct ServerData{
|
||||||
domain: String,
|
pub domain: String,
|
||||||
ip: String,
|
pub ip: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Config{
|
pub struct Config{
|
||||||
@ -39,8 +39,11 @@ impl Config {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, server: ServerData){
|
pub fn add(&mut self, server: ServerData) -> bool{
|
||||||
self.l_servers.insert(server.domain, server.ip);
|
match self.l_servers.insert(server.domain, server.ip){
|
||||||
|
Some(_s) => true, //Modified instead of added
|
||||||
|
None => false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn del(&mut self, domain: String) -> bool {
|
pub fn del(&mut self, domain: String) -> bool {
|
||||||
@ -57,7 +60,9 @@ impl Config {
|
|||||||
servers: Vec::from_iter(self.l_servers.iter()
|
servers: Vec::from_iter(self.l_servers.iter()
|
||||||
.map(|(x, y)| ServerData{domain: (*x).clone(), ip: (*y).clone()})),
|
.map(|(x, y)| ServerData{domain: (*x).clone(), ip: (*y).clone()})),
|
||||||
};
|
};
|
||||||
println!("{}", serde_yaml::to_string(&conf).unwrap());
|
let mut file = File::create(FILE).unwrap();
|
||||||
|
file.write(&serde_yaml::to_string(&conf).unwrap().into_bytes());
|
||||||
|
file.flush();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,25 +74,26 @@ impl Config {
|
|||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_host(&self, host: &String) -> Option<(&String, &String)>{
|
pub fn get_host(&self, host: &String) -> Option<(&String, &String)>{
|
||||||
self.l_servers.get_key_value(host)
|
self.l_servers.get_key_value(host)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_server(&self, host: &String) -> Option<(String, u16)>{
|
pub fn get_server(&self, host: &String) -> Option<(String, u16)>{
|
||||||
let raw: Vec<&str>;
|
let raw: Vec<&str> =
|
||||||
match self.get_host(host){
|
match self.get_host(host){
|
||||||
Some(h) => raw=h.1.split(":").collect(),
|
Some(h) => h.1.split(":").collect(),
|
||||||
None => return None,
|
None => return None,
|
||||||
}
|
};
|
||||||
|
|
||||||
if raw.len() == 2 {
|
match raw.len() {
|
||||||
|
1 => Some((String::from(raw[0]),DEF_PORT)),
|
||||||
match FromStr::from_str(raw[1]) {
|
2 => {
|
||||||
Ok(p) => return Some((String::from(raw[0]),p)),
|
match FromStr::from_str(raw[1]) {
|
||||||
Err(_e) => return None,
|
Ok(p) => Some((String::from(raw[0]),p)),
|
||||||
}
|
Err(_e) => None,
|
||||||
} else {
|
}
|
||||||
return None;
|
},
|
||||||
|
_=> None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,92 +0,0 @@
|
|||||||
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 ser = ConfSer::new(String::from("mineproxy"));
|
|
||||||
|
|
||||||
for stream in ser.listener.incoming() {
|
|
||||||
match stream{
|
|
||||||
Ok(s) => {
|
|
||||||
ConfCon{
|
|
||||||
conf: conf.clone(),
|
|
||||||
stream: s,
|
|
||||||
buf: [1; 256],
|
|
||||||
}.process_reques();
|
|
||||||
},
|
|
||||||
Err(_e) => println!("{}",_e),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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" => {
|
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
use conf::server_conf;
|
//use server_conf::server_conf;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
mod client;
|
mod client;
|
||||||
mod conf;
|
mod conf;
|
||||||
|
mod server_conf;
|
||||||
mod protocol;
|
mod protocol;
|
||||||
mod server_proxy;
|
mod server_proxy;
|
||||||
|
|
||||||
@ -11,8 +12,8 @@ fn main() {
|
|||||||
let servers = Arc::new(RwLock::new(conf::Config::new()));
|
let servers = Arc::new(RwLock::new(conf::Config::new()));
|
||||||
let s1 = servers.clone();
|
let s1 = servers.clone();
|
||||||
let s2 = servers.clone();
|
let s2 = servers.clone();
|
||||||
let stop1 = thread::spawn(|| server_proxy::start_proxy(s1));
|
let stop1 = thread::spawn(|| server_proxy::start(s1));
|
||||||
let stop2 = thread::spawn(|| conf::server_conf::server_conf(s2));
|
let stop2 = thread::spawn(|| server_conf::server::start(s2));
|
||||||
stop1.join();
|
stop1.join();
|
||||||
stop2.join();
|
stop2.join();
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,9 @@ minecraft_proxy_sources = [
|
|||||||
'client/guard.rs',
|
'client/guard.rs',
|
||||||
'protocol/mod.rs',
|
'protocol/mod.rs',
|
||||||
'conf/mod.rs',
|
'conf/mod.rs',
|
||||||
'conf/server_conf.rs',
|
'server_conf/mod.rs',
|
||||||
|
'server_conf/server.rs',
|
||||||
|
'server_conf/conexion.rs',
|
||||||
'server_proxy.rs',
|
'server_proxy.rs',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
101
src/server_conf/conexion.rs
Normal file
101
src/server_conf/conexion.rs
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
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};
|
||||||
|
|
||||||
|
const OP_ADD: u8 = 0;
|
||||||
|
const OP_DEL: u8 = 1;
|
||||||
|
const OP_EXIT: u8 = 2;
|
||||||
|
|
||||||
|
const RE_ADD: u8 = 0;
|
||||||
|
const RE_MOD: u8 = 1;
|
||||||
|
|
||||||
|
const RE_OK: u8 = 0;
|
||||||
|
const RE_KO: u8 = 1;
|
||||||
|
|
||||||
|
|
||||||
|
pub struct Conexion {
|
||||||
|
conf: Arc<RwLock<conf::Config>>,
|
||||||
|
stream: UnixStream,
|
||||||
|
buf:[u8; 256],
|
||||||
|
exit: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Conexion {
|
||||||
|
pub fn new(conf: Arc<RwLock<conf::Config>>, stream: UnixStream) -> Self {
|
||||||
|
Self{
|
||||||
|
conf: conf,
|
||||||
|
stream: stream,
|
||||||
|
buf: [1; 256],
|
||||||
|
exit: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process_reques(&mut self) {
|
||||||
|
let mut state: [u8; 1] = [1; 1];
|
||||||
|
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);},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_domain_info(&mut self) ->
|
||||||
|
Result<conf::ServerData, std::io::Error>{
|
||||||
|
let mut len = self.stream.read(&mut self.buf)?;
|
||||||
|
let end_domain = (self.buf[0] + 1) as usize;
|
||||||
|
let start_ip = end_domain + 1 as usize;
|
||||||
|
let end_ip = self.buf[end_domain] as usize + start_ip;
|
||||||
|
let domain = String::from_utf8(self.buf[1 .. end_domain].to_vec()).unwrap();
|
||||||
|
let ip = String::from_utf8(self.buf[start_ip .. end_ip].to_vec()).unwrap();
|
||||||
|
Ok(conf::ServerData{
|
||||||
|
domain: domain,
|
||||||
|
ip: ip,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_unique_info(&mut self) ->
|
||||||
|
Result<String, std::io::Error>{
|
||||||
|
self.stream.read_exact(&mut self.buf[0 .. 1])?;
|
||||||
|
let len = self.buf[0] as usize;
|
||||||
|
self.stream.read_exact(&mut self.buf[0 .. len])?;
|
||||||
|
Ok(String::from_utf8(self.buf[0 .. len].to_vec()).unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_state(&mut self, state: u8) {
|
||||||
|
match state{
|
||||||
|
OP_ADD => {
|
||||||
|
match self.read_domain_info() {
|
||||||
|
Ok(sd) => {
|
||||||
|
match self.conf.write().unwrap().add(sd) {
|
||||||
|
true => self.stream.write(&[RE_MOD]),
|
||||||
|
false => self.stream.write(&[RE_ADD]),
|
||||||
|
};
|
||||||
|
self.conf.read().unwrap().flush();
|
||||||
|
},
|
||||||
|
Err(e) => {self.exit=true; println!("e: {}", e);},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
OP_DEL => match self.read_unique_info(){
|
||||||
|
Ok(domain) => {
|
||||||
|
match self.conf.write().unwrap().del(domain){
|
||||||
|
true => {
|
||||||
|
self.stream.write(&[RE_OK]);
|
||||||
|
},
|
||||||
|
false => {self.stream.write(&[RE_KO]);},
|
||||||
|
};
|
||||||
|
self.conf.read().unwrap().flush();
|
||||||
|
}
|
||||||
|
Err(e) => {self.exit=true; println!("e: {}", e);},
|
||||||
|
},
|
||||||
|
OP_EXIT => self.exit=true,
|
||||||
|
_=> println!("no recognice option: {}", state),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2
src/server_conf/mod.rs
Normal file
2
src/server_conf/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod server;
|
||||||
|
pub mod conexion;
|
45
src/server_conf/server.rs
Normal file
45
src/server_conf/server.rs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
use std::net::{TcpListener, TcpStream};
|
||||||
|
use std::os::unix::net::{UnixListener, UnixStream};
|
||||||
|
use std::io::prelude::*;
|
||||||
|
use std::result;
|
||||||
|
use crate::conf;
|
||||||
|
use crate::server_conf::conexion::Conexion;
|
||||||
|
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 start(conf: Arc<RwLock<conf::Config>>){
|
||||||
|
let ser = ConfSer::new(String::from("mineproxy"));
|
||||||
|
|
||||||
|
for stream in ser.listener.incoming() {
|
||||||
|
match stream{
|
||||||
|
Ok(s) => {
|
||||||
|
Conexion::new(conf.clone(), s).process_reques();
|
||||||
|
},
|
||||||
|
Err(_e) => println!("{}",_e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -8,7 +8,7 @@ use std::time::Duration;
|
|||||||
use crate::protocol;
|
use crate::protocol;
|
||||||
use crate::conf;
|
use crate::conf;
|
||||||
|
|
||||||
pub fn start_proxy(servers: Arc<RwLock<conf::Config>>){
|
pub fn start(servers: Arc<RwLock<conf::Config>>){
|
||||||
let listener = TcpListener::bind(String::from("0.0.0.0:") + servers.read().unwrap().get_port()).unwrap();
|
let listener = TcpListener::bind(String::from("0.0.0.0:") + servers.read().unwrap().get_port()).unwrap();
|
||||||
let guard = Arc::new(RwLock::new(guard::Guard::new()));
|
let guard = Arc::new(RwLock::new(guard::Guard::new()));
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
|
Loading…
Reference in New Issue
Block a user