Make TCPIntermediateO async

This commit is contained in:
Dan 2018-10-08 20:17:31 +02:00
parent 78a768f52c
commit 1fc160c566

View File

@ -35,8 +35,8 @@ class TCPIntermediateO(TCP):
self.encrypt = None self.encrypt = None
self.decrypt = None self.decrypt = None
def connect(self, address: tuple): async def connect(self, address: tuple):
super().connect(address) await super().connect(address)
while True: while True:
nonce = bytearray(os.urandom(64)) nonce = bytearray(os.urandom(64))
@ -54,25 +54,25 @@ class TCPIntermediateO(TCP):
nonce[56:64] = AES.ctr256_encrypt(nonce, *self.encrypt)[56:64] nonce[56:64] = AES.ctr256_encrypt(nonce, *self.encrypt)[56:64]
super().sendall(nonce) await super().send(nonce)
def sendall(self, data: bytes, *args): async def send(self, data: bytes, *args):
super().sendall( await super().send(
AES.ctr256_encrypt( AES.ctr256_encrypt(
pack("<i", len(data)) + data, pack("<i", len(data)) + data,
*self.encrypt *self.encrypt
) )
) )
def recvall(self, length: int = 0) -> bytes or None: async def recv(self, length: int = 0) -> bytes or None:
length = super().recvall(4) length = await super().recv(4)
if length is None: if length is None:
return None return None
length = AES.ctr256_decrypt(length, *self.decrypt) length = AES.ctr256_decrypt(length, *self.decrypt)
data = super().recvall(unpack("<i", length)[0]) data = await super().recv(unpack("<i", length)[0])
if data is None: if data is None:
return None return None