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>{
client: Arc<Mutex<TcpStream>>,
server:Arc<Mutex<TcpStream>>,
//client: &'static TcpStream,
//server: &'static TcpStream,
hs: protocol::HandShake<'a>,
run : Arc<RwLock<bool>>,
//run: &'static bool
}
impl<'a> Client<'a> {
@ -32,43 +29,36 @@ impl<'a> Client<'a> {
println!("len_pack {}", self.hs.getHostName());
}
//pub get_hostname(buf &mod)
fn join_conexions_mutex(c1: Arc<Mutex<TcpStream>>,
c2: Arc<Mutex<TcpStream>>,
run: Arc<RwLock<bool>>,
id: i32){
run: Arc<RwLock<bool>>){
let mut buf: [u8; 1000000] = [0; 1000000];
let mut client = c1.lock().unwrap().try_clone().unwrap();
while *run.read().unwrap() {
println!("read{}",id);
let res=client.read(&mut buf);
match res {
Ok(leng) => {
println!("rx {}",leng);
if leng == 0 {
*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;},
}
println!("write{}",id);
}
}
pub fn start_proxy(&self) -> (thread::JoinHandle<()>, thread::JoinHandle<()>) {
//let c = client.client;
let c1 = self.client.clone();
let s1 = self.server.clone();
let r1 = self.run.clone();
let handler1 = thread::spawn( || {
Self::join_conexions_mutex(s1,
c1,
r1,1)}
r1)}
);
let c2 = self.client.clone();
@ -77,20 +67,9 @@ impl<'a> Client<'a> {
let handler2 = thread::spawn( || {
Self::join_conexions_mutex(c2,
s2,
r2,2)}
r2)}
);
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::collections::HashMap;
static file: &str = "mrprox.conf";
static FILE: &str = "mrprox.conf";
pub struct Servers{
l_servers : HashMap<String, String>,
@ -18,7 +18,7 @@ impl Servers {
}
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 ret = HashMap::new();
f.read_to_string(&mut s).unwrap();

View File

@ -10,43 +10,26 @@ fn main() {
let servers = conf::Servers::new();
for stream in listener.incoming() {
//stream.unwrap().write(buf);
match stream {
Ok(mut stream) => {
/*println!("Go!");
stream.write("test succes/n".as_bytes());
stream.read(&mut buf);
stream.flush();*/
println!("Go!");
let leng = stream.read(&mut buf).unwrap();
let mut hs = protocol::HandShake::new(&mut buf[.. leng]);
if hs.get_raw()[0] < 200 {
let mut sstream;
if hs.get_raw()[6]==49 {
//buf[(buf[0]-1) as usize] -= 2;
println!("p0:{}",hs.get_port());
hs.replace_port(servers.get_server(&hs.getHostName()).unwrap().1);
println!("p1:{}",hs.get_port());
sstream = TcpStream::connect("127.0.0.1:25565").unwrap();
} else {
println!("p0:{}",hs.get_port());
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();
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())
}
//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),
}

View File

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