def uploadFile(conn, fileName, myIP, clientIP, key):
    filePath = fileName
    header = None
    replyMsgObj = None
    replyMsg = None
    
    try:
        filePtr = open(filePath, "r")
    except IOError:
        print("Unable to open file: ", fileName)
        header = st.Header(st.SERVICEERROR, myIP, clientIP)
        replyMsgObj = st.Message()
        replyMsgObj.header = header
        replyMsg = pickle.dumps(replyMsgObj)
        conn.send(replyMsg)
        return False
    
    flag = False
    try:
        i = 1
        fileStat = os.stat(filePath)
        fileSize = fileStat.st_size
        conn.send(str(fileSize).encode('ascii'))
        while True:
            data = filePtr.read(st.MAX_BUFF_SIZE)

            # Encrypt the data
            data = st.encryptString(key, data)

            replyMsg = data
            
            if data == b'' or len(data) < st.MAX_BUFF_SIZE:
                flag = True
            conn.send(replyMsg.encode('ascii')) 
            print("sending chunk:" ,i)
            print("chunk data after encryption is:\n", data)
            
            i += 1
            if flag:
                print("This is the last chunk to be sent from server..")
                break

        if flag:
            print("File sent to the client successfully..")
        return flag
    except:
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print("Error occurred while sending the file to the client..")
        print("*** print_exception:")
        traceback.print_exception(exc_type, exc_value, exc_traceback,limit=2, file=sys.stdout)
        print("*** print_tb:")
        traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)

        filePtr.close()
        header = st.Header(st.SERVICEERROR, myIP, clientIP)
        replyMsgObj = st.Message()
        replyMsgObj.header = header
        replyMsg = pickle.dumps(replyMsgObj)
        conn.send(replyMsg)
        return False
def authenticate(conn, myIP, serverIP):
    id = input("Enter the this client's ID: ")
    password = input("Enter the client's password: "******"Client successfully authenticated at server..")
        return True
    else:
        print("Error occurred while authenticating client at server..")
        # Needs to close the connection at the server
        # conn.close()
        # closeConnection(conn)
        # quit()
    return False
def establiishKey(conn, myIP, serverIP):
    global SESSION_KEY
    Y_A = pow(st.alpha, X_A, st.prime)
    
    header = st.Header(st.KEYESTAB, myIP, serverIP)
    reqMsgObj = st.Message()
    reqMsgObj.header = header
    reqMsgObj.dummy = Y_A
    
    # Serializing object into ByteString
    reqMsg = pickle.dumps(reqMsgObj)
    conn.send(reqMsg)
    
    # Deserializing received string into object
    replyMsg= conn.recv(st.MAX_LEN)
    replyMsgObj = pickle.loads(replyMsg)

    if replyMsgObj.status == st.SUCCESSFUL:
        Y_B = replyMsgObj.dummy
        SESSION_KEY = pow(Y_B, X_A, st.prime)
        print("Session key sucessfully established and is: ", SESSION_KEY)
        return True
    else:
        print("Unable to setup session key as Y_B not supplied by server..")
        # Needs to close the connection at the server
        # conn.close()
        closeConnection(conn)
        quit()
    return False
def closeConnection(conn):
    header = st.Header(st.EXIT, myIP, serverIP)
    reqMsgObj = st.Message()
    reqMsgObj.header = header
    reqMsg = pickle.dumps(reqMsgObj)
    conn.send(reqMsg)
    conn.close()
Beispiel #5
0
def loginCreate(conn, myIP, serverIP):
    # Later all the valid fields of Message object needs to be encrypted using the already established key between this client and server -- ToDo
    id = input("Enter the client's ID: ")
    password = input("Enter the client's password: "******"Client successfully registered at server..")
        return True
    else:
        print("Error occurred while registering client at server..")
        # Needs to close the connection at the server
        # conn.close()
        closeConnection(conn)
        quit()
    return False
def downloadFile(conn, myIP, serverIP):

    fileName = input("Enter the file name to be downloaded: ")
    header = st.Header(st.SERVICEREQUEST, myIP, serverIP)
    reqMsgObj = st.Message()
    reqMsgObj.header = header
    reqMsgObj.file = fileName

    # First Message needs to be encrypted -- ToDo
    # do not encrypt header
    reqMsgObj = st.encryptMessageObj(SESSION_KEY, reqMsgObj)

    reqMsg = pickle.dumps(reqMsgObj)
    conn.send(reqMsg)

    try:
        filePath = "downloads/" + fileName
        filePtr = open(filePath, "w")
    except IOError:
        print("Unable to open file: ", fileName)
        return
        # closeConnection(conn)
    
    # File opened successfully
    try:
        replyMsg = conn.recv(st.MAX_LEN)
        replyMsg = replyMsg.decode('ascii')
        fileSize = int(replyMsg)
        bytesRead = 0
        i = 1
        while True:
            replyMsg = conn.recv(st.MAX_BUFF_SIZE)
            replyMsg = replyMsg.decode('ascii')
            print("Chunk number is: ", i)
            data = replyMsg
            print("Chunk before decryption is: ", data)
            # Decrypt the data
            data = st.decryptString(SESSION_KEY, data)
            print("Chunk after decryption is: ", data)

            i += 1
            filePtr.write(data)
            bytesRead += len(data)
            # checking whether the current chunk is the last one
            if bytesRead >= fileSize:
                print("File has been downloaded successfully..")
                filePtr.close()
                break
    except:
        print("Exception occured while downloading the file from server..")
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print("*** print_exception:")
        traceback.print_exception(exc_type, exc_value, exc_traceback,limit=2, file=sys.stdout)
        print("*** print_tb:")
        traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
        filePtr.close()
        return
Beispiel #7
0
def downloadFile(conn, myIP, serverIP):

    fileName = input("Enter the file name to be downloaded: ")
    header = st.Header(st.SERVICEREQUEST, myIP, serverIP)
    reqMsgObj = st.Message()
    reqMsgObj.header = header
    reqMsgObj.file = fileName
    reqMsg = pickle.dumps(reqMsgObj)
    conn.send(reqMsg)

    try:
        filePath = "downloads/" + fileName
        filePtr = open(filePath, "wb")
    except IOError:
        print("Unable to open file: ", fileName)
        return
        # closeConnection(conn)
    try:
        replyMsg = conn.recv(st.MAX_LEN)
        replyMsg = replyMsg.decode('ascii')
        fileSize = int(replyMsg)
        bytesRead = 0
        i = 1
        while True:
            replyMsg = conn.recv(st.MAX_BUFF_SIZE)
            print("Chunk number is: ", i)
            data = replyMsg
            # print("chunk:",i," is:\n", data)
            i += 1
            filePtr.write(data)
            bytesRead += len(data)
            # checking whether the current chunk is the last one
            if bytesRead >= fileSize:  # replyMsgObj.status == st.SUCCESSFUL
                print("File has been downloaded successfully..")
                filePtr.close()
                break
    except:
        print("Exception occured while downloading the file from server..")
        exc_type, exc_value, exc_traceback = sys.exc_info()
        print("*** print_exception:")
        traceback.print_exception(exc_type,
                                  exc_value,
                                  exc_traceback,
                                  limit=2,
                                  file=sys.stdout)
        print("*** print_tb:")
        traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
        filePtr.close()
        return
Beispiel #8
0
def processClient(conn, clientAddr, myIP):
    replyMsg = "You are now connected with " + myIP + "\n"
    conn.send(replyMsg.encode('ascii'))
    clientIP = clientAddr[0]
    while True:
        #
        rcvdMsg = conn.recv(st.MAX_LEN)
        # print("From client: ",rcvdMsg)
        rcvdMsgObj = pickle.loads(rcvdMsg)

        # First Decrypt the received message -- ToDo
        # It is assumed that header is not encrypted
        opcode = rcvdMsgObj.header.opcode

        if opcode == st.EXIT:
            print("inside EXIT")
            conn.close()
            break

        elif opcode == st.KEYESTAB:
            print("Inside KEYESTAB")
            Y_A = rcvdMsgObj.dummy
            key = pow(Y_A, X_B, st.prime)
            keyDict[clientAddr] = key
            print("Session Key for client: ", clientAddr, " is: ", key)
            Y_B = pow(st.alpha, X_B, st.prime)
            header = st.Header(st.KEYESTABDONE, myIP, clientIP)
            replyMsgObj = st.Message()
            replyMsgObj.header = header
            replyMsgObj.dummy = Y_B
            replyMsgObj.status = st.SUCCESSFUL
            replyMsg = pickle.dumps(replyMsgObj)
            conn.send(replyMsg)

        elif opcode == st.LOGINCREAT:
            print("Inside LOGINCREAT")

            rv = loginCreate(rcvdMsgObj)
            header = st.Header(st.LOGINREPLY, myIP, clientIP)
            replyMsgObj = st.Message()
            replyMsgObj.header = header
            if rv:
                replyMsgObj.status = st.SUCCESSFUL
            else:
                replyMsgObj.status = st.UNSUCCESSFUL

            # Encrypt the reply message object -- ToDo
            replyMsg = pickle.dumps(replyMsgObj)
            conn.send(replyMsg)

        elif opcode == st.AUTHREQUEST:
            print("Inside AUTHREQUEST")

            rv = authenticateClient(rcvdMsgObj.id, rcvdMsgObj.password)
            header = st.Header(st.AUTHREPLY, myIP, clientIP)
            replyMsgObj = st.Message()
            replyMsgObj.header = header
            if rv:
                replyMsgObj.status = st.SUCCESSFUL
            else:
                replyMsgObj.status = st.UNSUCCESSFUL

            # Encrypt the reply message object -- ToDo
            replyMsg = pickle.dumps(replyMsgObj)
            conn.send(replyMsg)

        elif opcode == st.SERVICEREQUEST:
            print("Inside SERVICEREQUEST")
            uploadFile(conn, rcvdMsgObj.file, myIP, clientIP)
        else:
            print("Inside else")
            pass