Example #1
0
    def handle(self):
        global last_nonce

        self.data = self.request.recv(1024).strip()
        route = decrypt(self.data)
        route = Route.fromrequest(route)

        if (route.get_sel() == "init"):
            print("A client node is trying to authenticate itself")
            # Client has sent the session key encrypted using this FS's key
            enc_key = route.body['key']

            try:
                session_key = decrypt(bytes.fromhex(enc_key), key)
                last_nonce = random.randrange(1000)

                response = json.dumps({'nonce': str(last_nonce)})
            except:
                response = json.dumps({'error': "1"})
            finally:
                response = encrypt(response)
                self.request.sendall(response)

        elif (route.get_sel() == "confirm"):
            print("Received nonce and RPC")
            nonce = int(route.body['nonce'])

            if (nonce == last_nonce + 1):
                print("Execute:", route.body['command'])

                command = route.body['command']
                words = command.split(' ')

                if (words[0] == "cat"):
                    response = cat(words[1])

                elif (words[0] == "cp"):
                    response = cp(words[1], words[2])
            else:
                response = json.dumps({'result': 'Authentication failed'})

            response = encrypt(response)
            self.request.sendall(response)
    def handle(self):
        self.data = self.request.recv(1024).strip()
        route = decrypt(self.data)
        route = Route.fromrequest(route)

        # print(route.get_sel())

        if (route.get_sel() == "init"):
            print("A file system node is initialising the connection")

            # A file system node is initialising the connection
            fs = FileSystem(len(filesystems) + 1,
                            port=route.body['port'],
                            files=route.body['files'])

            # Save the generated id and key pair (for faster lookups)
            filesystems[fs.get_id()] = fs
            response = fs.serialize()

            print("Registering file system node with id:", fs.get_id())
            response = encrypt(response)

            self.request.sendall(response)

        elif (route.get_sel() == "update"):
            print("A file system node is updating its files")

            id = route.body['id']
            update_fs = filesystems[id]

            update_fs.set_files(route.body['files'])

            response = encrypt(json.dumps({'result': 'successful'}))

            self.request.sendall(response)

        elif (route.get_sel() == "login"):
            print("A client node is trying to login")
            if (route.body['username'] == "admin"
                    and route.body['password'] == "password"):
                # Successful login
                cl = Client(len(clients) + 1)
                clients[cl.get_id()] = cl
                response = cl.serialize()

                print("Successful login and key generated for client:",
                      cl.get_id())
                response = encrypt(response)

                self.request.sendall(response)
            else:
                # Login failed
                self.request.sendall(bytes('\0', 'utf-8'))

        elif (route.get_sel() == "ls"):
            print("A client node is requesting all files")
            response = generate_listing()
            response = encrypt(json.dumps(response))

            self.request.sendall(response)

        elif route.get_sel() == "comm":
            # Initial communication for Needham Schroeder
            fs_id = route.body['id']
            fs_key = filesystems[fs_id].get_key()

            print("A client node is initialising communication with a FS node",
                  fs_id)

            # Generate session key
            session_key = os.urandom(16).hex()
            encrypted_session_key = encrypt(session_key, fs_key)

            response = encrypt(
                json.dumps({
                    'key': session_key,
                    'encrypted': encrypted_session_key.hex()
                }))

            self.request.sendall(response)