Skip to content

Commit

Permalink
Update.
Browse files Browse the repository at this point in the history
  • Loading branch information
xOS committed Apr 22, 2022
1 parent 313397c commit 321dda8
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 35 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.

12 changes: 9 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "realm"
version = "2.1.3"
version = "2.1.4"
authors = ["zhboner <zhboner@gmail.com>"]
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 All @@ -59,18 +59,24 @@ daemonize = "0.4"
[profile.release]
opt-level = 3
lto = true
codegen-units = 1
incremental = false
panic = "abort"
strip = true

[profile.dev]
opt-level = 0

[features]
default = ["udp", "tfo", "zero-copy", "trust-dns", "multi-thread", "proxy-protocol", "transport"]
default = ["udp", "tfo", "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
36 changes: 22 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,46 @@ Realm can be run in a container with OCI (like Docker, Podman, Kubernetes, etc),

## Build Guides

Install rust toolchains with [rustup](https://rustup.rs/).
Install rust **nightly** toolchains with [rustup](https://rustup.rs/):

```shell
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```

Clone this repository
Clone this repository:

```shell
git clone https://github.com/zephyrchien/realm
git clone https://github.com/zhboner/realm
```

Enter the directory and build
Enter the directory and build:

```shell
cd realm
git submodule sync && git submodule update --init --recursive

# build release
cargo build --release

# allow more possible optimizations, make it even faster
RUSTFLAGS='-C target_cpu=native' 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.

Default: udp + trust-dns + zero-copy + multi-thread + transport + transport-boost.

See also: `Cargo.toml`
See also: `Cargo.toml`.

Examples:

Expand All @@ -61,7 +69,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..
```

## 安装脚本
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.1.3";
pub const VERSION: &str = "2.1.4";
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 321dda8

Please sign in to comment.