def read(conn, mask): data = conn.recv(1024) client_infos = clients_information[conn] if data: data = json.loads(data) #client has done dh shandshake if client_infos["dh_handshake"]: data["header"] = symmetriccrypt.decrypt( client_infos["common_secret"], data["header"].encode(), "AES-128", "CBC").decode() if data["header"] == "CN": #client name #receive client name do_CN(conn, data, client_infos) elif data["header"] == "NM": #new message do_NM(conn, data, client_infos) #client has not done the dh handshake yet else: #client confirm received dh parameters if data["header"] == "RDHP": #send my dh public numbers to client conn.send( json.dumps({ "header": "SDHN", "data": client_infos["my_dh_public_number"] }).encode()) logger.debug("sended dh public number to new client") #do dh handshake else: do_CDHN(conn, data, client_infos) #empty data closing client connection else: logger.info('closing {}'.format(client_infos["addr"])) response = "{} leave on chat".format(client_infos["name"]) clients_connection.remove(conn) del clients_information[conn] sel.unregister(conn) conn.close() #inform others who leave for participant in clients_connection: if not (clients_information[participant]["dh_handshake"]): continue response = { "header": symmetriccrypt.encrypt( clients_information[participant]["common_secret"], "SM", "AES-128", "CBC").decode(), "data": symmetriccrypt.encrypt( clients_information[participant]["common_secret"], response, "AES-128", "CBC").decode() } participant.send(json.dumps(response).encode())
def server_message_received(conn): data = conn.recv(1024) #client has done dh shandshake if server_connection["dh_handshake"]: data = json.loads(data) data["header"] = symmetriccrypt.decrypt(server_connection["common_secret"], data["header"].encode(), "AES-128", "CBC").decode() #message chat settings if data["header"] == "SM": do_SM(conn, data) #chat messages elif data["header"] == "OM": do_OM(conn, data) #this client has not done the dh handshake yet else: if server_connection["have_dh_parameters"]: #receive server dh public numbers do_SDHN(conn, data) else: #receive dh parameters from server do_DHP(conn, data)
def do_CN(conn, data, client_infos): data["data"] = symmetriccrypt.decrypt(client_infos["common_secret"], data["data"].encode(), "AES-128", "CBC").decode() clients_information[conn]["name"] = data["data"] #introduces the new client to others sey_to_others = "now {} is connected".format(client_infos["name"]) for participant in clients_connection: if participant == conn or not ( clients_information[participant]["dh_handshake"]): continue response = { "header": symmetriccrypt.encrypt( clients_information[participant]["common_secret"], "SM", "AES-128", "CBC").decode(), "data": symmetriccrypt.encrypt( clients_information[participant]["common_secret"], sey_to_others, "AES-128", "CBC").decode() } participant.send(json.dumps(response).encode()) logger.debug("fineshed do_CN, client name is {}".format(data["data"]))
def do_NM(conn, data, client_infos): data["data"] = symmetriccrypt.decrypt(client_infos["common_secret"], data["data"].encode(), "AES-128", "CBC").decode() now = datetime.datetime.now() now = "{}:{}".format(now.hour, now.minute) data["data"] = "{};{};{}".format(client_infos["name"], data["data"], now) for participant in clients_connection: if participant == conn or not ( clients_information[participant]["dh_handshake"]): continue response = { "header": symmetriccrypt.encrypt( clients_information[participant]["common_secret"], "OM", "AES-128", "CBC").decode(), "data": symmetriccrypt.encrypt( clients_information[participant]["common_secret"], data["data"], "AES-128", "CBC").decode() } participant.send(json.dumps(response).encode()) logger.debug("finished do_NM")
def do_OM(conn, data): data = symmetriccrypt.decrypt(server_connection["common_secret"], data["data"].encode(), "AES-128", "CBC").decode() data = data.split(";") window.manager.current_screen.receive_chat_participants_message(data)
def do_SM(conn, data): data = symmetriccrypt.decrypt(server_connection["common_secret"], data["data"].encode(), "AES-128", "CBC").decode() window.manager.current_screen.receive_server_nofication(data)