Skip to content

Commit

Permalink
Upgrade to tokio 1.0 (#358)
Browse files Browse the repository at this point in the history
* upgrade to tokio v1.0

- removed tokio::prelude
- upgrade hyper to v0.14, tokio-rustls to v0.22

still working on migrating trust-dns-* and tokio-native-tls

ref #354

* tokio v1.0 removed with_poll

fix #355, ref #354

* removed CTRL-BREAK signal handler

ref #354

* fixes compliation error, add missing return

fixes #355

* allow setting SO_SNDBUF and SO_RCVBUF for sockets

ref #352

* completely removed unix socket based DNS resolving

ref shadowsocks/shadowsocks-android#2622

* fix build issue on Windows

* fixed uds poll_write loop, fixed udp outbound loopback check

ref #355

* disable default trust-dns resolver for andorid, macos, ios

This commit also:

- Optimized resolve() logging with elapsed time
- updated tokio-native-tls

* local-dns removed from default features

* fix rustc version with rust-toolchain

* Bump bytes from 0.6.0 to 1.0.0

* add dependabot badge

* indirectly depend on trust_dns_proto via trust_dns_resolver

* auto reconnect if udp sendto failed

* recreate proxied socket if recv() returns failure

* increase score precision to 0.0001

* example of log4rs configuration

* PingBalancer instance shouldn't kill probing task when dropping

- Probing task should be controlled by the internal shared state

* switch to trust-dns main for latest tokio 1.0 support
  • Loading branch information
zonyitoo authored Dec 28, 2020
1 parent 4d32f6d commit 303f96c
Show file tree
Hide file tree
Showing 54 changed files with 1,906 additions and 1,180 deletions.
285 changes: 223 additions & 62 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ default = [
"server",
"manager",
"utility",
"local-dns",
"local-http",
"local-http-rustls",
"local-tunnel",
Expand Down Expand Up @@ -112,7 +111,7 @@ cfg-if = "1"
qrcode = { version = "0.12", default-features = false }

futures = "0.3"
tokio = { version = "0.3.1", features = ["full"] }
tokio = { version = "1.0", features = ["rt", "rt-multi-thread", "signal"] }

mimalloc = { version = "0.1", optional = true }
tcmalloc = { version = "0.3", optional = true }
Expand All @@ -127,3 +126,4 @@ daemonize = "0.4"
byteorder = "1.3"
env_logger = "0.8"
byte_string = "1.0"
tokio = { version = "1.0", features = ["net", "time", "macros", "io-util"]}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[![License](https://img.shields.io/github/license/zonyitoo/shadowsocks-rust.svg)](https://github.com/zonyitoo/shadowsocks-rust)
[![crates.io](https://img.shields.io/crates/v/shadowsocks-rust.svg)](https://crates.io/crates/shadowsocks-rust)
[![Release](https://img.shields.io/github/release/shadowsocks/shadowsocks-rust.svg)](https://github.com/shadowsocks/shadowsocks-rust/releases)
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=shadowsocks%2fshadowsocks-rust)](https://dependabot.com)

This is a port of [shadowsocks](https://github.com/shadowsocks/shadowsocks).

Expand Down
17 changes: 3 additions & 14 deletions bin/common/monitor/windows.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
use futures::{
future::{self, Either, FutureExt},
StreamExt,
};
use log::info;
use std::io;
use tokio::signal::{ctrl_c, windows::ctrl_break};
use tokio::signal::ctrl_c;

pub async fn create_signal_monitor() -> io::Result<()> {
let cc = ctrl_c();
let cb = ctrl_break()?;

let signal_name = match future::select(cc.boxed(), cb.into_future().boxed()).await {
Either::Left(..) => "CTRL-C",
Either::Right(..) => "CTRL-BREAK",
};

info!("received {}, exiting", signal_name);
ctrl_c().await;
info!("received CTRL-C, exiting");

Ok(())
}
8 changes: 0 additions & 8 deletions bin/common/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use std::net::SocketAddr;

#[cfg(feature = "local-dns")]
use shadowsocks_service::config::NameServerAddr;
use shadowsocks_service::shadowsocks::{relay::socks5::Address, ManagerAddr, ServerAddr, ServerConfig};

macro_rules! validate_type {
Expand All @@ -31,12 +29,6 @@ validate_type!(
ManagerAddr,
"should be either ip:port, domain:port or /path/to/unix.sock"
);
#[cfg(feature = "local-dns")]
validate_type!(
validate_name_server_addr,
NameServerAddr,
"should be either ip:port, domain:port or /path/to/unix.sock"
);
validate_type!(validate_u64, u64, "should be unsigned integer");
validate_type!(validate_u32, u32, "should be unsigned integer");

Expand Down
30 changes: 21 additions & 9 deletions bin/sslocal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ fn main() {
(@arg UDP_MAX_ASSOCIATIONS: --("udp-max-associations") +takes_value {validator::validate_u64} "Maximum associations to be kept simultaneously for UDP relay")

(@arg UDP_BIND_ADDR: --("udp-bind-addr") +takes_value {validator::validate_server_addr} "UDP relay's bind address, default is the same as local-addr")

(@arg INBOUND_SEND_BUFFER_SIZE: --("inbound-send-buffer-size") +takes_value {validator::validate_u32} "Set inbound sockets' SO_SNDBUF option")
(@arg INBOUND_RECV_BUFFER_SIZE: --("inbound-redv-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-redv-buffer-size") +takes_value {validator::validate_u32} "Set outbound sockets' SO_RCVBUF option")
);

// FIXME: -6 is not a identifier, so we cannot build it with clap_app!
Expand Down Expand Up @@ -137,7 +142,7 @@ fn main() {
#[cfg(feature = "local-dns")]
{
app = clap_app!(@app (app)
(@arg LOCAL_DNS_ADDR: --("local-dns-addr") +takes_value required_if("PROTOCOL", "dns") {validator::validate_name_server_addr} "Specify the address of local DNS server, send queries directly")
(@arg LOCAL_DNS_ADDR: --("local-dns-addr") +takes_value required_if("PROTOCOL", "dns") {validator::validate_socket_addr} "Specify the address of local DNS server, send queries directly")
(@arg REMOTE_DNS_ADDR: --("remote-dns-addr") +takes_value required_if("PROTOCOL", "dns") {validator::validate_address} "Specify the address of remote DNS server, send queries through shadowsocks' tunnel")
(@arg DNS_LOCAL_ADDR: --("dns-addr") +takes_value requires_all(&["REMOTE_DNS_ADDR"]) {validator::validate_server_addr} "DNS address, listen to this address if specified")
);
Expand Down Expand Up @@ -235,10 +240,10 @@ fn main() {

#[cfg(feature = "local-dns")]
{
use shadowsocks_service::config::NameServerAddr;
use std::net::SocketAddr;

if let Some(local_dns_addr) = matches.value_of("LOCAL_DNS_ADDR") {
let addr = local_dns_addr.parse::<NameServerAddr>().expect("local dns address");
let addr = local_dns_addr.parse::<SocketAddr>().expect("local dns address");
config.local_dns_addr = Some(addr);
}

Expand All @@ -258,12 +263,6 @@ fn main() {
// A socket `protect_path` in CWD
// Same as shadowsocks-libev's android.c
config.outbound_vpn_protect_path = Some(From::from("protect_path"));

// Set default config.local_dns_addr
#[cfg(feature = "local-dns")]
if config.local_dns_addr.is_none() {
config.local_dns_addr = Some(From::from("local_dns_path"));
}
}

if let Some(local_addr) = matches.value_of("LOCAL_ADDR") {
Expand Down Expand Up @@ -341,6 +340,19 @@ fn main() {
config.udp_bind_addr = Some(udp_bind_addr.parse::<ServerAddr>().expect("udp-bind-addr"));
}

if let Some(bs) = matches.value_of("INBOUND_SEND_BUFFER_SIZE") {
config.inbound_send_buffer_size = Some(bs.parse::<u32>().expect("inbound-send-buffer-size"));
}
if let Some(bs) = matches.value_of("INBOUND_RECV_BUFFER_SIZE") {
config.inbound_recv_buffer_size = Some(bs.parse::<u32>().expect("inbound-recv-buffer-size"));
}
if let Some(bs) = matches.value_of("OUTBOUND_SEND_BUFFER_SIZE") {
config.outbound_send_buffer_size = Some(bs.parse::<u32>().expect("outbound-send-buffer-size"));
}
if let Some(bs) = matches.value_of("OUTBOUND_RECV_BUFFER_SIZE") {
config.outbound_recv_buffer_size = Some(bs.parse::<u32>().expect("outbound-recv-buffer-size"));
}

// DONE READING options

if config.local_addr.is_none() {
Expand Down
18 changes: 18 additions & 0 deletions bin/ssmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ fn main() {

(@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 INBOUND_SEND_BUFFER_SIZE: --("inbound-send-buffer-size") +takes_value {validator::validate_u32} "Set inbound sockets' SO_SNDBUF option")
(@arg INBOUND_RECV_BUFFER_SIZE: --("inbound-redv-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-redv-buffer-size") +takes_value {validator::validate_u32} "Set outbound sockets' SO_RCVBUF option")
);

#[cfg(feature = "logging")]
Expand Down Expand Up @@ -191,6 +196,19 @@ fn main() {
config.ipv6_first = true;
}

if let Some(bs) = matches.value_of("INBOUND_SEND_BUFFER_SIZE") {
config.inbound_send_buffer_size = Some(bs.parse::<u32>().expect("inbound-send-buffer-size"));
}
if let Some(bs) = matches.value_of("INBOUND_RECV_BUFFER_SIZE") {
config.inbound_recv_buffer_size = Some(bs.parse::<u32>().expect("inbound-recv-buffer-size"));
}
if let Some(bs) = matches.value_of("OUTBOUND_SEND_BUFFER_SIZE") {
config.outbound_send_buffer_size = Some(bs.parse::<u32>().expect("outbound-send-buffer-size"));
}
if let Some(bs) = matches.value_of("OUTBOUND_RECV_BUFFER_SIZE") {
config.outbound_recv_buffer_size = Some(bs.parse::<u32>().expect("outbound-recv-buffer-size"));
}

// DONE reading options

if config.manager.is_none() {
Expand Down
18 changes: 18 additions & 0 deletions bin/ssserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ fn main() {

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

(@arg INBOUND_SEND_BUFFER_SIZE: --("inbound-send-buffer-size") +takes_value {validator::validate_u32} "Set inbound sockets' SO_SNDBUF option")
(@arg INBOUND_RECV_BUFFER_SIZE: --("inbound-redv-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-redv-buffer-size") +takes_value {validator::validate_u32} "Set outbound sockets' SO_RCVBUF option")
);

#[cfg(feature = "logging")]
Expand Down Expand Up @@ -217,6 +222,19 @@ fn main() {
config.udp_max_associations = Some(udp_max_assoc.parse::<usize>().expect("udp-max-associations"));
}

if let Some(bs) = matches.value_of("INBOUND_SEND_BUFFER_SIZE") {
config.inbound_send_buffer_size = Some(bs.parse::<u32>().expect("inbound-send-buffer-size"));
}
if let Some(bs) = matches.value_of("INBOUND_RECV_BUFFER_SIZE") {
config.inbound_recv_buffer_size = Some(bs.parse::<u32>().expect("inbound-recv-buffer-size"));
}
if let Some(bs) = matches.value_of("OUTBOUND_SEND_BUFFER_SIZE") {
config.outbound_send_buffer_size = Some(bs.parse::<u32>().expect("outbound-send-buffer-size"));
}
if let Some(bs) = matches.value_of("OUTBOUND_RECV_BUFFER_SIZE") {
config.outbound_recv_buffer_size = Some(bs.parse::<u32>().expect("outbound-recv-buffer-size"));
}

// DONE READING options

if config.server.is_empty() {
Expand Down
25 changes: 25 additions & 0 deletions configs/log4rs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
refresh_rate: 30 seconds
appenders:
stdout:
kind: console
encoder:
pattern: "{d} {h({l}):<5} {m}{n}"
file:
kind: rolling_file
path: shadowsocks.log
encoder:
kind: pattern
pattern: "{d} {h({l}):<5} {m}{n}"
policy:
trigger:
kind: size
limit: 10 mb
roller:
kind: fixed_window
pattern: shadowsocks.{}.log
count: 5
root:
level: debug
appenders:
- stdout
- file
16 changes: 7 additions & 9 deletions crates/shadowsocks-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ full = [
"server",
"manager",
"trust-dns",
"local-dns",
"local-http",
"local-http-rustls",
"local-redir",
Expand All @@ -42,7 +41,7 @@ dns-over-tls = ["trust-dns", "trust-dns-resolver/dns-over-tls", "trust-dns-resol
dns-over-https = ["trust-dns", "trust-dns-resolver/dns-over-https"]

# Enable DNS-relay
local-dns = ["local", "trust-dns-proto", "rand"]
local-dns = ["local", "trust-dns", "rand"]
# Backward compatibility, DO NOT USE
local-dns-relay = ["local-dns"]
# Enable client flow statistic report
Expand Down Expand Up @@ -72,16 +71,16 @@ thiserror = "1.0"

spin = "0.7"
lru_time_cache = "0.11"
bytes = "0.6"
bytes = "1.0"
byte_string = "1.0"
byteorder = "1.3"
rand = { version = "0.8", optional = true }

futures = "0.3"
tokio = { version = "0.3.1", features = ["full"] }
tokio-native-tls = { version = "0.2", optional = true }
tokio = { version = "1.0", features = ["full"] }
tokio-native-tls = { version = "0.3", optional = true }
native-tls = { version = "0.2", optional = true }
tokio-rustls = { version = "0.21", optional = true }
tokio-rustls = { version = "0.22", optional = true }
webpki-roots = { version = "0.21", optional = true }
rustls-native-certs = { version = "0.5", optional = true }
async-trait = "0.1"
Expand All @@ -91,11 +90,10 @@ socket2 = "0.3"
libc = "0.2"

http = { version = "0.2", optional = true }
hyper = { git = "https://github.com/hyperium/hyper.git", optional = true, features = ["full"] }
tower = { git = "https://github.com/tower-rs/tower.git", optional = true }
hyper = { version = "0.14", optional = true, features = ["full"] }
tower = { version = "0.3", optional = true }

trust-dns-resolver = { git = "https://github.com/bluejekyll/trust-dns.git", branch = "main", optional = true, features = ["serde-config"] }
trust-dns-proto = { git = "https://github.com/bluejekyll/trust-dns.git", branch = "main", optional = true }

ipnet = "2.3"
iprange = "0.6"
Expand Down
Loading

0 comments on commit 303f96c

Please sign in to comment.