Skip to content

Commit

Permalink
add only_ipv6 option. zhboner#124
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Mar 15, 2024
1 parent dd5461f commit 9660c29
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 157 deletions.
202 changes: 101 additions & 101 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "realm"
version = "2.5.2"
version = "2.5.4"
authors = ["zhboner <[email protected]>"]
edition = "2021"

Expand Down
89 changes: 50 additions & 39 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,61 +101,60 @@ Or have a look at [Cross](https://github.com/cross-rs/cross), it makes things ea
## Usage

```shell
Realm 2.5.0 [proxy][balance][brutal][transport][multi-thread]
A high efficiency relay tool

USAGE:
realm [FLAGS] [OPTIONS]
Usage: realm [FLAGS] [OPTIONS]

Commands:
convert convert your legacy configuration into an advanced one

FLAGS:
-h, --help show help
-v, --version show version
-d, --daemon run as a unix daemon
-u, --udp force enable udp forward
-t, --ntcp force disable tcp forward
-f, --tfo force enable tcp fast open -- deprecated
-z, --splice force enable tcp zero copy -- deprecated
-h, --help show help
-v, --version show version
-d, --daemon run as a unix daemon
-u, --udp force enable udp forward
-t, --ntcp force disable tcp forward
-6, --ipv6 force disable ipv6 mapped ipv4
-f, --tfo force enable tcp fast open -- deprecated
-z, --splice force enable tcp zero copy -- deprecated

OPTIONS:
-c, --config <path> use config file
-l, --listen <address> listen address
-r, --remote <address> remote address
-x, --through <address> send through ip or address
-i, --interface <device> bind to interface
-a, --listen-transport <options> listen transport
-b, --remote-transport <options> remote transport
-c, --config <path> use config file
-l, --listen <address> listen address
-r, --remote <address> remote address
-x, --through <address> send through ip or address
-i, --interface <device> bind to interface
-a, --listen-transport <options> listen transport
-b, --remote-transport <options> remote transport

SYS OPTIONS:
-n, --nofile <limit> set nofile limit
-p, --pipe-page <number> set pipe capacity
-j, --pre-conn-hook <path> set pre-connect hook
-n, --nofile <limit> set nofile limit
-p, --pipe-page <number> set pipe capacity
-j, --pre-conn-hook <path> set pre-connect hook

LOG OPTIONS:
--log-level <level> override log level
--log-output <path> override log output
--log-level <level> override log level
--log-output <path> override log output

DNS OPTIONS:
--dns-mode <mode> override dns mode
--dns-min-ttl <second> override dns min ttl
--dns-max-ttl <second> override dns max ttl
--dns-cache-size <number> override dns cache size
--dns-protocol <protocol> override dns protocol
--dns-servers <servers> override dns servers
--dns-mode <mode> override dns mode
--dns-min-ttl <second> override dns min ttl
--dns-max-ttl <second> override dns max ttl
--dns-cache-size <number> override dns cache size
--dns-protocol <protocol> override dns protocol
--dns-servers <servers> override dns servers

PROXY OPTIONS:
--send-proxy send proxy protocol header
--send-proxy-version <version> send proxy protocol version
--accept-proxy accept proxy protocol header
--accept-proxy-timeout <second> accept proxy protocol timeout
--send-proxy <send_proxy> send proxy protocol header
--send-proxy-version <version> send proxy protocol version
--accept-proxy <accept_proxy> accept proxy protocol header
--accept-proxy-timeout <second> accept proxy protocol timeout

TIMEOUT OPTIONS:
--tcp-timeout <second> override tcp timeout(5s)
--udp-timeout <second> override udp timeout(30s)
--tcp-keepalive <second> override default tcp keepalive interval(15s)
--tcp-keepalive-probe <count> override default tcp keepalive count(3)

SUBCOMMANDS:
convert convert your legacy configuration into an advanced one
--tcp-timeout <second> override tcp timeout(5s)
--udp-timeout <second> override udp timeout(30s)
--tcp-keepalive <second> override default tcp keepalive interval(15s)
--tcp-keepalive-probe <count> override default tcp keepalive count(3)
```

Start from command line arguments:
Expand Down Expand Up @@ -262,6 +261,7 @@ remote = "www.google.com:443"
├── network
│ ├── no_tcp
│ ├── use_udp
│ ├── ipv6_only
│ ├── tcp_timeout
│ ├── udp_timeout
│ ├── tcp_keepalive
Expand Down Expand Up @@ -474,6 +474,17 @@ Due to the receiver side not limiting access to the association, the relay works

default: false

#### network.ipv6_only: bool

Disable ipv4-mapped-ipv6 when binding to an ipv6 address.

E.g.:
`[::0]:port` with (ipv6_only=false) binds to `*:port`

`[::0]:port` with (ipv6_only=true) binds to `[::]:port`

default: false

#### ~~network.zero_copy: bool~~ deprecated

~~Require `zero-copy` feature.~~
Expand Down
2 changes: 1 addition & 1 deletion realm_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "realm_core"
version = "0.3.8"
version = "0.3.9"
authors = ["Realm Contributors"]
description = "Realm's core facilities."
repository = "https://github.com/zhboner/realm"
Expand Down
15 changes: 14 additions & 1 deletion realm_core/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ pub struct ConnectOpts {
pub balancer: Balancer,
}

#[derive(Debug, Default, Clone)]
pub struct BindOpts {
pub ipv6_only: bool,
}

/// Relay endpoint.
#[derive(Debug, Clone)]
pub struct Endpoint {
pub laddr: SocketAddr,
pub raddr: RemoteAddr,
pub bind_opts: BindOpts,
pub conn_opts: ConnectOpts,
pub extra_raddrs: Vec<RemoteAddr>,
}
Expand All @@ -81,7 +87,14 @@ impl Display for Endpoint {
for raddr in self.extra_raddrs.iter() {
write!(f, "|{}", raddr)?;
}
write!(f, "]; options: {}", &self.conn_opts)
write!(f, "]; options: {}; {}", &self.bind_opts, &self.conn_opts)
}
}

impl Display for BindOpts {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let BindOpts { ipv6_only } = self;
write!(f, "ipv6_only={}", ipv6_only)
}
}

Expand Down
3 changes: 2 additions & 1 deletion realm_core/src/tcp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub async fn run_tcp(endpoint: Endpoint) -> Result<()> {
let Endpoint {
laddr,
raddr,
bind_opts,
conn_opts,
extra_raddrs,
} = endpoint;
Expand All @@ -33,7 +34,7 @@ pub async fn run_tcp(endpoint: Endpoint) -> Result<()> {
let conn_opts = Ref::new(&conn_opts);
let extra_raddrs = Ref::new(&extra_raddrs);

let lis = socket::bind(&laddr).unwrap_or_else(|e| panic!("[tcp]failed to bind {}: {}", &laddr, e));
let lis = socket::bind(&laddr, bind_opts).unwrap_or_else(|e| panic!("[tcp]failed to bind {}: {}", &laddr, e));
let keepalive = socket::keepalive::build(&conn_opts);

loop {
Expand Down
13 changes: 9 additions & 4 deletions realm_core/src/tcp/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ use tokio::net::{TcpSocket, TcpStream, TcpListener};

use crate::dns::resolve_addr;
use crate::time::timeoutfut;
use crate::endpoint::{RemoteAddr, ConnectOpts};
use crate::endpoint::{RemoteAddr, BindOpts, ConnectOpts};

#[allow(clippy::clone_on_copy)]
pub fn bind(laddr: &SocketAddr) -> Result<TcpListener> {
pub fn bind(laddr: &SocketAddr, bind_opts: BindOpts) -> Result<TcpListener> {
let BindOpts { ipv6_only } = bind_opts;
let socket = new_tcp_socket(laddr)?;

// ipv6_only
if let SocketAddr::V6(_) = laddr {
socket.set_only_v6(ipv6_only)?;
}

// ignore error
let _ = socket.set_reuse_address(true);

socket.bind(&laddr.clone().into())?;
socket.bind(&(*laddr).into())?;
socket.listen(1024)?;

TcpListener::from_std(socket.into())
Expand Down
3 changes: 2 additions & 1 deletion realm_core/src/udp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ pub async fn run_udp(endpoint: Endpoint) -> Result<()> {
let Endpoint {
laddr,
raddr,
bind_opts,
conn_opts,
..
} = endpoint;

let sockmap = SockMap::new();

let lis = socket::bind(&laddr).unwrap_or_else(|e| panic!("[udp]failed to bind {}: {}", laddr, e));
let lis = socket::bind(&laddr, bind_opts).unwrap_or_else(|e| panic!("[udp]failed to bind {}: {}", laddr, e));

loop {
if let Err(e) = associate_and_relay(&lis, &raddr, &conn_opts, &sockmap).await {
Expand Down
10 changes: 8 additions & 2 deletions realm_core/src/udp/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ use std::net::SocketAddr;
use tokio::net::UdpSocket;
use realm_syscall::new_udp_socket;

use crate::endpoint::ConnectOpts;
use crate::endpoint::{BindOpts, ConnectOpts};

#[allow(clippy::clone_on_copy)]
pub fn bind(laddr: &SocketAddr) -> Result<UdpSocket> {
pub fn bind(laddr: &SocketAddr, bind_opts: BindOpts) -> Result<UdpSocket> {
let BindOpts { ipv6_only } = bind_opts;
let socket = new_udp_socket(laddr)?;

// ipv6_only
if let SocketAddr::V6(_) = laddr {
socket.set_only_v6(ipv6_only)?;
}

// ignore error
let _ = socket.set_reuse_address(true);

Expand Down
4 changes: 2 additions & 2 deletions src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ fn start_from_conf(full: FullConf) {

let endpoints: Vec<EndpointInfo> = endpoints_conf
.into_iter()
.map(|x| x.build())
.inspect(|x| println!("inited: {}", &x.endpoint))
.map(Config::build)
.inspect(|x| println!("inited: {}", x.endpoint))
.collect();

execute(endpoints);
Expand Down
10 changes: 8 additions & 2 deletions src/cmd/flag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,24 @@ pub fn add_flags(app: Command) -> Command {
.help("force disable tcp forward")
.action(ArgAction::SetTrue)
.display_order(4),
Arg::new("ipv6_only")
.short('6')
.long("ipv6")
.help("force disable ipv6 mapped ipv4")
.action(ArgAction::SetTrue)
.display_order(5),
Arg::new("fast_open")
.short('f')
.long("tfo")
.help("force enable tcp fast open -- deprecated")
.action(ArgAction::SetTrue)
.display_order(5),
.display_order(6),
Arg::new("zero_copy")
.short('z')
.long("splice")
.help("force enable tcp zero copy -- deprecated")
.action(ArgAction::SetTrue)
.display_order(6),
.display_order(7),
])
}

Expand Down
2 changes: 2 additions & 0 deletions src/conf/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl Config for EndpointConf {

// build partial conn_opts from netconf
let NetInfo {
bind_opts,
mut conn_opts,
no_tcp,
use_udp,
Expand Down Expand Up @@ -184,6 +185,7 @@ impl Config for EndpointConf {
endpoint: Endpoint {
laddr,
raddr,
bind_opts,
conn_opts,
extra_raddrs,
},
Expand Down
Loading

0 comments on commit 9660c29

Please sign in to comment.