Пример #1
0
    def login(self, client_connection):
        credentials = util.get_instructions(client_connection, self.chunk_size)
        #print(f'got credentials {credentials}')
        try:
            username, password = credentials.split()
        except:
            username = -1
            password = -1

        if username == -1:
            return False

        logged_in = 0

        for node in self.data_nodes:
            util.send_message(node.socket, f'login {username}-{password}',
                              self.chunk_size)

            result = util.get_instructions(node.socket, self.chunk_size)
            logger.debug(
                f'answer from server {node.host} is {result} type of result is {type(result)}'
            )
            if result == '1':
                logged_in += 1

        if logged_in == len(self.data_nodes):
            util.send_message(client_connection, 'welcome', self.chunk_size)
            return True
        else:
            util.send_message(client_connection, 'failed to log in',
                              self.chunk_size)
            return False
Пример #2
0
    def create_account(self, client_connection, command):
        username, password = (util.get_instructions(client_connection,
                                                    self.chunk_size,
                                                    False)).split()
        #util.send_message(connection, 'welcome', self.chunk_size)
        logged_in = 0
        for node in self.data_nodes:
            data = f"{command} {username}-{password}"
            logger.debug("Data: " + data)

            util.send_message(node.socket, data, self.chunk_size)

            response = util.get_instructions(node.socket, self.chunk_size)

            print(
                f'\n\n response is {response} len of resp is {len(response)}')
            if response == '1':
                #util.send_message(connection, 'welcome', self.chunk_size)
                logged_in += 1

            elif response == '2':
                #util.send_message(connection, 'error: account already exists, try a different username or login', self.chunk_size)
                continue

            elif response == '-1':
                util.send_message(client_connection, 'something went wrong',
                                  self.chunk_size)

        if logged_in == len(self.data_nodes):
            util.send_message(client_connection, 'welcome', self.chunk_size)
        else:
            util.send_message(client_connection, 'not welcome',
                              self.chunk_size)

        return logged_in == len(self.data_nodes)
Пример #3
0
    def list_all(self, client_connection):
        chunk = b''

        util.send_message(self.data_nodes[0].socket, 'list', self.chunk_size)
        chunk = util.get_instructions(self.data_nodes[0].socket,
                                      self.chunk_size)

        if chunk == '-1':
            util.send_message(client_connection,
                              util.stop_phrase.decode("utf-8"),
                              self.chunk_size)
            return False

        while chunk.find(util.stop_phrase.decode("utf-8")) == -1:
            util.send_message(client_connection, chunk, self.chunk_size)

            self.directory.append(chunk)
            chunk = util.get_instructions(self.data_nodes[0].socket,
                                          self.chunk_size)
            if chunk == '-1':
                util.send_message(client_connection,
                                  util.stop_phrase.decode("utf-8"),
                                  self.chunk_size)
                return False
        util.send_message(client_connection, util.stop_phrase.decode("utf-8"),
                          self.chunk_size)
        return True
Пример #4
0
 def _receive_list(self):
     #data = b''
     chunk = util.get_instructions(self.socket, self.chunk_size)
     print('---------------')
     while chunk.find((util.stop_phrase).decode('utf-8')) == -1:
         logger.debug(f'in list: received {chunk}')
         print(f'{chunk}')
         chunk = util.get_instructions(self.socket, self.chunk_size)
     print('---------------')
     return True
Пример #5
0
    def name(self):
        util.send_message(self.socket, IDENTITY, self.chunk_size)
        self.server_id = util.get_instructions(self.socket, self.chunk_size)

        #throwaway = util.get_instructions(self.socket, self.chunk_size)

        #logger.debug(f'throwaway chunk = {throwaway}')
        return self.server_id
Пример #6
0
    def _login(self):
        username = input('username >> ')
        password = getpass('password >> ')
        util.send_message(self.socket, f'{username} {password}',
                          self.chunk_size)

        instructions = util.get_instructions(self.socket, self.chunk_size)

        logger.debug(f'command received by client is {instructions}')
        if instructions == 'welcome':
            return True
        else:
            return False
Пример #7
0
    def create_account(self):
        counter = 0
        password2, password, username = '', '', ''
        while counter <= 5:

            while counter <= 5 and (len(username) < 2
                                    or username.find(' ') > -1):
                print('username must be at least 2 characters. no spaces')
                username = input('username\n>> ')
                counter += 1
                continue

            while counter <= 5 and (len(password) < 4
                                    or password.find(' ') > -1):
                print('password must be at least 4 characters, no spaces')
                password = getpass('password:\n>> ')
                counter += 1
                continue

            while password != password2:
                password2 = getpass('repeat password:\n>> ')
                if password != password2:
                    print('passwords don\'t match')

            util.send_message(self.socket, f'{username} {password}',
                              self.chunk_size)

            instructions = util.get_instructions(self.socket, self.chunk_size)

            if instructions == 'welcome':
                return True
            else:
                counter += 1

        if counter > 5:
            print('error getting credentials for new account')
            return False
Пример #8
0
    def server_protocol(self):

        logged_in = False
        first_empty_data_received = True
        empty_data_count = 0
        while True:
            if first_empty_data_received:
                print('waiting for instructions...')

            data = util.get_instructions(self.middleware_socket,
                                         self.chunk_size)

            if (len(data) == 0):
                empty_data_count += 1
                if first_empty_data_received:
                    logger.error(
                        f'in {self.current_path}: data: {data} type {type(data)} len {len(data)}'
                    )

                if empty_data_count > 4:
                    break
                first_empty_data_received = False
                continue

            else:
                first_empty_data_received = True
                empty_data_count = 0

            if data.find(' ') > -1:
                command, username_password = str(data).split()
                logger.debug(
                    f"in {self.current_path}: received {command} and {username_password}"
                )
            else:
                command = str(data)
                logger.debug(
                    f'in {self.current_path}: received {command} type of command {type(command)}'
                )

            if command == ID:
                util.send_message(self.middleware_socket, self.identity,
                                  self.chunk_size)
                continue
            elif command == CLOSE:
                return True

            elif command == LOGOUT:
                logged_in = False
                self.current_path = self.dir_path
                continue

            if logged_in == False:
                if command == LOGIN:
                    result, self.current_path = self.login(username_password)

                    if (self.current_path != self.dir_path):

                        util.send_message(self.middleware_socket, result,
                                          self.chunk_size)
                        logged_in = True
                        continue
                    else:
                        util.send_message(self.middleware_socket, result,
                                          self.chunk_size)
                        continue

                elif command == ACC:

                    result, self.current_path = self.create_account(
                        username_password)
                    if self.current_path != self.dir_path:
                        # account creation is successful
                        util.send_message(self.middleware_socket, '1',
                                          self.chunk_size)

                    else:
                        # account creation is not successful
                        util.send_message(self.middleware_socket, '2',
                                          self.chunk_size)
                else:
                    # command is unknown
                    util.send_message(self.middleware_socket, '-1',
                                      self.chunk_size)
                    continue

            elif logged_in == True:
                if command == DELETE:
                    try:
                        os.remove(f'{self.current_path}/{username_password}')
                        logger.info(
                            f"{self.current_path}/{username_password}' removed successfully"
                        )

                    except FileNotFoundError:
                        logger.error(
                            f"{self.current_path}/{username_password}' DNE")

                elif command == LIST:
                    logger.debug(f'got to {command}')
                    self.list_acc()

                elif command == UPLOAD:
                    util.receive_file(
                        self.middleware_socket,
                        f'{self.current_path}/{username_password}',
                        self.chunk_size)

                elif command == DOWNLOAD:
                    util.send_file(self.middleware_socket,
                                   f'{self.current_path}/{username_password}',
                                   self.chunk_size)

                else:
                    logger.info(
                        f'{self.middleware_socket} invalid command {command}, {self.chunk_size}'
                    )
Пример #9
0
    def run_protocol(self, client_connection):
        logged_in = False
        while True:
            #    self.directory = self.list_all(client_connection)
            print('waiting for instructions...')
            data = util.get_instructions(client_connection, self.chunk_size)

            logger.debug(f"Received data from get_instructions: {data}")
            if data == CLOSE:
                client_connection.close()
                return True
            if data == LOGOUT:
                for node in self.data_nodes:
                    util.send_message(node.socket, LOGOUT, self.chunk_size)
                logged_in = False
                continue

            if not logged_in:
                if data.find(LOGIN) > -1:
                    #self.list_all(client_connection, False)
                    logged_in = self.login(client_connection)
                    if logged_in:
                        #self.list_all(client_connection, False)
                        logger.info('successful login')
                    else:
                        logger.info('failed to log in')
                elif data.find(ACCOUNT) > -1:

                    logged_in = self.create_account(client_connection, data)

                else:
                    util.send_message(client_connection,
                                      'login or acc to create an account',
                                      self.chunk_size)
            else:
                commands = data.split()
                if len(commands) < 2:
                    if commands[0] == LIST:
                        result = self.list_all(client_connection)
                    else:
                        logger.debug(
                            f'{client_connection}, wrong command {commands}, {self.chunk_size}'
                        )

                elif len(commands) == 2:
                    command, filename = commands[0], commands[1]
                    if command == DOWNLOAD:
                        result = self.assemble_data(client_connection,
                                                    filename)
                        print('------downloaded------')
                    elif command == UPLOAD:
                        self.to_servers(client_connection, filename)
                        print('-------uploaded-------')

                    elif command == DELETE:
                        result = self.delete_file(filename)
                        continue
                    else:
                        # TODO: handle wrong command without writing it into file:
                        # send stop phrase padded with 'wrong message
                        logger.debug(
                            f'{client_connection}, wrong command {command}, {self.chunk_size}'
                        )