Skip to content

Commit

Permalink
support TFO on Linux, FreeBSD, macOS and Windows
Browse files Browse the repository at this point in the history
- ref #184
  • Loading branch information
zonyitoo committed Apr 9, 2021
1 parent 1746c62 commit 9496b2c
Show file tree
Hide file tree
Showing 33 changed files with 1,672 additions and 621 deletions.
12 changes: 5 additions & 7 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadowsocks-rust"
version = "1.10.4"
version = "1.11.0"
authors = ["Shadowsocks Contributors"]
description = "shadowsocks is a fast tunnel proxy that helps you bypass firewalls."
repository = "https://github.com/shadowsocks/shadowsocks-rust"
Expand Down Expand Up @@ -132,3 +132,6 @@ byteorder = "1.3"
env_logger = "0.8"
byte_string = "1.0"
tokio = { version = "1", features = ["net", "time", "macros", "io-util"]}

[patch.crates-io]
tokio = { git = "https://github.com/tokio-rs/tokio.git", features = ["full"] }
7 changes: 6 additions & 1 deletion bin/sslocal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn main() {


(@arg NO_DELAY: --("no-delay") !takes_value "Set TCP_NODELAY option for socket")
(@arg FAST_OPEN: --("fast-open") !takes_value "Enable TCP Fast Open (TFO)")
(@arg NOFILE: -n --nofile +takes_value "Set RLIMIT_NOFILE with both soft and hard limit (only for *nix systems)")
(@arg ACL: --acl +takes_value "Path to ACL (Access Control List)")
(@arg DNS: --dns +takes_value "DNS nameservers, formatted like [(tcp|udp)://]host[:port][,host[:port]]..., or unix:///path/to/dns, or predefined keys like \"google\", \"cloudflare\"")
Expand Down Expand Up @@ -339,14 +340,18 @@ fn main() {
config.no_delay = true;
}

if matches.is_present("FAST_OPEN") {
config.fast_open = true;
}

#[cfg(any(target_os = "linux", target_os = "android"))]
if let Some(mark) = matches.value_of("OUTBOUND_FWMARK") {
config.outbound_fwmark = Some(mark.parse::<u32>().expect("an unsigned integer for `outbound-fwmark`"));
}

#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))]
if let Some(iface) = matches.value_of("OUTBOUND_BIND_INTERFACE") {
config.outbound_bind_interface = Some(From::from(iface.to_owned()));
config.outbound_bind_interface = Some(iface.to_owned());
}

if let Some(nofile) = matches.value_of("NOFILE") {
Expand Down
9 changes: 7 additions & 2 deletions bin/ssmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn main() {
(@arg SERVER_HOST: -s --("server-host") +takes_value "Host name or IP address of your remote server")

(@arg NO_DELAY: --("no-delay") !takes_value "Set TCP_NODELAY option for socket")
(@arg FAST_OPEN: --("fast-open") !takes_value "Enable TCP Fast Open (TFO)")

(@arg MANAGER_ADDRESS: --("manager-address") +takes_value {validator::validate_manager_addr} "ShadowSocks Manager (ssmgr) address, could be ip:port, domain:port or /path/to/unix.sock")
(@arg ENCRYPT_METHOD: -m --("encrypt-method") +takes_value possible_values(available_ciphers()) "Default encryption method")
Expand Down Expand Up @@ -143,14 +144,18 @@ fn main() {
config.no_delay = true;
}

if matches.is_present("FAST_OPEN") {
config.fast_open = true;
}

#[cfg(any(target_os = "linux", target_os = "android"))]
if let Some(mark) = matches.value_of("OUTBOUND_FWMARK") {
config.outbound_fwmark = Some(mark.parse::<u32>().expect("an unsigned integer for `outbound-fwmark`"));
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))]
if let Some(iface) = matches.value_of("OUTBOUND_BIND_INTERFACE") {
config.outbound_bind_interface = Some(From::from(iface.to_owned()));
config.outbound_bind_interface = Some(iface.to_owned());
}

if let Some(m) = matches.value_of("MANAGER_ADDRESS") {
Expand Down
11 changes: 7 additions & 4 deletions bin/ssserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn main() {
(@arg MANAGER_ADDRESS: --("manager-address") +takes_value "ShadowSocks Manager (ssmgr) address, could be \"IP:Port\", \"Domain:Port\" or \"/path/to/unix.sock\"")

(@arg NO_DELAY: --("no-delay") !takes_value "Set TCP_NODELAY option for socket")
(@arg FAST_OPEN: --("fast-open") !takes_value "Enable TCP Fast Open (TFO)")
(@arg NOFILE: -n --nofile +takes_value "Set RLIMIT_NOFILE with both soft and hard limit (only for *nix systems)")
(@arg ACL: --acl +takes_value "Path to ACL (Access Control List)")
(@arg DNS: --dns +takes_value "DNS nameservers, formatted like [(tcp|udp)://]host[:port][,host[:port]]..., or unix:///path/to/dns, or predefined keys like \"google\", \"cloudflare\"")
Expand All @@ -68,8 +69,6 @@ fn main() {
(@arg INBOUND_RECV_BUFFER_SIZE: --("inbound-recv-buffer-size") +takes_value {validator::validate_u32} "Set inbound sockets' SO_RCVBUF option")
(@arg OUTBOUND_SEND_BUFFER_SIZE: --("outbound-send-buffer-size") +takes_value {validator::validate_u32} "Set outbound sockets' SO_SNDBUF option")
(@arg OUTBOUND_RECV_BUFFER_SIZE: --("outbound-recv-buffer-size") +takes_value {validator::validate_u32} "Set outbound sockets' SO_RCVBUF option")

(@arg SINGLE_THREADED: --("single-threaded") "Run the program all in one thread")
);

#[cfg(feature = "logging")]
Expand Down Expand Up @@ -188,14 +187,18 @@ fn main() {
config.no_delay = true;
}

if matches.is_present("FAST_OPEN") {
config.fast_open = true;
}

#[cfg(any(target_os = "linux", target_os = "android"))]
if let Some(mark) = matches.value_of("OUTBOUND_FWMARK") {
config.outbound_fwmark = Some(mark.parse::<u32>().expect("an unsigned integer for `outbound-fwmark`"));
}

#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))]
if let Some(iface) = matches.value_of("OUTBOUND_BIND_INTERFACE") {
config.outbound_bind_interface = Some(From::from(iface.to_owned()));
config.outbound_bind_interface = Some(iface.to_owned());
}

if let Some(m) = matches.value_of("MANAGER_ADDRESS") {
Expand Down
2 changes: 1 addition & 1 deletion crates/shadowsocks-service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadowsocks-service"
version = "1.10.3"
version = "1.11.0"
authors = ["Shadowsocks Contributors"]
description = "shadowsocks is a fast tunnel proxy that helps you bypass firewalls."
repository = "https://github.com/shadowsocks/shadowsocks-rust"
Expand Down
22 changes: 18 additions & 4 deletions crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
//!
//! These defined server will be used with a load balancing algorithm.

#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))]
use std::ffi::OsString;
#[cfg(any(unix, features = "local-flow-stat"))]
use std::path::PathBuf;
use std::{
convert::{From, Infallible},
default::Default,
Expand All @@ -51,7 +51,7 @@ use std::{
io::Read,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
option::Option,
path::{Path, PathBuf},
path::Path,
str::FromStr,
string::ToString,
time::Duration,
Expand Down Expand Up @@ -127,6 +127,8 @@ struct SSConfig {
nofile: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
ipv6_first: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
fast_open: Option<bool>,
}

#[derive(Serialize, Deserialize, Debug, Default)]
Expand Down Expand Up @@ -717,6 +719,8 @@ pub struct Config {

/// Set `TCP_NODELAY` socket option
pub no_delay: bool,
/// Set `TCP_FASTOPEN` socket option
pub fast_open: bool,
/// `RLIMIT_NOFILE` option for *nix systems
pub nofile: Option<u64>,

Expand All @@ -725,7 +729,7 @@ pub struct Config {
pub outbound_fwmark: Option<u32>,
/// Set `SO_BINDTODEVICE` socket option for outbound sockets
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))]
pub outbound_bind_interface: Option<OsString>,
pub outbound_bind_interface: Option<String>,
/// Path to protect callback unix address, only for Android
#[cfg(target_os = "android")]
pub outbound_vpn_protect_path: Option<PathBuf>,
Expand Down Expand Up @@ -829,6 +833,7 @@ impl Config {
ipv6_first: false,

no_delay: false,
fast_open: false,
nofile: None,

#[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down Expand Up @@ -1275,6 +1280,11 @@ impl Config {
nconfig.no_delay = b;
}

// TCP fast open
if let Some(b) = config.fast_open {
nconfig.fast_open = b;
}

// UDP
nconfig.udp_timeout = config.udp_timeout.map(Duration::from_secs);

Expand Down Expand Up @@ -1758,6 +1768,10 @@ impl fmt::Display for Config {
jconf.no_delay = Some(self.no_delay);
}

if self.fast_open {
jconf.fast_open = Some(self.fast_open);
}

match self.dns {
DnsConfig::System => {}
#[cfg(feature = "trust-dns")]
Expand Down
8 changes: 4 additions & 4 deletions crates/shadowsocks-service/src/local/dns/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use shadowsocks::{
use tokio::net::UnixStream;
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
net::{TcpStream, UdpSocket},
net::UdpSocket,
time,
};
use trust_dns_resolver::proto::{
Expand All @@ -31,7 +31,7 @@ use crate::net::{FlowStat, MonProxySocket, MonProxyStream};
/// Collection of various DNS connections
pub enum DnsClient {
TcpLocal {
stream: TcpStream,
stream: ShadowTcpStream,
},
UdpLocal {
socket: UdpSocket,
Expand All @@ -42,7 +42,7 @@ pub enum DnsClient {
stream: UnixStream,
},
TcpRemote {
stream: ProxyClientStream<MonProxyStream<TcpStream>>,
stream: ProxyClientStream<MonProxyStream<ShadowTcpStream>>,
},
UdpRemote {
socket: MonProxySocket,
Expand All @@ -53,7 +53,7 @@ pub enum DnsClient {
impl DnsClient {
/// Connect to local provided TCP DNS server
pub async fn connect_tcp_local(ns: SocketAddr, connect_opts: &ConnectOpts) -> io::Result<DnsClient> {
let stream = ShadowTcpStream::connect_with_opts(&ns, connect_opts).await?.into();
let stream = ShadowTcpStream::connect_with_opts(&ns, connect_opts).await?;
Ok(DnsClient::TcpLocal { stream })
}

Expand Down
8 changes: 5 additions & 3 deletions crates/shadowsocks-service/src/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use futures::{
stream::{FuturesUnordered, StreamExt},
FutureExt,
};
use log::{error, trace, warn};
use log::{error, trace};
use shadowsocks::{
config::Mode,
net::{AcceptOpts, ConnectOpts},
Expand Down Expand Up @@ -53,7 +53,7 @@ pub async fn run(mut config: Config) -> io::Result<()> {
#[cfg(feature = "stream-cipher")]
for server in config.server.iter() {
if server.method().is_stream() {
warn!("stream cipher {} for server {} have inherent weaknesses (see discussion in https://github.com/shadowsocks/shadowsocks-org/issues/36). \
log::warn!("stream cipher {} for server {} have inherent weaknesses (see discussion in https://github.com/shadowsocks/shadowsocks-org/issues/36). \
DO NOT USE. It will be removed in the future.", server.method(), server.addr());
}
}
Expand All @@ -62,7 +62,7 @@ pub async fn run(mut config: Config) -> io::Result<()> {
if let Some(nofile) = config.nofile {
use crate::sys::set_nofile;
if let Err(err) = set_nofile(nofile) {
warn!("set_nofile {} failed, error: {}", nofile, err);
log::warn!("set_nofile {} failed, error: {}", nofile, err);
}
}

Expand All @@ -82,12 +82,14 @@ pub async fn run(mut config: Config) -> io::Result<()> {
};
connect_opts.tcp.send_buffer_size = config.outbound_send_buffer_size;
connect_opts.tcp.recv_buffer_size = config.outbound_recv_buffer_size;
connect_opts.tcp.fastopen = config.fast_open;
context.set_connect_opts(connect_opts);

let mut accept_opts = AcceptOpts::default();
accept_opts.tcp.send_buffer_size = config.inbound_send_buffer_size;
accept_opts.tcp.recv_buffer_size = config.inbound_recv_buffer_size;
accept_opts.tcp.nodelay = config.no_delay;
accept_opts.tcp.fastopen = config.fast_open;

if let Some(resolver) = build_dns_resolver(config.dns, config.ipv6_first, context.connect_opts_ref()).await {
context.set_dns_resolver(Arc::new(resolver));
Expand Down
26 changes: 10 additions & 16 deletions crates/shadowsocks-service/src/local/net/tcp/auto_proxy_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ use shadowsocks::{
tcprelay::proxy_stream::{ProxyClientStream, ProxyClientStreamReadHalf, ProxyClientStreamWriteHalf},
},
};
use tokio::{
io::{AsyncRead, AsyncWrite, ReadBuf},
net::{
tcp::{OwnedReadHalf, OwnedWriteHalf},
TcpStream as TokioTcpStream,
},
};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf, ReadHalf, WriteHalf};

use crate::{
local::{context::ServiceContext, loadbalancing::ServerIdent},
Expand All @@ -34,8 +28,8 @@ use super::auto_proxy_io::AutoProxyIo;
/// Unified stream for bypassed and proxied connections
#[pin_project(project = AutoProxyClientStreamProj)]
pub enum AutoProxyClientStream {
Proxied(#[pin] ProxyClientStream<MonProxyStream<TokioTcpStream>>),
Bypassed(#[pin] TokioTcpStream),
Proxied(#[pin] ProxyClientStream<MonProxyStream<TcpStream>>),
Bypassed(#[pin] TcpStream),
}

impl AutoProxyClientStream {
Expand Down Expand Up @@ -160,8 +154,8 @@ impl AsyncWrite for AutoProxyClientStream {
}
}

impl From<ProxyClientStream<MonProxyStream<TokioTcpStream>>> for AutoProxyClientStream {
fn from(s: ProxyClientStream<MonProxyStream<TokioTcpStream>>) -> Self {
impl From<ProxyClientStream<MonProxyStream<TcpStream>>> for AutoProxyClientStream {
fn from(s: ProxyClientStream<MonProxyStream<TcpStream>>) -> Self {
AutoProxyClientStream::Proxied(s)
}
}
Expand All @@ -177,7 +171,7 @@ impl AutoProxyClientStream {
)
}
AutoProxyClientStream::Bypassed(s) => {
let (r, w) = s.into_split();
let (r, w) = tokio::io::split(s);
(
AutoProxyClientStreamReadHalf::Bypassed(r),
AutoProxyClientStreamWriteHalf::Bypassed(w),
Expand All @@ -189,8 +183,8 @@ impl AutoProxyClientStream {

#[pin_project(project = AutoProxyClientStreamReadHalfProj)]
pub enum AutoProxyClientStreamReadHalf {
Proxied(#[pin] ProxyClientStreamReadHalf<MonProxyStream<TokioTcpStream>>),
Bypassed(#[pin] OwnedReadHalf),
Proxied(#[pin] ProxyClientStreamReadHalf<MonProxyStream<TcpStream>>),
Bypassed(#[pin] ReadHalf<TcpStream>),
}

impl AutoProxyIo for AutoProxyClientStreamReadHalf {
Expand All @@ -210,8 +204,8 @@ impl AsyncRead for AutoProxyClientStreamReadHalf {

#[pin_project(project = AutoProxyClientStreamWriteHalfProj)]
pub enum AutoProxyClientStreamWriteHalf {
Proxied(#[pin] ProxyClientStreamWriteHalf<MonProxyStream<TokioTcpStream>>),
Bypassed(#[pin] OwnedWriteHalf),
Proxied(#[pin] ProxyClientStreamWriteHalf<MonProxyStream<TcpStream>>),
Bypassed(#[pin] WriteHalf<TcpStream>),
}

impl AutoProxyIo for AutoProxyClientStreamWriteHalf {
Expand Down
Loading

0 comments on commit 9496b2c

Please sign in to comment.