Skip to content

Commit

Permalink
style: add more detailed output
Browse files Browse the repository at this point in the history
1. add more detailed output
2. modifiy some comments
  • Loading branch information
X1r0z committed Dec 21, 2024
1 parent 1975e5c commit 64ef11b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
49 changes: 39 additions & 10 deletions src/forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ impl Forward {
let stream1 = tcp::NetStream::from_acceptor(stream1, acceptor1).await;
let stream2 = tcp::NetStream::from_acceptor(stream2, acceptor2).await;

info!("Open pipe: {} <=> {}", addr1, addr2);
if let Err(e) = tcp::handle_forward(stream1, stream2).await {
error!("Failed to forward: {}", e)
}
info!("Close pipe: {} <=> {}", addr1, addr2);
});
}
}
Expand All @@ -129,19 +131,23 @@ impl Forward {
let (stream, addr) = listener.accept().await?;
let remote = TcpStream::connect(&self.remote_addrs[0]).await?;

let peer_addr = remote.peer_addr()?;

let acceptor = acceptor.clone();
let connector = connector.clone();

info!("Accept connection from {}", addr);
info!("Connect to {} success", remote.peer_addr()?);
info!("Connect to {} success", peer_addr);

tokio::spawn(async move {
let stream = tcp::NetStream::from_acceptor(stream, acceptor).await;
let remote = tcp::NetStream::from_connector(remote, connector).await;

info!("Open pipe: {} <=> {}", addr, peer_addr);
if let Err(e) = tcp::handle_forward(stream, remote).await {
error!("failed to forward: {}", e)
}
info!("Close pipe: {} <=> {}", addr, peer_addr);
});
}
}
Expand All @@ -161,22 +167,25 @@ impl Forward {
let stream1 = TcpStream::connect(&self.remote_addrs[0]).await?;
let stream2 = TcpStream::connect(&self.remote_addrs[1]).await?;

info!("Connect to {} success", stream1.peer_addr()?);
info!("Connect to {} success", stream2.peer_addr()?);
let peer_addr_1 = stream1.peer_addr()?;
let peer_addr_2 = stream2.peer_addr()?;

info!("Connect to {} success", peer_addr_1);
info!("Connect to {} success", peer_addr_2);

let connector1 = connector1.clone();
let connector2 = connector2.clone();

let stream1 = tcp::NetStream::from_connector(stream1, connector1).await;
let stream2 = tcp::NetStream::from_connector(stream2, connector2).await;

info!("Open pipe: {} <=> {}", peer_addr_1, peer_addr_2);
tcp::handle_forward(stream1, stream2).await?;
info!("Close pipe: {} <=> {}", peer_addr_1, peer_addr_2)
}
}

async fn socket_to_local_tcp(&self) -> Result<()> {
let socket_path = self.socket.as_ref().unwrap();

let local_listener = TcpListener::bind(&self.local_addrs[0]).await?;
info!("Bind to {} success", local_listener.local_addr()?);

Expand All @@ -187,7 +196,15 @@ impl Forward {

loop {
let (local_stream, addr) = local_listener.accept().await?;
let unix_stream = UnixStream::connect(socket_path).await?;
let unix_stream = UnixStream::connect(self.socket.as_ref().unwrap()).await?;

let unix_addr = unix_stream.peer_addr()?;
let socket_path = unix_addr
.as_pathname()
.unwrap()
.to_str()
.unwrap()
.to_string();

info!("Accept connection from {}", addr);
info!("Connect to {} success", socket_path);
Expand All @@ -198,34 +215,46 @@ impl Forward {
let local_stream = tcp::NetStream::from_acceptor(local_stream, acceptor).await;
let unix_stream = tcp::NetStream::Unix(unix_stream);

info!("Open pipe: {} <=> {}", socket_path, addr);
if let Err(e) = tcp::handle_forward(unix_stream, local_stream).await {
error!("Failed to forward: {}", e)
}
info!("Close pipe: {} <=> {}", socket_path, addr);
});
}
}

async fn socket_to_remote_tcp(&self) -> Result<()> {
let socket_path = self.socket.as_ref().unwrap();

let connector = Arc::new(match self.remote_opts[0] {
true => Some(crypto::get_tls_connector()),
false => None,
});

loop {
let unix_stream = UnixStream::connect(socket_path).await?;
let unix_stream = UnixStream::connect(self.socket.as_ref().unwrap()).await?;
let remote_stream = TcpStream::connect(&self.remote_addrs[0]).await?;

let peer_addr = remote_stream.peer_addr()?;
let unix_addr = unix_stream.peer_addr()?;

let socket_path = unix_addr
.as_pathname()
.unwrap()
.to_str()
.unwrap()
.to_string();

info!("Connect to {} success", socket_path);
info!("Connect to {} success", remote_stream.peer_addr()?);
info!("Connect to {} success", peer_addr);

let connector = connector.clone();

let unix_stream = tcp::NetStream::Unix(unix_stream);
let remote_stream = tcp::NetStream::from_connector(remote_stream, connector).await;

info!("Open pipe: {} <=> {}", socket_path, peer_addr);
tcp::handle_forward(unix_stream, remote_stream).await?;
info!("Close pipe: {} <=> {}", socket_path, peer_addr);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::io::Result;

use clap::Parser;
use rsproxy::Cli;
use std::io::Result;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down
14 changes: 7 additions & 7 deletions src/socks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::tcp::{self, NetStream};
pub async fn handle_connection(stream: NetStream) -> Result<()> {
let (mut reader, mut writer) = stream.split();

// 1. 认证协商
// 1. auth negotiation
let mut buf = [0u8; 2];
reader.read_exact(&mut buf).await?;

Expand All @@ -25,10 +25,10 @@ pub async fn handle_connection(stream: NetStream) -> Result<()> {
let mut methods = vec![0u8; nmethods];
reader.read_exact(&mut methods).await?;

// 不需要认证
// no auth needed
writer.write_all(&[0x05, 0x00]).await?;

// 2. 请求处理
// 2. handle request
let mut header = [0u8; 4];
reader.read_exact(&mut header).await?;

Expand Down Expand Up @@ -63,7 +63,7 @@ pub async fn handle_connection(stream: NetStream) -> Result<()> {
)
}
0x03 => {
// 域名
// domain
let len = reader.read_u8().await? as usize;
let mut domain = vec![0u8; len];
reader.read_exact(&mut domain).await?;
Expand All @@ -89,7 +89,7 @@ pub async fn handle_connection(stream: NetStream) -> Result<()> {
}
};

// 3. 连接目标服务器
// 3. connect to the target server
let target = NetStream::Tcp(match TcpStream::connect(&addr).await {
Ok(stream) => stream,
Err(e) => {
Expand All @@ -100,11 +100,11 @@ pub async fn handle_connection(stream: NetStream) -> Result<()> {
}
});

// 4. 发送连接成功响应
// 4. send success response
writer
.write_all(&[0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0])
.await?;

// 5. 转发数据
// 5. forward data
tcp::handle_forward_splitted(reader, writer, target).await
}

0 comments on commit 64ef11b

Please sign in to comment.