Skip to content

Commit

Permalink
1. Set nodelay on sockets.
Browse files Browse the repository at this point in the history
2. Increased the buffer size during read and write process.
3. Shorten the intervel in resolving addresses.
  • Loading branch information
zhboner committed Jul 16, 2021
1 parent 5a28e8d commit 8ad8f04
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tokio = { version = "1.5", features = ["full"] }
tokio = { version = "1.8.1", features = ["full"] }
trust-dns-resolver = "0.20"
futures = "0.3"
structopt = "0.3"
Expand Down
28 changes: 25 additions & 3 deletions src/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use std::net::{IpAddr, SocketAddr};
use std::sync::{Arc, RwLock};
use tokio;
use tokio::io;
use tokio::io::AsyncWriteExt;
use tokio::io::{AsyncWriteExt, AsyncReadExt};
use tokio::net;


use crate::resolver;
use crate::udp;
use realm::RelayConfig;
use tokio::net::tcp::{ReadHalf, WriteHalf};
use std::fs::read;

// Initialize DNS recolver
// Set up channel between listener and resolver
Expand Down Expand Up @@ -64,6 +67,7 @@ pub async fn run(config: RelayConfig, remote_ip: Arc<RwLock<IpAddr>>) {
loop {
match tcp_listener.accept().await {
Ok((inbound, _)) => {
inbound.set_nodelay(true).unwrap();
remote_socket = format!("{}:{}", &(remote_ip.read().unwrap()), config.remote_port)
.parse()
.unwrap();
Expand All @@ -90,20 +94,38 @@ async fn transfer_tcp(
remote_socket: SocketAddr,
) -> Result<(), Box<dyn Error>> {
let mut outbound = net::TcpStream::connect(remote_socket).await?;
outbound.set_nodelay(true).unwrap();
let (mut ri, mut wi) = inbound.split();
let (mut ro, mut wo) = outbound.split();

let client_to_server = async {
io::copy(&mut ri, &mut wo).await?;
// io::copy(&mut ri, &mut wo).await?;
copy_data(&mut ri, &mut wo).await?;
wo.shutdown().await
};

let server_to_client = async {
io::copy(&mut ro, &mut wi).await?;
// io::copy(&mut ro, &mut wi).await?;
copy_data(&mut ro, &mut wi).await?;
wi.shutdown().await
};

try_join(client_to_server, server_to_client).await?;

Ok(())
}

async fn copy_data(reader: &mut ReadHalf<'_>, writer: &mut WriteHalf<'_>) -> Result<(), std::io::Error> {
let mut buf = vec![0u8; 0x4000];
let mut n: usize;

loop {
n = reader.read(&mut buf).await?;
if n == 0 {
break;
}
writer.write_all(&buf[..n]).await?;
}
writer.flush().await?;
Ok(())
}
2 changes: 1 addition & 1 deletion src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub async fn resolve(addr_list: Vec<String>, ip_list: Vec<Arc<RwLock<net::IpAddr
}
}

sleep(Duration::from_secs(60 * 60 * 6)).await;
sleep(Duration::from_secs(60)).await;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::net::UdpSocket;
use tokio::sync::oneshot;
use tokio::time::sleep;

const BUFFERSIZE: usize = 2048;
const BUFFERSIZE: usize = 0x4000;
const TIMEOUT: Duration = Duration::from_secs(60 * 15);

pub async fn transfer_udp(
Expand Down

0 comments on commit 8ad8f04

Please sign in to comment.