Browse Source

check encryptor object

dev
BreakWa11 9 years ago
parent
commit
5e46aa0b47
  1. 80
      shadowsocks/tcprelay.py

80
shadowsocks/tcprelay.py

@ -268,7 +268,7 @@ class TCPRelayHandler(object):
if self._is_local: if self._is_local:
pass pass
else: else:
if sock == self._local_sock and self._encrypt_correct: if sock == self._local_sock and self._encrypt_correct and (self._obfs is not None):
obfs_encode = self._obfs.server_encode(data) obfs_encode = self._obfs.server_encode(data)
data = obfs_encode data = obfs_encode
if data: if data:
@ -338,9 +338,10 @@ class TCPRelayHandler(object):
def _handle_stage_connecting(self, data): def _handle_stage_connecting(self, data):
if self._is_local: if self._is_local:
data = self._protocol.client_pre_encrypt(data) if self._encryptor is not None:
data = self._encryptor.encrypt(data) data = self._protocol.client_pre_encrypt(data)
data = self._obfs.client_encode(data) data = self._encryptor.encrypt(data)
data = self._obfs.client_encode(data)
if data: if data:
self._data_to_write_to_remote.append(data) self._data_to_write_to_remote.append(data)
if self._is_local and not self._fastopen_connected and \ if self._is_local and not self._fastopen_connected and \
@ -436,9 +437,10 @@ class TCPRelayHandler(object):
data = b'\x88' + struct.pack('>H', total_len) + chr(rnd_len) + (b' ' * (rnd_len - 1)) + data data = b'\x88' + struct.pack('>H', total_len) + chr(rnd_len) + (b' ' * (rnd_len - 1)) + data
crc = (0xffffffff - binascii.crc32(data)) & 0xffffffff crc = (0xffffffff - binascii.crc32(data)) & 0xffffffff
data += struct.pack('<I', crc) data += struct.pack('<I', crc)
data = self._protocol.client_pre_encrypt(data) if self._encryptor is not None:
data_to_send = self._encryptor.encrypt(data) data = self._protocol.client_pre_encrypt(data)
data_to_send = self._obfs.client_encode(data_to_send) data_to_send = self._encryptor.encrypt(data)
data_to_send = self._obfs.client_encode(data_to_send)
if data_to_send: if data_to_send:
self._data_to_write_to_remote.append(data_to_send) self._data_to_write_to_remote.append(data_to_send)
# notice here may go into _handle_dns_resolved directly # notice here may go into _handle_dns_resolved directly
@ -573,27 +575,31 @@ class TCPRelayHandler(object):
ogn_data = data ogn_data = data
self._update_activity(len(data)) self._update_activity(len(data))
if not is_local: if not is_local:
if self._encrypt_correct: if self._encryptor is not None:
obfs_decode = self._obfs.server_decode(data) if self._encrypt_correct:
if obfs_decode[2]: obfs_decode = self._obfs.server_decode(data)
self._write_to_sock(b'', self._local_sock) if obfs_decode[2]:
if obfs_decode[1]: self._write_to_sock(b'', self._local_sock)
data = self._encryptor.decrypt(obfs_decode[0]) if obfs_decode[1]:
else: data = self._encryptor.decrypt(obfs_decode[0])
data = obfs_decode[0] else:
try: data = obfs_decode[0]
data = self._protocol.server_post_decrypt(data) try:
except Exception as e: data = self._protocol.server_post_decrypt(data)
shell.print_exception(e) except Exception as e:
self.destroy() shell.print_exception(e)
self.destroy()
else:
return
if not data: if not data:
return return
self._server.server_transfer_ul += len(data) self._server.server_transfer_ul += len(data)
if self._stage == STAGE_STREAM: if self._stage == STAGE_STREAM:
if self._is_local: if self._is_local:
data = self._protocol.client_pre_encrypt(data) if self._encryptor is not None:
data = self._encryptor.encrypt(data) data = self._protocol.client_pre_encrypt(data)
data = self._obfs.client_encode(data) data = self._encryptor.encrypt(data)
data = self._obfs.client_encode(data)
self._write_to_sock(data, self._remote_sock) self._write_to_sock(data, self._remote_sock)
return return
elif is_local and self._stage == STAGE_INIT: elif is_local and self._stage == STAGE_INIT:
@ -634,19 +640,22 @@ class TCPRelayHandler(object):
if not data: if not data:
self.destroy() self.destroy()
return return
self._server.server_transfer_dl += len(data) if self._encryptor is not None:
self._update_activity(len(data)) self._server.server_transfer_dl += len(data)
if self._is_local: self._update_activity(len(data))
obfs_decode = self._obfs.client_decode(data) if self._is_local:
if obfs_decode[1]: obfs_decode = self._obfs.client_decode(data)
send_back = self._obfs.client_encode(b'') if obfs_decode[1]:
self._write_to_sock(send_back, self._remote_sock) send_back = self._obfs.client_encode(b'')
data = self._encryptor.decrypt(obfs_decode[0]) self._write_to_sock(send_back, self._remote_sock)
data = self._protocol.client_post_decrypt(data) data = self._encryptor.decrypt(obfs_decode[0])
data = self._protocol.client_post_decrypt(data)
else:
if self._encrypt_correct:
data = self._protocol.server_pre_encrypt(data)
data = self._encryptor.encrypt(data)
else: else:
if self._encrypt_correct: return
data = self._protocol.server_pre_encrypt(data)
data = self._encryptor.encrypt(data)
try: try:
self._write_to_sock(data, self._local_sock) self._write_to_sock(data, self._local_sock)
except Exception as e: except Exception as e:
@ -770,6 +779,7 @@ class TCPRelayHandler(object):
if self._protocol: if self._protocol:
self._protocol.dispose() self._protocol.dispose()
self._protocol = None self._protocol = None
self._encryptor = None
self._dns_resolver.remove_callback(self._handle_dns_resolved) self._dns_resolver.remove_callback(self._handle_dns_resolved)
self._server.remove_handler(self) self._server.remove_handler(self)
self._server.add_connection(-1) self._server.add_connection(-1)

Loading…
Cancel
Save