Queer European MD passionate about IT
Parcourir la source

Quotes prevent variable splitting if file name has spaces

Davte il y a 5 ans
Parent
commit
4352f2908b
2 fichiers modifiés avec 21 ajouts et 7 suppressions
  1. 9 3
      src/client.py
  2. 12 4
      src/server.py

+ 9 - 3
src/client.py

@@ -85,7 +85,7 @@ class Client:
             _subprocess = await asyncio.create_subprocess_shell(
                 "openssl enc -aes-256-cbc "
                 "-md sha512 -pbkdf2 -iter 100000 -salt "
-                f"-in {input_file} -out {output_file} "
+                f"-in \"{input_file}\" -out \"{output_file}\" "
                 f"-pass pass:{self.password}"
             )
             stdout, stderr = await _subprocess.communicate()
@@ -105,14 +105,18 @@ class Client:
         file_path = self.file_path
         if self.password:
             file_path = self.file_path + '.enc'
+            # Remove already-encrypted file if present (salt would differ)
+            if os.path.isfile(file_path):
+                os.remove(file_path)
             asyncio.ensure_future(
                 self.encrypt_file(
                     input_file=self.file_path,
                     output_file=file_path
                 )
             )
+            # Give encryption an edge
             while not os.path.isfile(file_path):
-                await asyncio.sleep(0.1)  # Let the encryption begin
+                await asyncio.sleep(.5)
         logging.info("Sending file...")
         with open(file_path, 'rb') as file_to_send:
             while not self.stopping:
@@ -146,6 +150,7 @@ class Client:
     async def receive(self, reader: asyncio.StreamReader):
         self._working = True
         file_path = self.file_path
+        logging.info("Receiving file...")
         if self.password:
             file_path += '.enc'
         with open(file_path, 'wb') as file_to_receive:
@@ -154,6 +159,7 @@ class Client:
                 if not input_data:
                     break
                 file_to_receive.write(input_data)
+        logging.info("File received.")
         if self.password:
             logging.info("Decrypting file...")
             stdout, stderr = ''.encode(), ''.encode()
@@ -161,7 +167,7 @@ class Client:
                 _subprocess = await asyncio.create_subprocess_shell(
                     "openssl enc -aes-256-cbc "
                     "-md sha512 -pbkdf2 -iter 100000 -salt -d "
-                    f"-in {file_path} -out {self.file_path} "
+                    f"-in \"{file_path}\" -out \"{self.file_path}\" "
                     f"-pass pass:{self.password}"
                 )
                 stdout, stderr = await _subprocess.communicate()

+ 12 - 4
src/server.py

@@ -11,9 +11,12 @@ class Server:
         self._host = host
         self._port = port
         self._stopping = False
-        self.buffer = collections.deque()  # Shared queue of bytes
-        self._buffer_chunk_size = buffer_chunk_size   # How many bytes per chunk
-        self._buffer_length_limit = buffer_length_limit  # How many chunks in buffer
+        # Shared queue of bytes
+        self.buffer = collections.deque()
+        # How many bytes per chunk
+        self._buffer_chunk_size = buffer_chunk_size
+        # How many chunks in buffer
+        self._buffer_length_limit = buffer_length_limit
         self._working = False
         self._server = None
         self._ssl_context = None
@@ -102,15 +105,20 @@ class Server:
         """
         client_hello = await reader.readline()
         peer_is_sender = client_hello.decode('utf-8') == 'sender\n'
-        writer.write("Start!\n".encode('utf-8'))  # Send start signal to client
         await writer.drain()
         if peer_is_sender:
             self._working = True
             logging.info("Sender is connecting...")
+            # Send start signal to client
+            writer.write("Start!\n".encode('utf-8'))
             await self.run_reader(reader=reader)
             logging.info("Incoming transmission ended")
         else:
             logging.info("Receiver is connecting...")
+            while len(self.buffer) == 0:
+                await asyncio.sleep(.5)
+            # Send start signal to client
+            writer.write("Start!\n".encode('utf-8'))
             await self.run_writer(writer=writer)
             logging.info("Outgoing transmission ended")
             self._working = False