From 230d5d1c82f2eb7fb86cfc357d62bd547dc08879 Mon Sep 17 00:00:00 2001 From: groche97 Date: Sat, 24 Sep 2022 14:26:07 +0200 Subject: [PATCH] add conexion number control --- src/client/guard.rs | 44 +++++++++++++++++++++++++++++++++++++++++++ src/client/mod.rs | 6 ++---- src/conf/mod.rs | 12 +++++------- src/main.rs | 46 ++++++++++++++++++++++++--------------------- src/meson.build | 1 + src/protocol/mod.rs | 4 ++-- 6 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 src/client/guard.rs diff --git a/src/client/guard.rs b/src/client/guard.rs new file mode 100644 index 0000000..be26a11 --- /dev/null +++ b/src/client/guard.rs @@ -0,0 +1,44 @@ +use std::collections::HashMap; +use std::sync::{Arc, RwLock}; +use std::thread; + +const MAX_THREADS : usize = 200; + +pub struct Guard { + cont: Arc>, +} + +impl Guard { + pub fn new() -> Self { + Self{ + //threads: HashMap::new(), + cont: Arc::new(RwLock::new(0)), + } + } + + pub fn can_add(&self) -> bool { + *self.cont.read().unwrap() < MAX_THREADS + } + + pub fn add_thread(&mut self, + threads: (thread::JoinHandle<()>, thread::JoinHandle<()>))-> bool { + if self.can_add() { + //self.threads.insert(id, threads); + *self.cont.write().unwrap() += 1; + let cont: Arc> = self.cont.clone(); + thread::spawn(move || {Self::whatch_client(cont, threads)}); + return true; + } else { + false + } + } + + fn whatch_client(cont: Arc>, + threads: (thread::JoinHandle<()>, thread::JoinHandle<()>)){ + threads.0.join(); + threads.1.join(); + println!("Cliente muerto {}", *cont.read().unwrap()); + *cont.write().unwrap() -= 1; + + } +} diff --git a/src/client/mod.rs b/src/client/mod.rs index a50d589..e2cf67b 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -4,6 +4,7 @@ use std::thread; use std::sync::{Arc, Mutex, RwLock}; use crate::protocol; +pub mod guard; pub struct Client<'a>{ client: Arc>, @@ -17,11 +18,8 @@ impl<'a> Client<'a> { Client { client: Arc::new(Mutex::new(client)), server: Arc::new(Mutex::new(server)), - //client: client, - //server: server, hs: handshake, run: Arc::new(RwLock::new(true)), - //run: &true, } } @@ -32,7 +30,7 @@ impl<'a> Client<'a> { fn join_conexions_mutex(c1: Arc>, c2: Arc>, run: Arc>){ - let mut buf: [u8; 1000000] = [0; 1000000]; + let mut buf: [u8; 100000] = [0; 100000]; let mut client = c1.lock().unwrap().try_clone().unwrap(); while *run.read().unwrap() { let res=client.read(&mut buf); diff --git a/src/conf/mod.rs b/src/conf/mod.rs index 6810f31..ce74e67 100644 --- a/src/conf/mod.rs +++ b/src/conf/mod.rs @@ -11,8 +11,8 @@ pub struct Servers{ } impl Servers { - pub fn new() -> Servers { - Servers{ + pub fn new() -> Self { + Self{ l_servers: Self::get_servers(), } } @@ -23,15 +23,13 @@ impl Servers { let mut ret = HashMap::new(); f.read_to_string(&mut s).unwrap(); let docs = yaml::YamlLoader::load_from_str(&s).unwrap(); - let docs2; - match &docs[0]["servers"] { - yaml::Yaml::Hash(ref h) => docs2 = h, + let docs2 = match &docs[0]["servers"] { + yaml::Yaml::Hash(ref h) => h, _ => return ret, - } + }; for (k, v) in docs2{ - //println!("{}",String::from(doc.as_str().unwrap())); ret.insert(String::from(k.as_str().unwrap()), String::from(v.as_str().unwrap())); } diff --git a/src/main.rs b/src/main.rs index 53fad4d..0aeeb72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use std::net::{TcpListener, TcpStream}; use std::io::prelude::*; +use crate::client::guard; + mod client; mod conf; mod protocol; @@ -8,30 +10,32 @@ fn main() { let listener = TcpListener::bind("127.0.0.1:25567").unwrap(); let mut buf: [u8; 256] = [1; 256]; let servers = conf::Servers::new(); - + let mut guard: guard::Guard = guard::Guard::new(); for stream in listener.incoming() { - match stream { - Ok(mut stream) => { - println!("Go!"); - let leng = stream.read(&mut buf).unwrap(); - let mut hs = protocol::HandShake::new(&mut buf[.. leng]); - if hs.get_raw()[0] < 200 { //Filtra los ping, solo controlamos los handshakes - match servers.get_server(&hs.getHostName()) { - Some(s) => { - hs.replace_port(s.1); - let mut sstream = TcpStream::connect(s.0 + ":" + &s.1.to_string()).unwrap(); - println!("{}",hs.get_port()); - sstream.write(hs.get_raw()); - let c1 = client::Client::new(stream,sstream, hs); - c1.start_proxy(); - }, - None => println!("No server found for{}", hs.getHostName()) + if guard.can_add(){ + match stream { + Ok(mut stream) => { + let leng = stream.read(&mut buf).unwrap(); + let mut hs = protocol::HandShake::new(&mut buf[.. leng]); + if hs.get_raw()[0] < 200 { //Filtra los ping, solo controlamos los handshakes + match servers.get_server(&hs.getHostName()) { + Some(s) => { + hs.replace_port(s.1); + let mut sstream = TcpStream::connect(s.0 + ":" + &s.1.to_string()).unwrap(); + let p_id = sstream.local_addr().unwrap().port(); + println!("port4: {}",sstream.peer_addr().unwrap().port()); + sstream.write(hs.get_raw()); + let c1 = client::Client::new(stream,sstream, hs); + guard.add_thread(c1.start_proxy()); + }, + None => println!("No server found for{}", hs.getHostName()) + } + } + }, - } - }, - - Err(_e) => println!("{}",_e), + Err(_e) => println!("{}",_e), + } } } } diff --git a/src/meson.build b/src/meson.build index 78f464e..e6871b7 100644 --- a/src/meson.build +++ b/src/meson.build @@ -2,6 +2,7 @@ minecraft_proxy_sources = [ cargo_sources, 'main.rs', 'client/mod.rs', + 'client/guard.rs', 'protocol/mod.rs', 'conf/mod.rs', ] diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index d75e13a..8331e8c 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -8,10 +8,10 @@ pub struct HandShake<'a> { } impl<'a> HandShake<'a>{ - pub fn new(data: &'a mut[u8]) -> HandShake { + pub fn new(data: &'a mut[u8]) -> Self { let len_pack = data[0]; let len_dom = data[4]; - HandShake { + Self { len_pack: len_pack, len_dom: len_dom, datagram: data,