Removed logging

This commit is contained in:
2025-08-12 19:15:25 -04:00
parent fb97737a29
commit a70bf816e0
+3 -35
View File
@@ -7,23 +7,16 @@ use crate::route_tcp;
pub fn router() { pub fn router() {
let listener = TcpListener::bind("0.0.0.0:7879").unwrap(); let listener = TcpListener::bind("0.0.0.0:7879").unwrap();
println!("Router started on port 7879");
for stream in listener.incoming() { for stream in listener.incoming() {
match stream { match stream {
Ok(mut client_recv) => { Ok(mut client_recv) => {
std::thread::spawn(|| { std::thread::spawn(|| {
println!(
"Incoming connection from {}",
client_recv.peer_addr().unwrap()
);
let mut address = [0; 6]; let mut address = [0; 6];
let bytes_read = client_recv.read(&mut address).unwrap(); let bytes_read = client_recv.read(&mut address).unwrap();
if bytes_read == 0 { if bytes_read == 0 {
println!("Client disconnected before sending address."); return;
} }
println!("Received target address: {:?}", address);
let address = format!( let address = format!(
"{}.{}.{}.{}:{}", "{}.{}.{}.{}:{}",
@@ -34,30 +27,22 @@ pub fn router() {
u16::from_be_bytes([address[4], address[5]]) u16::from_be_bytes([address[4], address[5]])
); );
println!("Connecting to routed server at {address}");
let mut client_send = client_recv.try_clone().unwrap(); let mut client_send = client_recv.try_clone().unwrap();
let mut server_recv = TcpStream::connect(&address).unwrap(); let mut server_recv = TcpStream::connect(&address).unwrap();
let mut server_send = server_recv.try_clone().unwrap(); let mut server_send = server_recv.try_clone().unwrap();
println!("Connected to routed server at {address}");
// Client -> Server // Client -> Server
std::thread::spawn(move || { std::thread::spawn(move || {
while !route_tcp(&mut client_recv, &mut server_send) {} while !route_tcp(&mut client_recv, &mut server_send) {}
server_send.shutdown(Shutdown::Both).unwrap(); server_send.shutdown(Shutdown::Both).unwrap();
println!("Client -> Server routing stopped");
}); });
// Server -> Client // Server -> Client
while !route_tcp(&mut server_recv, &mut client_send) {} while !route_tcp(&mut server_recv, &mut client_send) {}
client_send.shutdown(Shutdown::Both).unwrap(); client_send.shutdown(Shutdown::Both).unwrap();
println!("Server -> Client routing stopped");
}); });
} }
Err(e) => { Err(_) => {}
println!("Connection failed: {}", e);
}
} }
} }
} }
@@ -65,50 +50,33 @@ pub fn router() {
pub fn run() -> std::io::Result<()> { pub fn run() -> std::io::Result<()> {
let listener = TcpListener::bind("0.0.0.0:7878")?; let listener = TcpListener::bind("0.0.0.0:7878")?;
let mut connections: Vec<[u8; 6]> = Vec::new(); let mut connections: Vec<[u8; 6]> = Vec::new();
println!("Gateway started on port 7878");
std::thread::spawn(router); std::thread::spawn(router);
for stream in listener.incoming() { for stream in listener.incoming() {
match stream { match stream {
Ok(mut stream) => { Ok(mut stream) => {
println!("New client connected from {}", stream.peer_addr().unwrap());
let mut address = [0; 6]; let mut address = [0; 6];
let bytes_read = stream.read(&mut address)?; let bytes_read = stream.read(&mut address)?;
if bytes_read == 0 { if bytes_read == 0 {
println!("Client disconnected before sending data.");
break; break;
} }
println!("Received address: {:?}", address);
if let Some(con) = connections.last() { if let Some(con) = connections.last() {
println!("Sending previous connection address: {:?}", con);
stream.write_all(con)?; stream.write_all(con)?;
} else { } else {
let default_addr = [0, 0, 0, 0, 0x1E, 0xC7]; let default_addr = [0, 0, 0, 0, 0x1E, 0xC7];
println!(
"No previous connections. Sending default address: {:?}",
default_addr
);
stream.write_all(&default_addr)?; stream.write_all(&default_addr)?;
} }
connections.push(address); connections.push(address);
println!(
"Stored new connection. Total connections: {}",
connections.len()
);
while stream.read(&mut []).unwrap() != 0 {} while stream.read(&mut []).unwrap() != 0 {}
println!("Client {} disconnected", stream.peer_addr().unwrap());
stream.shutdown(Shutdown::Both).unwrap(); stream.shutdown(Shutdown::Both).unwrap();
connections.remove(connections.iter().position(|v| v == &address).unwrap()); connections.remove(connections.iter().position(|v| v == &address).unwrap());
} }
Err(e) => { Err(_) => {}
println!("Connection failed: {}", e);
}
} }
} }