From a30e53ee650c783160fd1bb3c67a69e01adf6942 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sat, 24 May 2014 21:29:40 +0800 Subject: [PATCH] add timeout in local --- CHANGES | 3 +++ setup.py | 2 +- shadowsocks/local.py | 17 ++++++++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index b7f2762..10805e3 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,6 @@ +1.4.5 2014-05-24 +- Add timeout in server + 1.4.4 2014-05-17 - Support multiple workers diff --git a/setup.py b/setup.py index 4f4a943..259c629 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open('README.rst') as f: setup( name="shadowsocks", - version="1.4.4", + version="1.4.5", license='MIT', description="a lightweight tunnel proxy", author='clowwindy', diff --git a/shadowsocks/local.py b/shadowsocks/local.py index 5159a76..049c8ea 100755 --- a/shadowsocks/local.py +++ b/shadowsocks/local.py @@ -69,6 +69,11 @@ def send_all(sock, data): class ThreadingTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): allow_reuse_address = True + def get_request(self): + connection = self.socket.accept() + connection[0].settimeout(config_timeout) + return connection + class Socks5Server(SocketServer.StreamRequestHandler): @staticmethod @@ -101,7 +106,10 @@ class Socks5Server(SocketServer.StreamRequestHandler): fdset = [sock, remote] while True: should_break = False - r, w, e = select.select(fdset, [], []) + r, w, e = select.select(fdset, [], [], config_timeout) + if not r: + logging.warn('read time out') + break if sock in r: if not connected and config_fast_open: data = sock.recv(4096) @@ -243,7 +251,9 @@ class Socks5Server(SocketServer.StreamRequestHandler): addr_to_send, a_server, a_port) else: logging.info('connecting %s:%d' % (addr, port[0])) - remote = socket.create_connection((a_server, a_port)) + remote = socket.create_connection((a_server, a_port), + timeout=config_timeout) + remote.settimeout(config_timeout) remote.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) Socks5Server.handle_tcp(sock, remote, encryptor, @@ -258,7 +268,7 @@ class Socks5Server(SocketServer.StreamRequestHandler): def main(): global config_server, config_server_port, config_password, config_method,\ - config_fast_open + config_fast_open, config_timeout logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)-8s %(message)s', @@ -350,6 +360,7 @@ def main(): config_method, int(config_timeout), True).start() server = ThreadingTCPServer((config_local_address, config_local_port), Socks5Server) + server.timeout = int(config_timeout) logging.info("starting local at %s:%d" % tuple(server.server_address[:2])) server.serve_forever()