0

所以我正在尝试用 tkinter gui 制作一个消息应用程序。我有一个客户端和一个服务器,目前只是在本地运行。

所以我生成了一个我首先发送到服务器的 fernet 密钥,所以我们都可以使用相同的密钥进行加密(我知道这可能应该在我发送时加密)。

我第一次调用 client.connect() 它工作正常,获取密钥并检查 roomId 是否存在。如果没有,它会吐出一个错误,一切都很好。但是,如果我再试一次,调用 client.connect() 服务器收到的 fernet 密钥就会变得一团糟。

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

正如您第一次看到它工作正常

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

即使我正在调用完全相同的方法,并且它进行了新的连接,并且它似乎正在发送一个完美的密钥,但服务器收到了完全不同的东西。是的,我只有一个客户端连接到服务器

客户端连接功能


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)

服务器接收功能


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()

4

0 回答 0