Improve log for htt-proxy

This commit is contained in:
Σrebe - Romain GERARD 2024-01-09 13:04:13 +01:00
parent 7d88446453
commit 5226360942
No known key found for this signature in database
GPG key ID: 7A42B4B97E0332F4
3 changed files with 11 additions and 5 deletions

View file

@ -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<LocalToRemote>,

View file

@ -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<u32>) -> 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<String>,
@ -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)
}

View file

@ -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<TransportStream>;
type Error = anyhow::Error;
#[instrument(level = "trace", name = "cnx_server", skip_all)]
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
let (host, port) = &self.remote_addr;
let so_mark = self.socket_so_mark;