Created
June 20, 2016 15:31
-
-
Save secondwtq/c1ec0ac63f8ad0811dc45d368634a7e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 6c3df80118881edbb05a51ef9fb06bc13e75cdc4 Mon Sep 17 00:00:00 2001 | |
From: secondwtq <lovejay-lovemusic@outlook.com> | |
Date: Mon, 20 Jun 2016 01:11:11 +0800 | |
Subject: [PATCH] Add support for TCP Keep Alive. | |
--- | |
shadowsocks/shell.py | 12 ++++++++++-- | |
shadowsocks/tcprelay.py | 4 ++++ | |
2 files changed, 14 insertions(+), 2 deletions(-) | |
diff --git a/shadowsocks/shell.py b/shadowsocks/shell.py | |
index 42efbc0..0895611 100644 | |
--- a/shadowsocks/shell.py | |
+++ b/shadowsocks/shell.py | |
@@ -135,10 +135,10 @@ def get_config(is_local): | |
longopts = ['help', 'fast-open', 'pid-file=', 'log-file=', 'user=', | |
'version'] | |
else: | |
- shortopts = 'hd:s:p:k:m:c:t:vqa' | |
+ shortopts = 'hd:s:p:k:m:c:t:i:vqa' | |
longopts = ['help', 'fast-open', 'pid-file=', 'log-file=', 'workers=', | |
'forbidden-ip=', 'user=', 'manager-address=', 'version', | |
- 'prefer-ipv6'] | |
+ 'prefer-ipv6', 'keep-intvl='] | |
try: | |
config_path = find_config() | |
optlist, args = getopt.getopt(sys.argv[1:], shortopts, longopts) | |
@@ -180,6 +180,10 @@ def get_config(is_local): | |
config['one_time_auth'] = True | |
elif key == '-t': | |
config['timeout'] = int(value) | |
+ elif key == '-i': | |
+ config['keepidle'] = int(value) | |
+ elif key == '--keep-intvl': | |
+ config['keepintvl'] = int(value) | |
elif key == '--fast-open': | |
config['fast_open'] = True | |
elif key == '--workers': | |
@@ -224,6 +228,8 @@ def get_config(is_local): | |
config['method'] = to_str(config.get('method', 'aes-256-cfb')) | |
config['port_password'] = config.get('port_password', None) | |
config['timeout'] = int(config.get('timeout', 300)) | |
+ config['keepidle'] = int(config.get('keepidle', 0)) | |
+ config['keepintvl'] = min(int(config.get('keepintvl', config['keepidle'])), config['keepidle']) | |
config['fast_open'] = config.get('fast_open', False) | |
config['workers'] = config.get('workers', 1) | |
config['pid-file'] = config.get('pid-file', '/var/run/shadowsocks.pid') | |
@@ -324,6 +330,8 @@ Proxy options: | |
-m METHOD encryption method, default: aes-256-cfb | |
-t TIMEOUT timeout in seconds, default: 300 | |
-a ONE_TIME_AUTH one time auth | |
+ -i IDLE_TIME idle time before sending keepalive probes, default: 0 | |
+ --keep-intvl interval time between keepalive probes, default equals to IDLE_TIME | |
--fast-open use TCP_FASTOPEN, requires Linux 3.7+ | |
--workers WORKERS number of workers, available on Unix/Linux | |
--forbidden-ip IPLIST comma seperated IP list forbidden to connect | |
diff --git a/shadowsocks/tcprelay.py b/shadowsocks/tcprelay.py | |
index cc49d67..b4a3d5b 100644 | |
--- a/shadowsocks/tcprelay.py | |
+++ b/shadowsocks/tcprelay.py | |
@@ -146,6 +146,10 @@ class TCPRelayHandler(object): | |
fd_to_handlers[local_sock.fileno()] = self | |
local_sock.setblocking(False) | |
local_sock.setsockopt(socket.SOL_TCP, socket.TCP_NODELAY, 1) | |
+ if not is_local and self._config['keepidle']: | |
+ local_sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) | |
+ local_sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, self._config['keepidle']) | |
+ local_sock.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, self._config['keepintvl']) | |
loop.add(local_sock, eventloop.POLL_IN | eventloop.POLL_ERR, | |
self._server) | |
self.last_activity = 0 | |
-- | |
2.4.2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment