Skip to content

Commit

Permalink
added logging
Browse files Browse the repository at this point in the history
  • Loading branch information
anmolbhatia05 committed Oct 13, 2023
1 parent 401dfd4 commit 6825c63
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
22 changes: 11 additions & 11 deletions socksx/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use anyhow::Result;
use clap::Parser;
use dotenv::dotenv;
use itertools::Itertools;
use log::LevelFilter;
use log::{info, LevelFilter};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::Semaphore;
use tokio::time::Instant;
Expand All @@ -37,7 +37,7 @@ struct Args {
debug: bool,

/// Host (IP) for the SOCKS server
#[clap(short, long, env = "HOST", default_value = "0.0.0.0")]
#[clap(short='H', long, env = "HOST", default_value = "0.0.0.0")]
host: String,

/// Concurrent connections limit (0=unlimted)
Expand Down Expand Up @@ -68,16 +68,16 @@ async fn main() -> Result<()> {
logger.filter_level(LevelFilter::Debug).init();
} else {
logger.filter_level(LevelFilter::Info).init();

// Setup human-friendly panic messages
setup_panic!(Metadata {
name: "SOCKSX".into(),
version: env!("CARGO_PKG_VERSION").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
});
}

// Setup human-friendly panic messages
setup_panic!(Metadata {
name: "SOCKSX".into(),
version: env!("CARGO_PKG_VERSION").into(),
authors: env!("CARGO_PKG_AUTHORS").replace(":", ", ").into(),
homepage: env!("CARGO_PKG_HOMEPAGE").into(),
});

// TODO: validate host

// Convert and collect chain arguments
Expand Down Expand Up @@ -142,7 +142,7 @@ async fn process(
}

// Log the time taken to process the request
println!("{}ms", Instant::now().saturating_duration_since(start_time).as_millis());
info!("Request fulfilled in {}ms", Instant::now().saturating_duration_since(start_time).as_millis());

Ok(())
}
4 changes: 3 additions & 1 deletion socksx/src/socks5/s5_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::convert::TryInto;
use std::net::SocketAddr;

use log::info;
use anyhow::Result;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
Expand Down Expand Up @@ -63,7 +64,8 @@ impl Socks5Client {
let request = Socks5Request::new(SOCKS_CMD_CONNECT, destination.try_into()?);

let mut stream = TcpStream::connect(&self.proxy_addr).await?;

info!("Connecting to socks address at {}", stream.peer_addr()?);

// Enter authentication negotiation.
let auth_method = self.negotiate_auth_method(&mut stream).await?;
if auth_method == SOCKS_AUTH_USERNAME_PASSWORD {
Expand Down
5 changes: 3 additions & 2 deletions socksx/src/socks6/s6_client.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{convert::TryInto, net::SocketAddr};

use log::info;
use anyhow::{ensure, Result};
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
Expand Down Expand Up @@ -58,8 +59,8 @@ impl Socks6Client {
A: TryInto<Address, Error = anyhow::Error>,
{
let mut stream = TcpStream::connect(&self.proxy_addr).await?;
info!("Connecting to socks address at {}", stream.peer_addr()?);
let binding = self.handshake(destination, initial_data, options, &mut stream).await?;

Ok((stream, binding))
}

Expand Down Expand Up @@ -125,7 +126,7 @@ impl Socks6Client {
// Wait for authentication and operation reply.
let _ = socks6::read_no_authentication(stream).await?;
let (binding, _) = socks6::read_reply(stream).await?;

Ok(binding)
}
}
2 changes: 2 additions & 0 deletions socksx/src/socks6/s6_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::Result;
use async_trait::async_trait;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
use log::info;

use crate::{Socks6Client, SocksHandler};
use crate::addresses::ProxyAddress;
Expand Down Expand Up @@ -87,6 +88,7 @@ impl SocksHandler for Socks6Handler {
socks6::write_no_authentication(source).await?;

let destination = request.destination.to_string();
info!("Connecting to destination - {}", destination);
let chain = request.chain(&self.static_links)?;

let mut destination = if let Some(mut chain) = chain {
Expand Down

0 comments on commit 6825c63

Please sign in to comment.