From 176e97bb452d5715417a6e9e7cb87a2ff68a935f Mon Sep 17 00:00:00 2001 From: fa08c <0xfa08c@gmail.com> Date: Thu, 8 Jan 2015 11:03:12 +0800 Subject: [PATCH 1/5] Fixing some minor issues --- shadowsocks/crypto/ctypes_openssl.py | 2 +- shadowsocks/eventloop.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shadowsocks/crypto/ctypes_openssl.py b/shadowsocks/crypto/ctypes_openssl.py index 9e0dfca..5ddff15 100644 --- a/shadowsocks/crypto/ctypes_openssl.py +++ b/shadowsocks/crypto/ctypes_openssl.py @@ -83,9 +83,9 @@ def load_cipher(cipher_name): class CtypesCrypto(object): def __init__(self, cipher_name, key, iv, op): + self._ctx = None if not loaded: load_openssl() - self._ctx = None cipher = libcrypto.EVP_get_cipherbyname(cipher_name) if not cipher: cipher = load_cipher(cipher_name) diff --git a/shadowsocks/eventloop.py b/shadowsocks/eventloop.py index 55c30bb..2b2459f 100644 --- a/shadowsocks/eventloop.py +++ b/shadowsocks/eventloop.py @@ -234,7 +234,7 @@ class EventLoop(object): traceback.print_exc() for handler in self._handlers_to_remove: self._handlers.remove(handler) - self._handlers_to_remove = [] + self._handlers_to_remove = [] self._iterating = False From 18da3554ff3e51aaa53a6b051fc8e35b04cda6b8 Mon Sep 17 00:00:00 2001 From: fa08c <0xfa08c@gmail.com> Date: Thu, 8 Jan 2015 08:27:28 -0600 Subject: [PATCH 2/5] Minor fix: never use except: unless you reraise --- shadowsocks/crypto/rc4_md5.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shadowsocks/crypto/rc4_md5.py b/shadowsocks/crypto/rc4_md5.py index 3062dcc..aa22b16 100644 --- a/shadowsocks/crypto/rc4_md5.py +++ b/shadowsocks/crypto/rc4_md5.py @@ -39,7 +39,7 @@ def create_cipher(alg, key, iv, op, key_as_bytes=0, d=None, salt=None, try: from shadowsocks.crypto import ctypes_openssl return ctypes_openssl.CtypesCrypto(b'rc4', rc4_key, b'', op) - except: + except Exception: import M2Crypto.EVP return M2Crypto.EVP.Cipher(b'rc4', rc4_key, b'', op, key_as_bytes=0, d='md5', salt=None, i=1, From 3294a92a61463abd8527eb1e3f83b1666b476204 Mon Sep 17 00:00:00 2001 From: fa08c <0xfa08c@gmail.com> Date: Sat, 10 Jan 2015 22:26:20 +0800 Subject: [PATCH 3/5] Search libcrypto.so in more locations Some linux distro may store .so in some alternative locations. Now we try harder to find them. --- shadowsocks/crypto/ctypes_openssl.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/shadowsocks/crypto/ctypes_openssl.py b/shadowsocks/crypto/ctypes_openssl.py index 5ddff15..6e80461 100644 --- a/shadowsocks/crypto/ctypes_openssl.py +++ b/shadowsocks/crypto/ctypes_openssl.py @@ -45,9 +45,29 @@ def load_openssl(): if libcrypto_path: break else: + # We may get here when find_library fails because, for example, + # the user does not have sufficient privileges to access those + # tools underlying find_library on linux. + import glob - for libcrypto_path in glob.glob('/usr/lib/libcrypto.*'): - pass + import sys + + patterns = ['/usr/lib/libcrypto.*'] + + # Some linux distros may store so in alternative locations + if sys.maxsize > 2 ** 32: + # Python is 64-bit + patterns.extend(['/usr/lib64/libcrypto.*']) + else: + # Python is 32-bit + patterns.extend(['/usr/lib32/libcrypto.*']) + + for pat in patterns: + files = glob.glob(pat) + if files: + libcrypto_path = files[0] + break + if libcrypto_path is None: raise Exception('libcrypto(OpenSSL) not found') logging.info('loading libcrypto from %s', libcrypto_path) From 6eadfca78ec2e731e9f7f9445c1143899009ed58 Mon Sep 17 00:00:00 2001 From: fa08c <0xfa08c@gmail.com> Date: Sun, 11 Jan 2015 15:19:31 +0800 Subject: [PATCH 4/5] Removing trailing whites --- shadowsocks/crypto/ctypes_openssl.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shadowsocks/crypto/ctypes_openssl.py b/shadowsocks/crypto/ctypes_openssl.py index 6e80461..22238c0 100644 --- a/shadowsocks/crypto/ctypes_openssl.py +++ b/shadowsocks/crypto/ctypes_openssl.py @@ -45,8 +45,8 @@ def load_openssl(): if libcrypto_path: break else: - # We may get here when find_library fails because, for example, - # the user does not have sufficient privileges to access those + # We may get here when find_library fails because, for example, + # the user does not have sufficient privileges to access those # tools underlying find_library on linux. import glob From ebfd1486d884190dd26b7af5c694b9dadccd85c3 Mon Sep 17 00:00:00 2001 From: fa08c <0xfa08c@gmail.com> Date: Mon, 12 Jan 2015 10:32:03 +0800 Subject: [PATCH 5/5] Removing the overhead of creating a new list per event --- shadowsocks/eventloop.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/shadowsocks/eventloop.py b/shadowsocks/eventloop.py index 2b2459f..304b229 100644 --- a/shadowsocks/eventloop.py +++ b/shadowsocks/eventloop.py @@ -232,9 +232,10 @@ class EventLoop(object): logging.error(e) import traceback traceback.print_exc() - for handler in self._handlers_to_remove: - self._handlers.remove(handler) - self._handlers_to_remove = [] + if self._handlers_to_remove: + for handler in self._handlers_to_remove: + self._handlers.remove(handler) + self._handlers_to_remove = [] self._iterating = False