def edit_file_command(client: Client, name: bytes, initial_data): i_data = b'' if initial_data != b'': i_data = decrypt_aes(client.aes_key, initial_data) data = edit_file(decode_utf8(name) + '.swap', initial_data=i_data) encrypted = encrypt_aes(client.aes_key, data) client.sock.send(encrypted, flag=AES_ENCODED, input_wanted=NO_INPUT)
def receive_loop(self): from client_command import perform_by_name while True: response = self.sock.recv() if response.response_code == REFRESH: self.auth() continue if response.response_code != SUCCESS: print(decode_utf8(response.body)) print("Response code: {}".format(response.response_code)) continue if response.encoded_flag == PLAIN_TEXT: if response.body[0] == 33: perform_by_name(response.body, self) continue else: print(decode_utf8(response.body)) elif response.encoded_flag == AES_ENCODED: print( decode_utf8(crypt.decrypt_aes(self.aes_key, response.body))) else: print(response.body) if response.input_wanted_flag == NO_INPUT: continue self.sock.send_string(input())
def get_for_user(user: str, file: str, key: bytes) -> bytes: assert_system_file(file) f = open(STORAGE_PATH + '/' + user + '/' + file, 'rb') data = f.read() f.close() return crypt.decrypt_aes(key, data)
def perform_new(session: Session): session.sock.send_string("Please, enter filename to create.") file = decode_utf8(session.sock.recv().body) session.sock.send_string("!editor{}file{}".format(COMMAND_SEPARATOR, COMMAND_SEPARATOR)) raw = session.sock.recv().body data = decrypt_aes(session.key, raw) storage.create_for_user(session.username, file, data, session.server_aes_key)
def printing_replicate_from_server(self): response = self.s_sock.recv() print(response.body) if response.encoded_flag == AES_ENCODED: print(crypt.decrypt_aes(self.key, response.body)) self.sock.send(response.body, flag=response.encoded_flag, input_wanted=response.input_wanted_flag, response_code=response.response_code) return response
def perform_edit(session: Session): session.sock.send_string("Please, enter filename to edit.") file = decode_utf8(session.sock.recv().body) try: contents = storage.get_for_user(session.username, file, session.server_aes_key) session.sock.send( "!editor{}{}{}".format(COMMAND_SEPARATOR, session.username, COMMAND_SEPARATOR).encode('utf-8') + encrypt_aes(session.key, contents)) raw = session.sock.recv().body data = decrypt_aes(session.key, raw) storage.create_for_user(session.username, file, data, session.server_aes_key) except FileNotFoundError: session.sock.send(b'File not found', input_wanted=NO_INPUT, response_code=NOT_FOUND)
def authenticate(self, is_first=False): if is_first: self.rsa_pub = self.sock.recv().body self.key = os.urandom(16) encoded_key = crypt.encrypt_rsa(self.rsa_pub, self.key) self.sock.send(encoded_key) self.last_token_update = datetime.datetime.now() if is_first: self.sock.send_string("Enter login:"******"{} trying to authenticate".format(self.username)) self.sock.send_string("Enter password:"******"Invalid login {} supplied. Closing the socket.".format( self.username)) self.sock.send(b'', input_wanted=NO_INPUT, response_code=FORBIDDEN) self.sock.close() return self.sock.send(b'', input_wanted=NO_INPUT, response_code=SUCCESS)