0

So i am trying to make a message application with tkinter gui. I have a client and a server, which is currently just running locally.

So i i generate a fernet key which i first send to the server, so we can both encrypt with the same key (i know this probably should be encrypted when i send it).

The first time i call the client.connect() it works fine, gets the key and checks if the roomId exists. If it does not it spits out an error and all is fine. However if I try again, calling client.connect() the fernet key which the server receives gets all messed up.

First time
Client
b'Nwb0wki6ydmeZgEyMqo0ZFvvPm_grlpnYicjhCMZTMg='
b'Nwb0wki6ydmeZgEyMqo0ZFvvPm_grlpnYicjhCMZTMg='
b'Nwb0wki6ydmeZgEyMqo0ZFvvPm_grlpnYicjhCMZTMg='
Server
b'Nwb0wki6ydmeZgEyMqo0ZFvvPm_grlpnYicjhCMZTMg='

As you can see the first time it works fine

Second time
Client
b'tq2uBDFfpV0vAmDNpJKmA-87ElJqa5Unsme7OGCTG80='
b'tq2uBDFfpV0vAmDNpJKmA-87ElJqa5Unsme7OGCTG80='
b'tq2uBDFfpV0vAmDNpJKmA-87ElJqa5Unsme7OGCTG80='
Server
b'tq2uBDFfpV0vAmDNpJKmA-87ElJqa5Unsme7OGCTG80=gAAAAABfmnNNS9XplQlqNf3-7vlEgk-VAHsq6EIJaWTUhLyaCqIictM7v5rnh6_dMhKKNvGc3otMi08SEhtmgfyK3KSXD6SckOk9abFGW6-KS36b3jtThBdmid1EXxCmu7B0IgmulmZF_K0VhKAiOEty74nqZ_YLkDzfaaKHzXnPsfx-39ssKSA='

Even though i am calling the exact same method, and its making a new connecting, and it seems to be sending a perfectly fine key, the server recievieves something totally different. And yes i only have one client connected to the server

Client connection function


def connect(self, username="joe", roomId="12346", host="127.0.0.1", port="9001"):
    self.host = host
    self.port = int(port)
    self.key = fernet.Fernet.generate_key()
    print(self.key)
    self.username = username
    try:
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((self.host, self.port))
        self.connected = True

        print(self.key)
        self.sock.send(self.key)
        print(self.key)
        print("sendte key")

        self.sock.sendall(Encryption().encryptMsg(self.username, self.key))
        print("sendte username")

        self.sock.sendall(Encryption().encryptMsg(roomId, self.key))
        print(roomId)
        print("sendte roomId")

        msg = self.sock.recv(1024)
        msg = Encryption().decryptMsg(msg, self.key)
        print(msg)

        if msg["msg"] == False:
            self.disconnect()
            return False

        self.clientId, self.roomId = msg["msg"]

        self.thread = threading.Thread(target=self.listen)
        self.thread.start()
        print("Started listening")
        print(self.username, self.roomId, self.clientId)

        return True

    except Exception as e:
        print(e)
        self.disconnect()
        return False

def disconnect(self):
    print("lukker sock")
    self.sock.shutdown(socket.SHUT_RDWR)

Server receiving function


def run(self):
    self.sock.bind((self.host, self.port))
    self.sock.listen(self.listenInt)
    while True:
        # try:
        conn, addr = self.sock.accept()
        print("Got connection from: ", addr)

        newCon = threading.Thread(
            target=self.handleConnection, args=(conn, addr))
        newCon.start()

    self.sock.close()

def handleConnection(self, conn, addr):
    print("startet handler")
    key = conn.recv(1024)
    print("fikk key")
    print(key)
    username = Encryption().decryptMsg(conn.recv(1024), key)["msg"]
    print("fikk username")
    print(username)

    roomId = int(Encryption().decryptMsg(conn.recv(1024), key)["msg"])
    print("fikk roomid")
    print(roomId)
    print(self.rooms)

    if roomId == 0:
        room = Room(self)
        roomId = room.roomId
        self.rooms[roomId] = room
        print("lagde rom")
    elif roomId not in self.rooms:
        print("finner ikke rom")
        conn.sendall(Encryption().encryptMsg(False, key))
        print("sendte false")
        conn.close()
        return

    room = self.rooms[roomId]

    newCon = threading.Thread(
        target=serverConnection, args=(conn, addr, key, room, username))
    newCon.start()

Kippster
  • 11
  • 2
  • I would suggest `conn.close()` when client disconnects and the same after his connection is terminated because of wrong key (server side) – Matiiss Oct 29 '20 at 08:22
  • @Matiiss okay i will try that – Kippster Oct 29 '20 at 09:05
  • @Matiiss i added conn.close() on the server, and seld.disconnect() on the client closes the socket. still does not work. (I updated the code above) – Kippster Oct 29 '20 at 09:26
  • so as I understand server is sending the messed up key? in the code you have shown only thng sent from a server is False message so can you show `serverConnection()`? – Matiiss Oct 29 '20 at 09:59

0 Answers0