Skip to content

Commit

Permalink
all tcp sockets will set with default Keep Alive timeout 15s
Browse files Browse the repository at this point in the history
- fixes #546
- ref #490
  • Loading branch information
zonyitoo committed Jun 5, 2021
1 parent df87125 commit a8de054
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
4 changes: 4 additions & 0 deletions crates/shadowsocks-service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,10 @@ pub struct Config {
/// Set `TCP_FASTOPEN` socket option
pub fast_open: bool,
/// Set TCP Keep-Alive duration, will set both `TCP_KEEPIDLE` and `TCP_KEEPINTVL`
///
/// https://github.com/shadowsocks/shadowsocks-rust/issues/546
///
/// If this is not set, sockets will be set with a default timeout
pub keep_alive: Option<Duration>,

/// `RLIMIT_NOFILE` option for *nix systems
Expand Down
9 changes: 7 additions & 2 deletions crates/shadowsocks-service/src/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ pub mod socks;
pub mod tunnel;
pub mod utils;

/// Default TCP Keep Alive timeout
///
/// This is borrowed from Go's `net` library's default setting
pub(crate) const LOCAL_DEFAULT_KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(15);

/// Starts a shadowsocks local server
pub async fn run(mut config: Config) -> io::Result<()> {
assert!(config.config_type == ConfigType::Local && !config.local.is_empty());
Expand Down Expand Up @@ -84,15 +89,15 @@ pub async fn run(mut config: Config) -> io::Result<()> {
connect_opts.tcp.recv_buffer_size = config.outbound_recv_buffer_size;
connect_opts.tcp.nodelay = config.no_delay;
connect_opts.tcp.fastopen = config.fast_open;
connect_opts.tcp.keepalive = config.keep_alive;
connect_opts.tcp.keepalive = config.keep_alive.or(Some(LOCAL_DEFAULT_KEEPALIVE_TIMEOUT));
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;
accept_opts.tcp.keepalive = config.keep_alive;
accept_opts.tcp.keepalive = config.keep_alive.or(Some(LOCAL_DEFAULT_KEEPALIVE_TIMEOUT));

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
5 changes: 3 additions & 2 deletions crates/shadowsocks-service/src/manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use shadowsocks::net::{AcceptOpts, ConnectOpts};
use crate::{
config::{Config, ConfigType},
dns::build_dns_resolver,
server::SERVER_DEFAULT_KEEPALIVE_TIMEOUT,
};

pub use self::server::Manager;
Expand Down Expand Up @@ -51,14 +52,14 @@ pub async fn run(config: Config) -> io::Result<()> {
connect_opts.tcp.recv_buffer_size = config.outbound_recv_buffer_size;
connect_opts.tcp.nodelay = config.no_delay;
connect_opts.tcp.fastopen = config.fast_open;
connect_opts.tcp.keepalive = config.keep_alive;
connect_opts.tcp.keepalive = config.keep_alive.or(Some(SERVER_DEFAULT_KEEPALIVE_TIMEOUT));

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;
accept_opts.tcp.keepalive = config.keep_alive;
accept_opts.tcp.keepalive = config.keep_alive.or(Some(SERVER_DEFAULT_KEEPALIVE_TIMEOUT));

if let Some(resolver) = build_dns_resolver(config.dns, config.ipv6_first, &connect_opts).await {
manager.set_dns_resolver(Arc::new(resolver));
Expand Down
11 changes: 8 additions & 3 deletions crates/shadowsocks-service/src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Shadowsocks server

use std::{io, sync::Arc};
use std::{io, sync::Arc, time::Duration};

use futures::{future, FutureExt};
use log::trace;
Expand All @@ -19,6 +19,11 @@ pub mod server;
mod tcprelay;
mod udprelay;

/// Default TCP Keep Alive timeout
///
/// This is borrowed from Go's `net` library's default setting
pub(crate) const SERVER_DEFAULT_KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(15);

/// Starts a shadowsocks server
pub async fn run(config: Config) -> io::Result<()> {
assert_eq!(config.config_type, ConfigType::Server);
Expand Down Expand Up @@ -64,14 +69,14 @@ pub async fn run(config: Config) -> io::Result<()> {
connect_opts.tcp.recv_buffer_size = config.outbound_recv_buffer_size;
connect_opts.tcp.nodelay = config.no_delay;
connect_opts.tcp.fastopen = config.fast_open;
connect_opts.tcp.keepalive = config.keep_alive;
connect_opts.tcp.keepalive = config.keep_alive.or(Some(SERVER_DEFAULT_KEEPALIVE_TIMEOUT));

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;
accept_opts.tcp.keepalive = config.keep_alive;
accept_opts.tcp.keepalive = config.keep_alive.or(Some(SERVER_DEFAULT_KEEPALIVE_TIMEOUT));

let resolver = build_dns_resolver(config.dns, config.ipv6_first, &connect_opts)
.await
Expand Down

0 comments on commit a8de054

Please sign in to comment.