Skip to content

Commit

Permalink
add transport-boost feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Apr 20, 2022
1 parent 92ed15e commit 45c041e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "realm"
version = "2.0.5"
version = "2.1.0"
authors = ["zhboner <[email protected]>"]
edition = "2021"

Expand Down Expand Up @@ -32,7 +32,7 @@ pin-project = "1"
lazy_static = "1"

# transport
kaminari = { version = "0.5.7", optional = true }
kaminari = { version = "0.6.0", optional = true }


# tfo
Expand Down Expand Up @@ -65,12 +65,15 @@ panic = "abort"
opt-level = 0

[features]
default = ["udp", "zero-copy", "trust-dns", "multi-thread", "proxy-protocol", "transport"]
default = ["udp", "zero-copy", "trust-dns", "multi-thread",
"proxy-protocol", "transport", "transport-boost"
]
udp = []
tfo = ["tokio-tfo"]
zero-copy = []
trust-dns = ["trust-dns-resolver"]
transport = ["kaminari"]
transport-boost = []
proxy-protocol = ["haproxy"]
multi-thread = ["tokio/rt-multi-thread"]
jemalloc = ["jemallocator"]
Expand Down
23 changes: 13 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ cargo build --release

### Build Options

- udp *(enabled by default)*
- trust-dns *(enabled by default)*
- zero-copy *(enabled on linux)*
- transport *(enabled by default)*
- multi-thread *(enabled by default)*
- tfo
- mi-malloc
- jemalloc
- udp: enable udp relay.
- trust-dns: enable trust-dns's async dns resolver.
- zero-copy: enable zero-copy on linux.
- transport: enable ws/tls/wss.
- transport-boost: enable optimizations for transport, at the cost of increasing binary size.
- multi-thread: enable tokio's multi-threaded IO scheduler.
- tfo: enable tcp-fast-open.
- mi-malloc: custom memory allocator.
- jemalloc: custom memory allocator.

See also: `Cargo.toml`
Default: udp + trust-dns + zero-copy + multi-thread + transport + transport-boost.

See also: `Cargo.toml`.

Examples:

Expand All @@ -67,7 +70,7 @@ Examples:
cargo build --release --no-default-features

# enable other options
cargo build --release --no-default-features --features udp, tfo, zero-copy, trust-dns
cargo build --release --no-default-features --features udp,tfo,zero-copy,trust-dns..
```

### Cross Compile
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pub mod conf;
pub mod utils;
pub mod relay;

pub const VERSION: &str = "2.0.5";
pub const VERSION: &str = "2.1.0";
pub const ENV_CONFIG: &str = "REALM_CONF";
25 changes: 11 additions & 14 deletions src/relay/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use cfg_if::cfg_if;
#[cfg(feature = "proxy-protocol")]
mod haproxy;

#[cfg(feature = "transport")]
mod transport;

cfg_if! {
if #[cfg(feature = "tfo")] {
mod tfo;
Expand All @@ -16,7 +19,6 @@ cfg_if! {
}

use std::io::Result;
use futures::try_join;
use log::debug;

use tokio::net::TcpSocket;
Expand Down Expand Up @@ -73,16 +75,11 @@ pub async fn connect_and_relay(
let res = {
#[cfg(feature = "transport")]
{
use kaminari::{AsyncAccept, AsyncConnect};

use transport::relay_transport;
if let Some((ac, cc)) = transport {
let (mut inbound, mut outbound) =
try_join!(ac.accept(inbound), cc.connect(outbound))?;
tokio::io::copy_bidirectional(&mut inbound, &mut outbound)
.await
.map(|_| ())
relay_transport(inbound, outbound, ac, cc).await
} else {
relay_plain(&mut inbound, &mut outbound, *zero_copy).await
relay_plain(inbound, outbound, *zero_copy).await
}
}
#[cfg(not(feature = "transport"))]
Expand All @@ -99,17 +96,17 @@ pub async fn connect_and_relay(

#[inline]
async fn relay_plain(
inbound: &mut TcpStream,
outbound: &mut TcpStream,
mut inbound: TcpStream,
mut outbound: TcpStream,
zero_copy: bool,
) -> Result<()> {
#[cfg(all(target_os = "linux", feature = "zero-copy"))]
if zero_copy {
zio::bidi_copy_pipe(inbound, outbound).await
zio::bidi_copy_pipe(&mut inbound, &mut outbound).await
} else {
zio::bidi_copy_buffer(inbound, outbound).await
zio::bidi_copy_buffer(&mut inbound, &mut outbound).await
}

#[cfg(not(all(target_os = "linux", feature = "zero-copy")))]
zio::bidi_copy_buffer(inbound, outbound).await
zio::bidi_copy_buffer(&mut inbound, &mut outbound).await
}
62 changes: 62 additions & 0 deletions src/relay/tcp/transport.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::io::Result;
use futures::try_join;
use tokio::io::copy_bidirectional;
use kaminari::{AsyncAccept, AsyncConnect, IOStream};
use kaminari::mix::{MixAccept, MixConnect};

pub async fn relay_transport<S: IOStream>(
src: S,
dst: S,
ac: &MixAccept,
cc: &MixConnect,
) -> Result<()> {
macro_rules! hs_relay {
($ac: expr, $cc: expr) => {
handshake_and_relay(src, dst, $ac, $cc).await
};
}

#[cfg(feature = "transport-boost")]
{
use MixConnect::*;
if let Some(ac) = ac.as_plain() {
return match cc {
Plain(cc) => hs_relay!(ac, cc),
Ws(cc) => hs_relay!(ac, cc),
Tls(cc) => hs_relay!(ac, cc),
Wss(cc) => hs_relay!(ac, cc),
};
}
}

#[cfg(feature = "transport-boost")]
{
use MixAccept::*;
if let Some(cc) = cc.as_plain() {
return match ac {
Plain(ac) => hs_relay!(ac, cc),
Ws(ac) => hs_relay!(ac, cc),
Tls(ac) => hs_relay!(ac, cc),
Wss(ac) => hs_relay!(ac, cc),
};
}
}

hs_relay!(ac, cc)
}

pub async fn handshake_and_relay<S, AC, CC>(
src: S,
dst: S,
ac: &AC,
cc: &CC,
) -> Result<()>
where
S: IOStream,
AC: AsyncAccept<S>,
CC: AsyncConnect<S>,
{
let (mut src, mut dst) = try_join!(ac.accept(src), cc.connect(dst))?;

copy_bidirectional(&mut src, &mut dst).await.map(|_| ())
}

0 comments on commit 45c041e

Please sign in to comment.