make usable

This commit is contained in:
Guillermo Roche 2022-09-21 13:30:10 +02:00
parent 91273cc193
commit aeb69bab26
4 changed files with 22 additions and 66 deletions

View File

@ -8,11 +8,8 @@ use crate::protocol;
pub struct Client<'a>{ pub struct Client<'a>{
client: Arc<Mutex<TcpStream>>, client: Arc<Mutex<TcpStream>>,
server:Arc<Mutex<TcpStream>>, server:Arc<Mutex<TcpStream>>,
//client: &'static TcpStream,
//server: &'static TcpStream,
hs: protocol::HandShake<'a>, hs: protocol::HandShake<'a>,
run : Arc<RwLock<bool>>, run : Arc<RwLock<bool>>,
//run: &'static bool
} }
impl<'a> Client<'a> { impl<'a> Client<'a> {
@ -31,44 +28,37 @@ impl<'a> Client<'a> {
pub fn to_string(&self){ pub fn to_string(&self){
println!("len_pack {}", self.hs.getHostName()); println!("len_pack {}", self.hs.getHostName());
} }
//pub get_hostname(buf &mod)
fn join_conexions_mutex(c1: Arc<Mutex<TcpStream>>, fn join_conexions_mutex(c1: Arc<Mutex<TcpStream>>,
c2: Arc<Mutex<TcpStream>>, c2: Arc<Mutex<TcpStream>>,
run: Arc<RwLock<bool>>, run: Arc<RwLock<bool>>){
id: i32){
let mut buf: [u8; 1000000] = [0; 1000000]; let mut buf: [u8; 1000000] = [0; 1000000];
let mut client = c1.lock().unwrap().try_clone().unwrap(); let mut client = c1.lock().unwrap().try_clone().unwrap();
while *run.read().unwrap() { while *run.read().unwrap() {
println!("read{}",id);
let res=client.read(&mut buf); let res=client.read(&mut buf);
match res { match res {
Ok(leng) => { Ok(leng) => {
println!("rx {}",leng);
if leng == 0 { if leng == 0 {
*run.write().unwrap()=false; *run.write().unwrap()=false;
} }
println!("tx {}",c2.lock().unwrap().write(&buf [.. leng]).unwrap()); c2.lock().unwrap().write(&buf [.. leng]);
}, },
//Ok(leng) => {c2.lock().unwrap().write(&buf);},
Err(_e) => {*run.write().unwrap()=false;}, Err(_e) => {*run.write().unwrap()=false;},
} }
println!("write{}",id);
} }
} }
pub fn start_proxy(&self) -> (thread::JoinHandle<()>, thread::JoinHandle<()>) { pub fn start_proxy(&self) -> (thread::JoinHandle<()>, thread::JoinHandle<()>) {
//let c = client.client;
let c1 = self.client.clone(); let c1 = self.client.clone();
let s1 = self.server.clone(); let s1 = self.server.clone();
let r1 = self.run.clone(); let r1 = self.run.clone();
let handler1 = thread::spawn( || { let handler1 = thread::spawn( || {
Self::join_conexions_mutex(s1, Self::join_conexions_mutex(s1,
c1, c1,
r1,1)} r1)}
); );
let c2 = self.client.clone(); let c2 = self.client.clone();
@ -77,20 +67,9 @@ impl<'a> Client<'a> {
let handler2 = thread::spawn( || { let handler2 = thread::spawn( || {
Self::join_conexions_mutex(c2, Self::join_conexions_mutex(c2,
s2, s2,
r2,2)} r2)}
); );
return (handler1, handler2); return (handler1, handler2);
/*let handler = thread::spawn( || {
Self::join_conexions(&mut client.client,
&mut client.server,
&mut client.run)}
);
let handler2 = thread::spawn( || {
Self::join_conexions(&mut client.client,
&mut client.server,
&mut client.run)}
);*/
} }
} }

View File

@ -4,7 +4,7 @@ use std::str::FromStr;
use std::io::prelude::*; use std::io::prelude::*;
use std::collections::HashMap; use std::collections::HashMap;
static file: &str = "mrprox.conf"; static FILE: &str = "mrprox.conf";
pub struct Servers{ pub struct Servers{
l_servers : HashMap<String, String>, l_servers : HashMap<String, String>,
@ -18,7 +18,7 @@ impl Servers {
} }
fn get_servers() -> HashMap<String, String> { fn get_servers() -> HashMap<String, String> {
let mut f = File::open(&file).unwrap(); let mut f = File::open(&FILE).unwrap();
let mut s = String::new(); let mut s = String::new();
let mut ret = HashMap::new(); let mut ret = HashMap::new();
f.read_to_string(&mut s).unwrap(); f.read_to_string(&mut s).unwrap();

View File

@ -10,43 +10,26 @@ fn main() {
let servers = conf::Servers::new(); let servers = conf::Servers::new();
for stream in listener.incoming() { for stream in listener.incoming() {
//stream.unwrap().write(buf);
match stream { match stream {
Ok(mut stream) => { Ok(mut stream) => {
/*println!("Go!");
stream.write("test succes/n".as_bytes());
stream.read(&mut buf);
stream.flush();*/
println!("Go!"); println!("Go!");
let leng = stream.read(&mut buf).unwrap(); let leng = stream.read(&mut buf).unwrap();
let mut hs = protocol::HandShake::new(&mut buf[.. leng]); let mut hs = protocol::HandShake::new(&mut buf[.. leng]);
if hs.get_raw()[0] < 200 { if hs.get_raw()[0] < 200 { //Filtra los ping, solo controlamos los handshakes
let mut sstream; match servers.get_server(&hs.getHostName()) {
Some(s) => {
if hs.get_raw()[6]==49 { hs.replace_port(s.1);
//buf[(buf[0]-1) as usize] -= 2; let mut sstream = TcpStream::connect(s.0 + ":" + &s.1.to_string()).unwrap();
println!("p0:{}",hs.get_port()); println!("{}",hs.get_port());
hs.replace_port(servers.get_server(&hs.getHostName()).unwrap().1); sstream.write(hs.get_raw());
println!("p1:{}",hs.get_port()); let c1 = client::Client::new(stream,sstream, hs);
sstream = TcpStream::connect("127.0.0.1:25565").unwrap(); c1.start_proxy();
} else { },
println!("p0:{}",hs.get_port()); None => println!("No server found for{}", hs.getHostName())
match servers.get_server(&hs.getHostName()) {
Some(s) => hs.replace_port(s.1),
None => hs.replace_port(25565),
}
println!("p1:{}",hs.get_port());
sstream = TcpStream::connect("127.0.0.1:25566").unwrap();
} }
//let mut sstream = TcpStream::connect("127.0.0.1:25565").unwrap();
sstream.write(hs.get_raw());
let c1 = client::Client::new(stream,sstream, hs);
c1.to_string();
c1.start_proxy();
} }
//c1.start_proxy().0.join(); },
//c1.start_proxy().1.join();
},
Err(_e) => println!("{}",_e), Err(_e) => println!("{}",_e),
} }

View File

@ -5,23 +5,17 @@ pub struct HandShake<'a> {
len_dom: u8, len_dom: u8,
port_pos: usize, port_pos: usize,
datagram: &'a mut [u8], datagram: &'a mut [u8],
//host: &'a [u8],
//port: &'a mut [u8],
} }
impl<'a> HandShake<'a>{ impl<'a> HandShake<'a>{
pub fn new(data: &'a mut[u8]) -> HandShake { pub fn new(data: &'a mut[u8]) -> HandShake {
let len_pack = data[0]; let len_pack = data[0];
println!("{}", len_pack);
let len_dom = data[4]; let len_dom = data[4];
println!("{}", len_dom);
HandShake { HandShake {
len_pack: len_pack, len_pack: len_pack,
len_dom: len_dom, len_dom: len_dom,
datagram: data, datagram: data,
port_pos: (len_pack - 2) as usize, port_pos: (len_pack - 2) as usize,
//host: (&mut data[5 .. ((len_dom + 5) as usize)]),
//port: (&mut data[((len_pack - 2) as usize) .. ((len_pack - 1) as usize)]),
} }
} }
@ -36,8 +30,8 @@ impl<'a> HandShake<'a>{
pub fn replace_port(&mut self, n_port: u16){ pub fn replace_port(&mut self, n_port: u16){
let s_port = n_port.to_ne_bytes(); let s_port = n_port.to_ne_bytes();
self.datagram[self.port_pos]=s_port[0]; self.datagram[self.port_pos]=s_port[1];
self.datagram[self.port_pos+1]=s_port[1]; self.datagram[self.port_pos+1]=s_port[0];
} }