コード例 #1
0
    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())
コード例 #2
0
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)
コード例 #3
0
ファイル: command.py プロジェクト: geobreze/networking-lab2
def perform_delete(session: Session):
    session.sock.send_string("Please, enter filename to delete.")
    file = decode_utf8(session.sock.recv().body)
    try:
        storage.delete_for_user(session.username, file)
    except FileNotFoundError:
        session.sock.send(b'File not found',
                          input_wanted=NO_INPUT,
                          response_code=NOT_FOUND)
コード例 #4
0
ファイル: command.py プロジェクト: geobreze/networking-lab2
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)
コード例 #5
0
ファイル: command.py プロジェクト: geobreze/networking-lab2
def perform_get(session: Session):
    session.sock.send_string("Please, enter filename to read.")
    file = decode_utf8(session.sock.recv().body)
    try:
        data = storage.get_for_user(session.username, file,
                                    session.server_aes_key)
        enc = encrypt_aes(session.key, data)
        session.sock.send(enc, flag=AES_ENCODED, input_wanted=NO_INPUT)
    except FileNotFoundError:
        session.sock.send(b'File not found',
                          input_wanted=NO_INPUT,
                          response_code=NOT_FOUND)
コード例 #6
0
 def auth(self, is_first=False):
     pub, private = client_storage.get_rsa_pair()
     if is_first:
         self.sock.send(pub)
     aes_key_coded = self.sock.recv().body
     self.aes_key = crypt.decrypt_rsa(private, aes_key_coded)
     if is_first:
         print(decode_utf8(self.sock.recv().body))
         username = input()
         self.sock.send_string(username)
     print(decode_utf8(self.sock.recv().body))
     password = getpass()
     self.sock.send(encrypt_aes(self.aes_key, encode_utf8(password)),
                    flag=AES_ENCODED)
     response = self.sock.recv()
     if response.response_code != SUCCESS:
         print("Failed to authenticate. Response code: {}".format(
             response.response_code))
         self.sock.close()
         return False
     print("Successfully authenticated!")
     return True
コード例 #7
0
ファイル: command.py プロジェクト: geobreze/networking-lab2
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)
コード例 #8
0
    def handle_request(self):
        from command import perform_by_name

        self.authenticate(is_first=True)
        while True:
            self.refresh_token()
            self.sock.send_string("Please, enter command.")
            cmd = decode_utf8(self.sock.recv().body)
            try:
                perform_by_name(cmd, self)
            except NameError as e:
                print(e)
                self.sock.send_string("Invalid name was supplied. Try again.",
                                      input_wanted=NO_INPUT,
                                      response_code=BAD_REQUEST)
            except PermissionError:
                print("Permission denied for user {}.".format(self.username))
                self.sock.send_string("Permission denied.",
                                      input_wanted=NO_INPUT,
                                      response_code=FORBIDDEN)
コード例 #9
0
    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)