From ce189ecd85b8de9fffce2b05846483b75879566f Mon Sep 17 00:00:00 2001 From: BreakWa11 Date: Wed, 25 May 2016 22:21:17 +0800 Subject: [PATCH] remove useless log add openssl rand_bytes --- shadowsocks/crypto/openssl.py | 12 ++++++++++++ shadowsocks/encrypt.py | 6 ++++-- shadowsocks/eventloop.py | 5 ----- shadowsocks/tcprelay.py | 15 ++++----------- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/shadowsocks/crypto/openssl.py b/shadowsocks/crypto/openssl.py index 3775b6c..7a4df2b 100644 --- a/shadowsocks/crypto/openssl.py +++ b/shadowsocks/crypto/openssl.py @@ -51,6 +51,10 @@ def load_openssl(): libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,) libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p,) + + libcrypto.RAND_bytes.restype = c_int + libcrypto.RAND_bytes.argtypes = (c_void_p, c_int) + if hasattr(libcrypto, 'OpenSSL_add_all_ciphers'): libcrypto.OpenSSL_add_all_ciphers() @@ -68,6 +72,14 @@ def load_cipher(cipher_name): return cipher() return None +def rand_bytes(length): + if not loaded: + load_openssl() + buf = create_string_buffer(length) + r = libcrypto.RAND_bytes(buf, length) + if r <= 0: + raise Exception('RAND_bytes return error') + return buf.raw class OpenSSLCrypto(object): def __init__(self, cipher_name, key, iv, op): diff --git a/shadowsocks/encrypt.py b/shadowsocks/encrypt.py index c3fc069..e8f51b7 100644 --- a/shadowsocks/encrypt.py +++ b/shadowsocks/encrypt.py @@ -34,8 +34,10 @@ method_supported.update(table.ciphers) def random_string(length): - return os.urandom(length) - + try: + return os.urandom(length) + except NotImplementedError as e: + return openssl.rand_bytes(length) cached_keys = {} diff --git a/shadowsocks/eventloop.py b/shadowsocks/eventloop.py index 746e728..ce9c11b 100644 --- a/shadowsocks/eventloop.py +++ b/shadowsocks/eventloop.py @@ -216,11 +216,6 @@ class EventLoop(object): handler.handle_event(sock, fd, event) except (OSError, IOError) as e: shell.print_exception(e) - try: - addr = sock.getpeername()[:2] - logging.error('exception peer name %s:%d' % (addr[0], addr[1])) - except: - logging.error('no peer name') now = time.time() if asap or now - self._last_time >= TIMEOUT_PRECISION: for callback in self._periodic_callbacks: diff --git a/shadowsocks/tcprelay.py b/shadowsocks/tcprelay.py index c0afadb..a58576a 100644 --- a/shadowsocks/tcprelay.py +++ b/shadowsocks/tcprelay.py @@ -769,23 +769,16 @@ class TCPRelayHandler(object): if self._local_sock: logging.error(eventloop.get_sock_error(self._local_sock)) logging.error("exception from %s:%d" % (self._client_address[0], self._client_address[1])) - try: - addr = self._local_sock.getpeername() - logging.error('local exception peer name %s:%d' % (addr[0], addr[1])) - except: - logging.error('no peer name') self.destroy() def _on_remote_error(self): logging.debug('got remote error') if self._remote_sock: logging.error(eventloop.get_sock_error(self._remote_sock)) - logging.error("exception from %s:%d" % (self._client_address[0], self._client_address[1])) - try: - addr = self._remote_sock.getpeername() - logging.error('remote exception peer name %s:%d' % (addr[0], addr[1])) - except: - logging.error('no peer name') + if self._remote_address: + logging.error("when connect to %s:%d from %s:%d" % (self._remote_address[0], self._remote_address[1], self._client_address[0], self._client_address[1])) + else: + logging.error("exception from %s:%d" % (self._client_address[0], self._client_address[1])) self.destroy() def handle_event(self, sock, event):