From 3cefe374c2b48fa7dda2fb152365a40d88086f20 Mon Sep 17 00:00:00 2001 From: clowwindy Date: Sun, 1 Jun 2014 14:53:06 +0800 Subject: [PATCH] socket.SO_REUSEADDR --- shadowsocks/encrypt.py | 6 +++--- shadowsocks/local.py | 2 +- shadowsocks/tcprelay.py | 1 + shadowsocks/utils.py | 15 +++++++++------ 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/shadowsocks/encrypt.py b/shadowsocks/encrypt.py index 267565c..9d94dd1 100644 --- a/shadowsocks/encrypt.py +++ b/shadowsocks/encrypt.py @@ -157,9 +157,9 @@ class Encryptor(object): self.cipher_iv = iv[:m[1]] # this iv is for cipher not decipher if method != 'salsa20-ctr': import M2Crypto.EVP - return M2Crypto.EVP.Cipher(method.replace('-', '_'), key, iv, op, - key_as_bytes=0, d='md5', salt=None, i=1, - padding=1) + return M2Crypto.EVP.Cipher(method.replace('-', '_'), key, iv, + op, key_as_bytes=0, d='md5', + salt=None, i=1, padding=1) else: return encrypt_salsa20.Salsa20Cipher(method, key, iv, op) diff --git a/shadowsocks/local.py b/shadowsocks/local.py index 7219c08..0ccc813 100755 --- a/shadowsocks/local.py +++ b/shadowsocks/local.py @@ -55,7 +55,7 @@ def main(): while sys.stdin.read(): pass except KeyboardInterrupt: - sys.exit(0) + os._exit(0) if __name__ == '__main__': main() diff --git a/shadowsocks/tcprelay.py b/shadowsocks/tcprelay.py index 335849b..b7902d9 100644 --- a/shadowsocks/tcprelay.py +++ b/shadowsocks/tcprelay.py @@ -294,6 +294,7 @@ class TCPRelay(object): (listen_addr, listen_port)) af, socktype, proto, canonname, sa = addrs[0] server_socket = socket.socket(af, socktype, proto) + server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_socket.bind(sa) server_socket.setblocking(False) server_socket.listen(1024) diff --git a/shadowsocks/utils.py b/shadowsocks/utils.py index 0daa507..180cdcb 100644 --- a/shadowsocks/utils.py +++ b/shadowsocks/utils.py @@ -56,12 +56,6 @@ def find_config(): def check_config(config): - config['password'] = config.get('password', None) - config['method'] = config.get('method', None) - config['port_password'] = config.get('port_password', None) - config['timeout'] = int(config.get('timeout', 300)) - config['fast_open'] = config.get('fast_open', False) - config['workers'] = config.get('workers', 1) if config.get('local_address', '') in ['0.0.0.0']: logging.warn('warning: local set to listen 0.0.0.0, which is not safe') if config.get('server', '') in ['127.0.0.1', 'localhost']: @@ -136,6 +130,15 @@ def get_config(is_local): sys.exit('config not specified, please read ' 'https://github.com/clowwindy/shadowsocks') + config['password'] = config.get('password', None) + config['method'] = config.get('method', None) + config['port_password'] = config.get('port_password', None) + config['timeout'] = int(config.get('timeout', 300)) + config['fast_open'] = config.get('fast_open', False) + config['workers'] = config.get('workers', 1) + config['verbose'] = config.get('verbose', False) + config['local_address'] = config.get('local_address', '127.0.0.1') + check_config(config) if config['verbose']: