Better logging
This commit is contained in:
parent
a33561eeb6
commit
ec13759704
22
src/main.rs
22
src/main.rs
@ -66,8 +66,10 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let (mut client_socket, _) = listener.accept().await?;
|
||||
|
||||
tokio::spawn(async move {
|
||||
let conn_id = rand_str(10);
|
||||
|
||||
log::info!(
|
||||
"Start new connection from {}",
|
||||
"[{conn_id}] Start new connection from {}",
|
||||
client_socket.peer_addr().unwrap()
|
||||
);
|
||||
|
||||
@ -77,7 +79,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
"{}-{}-{}",
|
||||
client_socket.peer_addr().unwrap().ip(),
|
||||
time(),
|
||||
rand_str(10)
|
||||
conn_id
|
||||
);
|
||||
|
||||
let mut req_file = OpenOptions::new()
|
||||
@ -120,13 +122,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
tokio::select! {
|
||||
count = client_read.read(&mut buf_client) => {
|
||||
let count = match count{ Ok(count) => count, Err(e) => {
|
||||
log::error!("Failed to read data from client, closing connection! {e}");
|
||||
log::error!("[{conn_id}] Failed to read data from client, closing connection! {e}");
|
||||
return;
|
||||
}};
|
||||
log::info!("Got a new client read {count} - {base_file_name}");
|
||||
log::info!("[{conn_id}] Got a new client read {count} - {base_file_name}");
|
||||
|
||||
if count == 0 {
|
||||
log::warn!("infinite loop (client)");
|
||||
log::warn!("[{conn_id}] infinite loop (client), closing connection");
|
||||
drop(upstream);
|
||||
return;
|
||||
}
|
||||
@ -150,26 +152,26 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
buf_client[..count].to_vec()
|
||||
};
|
||||
|
||||
upstream.write_all(&buff).await.expect("Failed to write to upstream");
|
||||
req_file.write_all(&buff).expect("Failed to write to req");
|
||||
upstream.write_all(&buff).await.unwrap_or_else(|_| panic!("[{conn_id}] Failed to write to upstream"));
|
||||
req_file.write_all(&buff).unwrap_or_else(|_| panic!("[{conn_id}] Failed to write to req"));
|
||||
}
|
||||
|
||||
count = upstream.read(&mut buf_server) => {
|
||||
let count = match count {
|
||||
Ok(count) => count,
|
||||
Err(e) => {
|
||||
log::error!("Failed to read from upstream! {e}");
|
||||
log::error!("[{conn_id}] Failed to read from upstream! {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
if count == 0 {
|
||||
log::warn!("infinite loop (upstream)");
|
||||
log::warn!("[{conn_id}] infinite loop (upstream), closing connection");
|
||||
drop(upstream);
|
||||
return;
|
||||
}
|
||||
|
||||
log::info!("Got a new upstream read {count} - {base_file_name}");
|
||||
log::info!("[{conn_id}] Got a new upstream read {count} - {base_file_name}");
|
||||
client_write.write_all(&buf_server[..count]).await.expect("Failed to write to client");
|
||||
res_file.write_all(&buf_server[..count]).expect("Failed to write to res");
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user