Skip to content

Commit 96eeded

Browse files
committed
support TFO on Linux, FreeBSD, macOS and Windows
- ref #184
1 parent 50286dc commit 96eeded

File tree

35 files changed

+1803
-669
lines changed

35 files changed

+1803
-669
lines changed

.github/workflows/build-and-test.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,16 @@ jobs:
3030
profile: minimal
3131
- name: Build & Test (Default)
3232
run: cargo test --verbose --no-fail-fast
33+
- name: Build & Test (Default) - shadowsocks
34+
run: cargo test --manifest-path ./crates/shadowsocks/Cargo.toml --verbose --no-fail-fast
3335
- name: Build & Test (--no-default-features)
3436
run: cargo test --verbose --no-default-features --no-fail-fast
37+
- name: Build & Test (--no-default-features) - shadowsocks
38+
run: cargo test --manifest-path ./crates/shadowsocks/Cargo.toml --verbose --no-default-features --no-fail-fast
3539
- name: Build with All Features Enabled
36-
run: cargo build --verbose --features "local-redir local-dns dns-over-tls dns-over-https stream-cipher"
40+
run: cargo build --verbose --features "local-dns dns-over-tls dns-over-https stream-cipher"
41+
- name: Build with All Features Enabled - shadowsocks
42+
run: cargo build --manifest-path ./crates/shadowsocks/Cargo.toml --verbose --features "stream-cipher"
3743

3844
build-windows:
3945
runs-on: windows-latest
@@ -53,10 +59,16 @@ jobs:
5359
profile: minimal
5460
- name: Build & Test (Default)
5561
run: cargo test --verbose --no-fail-fast
62+
- name: Build & Test (Default) - shadowsocks
63+
run: cargo test --manifest-path ./crates/shadowsocks/Cargo.toml --verbose --no-fail-fast
5664
- name: Build & Test (--no-default-features)
5765
run: cargo test --verbose --no-default-features --no-fail-fast
66+
- name: Build & Test (--no-default-features) - shadowsocks
67+
run: cargo test --manifest-path ./crates/shadowsocks/Cargo.toml --verbose --no-default-features --no-fail-fast
5868
- name: Build with All Features Enabled
5969
run: cargo build --verbose --features "local-dns dns-over-tls dns-over-https stream-cipher"
70+
- name: Build with All Features Enabled - shadowsocks
71+
run: cargo build --manifest-path ./crates/shadowsocks/Cargo.toml --verbose --features "stream-cipher"
6072

6173
build-macos:
6274
runs-on: macos-latest
@@ -81,7 +93,13 @@ jobs:
8193
profile: minimal
8294
- name: Build & Test (Default)
8395
run: cargo test --verbose --no-fail-fast
96+
- name: Build & Test (Default) - shadowsocks
97+
run: cargo test --manifest-path ./crates/shadowsocks/Cargo.toml --verbose --no-fail-fast
8498
- name: Build & Test (--no-default-features)
8599
run: cargo test --verbose --no-default-features --no-fail-fast
100+
- name: Build & Test (--no-default-features) - shadowsocks
101+
run: cargo test --manifest-path ./crates/shadowsocks/Cargo.toml --verbose --no-default-features --no-fail-fast
86102
- name: Build with All Features Enabled
87-
run: cargo build --verbose --features "local-redir local-dns dns-over-tls dns-over-https stream-cipher"
103+
run: cargo build --verbose --features "local-dns dns-over-tls dns-over-https stream-cipher"
104+
- name: Build with All Features Enabled - shadowsocks
105+
run: cargo build --manifest-path ./crates/shadowsocks/Cargo.toml --verbose --features "stream-cipher"

Cargo.lock

Lines changed: 23 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shadowsocks-rust"
3-
version = "1.10.5"
3+
version = "1.11.0"
44
authors = ["Shadowsocks Contributors"]
55
description = "shadowsocks is a fast tunnel proxy that helps you bypass firewalls."
66
repository = "https://github.com/shadowsocks/shadowsocks-rust"
@@ -132,3 +132,6 @@ byteorder = "1.3"
132132
env_logger = "0.8"
133133
byte_string = "1.0"
134134
tokio = { version = "1", features = ["net", "time", "macros", "io-util"]}
135+
136+
[patch.crates-io]
137+
tokio = { git = "https://github.com/tokio-rs/tokio.git", features = ["full"] }

bin/sslocal.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ fn main() {
6666
(@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\"")
6767

6868
(@arg TCP_NO_DELAY: --("tcp-no-delay") !takes_value alias("no-delay") "Set TCP_NODELAY option for socket")
69+
(@arg TCP_FAST_OPEN: --("tcp-fast-open") !takes_value alias("fast-open") "Enable TCP Fast Open (TFO)")
6970

7071
(@arg UDP_TIMEOUT: --("udp-timeout") +takes_value {validator::validate_u64} "Timeout seconds for UDP relay")
7172
(@arg UDP_MAX_ASSOCIATIONS: --("udp-max-associations") +takes_value {validator::validate_u64} "Maximum associations to be kept simultaneously for UDP relay")
7273

73-
7474
(@arg INBOUND_SEND_BUFFER_SIZE: --("inbound-send-buffer-size") +takes_value {validator::validate_u32} "Set inbound sockets' SO_SNDBUF option")
7575
(@arg INBOUND_RECV_BUFFER_SIZE: --("inbound-recv-buffer-size") +takes_value {validator::validate_u32} "Set inbound sockets' SO_RCVBUF option")
7676
(@arg OUTBOUND_SEND_BUFFER_SIZE: --("outbound-send-buffer-size") +takes_value {validator::validate_u32} "Set outbound sockets' SO_SNDBUF option")
@@ -346,6 +346,10 @@ fn main() {
346346
config.no_delay = true;
347347
}
348348

349+
if matches.is_present("TCP_FAST_OPEN") {
350+
config.fast_open = true;
351+
}
352+
349353
#[cfg(any(target_os = "linux", target_os = "android"))]
350354
if let Some(mark) = matches.value_of("OUTBOUND_FWMARK") {
351355
config.outbound_fwmark = Some(mark.parse::<u32>().expect("an unsigned integer for `outbound-fwmark`"));

bin/ssmanager.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ fn main() {
5151
(@arg BIND_ADDR: -b --("bind-addr") +takes_value {validator::validate_ip_addr} "Bind address, outbound socket will bind this address")
5252
(@arg SERVER_HOST: -s --("server-host") +takes_value "Host name or IP address of your remote server")
5353

54-
5554
(@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")
5655
(@arg ENCRYPT_METHOD: -m --("encrypt-method") +takes_value possible_values(available_ciphers()) "Default encryption method")
5756
(@arg TIMEOUT: --timeout +takes_value {validator::validate_u64} "Default timeout seconds for TCP relay")
@@ -60,6 +59,7 @@ fn main() {
6059
(@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\"")
6160

6261
(@arg TCP_NO_DELAY: --("tcp-no-delay") !takes_value alias("no-delay") "Set TCP_NODELAY option for socket")
62+
(@arg TCP_FAST_OPEN: --("tcp-fast-open") !takes_value alias("fast-open") "Enable TCP Fast Open (TFO)")
6363

6464
(@arg UDP_TIMEOUT: --("udp-timeout") +takes_value {validator::validate_u64} "Timeout seconds for UDP relay")
6565
(@arg UDP_MAX_ASSOCIATIONS: --("udp-max-associations") +takes_value {validator::validate_u64} "Maximum associations to be kept simultaneously for UDP relay")
@@ -154,6 +154,10 @@ fn main() {
154154
config.no_delay = true;
155155
}
156156

157+
if matches.is_present("TCP_FAST_OPEN") {
158+
config.fast_open = true;
159+
}
160+
157161
#[cfg(any(target_os = "linux", target_os = "android"))]
158162
if let Some(mark) = matches.value_of("OUTBOUND_FWMARK") {
159163
config.outbound_fwmark = Some(mark.parse::<u32>().expect("an unsigned integer for `outbound-fwmark`"));

bin/ssserver.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ fn main() {
6161
(@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\"")
6262

6363
(@arg TCP_NO_DELAY: --("tcp-no-delay") !takes_value alias("no-delay") "Set TCP_NODELAY option for socket")
64+
(@arg TCP_FAST_OPEN: --("tcp-fast-open") !takes_value alias("fast-open") "Enable TCP Fast Open (TFO)")
6465

6566
(@arg UDP_TIMEOUT: --("udp-timeout") +takes_value {validator::validate_u64} "Timeout seconds for UDP relay")
6667
(@arg UDP_MAX_ASSOCIATIONS: --("udp-max-associations") +takes_value {validator::validate_u64} "Maximum associations to be kept simultaneously for UDP relay")
@@ -194,6 +195,10 @@ fn main() {
194195
config.no_delay = true;
195196
}
196197

198+
if matches.is_present("TCP_FAST_OPEN") {
199+
config.fast_open = true;
200+
}
201+
197202
#[cfg(any(target_os = "linux", target_os = "android"))]
198203
if let Some(mark) = matches.value_of("OUTBOUND_FWMARK") {
199204
config.outbound_fwmark = Some(mark.parse::<u32>().expect("an unsigned integer for `outbound-fwmark`"));

crates/shadowsocks-service/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "shadowsocks-service"
3-
version = "1.10.4"
3+
version = "1.11.0"
44
authors = ["Shadowsocks Contributors"]
55
description = "shadowsocks is a fast tunnel proxy that helps you bypass firewalls."
66
repository = "https://github.com/shadowsocks/shadowsocks-rust"

crates/shadowsocks-service/src/config.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
//!
4242
//! These defined server will be used with a load balancing algorithm.
4343
44-
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))]
45-
use std::ffi::OsString;
44+
#[cfg(any(unix, features = "local-flow-stat"))]
45+
use std::path::PathBuf;
4646
use std::{
4747
convert::{From, Infallible},
4848
default::Default,
@@ -51,7 +51,7 @@ use std::{
5151
io::Read,
5252
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
5353
option::Option,
54-
path::{Path, PathBuf},
54+
path::Path,
5555
str::FromStr,
5656
string::ToString,
5757
time::Duration,
@@ -128,6 +128,8 @@ struct SSConfig {
128128
nofile: Option<u64>,
129129
#[serde(skip_serializing_if = "Option::is_none")]
130130
ipv6_first: Option<bool>,
131+
#[serde(skip_serializing_if = "Option::is_none")]
132+
fast_open: Option<bool>,
131133
}
132134

133135
#[derive(Serialize, Deserialize, Debug, Default)]
@@ -718,6 +720,8 @@ pub struct Config {
718720

719721
/// Set `TCP_NODELAY` socket option
720722
pub no_delay: bool,
723+
/// Set `TCP_FASTOPEN` socket option
724+
pub fast_open: bool,
721725
/// `RLIMIT_NOFILE` option for *nix systems
722726
#[cfg(all(unix, not(target_os = "android")))]
723727
pub nofile: Option<u64>,
@@ -727,7 +731,7 @@ pub struct Config {
727731
pub outbound_fwmark: Option<u32>,
728732
/// Set `SO_BINDTODEVICE` socket option for outbound sockets
729733
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios"))]
730-
pub outbound_bind_interface: Option<OsString>,
734+
pub outbound_bind_interface: Option<String>,
731735
/// Path to protect callback unix address, only for Android
732736
#[cfg(target_os = "android")]
733737
pub outbound_vpn_protect_path: Option<PathBuf>,
@@ -831,6 +835,7 @@ impl Config {
831835
ipv6_first: false,
832836

833837
no_delay: false,
838+
fast_open: false,
834839
#[cfg(all(unix, not(target_os = "android")))]
835840
nofile: None,
836841

@@ -1278,6 +1283,11 @@ impl Config {
12781283
nconfig.no_delay = b;
12791284
}
12801285

1286+
// TCP fast open
1287+
if let Some(b) = config.fast_open {
1288+
nconfig.fast_open = b;
1289+
}
1290+
12811291
// UDP
12821292
nconfig.udp_timeout = config.udp_timeout.map(Duration::from_secs);
12831293

@@ -1764,6 +1774,10 @@ impl fmt::Display for Config {
17641774
jconf.no_delay = Some(self.no_delay);
17651775
}
17661776

1777+
if self.fast_open {
1778+
jconf.fast_open = Some(self.fast_open);
1779+
}
1780+
17671781
match self.dns {
17681782
DnsConfig::System => {}
17691783
#[cfg(feature = "trust-dns")]

crates/shadowsocks-service/src/local/dns/upstream.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use shadowsocks::{
1818
use tokio::net::UnixStream;
1919
use tokio::{
2020
io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
21-
net::{TcpStream, UdpSocket},
21+
net::UdpSocket,
2222
time,
2323
};
2424
use trust_dns_resolver::proto::{
@@ -31,7 +31,7 @@ use crate::net::{FlowStat, MonProxySocket, MonProxyStream};
3131
/// Collection of various DNS connections
3232
pub enum DnsClient {
3333
TcpLocal {
34-
stream: TcpStream,
34+
stream: ShadowTcpStream,
3535
},
3636
UdpLocal {
3737
socket: UdpSocket,
@@ -42,7 +42,7 @@ pub enum DnsClient {
4242
stream: UnixStream,
4343
},
4444
TcpRemote {
45-
stream: ProxyClientStream<MonProxyStream<TcpStream>>,
45+
stream: ProxyClientStream<MonProxyStream<ShadowTcpStream>>,
4646
},
4747
UdpRemote {
4848
socket: MonProxySocket,
@@ -53,7 +53,7 @@ pub enum DnsClient {
5353
impl DnsClient {
5454
/// Connect to local provided TCP DNS server
5555
pub async fn connect_tcp_local(ns: SocketAddr, connect_opts: &ConnectOpts) -> io::Result<DnsClient> {
56-
let stream = ShadowTcpStream::connect_with_opts(&ns, connect_opts).await?.into();
56+
let stream = ShadowTcpStream::connect_with_opts(&ns, connect_opts).await?;
5757
Ok(DnsClient::TcpLocal { stream })
5858
}
5959

0 commit comments

Comments
 (0)