From 522636094209333449aeddcef4e5ee74de323483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=A3rebe=20-=20Romain=20GERARD?= Date: Tue, 9 Jan 2024 13:04:13 +0100 Subject: [PATCH] Improve log for htt-proxy --- src/main.rs | 2 +- src/tcp.rs | 12 ++++++++---- src/tunnel/mod.rs | 2 ++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index c461d61..0e5b404 100644 --- a/src/main.rs +++ b/src/main.rs @@ -108,7 +108,7 @@ struct Client { /// examples: /// 'tcp://1212:google.com:443' => listen on server for incoming tcp cnx on port 1212 and forward to google.com on port 443 from local machine /// 'udp://1212:1.1.1.1:53' => listen on server for incoming udp on port 1212 and forward to cloudflare dns 1.1.1.1 on port 53 from local machine - /// 'socks://[::1]:1212' => listen on server for incoming socks5 request on port 1212 and forward dynamically request from local machine + /// 'socks5://[::1]:1212' => listen on server for incoming socks5 request on port 1212 and forward dynamically request from local machine #[arg(short='R', long, value_name = "{tcp,udp,socks5}://[BIND:]PORT:HOST:PORT", value_parser = parse_tunnel_arg, verbatim_doc_comment)] remote_to_local: Vec, diff --git a/src/tcp.rs b/src/tcp.rs index 64af98c..786c6da 100644 --- a/src/tcp.rs +++ b/src/tcp.rs @@ -11,8 +11,8 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::net::{TcpListener, TcpSocket, TcpStream}; use tokio::time::timeout; use tokio_stream::wrappers::TcpListenerStream; -use tracing::debug; use tracing::log::info; +use tracing::{debug, instrument}; use url::{Host, Url}; fn configure_socket(socket: &mut TcpSocket, so_mark: &Option) -> Result<(), anyhow::Error> { @@ -58,7 +58,7 @@ pub async fn connect( let mut cnx = None; let mut last_err = None; for addr in socket_addrs { - debug!("connecting to {}", addr); + debug!("Connecting to {}", addr); let mut socket = match &addr { SocketAddr::V4(_) => TcpSocket::new_v4()?, @@ -96,6 +96,7 @@ pub async fn connect( } } +#[instrument(level = "info", name = "http_proxy", skip_all)] pub async fn connect_with_http_proxy( proxy: &Url, host: &Host, @@ -107,8 +108,9 @@ pub async fn connect_with_http_proxy( let proxy_host = proxy.host().context("Cannot parse proxy host")?.to_owned(); let proxy_port = proxy.port_or_known_default().unwrap_or(80); + info!("Connecting to http proxy {}:{}", proxy_host, proxy_port); let mut socket = connect(&proxy_host, proxy_port, so_mark, connect_timeout, dns_resolver).await?; - info!("Connected to http proxy {}:{}", proxy_host, proxy_port); + debug!("Connected to http proxy {}", socket.peer_addr().unwrap()); let authorization = if let Some((user, password)) = proxy.password().map(|p| (proxy.username(), p)) { let user = urlencoding::decode(user).with_context(|| format!("Cannot urldecode proxy user: {}", user))?; @@ -121,6 +123,7 @@ pub async fn connect_with_http_proxy( }; let connect_request = format!("CONNECT {host}:{port} HTTP/1.0\r\nHost: {host}:{port}\r\n{authorization}\r\n"); + debug!("Sending request:\n{}", connect_request); socket.write_all(connect_request.as_bytes()).await?; let mut buf = BytesMut::with_capacity(1024); @@ -163,7 +166,8 @@ pub async fn connect_with_http_proxy( )); } - info!("http proxy connected to remote host {}:{}", host, port); + debug!("Got response from proxy:\n{}", String::from_utf8_lossy(&buf)); + info!("Http proxy accepted connection to remote host {}:{}", host, port); Ok(socket) } diff --git a/src/tunnel/mod.rs b/src/tunnel/mod.rs index b235063..37ca84d 100644 --- a/src/tunnel/mod.rs +++ b/src/tunnel/mod.rs @@ -18,6 +18,7 @@ use std::task::{Context, Poll}; use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; use tokio::net::TcpStream; use tokio_rustls::client::TlsStream; +use tracing::instrument; use url::Host; use uuid::Uuid; @@ -134,6 +135,7 @@ impl ManageConnection for WsClientConfig { type Connection = Option; type Error = anyhow::Error; + #[instrument(level = "trace", name = "cnx_server", skip_all)] async fn connect(&self) -> Result { let (host, port) = &self.remote_addr; let so_mark = self.socket_so_mark;