Skip to content

Commit

Permalink
Improve log for htt-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
erebe committed Jan 9, 2024
1 parent 7d88446 commit 5226360
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,

Expand Down
12 changes: 8 additions & 4 deletions src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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()?,
Expand Down Expand Up @@ -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>,
Expand All @@ -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))?;
Expand All @@ -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);
Expand Down Expand Up @@ -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)
}

Expand Down
2 changes: 2 additions & 0 deletions src/tunnel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5226360

Please sign in to comment.