Skip to content

Commit 6e5a753

Browse files
author
clowwindy
committed
add timeout in TCP server
1 parent cef0063 commit 6e5a753

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

shadowsocks/local.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def main():
282282

283283
config_path = utils.find_config()
284284
try:
285-
optlist, args = getopt.getopt(sys.argv[1:], 's:b:p:k:l:m:c:',
285+
optlist, args = getopt.getopt(sys.argv[1:], 's:b:p:k:l:m:c:t:',
286286
['fast-open'])
287287
for key, value in optlist:
288288
if key == '-c':
@@ -300,7 +300,7 @@ def main():
300300
else:
301301
config = {}
302302

303-
optlist, args = getopt.getopt(sys.argv[1:], 's:b:p:k:l:m:c:',
303+
optlist, args = getopt.getopt(sys.argv[1:], 's:b:p:k:l:m:c:t:',
304304
['fast-open'])
305305
for key, value in optlist:
306306
if key == '-p':
@@ -328,7 +328,7 @@ def main():
328328
config_password = config['password']
329329
config_method = config.get('method', None)
330330
config_local_address = config.get('local_address', '127.0.0.1')
331-
config_timeout = config.get('timeout', 600)
331+
config_timeout = int(config.get('timeout', 300))
332332
config_fast_open = config.get('fast_open', False)
333333

334334
if not config_password and not config_path:

shadowsocks/server.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,22 @@ def server_activate(self):
7474
logging.error('warning: fast open is not available')
7575
self.socket.listen(self.request_queue_size)
7676

77+
def get_request(self):
78+
connection = self.socket.accept()
79+
connection[0].settimeout(config_timeout)
80+
return connection
81+
7782

7883
class Socks5Server(SocketServer.StreamRequestHandler):
7984
def handle_tcp(self, sock, remote):
8085
try:
8186
fdset = [sock, remote]
8287
while True:
8388
should_break = False
84-
r, w, e = select.select(fdset, [], [])
89+
r, w, e = select.select(fdset, [], [], config_timeout)
90+
if not r:
91+
logging.warn('read time out')
92+
break
8593
if sock in r:
8694
data = self.decrypt(sock.recv(4096))
8795
if len(data) <= 0:
@@ -147,7 +155,9 @@ def handle(self):
147155
port = struct.unpack('>H', self.decrypt(self.rfile.read(2)))
148156
try:
149157
logging.info('connecting %s:%d' % (addr, port[0]))
150-
remote = socket.create_connection((addr, port[0]))
158+
remote = socket.create_connection((addr, port[0]),
159+
timeout=config_timeout)
160+
remote.settimeout(config_timeout)
151161
remote.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
152162
except socket.error, e:
153163
# Connection refused
@@ -159,7 +169,8 @@ def handle(self):
159169

160170

161171
def main():
162-
global config_server, config_server_port, config_method, config_fast_open
172+
global config_server, config_server_port, config_method, config_fast_open, \
173+
config_timeout
163174

164175
logging.basicConfig(level=logging.DEBUG,
165176
format='%(asctime)s %(levelname)-8s %(message)s',
@@ -176,7 +187,7 @@ def main():
176187

177188
config_path = utils.find_config()
178189
try:
179-
optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:',
190+
optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:t:',
180191
['fast-open', 'workers:'])
181192
for key, value in optlist:
182193
if key == '-c':
@@ -194,7 +205,7 @@ def main():
194205
else:
195206
config = {}
196207

197-
optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:',
208+
optlist, args = getopt.getopt(sys.argv[1:], 's:p:k:m:c:t:',
198209
['fast-open', 'workers='])
199210
for key, value in optlist:
200211
if key == '-p':
@@ -205,6 +216,8 @@ def main():
205216
config['server'] = value
206217
elif key == '-m':
207218
config['method'] = value
219+
elif key == '-t':
220+
config['timeout'] = value
208221
elif key == '--fast-open':
209222
config['fast_open'] = True
210223
elif key == '--workers':
@@ -218,7 +231,7 @@ def main():
218231
config_key = config['password']
219232
config_method = config.get('method', None)
220233
config_port_password = config.get('port_password', None)
221-
config_timeout = config.get('timeout', 600)
234+
config_timeout = int(config.get('timeout', 300))
222235
config_fast_open = config.get('fast_open', False)
223236
config_workers = config.get('workers', 1)
224237

shadowsocks/utils.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,18 @@ def check_config(config):
8888
if (config.get('method', '') or '').lower() == 'rc4':
8989
logging.warn('warning: RC4 is not safe; please use a safer cipher, '
9090
'like AES-256-CFB')
91-
if (config.get('timeout', 600) or 600) < 100:
91+
if (int(config.get('timeout', 300)) or 300) < 100:
9292
logging.warn('warning: your timeout %d seems too short' %
93-
config.get('timeout'))
94-
if (config.get('timeout', 600) or 600) > 600:
93+
int(config.get('timeout')))
94+
if (int(config.get('timeout', 300)) or 300) > 600:
9595
logging.warn('warning: your timeout %d seems too long' %
96-
config.get('timeout'))
96+
int(config.get('timeout')))
9797

9898

9999
def print_local_help():
100100
print '''usage: sslocal [-h] -s SERVER_ADDR -p SERVER_PORT [-b LOCAL_ADDR]
101-
-l LOCAL_PORT -k PASSWORD -m METHOD [-c config] [--fast-open]
101+
-l LOCAL_PORT -k PASSWORD -m METHOD [-t TIMEOUT] [-c CONFIG]
102+
[--fast-open]
102103
103104
optional arguments:
104105
-h, --help show this help message and exit
@@ -108,21 +109,23 @@ def print_local_help():
108109
-l LOCAL_PORT local port
109110
-k PASSWORD password
110111
-m METHOD encryption method, for example, aes-256-cfb
112+
-t TIMEOUT timeout in seconds
111113
-c CONFIG path to config file
112114
--fast-open use TCP_FASTOPEN, requires Linux 3.7+
113115
'''
114116

115117

116118
def print_server_help():
117119
print '''usage: ssserver [-h] -s SERVER_ADDR -p SERVER_PORT -k PASSWORD
118-
-m METHOD [-c config] [--fast-open]
120+
-m METHOD [-t TIMEOUT] [-c CONFIG] [--fast-open]
119121
120122
optional arguments:
121123
-h, --help show this help message and exit
122124
-s SERVER_ADDR server address
123125
-p SERVER_PORT server port
124126
-k PASSWORD password
125127
-m METHOD encryption method, for example, aes-256-cfb
128+
-t TIMEOUT timeout in seconds
126129
-c CONFIG path to config file
127130
--fast-open use TCP_FASTOPEN, requires Linux 3.7+
128131
--workers WORKERS number of workers, available on Unix/Linux

0 commit comments

Comments
 (0)