Ejemplo n.º 1
0
def load_balance(c):

    # get the servers current ip
    ip = helper.find_ip()

    # get the list of files
    list_of_files = ledger.get_files_for_owner(ip)

    # send confirmation
    c.send(helper.pad_string("Server is ready load balance its files").encode())

    # call the clients to get all the files locally and push them back
    for file in list_of_files:

        # run a os call to get the file
        os.system("python3 client.py pull " + file)

        # run an os call to send back the file to all the network
        os.system("python3 client.py push " + file)

        # remove the local copy
        os.system("rm directory/" + file)


    # send confirmation
    c.send(helper.pad_string("Server has done load balancing its files").encode())
Ejemplo n.º 2
0
def lock_server(c, ip):
    if lock.unlocked():
        lock.acquire(ip)
        print("Server locked by ", ip)
        c.send(helper.pad_string("Server locked").encode())
    else:
        send_error("Error 124: Server already locked")
Ejemplo n.º 3
0
def update_ledger(c, filename, ip):

    if(ip == helper.find_ip()):
        print("Same server as client")
        c.send(helper.pad_string("Server doesnt need your ledger").encode())
        if lock.locked():
            lock.release()
        return

    if lock.locked() and not lock.check_lock(ip):
        send_error(c, "Error 123: Server currently busy")
        return

    # start with the time
    start = time.time()

    # open a temporary file to store the received bytes
    file = open(filename, 'wb')
    byte = 0

    # send confirmation
    c.send(helper.pad_string("Server is ready to update its ledger").encode())

    while True:

        # receive 1024 bytes at a time and write them to a file
        bytes = c.recv(BYTES_TO_SEND)
        bytes = encryption.decrypt_using_private_key(bytes)
        file.write(bytes)
        byte += BYTES_TO_SEND

        # break infinite loop once all bytes are transferred
        if not bytes:
            break

    # close the file once transfer is complete
    file.close()

    # time and space prints
    end = time.time()
    print("Finished running download of file %s in %.2f seconds" % (filename, float(end - start)))
    print(byte, "bytes sent")

    # Release the lock if one is present
    if lock.locked() and lock.check_lock(ip):
        print("Server lock released by ", lock.return_lock())
        lock.release()
Ejemplo n.º 4
0
def send_ledger(c, ip):

    start = time.time()

    # open the ledger if it exists
    try:
        f = open(ledger.LEDGER_PATH, 'rb')
    except:
        send_error(c, "Error 176: Ledger doesn't exist on server machine")

        c.close()
        return

    # send confirmation in plaintext
    c.send(helper.pad_string("Server is ready to send ledger").encode())

    # initialize an empty bytestring for the new pubkey
    encryptedClientPubkey = b''

    # client pubkey is received in 2 parts
    for i in range(2):
        encryptedClientPubkey += c.recv(REQUEST_MAX_LENGTH)

    # decrypt the client pubkey
    clientPubkey = encryption.decrypt_using_private_key(encryptedClientPubkey).decode()

    # read bytes and set up counter
    l = f.read(encryption.MESSAGE_CHUNK_LIMIT)
    byte = BYTES_TO_SEND

    # a forever loop untill file gets sent
    while (l):

        encrypted_l = encryption.encrypt_using_public_key(l, clientPubkey)

        # send the bytes
        c.send(encrypted_l)

        # read more bytes and incrementing counter
        l = f.read(encryption.MESSAGE_CHUNK_LIMIT)
        byte += BYTES_TO_SEND

    # time and space prints
    end = time.time()
    print("Sent ledger in %.2f seconds" %  float(end - start))
    print(byte, "bytes sent")

    # close the connection with the client
    c.close()
Ejemplo n.º 5
0
def receive_file(c, filename, ip):

    if lock.locked() and not lock.check_lock(ip):
        send_error(c, "Error 123: Server currently busy")
        return

    if lock.unlocked():
        send_error(c, "Error 125: Server needs to be locked before write")
        return

    start = time.time()

    # open a temporary file to store the received bytes
    try:
        file = open("fico/" + filename, 'wb')
    except:
        os.system("mkdir fico")
        file = open("fico/" + filename, 'wb')

    byte = 0

    # send confirmation
    c.send(helper.pad_string("Server is ready to recieve file").encode())

    while True:

        # receive 1024 bytes at a time and write them to a file
        bytes = c.recv(BYTES_TO_SEND)
        file.write(bytes)
        byte += BYTES_TO_SEND

        # break infinite loop once all bytes are transferred
        if not bytes:
            break

    # close the file once transfer is complete
    file.close()

    # time and space prints
    end = time.time()
    print("Finished running download of file %s in %.2f seconds" % (filename, float(end - start)))
    print(byte, "bytes sent")
Ejemplo n.º 6
0
def send_file(c, filename):

    start = time.time()

    # opening a file if possible
    try:
        f = open("fico/" + filename, 'rb')
    except:
        send_error(c, "Error 102: File doesn't exist on server machine")

        c.close()
        return

    # send confirmation
    print("Server is ready to send file")
    c.send(helper.pad_string("Server is ready to send file").encode())

    # read bytes and set up counter
    l = f.read(BYTES_TO_SEND)
    byte = BYTES_TO_SEND

    # a forever loop untill file gets sent
    while (l):

        # send the bytes
        c.send(l)

        # read more bytes and incrementing counter
        l = f.read(BYTES_TO_SEND)
        byte += BYTES_TO_SEND

    # time and space prints
    end = time.time()
    print("Finished running download of file in %.2f seconds" %  float(end - start))
    print(byte, "bytes sent")

    time.sleep(0.5)

    # Close the connection with the client
    c.close()
Ejemplo n.º 7
0
def send_error(c, errorMessage):
    error = helper.pad_string(errorMessage)
    c.send(error.encode())
    print(error)
Ejemplo n.º 8
0
def receive_file(filename):

    # check if the client is the owner of the file
    if not ledger.check_owner(filename, helper.find_ip()):
        print("File not owned by this client")
        return

    # create the downloaded directory if it doesnt exist, and open a file to write in
    try:
        file = open("directory/" + filename, 'wb')
    except:
        os.mkdir("directory")
        file = open("directory/" + filename, 'wb')

    # going through the list of ips and making the request
    for index, ip in enumerate(ledger.get_ips_for_file(filename)):

        # get the key of the server we want to send the file to
        serverPubkey = ledger.get_pubkey(ip)

        print(helper.find_ip(), ip)
        # check if the current ip in the ledger is the clients
        if(ip == helper.find_ip()):

            # get the shard name for the file that is stored on the clients computer
            shard = ledger.get_shard(filename, ip)

            # open the shard to combine it with the rest of the file
            tempFile = open("directory/" + shard, 'rb')

            # copy the contents of the shard to the new file and decrypt the data
            file.write(tempFile.read())

            print("here")

            # continue iterating through the loop
            continue


        # connect to the host you want to receive files from
        s = run_client(ip)

        # gets the shard filename as stored on the host computer
        shard = ledger.get_shard(filename, ip)

        # create a pull request for the server and encode it to bytes
        cmd = helper.pad_string("pull " + shard)

        # Encrypt cmd using servers public key
        encryptedCmd = encryption.encrypt_using_public_key(cmd.encode(), serverPubkey)
        s.send(encryptedCmd)

        # recieve confirmation response from server
        receivedMessage = s.recv(REQUEST_MAX_LENGTH).decode()


        # check if the servor responded with an error
        if(receivedMessage.split()[0] != "Error"):

            print("Receiving shard from", ip)

            while True:


                #receive 1024 bytes at a time and decrypt the data
                bytes = s.recv(1024)
                bytes = encryption.decrypt_using_private_key(bytes)

                #write the decrypted data to a file
                file.write(bytes)


                #break infinite loop once all bytes are transferred
                if not bytes:
                    break

            # Server responded with an error
        else:
            print("Something went wrong while receiving the file.")


    # try to remove the original sharded message
    try:
        os.remove("directory/" + ledger.get_shard(filename, helper.find_ip()))
    except:
        print("Unable to remove shard from local directory")

    #close the file once transfer is complete
    file.close()