Skip to content

Commit

Permalink
async-std to tokio.
Browse files Browse the repository at this point in the history
  • Loading branch information
b23r0 committed Apr 10, 2022
1 parent 566a0c6 commit b8fbc27
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 48 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rsocx"
version = "0.1.3"
version = "0.2.0"
authors = ["b23r0 <[email protected]>"]
edition = "2021"
description = "A high performence Socks5 proxy server with bind/reverse support implementation by Rust."
Expand All @@ -13,6 +13,5 @@ license = "MIT"
[dependencies]
simple_logger = "2.1.0"
log = "0.4.14"
futures = "0.3.0"
async-std = {version = "1" , features = ["attributes"]}
tokio = { version = "1", features = ["full"] }
getopts = "0.2"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ A high performence Socks5 proxy server with bind/reverse support

# Features

* Async-std
* Async
* No unsafe code
* Single executable
* Linux/Windows/Mac/BSD support
Expand Down
35 changes: 10 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ mod socks;
use log::LevelFilter;
use simple_logger::SimpleLogger;
use getopts::Options;
use futures::{AsyncReadExt, AsyncWriteExt, FutureExt, StreamExt};
use async_std::{io, net::{TcpListener, TcpStream}, task};
use futures::select;
use tokio::{io::{self, AsyncWriteExt, AsyncReadExt}, task, net::{TcpListener, TcpStream}};
use utils::MAGIC_FLAG;

fn usage(program: &str, opts: &Options) {
Expand All @@ -18,7 +16,7 @@ fn usage(program: &str, opts: &Options) {
}


#[async_std::main]
#[tokio::main]
async fn main() -> io::Result<()> {
SimpleLogger::new().with_utc_timestamps().with_utc_timestamps().with_colors(true).init().unwrap();
::log::set_max_level(LevelFilter::Info);
Expand Down Expand Up @@ -77,11 +75,9 @@ async fn main() -> io::Result<()> {
Ok(p) => p
};

let mut incoming = listener.incoming();

while let Some(stream) = incoming.next().await {
let stream = stream?;
task::spawn(async {
loop{
let (stream , _) = listener.accept().await.unwrap();
tokio::spawn(async {
socks::socksv5_handle(stream).await;
});
}
Expand Down Expand Up @@ -143,10 +139,8 @@ async fn main() -> io::Result<()> {
Ok(p) => p
};

let mut incoming = listener.incoming();

while let Some(stream) = incoming.next().await {
let mut stream = stream?;
loop {
let (mut stream , _) = listener.accept().await.unwrap();

match slave_stream.write_all(&[MAGIC_FLAG[0]]).await{
Err(e) => {
Expand All @@ -171,8 +165,8 @@ async fn main() -> io::Result<()> {
let mut buf2 = [0u8 ; 1024];

loop{
select! {
a = proxy_stream.read(&mut buf1).fuse() => {
tokio::select! {
a = proxy_stream.read(&mut buf1) => {

let len = match a {
Err(_) => {
Expand All @@ -191,7 +185,7 @@ async fn main() -> io::Result<()> {
break;
}
},
b = stream.read(&mut buf2).fuse() => {
b = stream.read(&mut buf2) => {
let len = match b{
Err(_) => {
break;
Expand All @@ -208,17 +202,8 @@ async fn main() -> io::Result<()> {
break;
}
},
complete => break,
}
}
match stream.shutdown(std::net::Shutdown::Both){
Err(_) => {},
_ => {}
};
match proxy_stream.shutdown(std::net::Shutdown::Both){
Err(_) => {},
_ => {}
};
log::info!("transfer [{}:{}] finished" , slave_addr.ip() , slave_addr.port());
});
}
Expand Down
24 changes: 5 additions & 19 deletions src/socks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use futures::{AsyncReadExt, AsyncWriteExt, FutureExt};
use async_std::{net::{TcpStream}};
use std::{ net::{Ipv6Addr, SocketAddrV6}};
use futures::select;

use tokio::{net::TcpStream, io::{AsyncWriteExt, AsyncReadExt}};

use crate::utils::makeword;

Expand Down Expand Up @@ -100,8 +99,8 @@ async fn tcp_transfer(stream : &mut TcpStream , addr : &Addr, address : &String
let mut buf1 = [0u8 ; 1024];
let mut buf2 = [0u8 ; 1024];
loop{
select! {
a = client.read(&mut buf1).fuse() => {
tokio::select! {
a = client.read(&mut buf1) => {

let len = match a {
Err(_) => {
Expand All @@ -120,7 +119,7 @@ async fn tcp_transfer(stream : &mut TcpStream , addr : &Addr, address : &String
break;
}
},
b = stream.read(&mut buf2).fuse() => {
b = stream.read(&mut buf2) => {
let len = match b{
Err(_) => {
break;
Expand All @@ -137,15 +136,8 @@ async fn tcp_transfer(stream : &mut TcpStream , addr : &Addr, address : &String
break;
}
},
complete => break,
}
}
match client.shutdown(std::net::Shutdown::Both){
Err(e) => {
log::info!("error : {}" , e);
},
_ => {}
};
}

pub async fn socksv5_handle(mut stream: TcpStream) {
Expand Down Expand Up @@ -276,10 +268,4 @@ pub async fn socksv5_handle(mut stream: TcpStream) {
log::info!("connection [{}] finished" , address);
break;
}
match stream.shutdown(std::net::Shutdown::Both){
Err(e) => {
log::error!("error : {}" , e);
},
_ => {}
};
}

0 comments on commit b8fbc27

Please sign in to comment.