|
@@ -4,6 +4,7 @@ import collections
|
|
|
import logging
|
|
|
# import signal
|
|
|
import os
|
|
|
+import ssl
|
|
|
|
|
|
|
|
|
class Client:
|
|
@@ -17,6 +18,7 @@ class Client:
|
|
|
self._buffer_length_limit = buffer_length_limit # How many chunks in buffer
|
|
|
self._file_path = None
|
|
|
self._working = False
|
|
|
+ self._ssl_context = None
|
|
|
|
|
|
@property
|
|
|
def host(self) -> str:
|
|
@@ -46,10 +48,18 @@ class Client:
|
|
|
def working(self) -> bool:
|
|
|
return self._working
|
|
|
|
|
|
+ @property
|
|
|
+ def ssl_context(self) -> ssl.SSLContext:
|
|
|
+ return self._ssl_context
|
|
|
+
|
|
|
+ def set_ssl_context(self, ssl_context: ssl.SSLContext):
|
|
|
+ self._ssl_context = ssl_context
|
|
|
+
|
|
|
async def run_sending_client(self, file_path='~/output.txt'):
|
|
|
self._file_path = file_path
|
|
|
reader, writer = await asyncio.open_connection(host=self.host,
|
|
|
- port=self.port)
|
|
|
+ port=self.port,
|
|
|
+ ssl=self.ssl_context)
|
|
|
writer.write("sender\n".encode('utf-8'))
|
|
|
await writer.drain()
|
|
|
await reader.readline() # Wait for server start signal
|
|
@@ -78,7 +88,8 @@ class Client:
|
|
|
async def run_receiving_client(self, file_path='~/input.txt'):
|
|
|
self._file_path = file_path
|
|
|
reader, writer = await asyncio.open_connection(host=self.host,
|
|
|
- port=self.port)
|
|
|
+ port=self.port,
|
|
|
+ ssl=self.ssl_context)
|
|
|
writer.write("receiver\n".encode('utf-8'))
|
|
|
await writer.drain()
|
|
|
await reader.readline() # Wait for server start signal
|
|
@@ -227,6 +238,15 @@ if __name__ == '__main__':
|
|
|
host=_host,
|
|
|
port=_port,
|
|
|
)
|
|
|
+ try:
|
|
|
+ from config import certificate
|
|
|
+ _ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
|
|
|
+ _ssl_context.check_hostname = False
|
|
|
+ _ssl_context.load_verify_locations(certificate)
|
|
|
+ client.set_ssl_context(_ssl_context)
|
|
|
+ except ImportError:
|
|
|
+ logging.info("Please consider using SSL.")
|
|
|
+ certificate, key = None, None
|
|
|
logging.info("Starting client...")
|
|
|
if _action == 'send':
|
|
|
loop.run_until_complete(
|