Skip to content

Commit

Permalink
添加socks5支持 & 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
editso committed Nov 29, 2021
1 parent 652fe39 commit 5ea7c94
Show file tree
Hide file tree
Showing 10 changed files with 359 additions and 164 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "fuso-socks"]
path = fuso-socks
url = [email protected]:editso/fuso-socks5.git
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ split-debuginfo = '...'
[workspace]
members = [
"./fuso-api",
"./fuso-core"
"./fuso-core",
"./fuso-socks"
]

[[bin]]
Expand All @@ -36,5 +37,6 @@ path = "src/no-log-client.rs"
clap = "3.0.0-beta.5"
log = {version = "0.4.14"}
env_logger = "0.9.0"
smol = {version = "1.2.5"}
fuso-core = {path = "./fuso-core"}
smol = {version = "1.2.5"}
fuso-socks = {path ="./fuso-socks"}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A fast, stable, cross-platform and efficient intranet penetration and port forwa
一款 快速 稳定 跨平台 高效的内网穿透,端口转发工具

[![Author](https://img.shields.io/badge/Author-editso-blueviolet)](https://github.com/editso)
[![Daza](https://img.shields.io/badge/Misc-1x2Bytes-blueviolet)](https://github.com/1x2Bytes)
[![Daza](https://img.shields.io/badge/Misc-1x2Bytes-blueviolet)](https://github.com/B1eed)
[![Daza](https://img.shields.io/badge/Misc-ifishzz-blueviolet)](https://github.com/ifishzz)
[![Bin](https://img.shields.io/badge/Fuso-Bin-ff69b4)](https://github.com/editso/fuso/releases)
[![GitHub issues](https://img.shields.io/github/issues/editso/fuso)](https://github.com/editso/fuso/issues)
Expand Down Expand Up @@ -47,8 +47,10 @@ A fast, stable, cross-platform and efficient intranet penetration and port forwa
| ---------------------- | -------------------------------------------------------------------------------- |
| 基本转发 (Forward) | <font color="green">✔</font> |
| 传输加密 (Encrypt) ||
| Sock5代理 (Socks5) | |
| Sock5代理 (Socks5) | <font color="green">✔(Achieved)</font> |
| UDP支持 (udp support) ||
| 多映射 ||
| 级联代理 ||
| 数据传输压缩 ||


Expand Down
2 changes: 1 addition & 1 deletion fuso-api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ where
let ret = Self::forward(self, to).await;

if ret.is_err() {
log::warn!("[fuso] Forward failure {}", ret.unwrap_err());
log::debug!("[fuso] Forward failure {}", ret.unwrap_err());
}
})
.detach();
Expand Down
130 changes: 66 additions & 64 deletions fuso-core/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@ pub struct Fuso {
accept_ax: Receiver<(TcpStream, TcpStream)>,
}


impl Fuso {
async fn handler_at_client(tcp: TcpListener, accept_tx: Sender<TcpStream>) -> Result<()> {
log::info!("[fus] Actual access address {}", tcp.local_addr().unwrap());

loop {
match tcp.accept().await {
Ok((stream, addr)) => {
log::info!("[fus] There is a new connection {}", addr);
if let Err(e) = accept_tx.send(stream).await {
break Err(Error::with_io(std::io::Error::new(
std::io::ErrorKind::Other,
e.to_string(),
)));
};
}
Err(e) => {
log::error!("[fus] An error occurred on the server {}", e);
break Err(e.into());
}
let stream = tcp.accept().await;

if stream.is_err() {
break Err(stream.unwrap_err().into());
}

let (stream, addr) = stream.unwrap();

log::debug!("[fus] Visitor connections from {}", addr);

if let Err(e) = accept_tx.send(stream).await {
break Err(Error::with_io(std::io::Error::new(
std::io::ErrorKind::Other,
e.to_string(),
)));
};
}
}

Expand All @@ -62,59 +62,63 @@ impl Fuso {
let mutex_sync = mutex_sync.clone();
async move {
loop {
match accept_ax.recv().await {
Err(e) => {
break Err(std::io::Error::new(
std::io::ErrorKind::Other,
e.to_string(),
)
.into())
}
Ok(mut stream) => {
let mut mutex_sync = mutex_sync.lock().await;
if let Some(accept_ax) = mutex_sync.as_mut() {
if let Err(_) = accept_ax.send(stream.clone()).await {
accept_ax.close();
let _ = mutex_sync.take();
let _ = stream.close().await;
}
} else {
log::warn!("[fus] Client is not ready");
let _ = stream.close().await;
}
let stream = accept_ax.recv().await;

if stream.is_err() {
break Err(std::io::Error::new(
std::io::ErrorKind::Other,
stream.unwrap_err().to_string(),
)
.into());
}

let mut stream = stream.unwrap();

let mut mutex_sync = mutex_sync.lock().await;
if let Some(accept_ax) = mutex_sync.as_mut() {
if let Err(_) = accept_ax.send(stream.clone()).await {
accept_ax.close();
let _ = mutex_sync.take();
let _ = stream.close().await;
}
} else {
log::debug!("[fus] Client is not ready");
let _ = stream.close().await;
}
}
}
},
async move {
loop {
match tcp.accept().await {
Err(e) => break Err(e.into()),
Ok((stream, addr)) => {
log::debug!("[fus] Client connection {}", addr);

let chief = Chief::auth(stream).await;

if chief.is_err() {
log::warn!("[fus] Client authentication failed");
} else {
chief
.unwrap()
.join(
tcp.clone(),
{
let (accept_ax, accept_tx) = unbounded();
*mutex_sync.lock().await = Some(accept_ax.clone());
accept_tx
},
accept_tx.clone(),
)
.await
.keep_live()
.await;
}
}
let stream = tcp.accept().await;

if stream.is_err() {
break Err(stream.unwrap_err().into());
}

let (stream, addr) = stream.unwrap();

log::debug!("[fus] Client connection {}", addr);

let chief = Chief::auth(stream).await;

if chief.is_err() {
log::warn!("[fus] Client authentication failed");
} else {
chief
.unwrap()
.join(
tcp.clone(),
{
let (accept_ax, accept_tx) = unbounded();
*mutex_sync.lock().await = Some(accept_ax.clone());
accept_tx
},
accept_tx.clone(),
)
.await
.keep_live()
.await;
}
}
},
Expand Down Expand Up @@ -179,7 +183,7 @@ impl Chief {
async move {
loop {
if let Err(_) = writer
.write(&Packet::new(CMD_PING, Bytes::new()).encode())
.send(Packet::new(CMD_PING, Bytes::new()))
.await
{
log::warn!("[fus] The client does not respond for a long time, the system judges that the connection is invalid");
Expand Down Expand Up @@ -268,8 +272,6 @@ impl Chief {
}
}



#[async_trait]
impl fuso_api::FusoListener<(TcpStream, TcpStream)> for Fuso {
#[inline]
Expand Down
1 change: 1 addition & 0 deletions fuso-socks
Submodule fuso-socks added at f7d04d
Loading

0 comments on commit 5ea7c94

Please sign in to comment.