Browse Source

remove useless log

add openssl rand_bytes
dev
BreakWa11 9 years ago
parent
commit
ce189ecd85
  1. 12
      shadowsocks/crypto/openssl.py
  2. 4
      shadowsocks/encrypt.py
  3. 5
      shadowsocks/eventloop.py
  4. 13
      shadowsocks/tcprelay.py

12
shadowsocks/crypto/openssl.py

@ -51,6 +51,10 @@ def load_openssl():
libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,) libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,)
libcrypto.EVP_CIPHER_CTX_free.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'): if hasattr(libcrypto, 'OpenSSL_add_all_ciphers'):
libcrypto.OpenSSL_add_all_ciphers() libcrypto.OpenSSL_add_all_ciphers()
@ -68,6 +72,14 @@ def load_cipher(cipher_name):
return cipher() return cipher()
return None 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): class OpenSSLCrypto(object):
def __init__(self, cipher_name, key, iv, op): def __init__(self, cipher_name, key, iv, op):

4
shadowsocks/encrypt.py

@ -34,8 +34,10 @@ method_supported.update(table.ciphers)
def random_string(length): def random_string(length):
try:
return os.urandom(length) return os.urandom(length)
except NotImplementedError as e:
return openssl.rand_bytes(length)
cached_keys = {} cached_keys = {}

5
shadowsocks/eventloop.py

@ -216,11 +216,6 @@ class EventLoop(object):
handler.handle_event(sock, fd, event) handler.handle_event(sock, fd, event)
except (OSError, IOError) as e: except (OSError, IOError) as e:
shell.print_exception(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() now = time.time()
if asap or now - self._last_time >= TIMEOUT_PRECISION: if asap or now - self._last_time >= TIMEOUT_PRECISION:
for callback in self._periodic_callbacks: for callback in self._periodic_callbacks:

13
shadowsocks/tcprelay.py

@ -769,23 +769,16 @@ class TCPRelayHandler(object):
if self._local_sock: if self._local_sock:
logging.error(eventloop.get_sock_error(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])) 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() self.destroy()
def _on_remote_error(self): def _on_remote_error(self):
logging.debug('got remote error') logging.debug('got remote error')
if self._remote_sock: if self._remote_sock:
logging.error(eventloop.get_sock_error(self._remote_sock)) logging.error(eventloop.get_sock_error(self._remote_sock))
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])) 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')
self.destroy() self.destroy()
def handle_event(self, sock, event): def handle_event(self, sock, event):

Loading…
Cancel
Save