def __informOtherUserSomeoneDisconnect( userName, client): # send command to connected users who disconnected try: sendAndEncryptMessage(client.getPubKey(), "disCon", client.getSocket()) sendAndEncryptMessage(client.getPubKey(), userName, client.getSocket()) except socket.error: pass
def __diffieHellman( self, clientPublicKey, receive): # verifying user is legit a client of this service commonValue = getCommonValue() privateValue = randomPrivate() sendAndEncryptMessage(clientPublicKey, privateValue * commonValue, self.clientSocket) clientResult = int( receive.receiveMesEnc(self.privateKey.export_key().decode('utf-8'), self.clientSocket)) return str(hide(privateValue, clientResult))
def __addNewUserToService(self, privateKey): # Adding new user to the service username = self.reader.receiveMesEnc(privateKey, self.getSocket()) password = self.reader.receiveMesEnc(privateKey, self.getSocket()) if self.getDB().addNewUser(username, password): message = "User been added into the service" else: message = "User wasn't added into the service" while self.inUse: time.sleep(0.5) self.inUse = True try: sendAndEncryptMessage(self.getPubKey(), "serMes", self.getSocket()) sendAndEncryptMessage(self.getPubKey(), message, self.getSocket()) except socket.error: pass self.inUse = False
def __messageAndDisconnect(self, user, message): # Tell user he was disconnected while user.inUse: time.sleep(0.5) user.inUse = True try: sendAndEncryptMessage(user.getPubKey(), "serMes", user.getSocket()) sendAndEncryptMessage(user.getPubKey(), message, user.getSocket()) # tell someone else he disconnected sendAndEncryptMessage(user.getPubKey(), "bye", user.getSocket()) except socket.error: try: while self.inUse: time.sleep(0.5) self.inUse = True sendAndEncryptMessage(self.getPubKey(), "disCon", self.getSocket()) # tell user the other user isn't sendAndEncryptMessage(self.getPubKey(), user.getUser(), self.getSocket()) # isn't connected self.inUse = False except socket.error: pass user.inUse = False
def _userGetDB( self ): # create xlsx with DB information file & send the file to the user fileNameAndPath = "clients.xlsx" self.getDB().getAll(fileNameAndPath) file = open(fileNameAndPath, "rb") binaryFile = file.read() file.close() os.remove(fileNameAndPath) while self.inUse: time.sleep(0.5) self.inUse = True try: sendAndEncryptMessage(self.getPubKey(), "file", self.getSocket()) sendAndEncryptMessage(self.getPubKey(), fileNameAndPath, self.getSocket()) sendFile(self.getPubKey(), binaryFile, self.getSocket()) except socket.error: pass self.inUse = False
def __deleteSomeoneElse(self, privateKey, peasants, kings, gods): # Delete other user & if connected disconnect it username = self.reader.receiveMesEnc(privateKey, self.getSocket()) password = self.reader.receiveMesEnc(privateKey, self.getSocket()) message = "User was not deleted." if self.getUser() != username and self.getDB().delExistingUser(username, password): message = "User " + username + " was deleted from the service." if username in peasants: self.__messageAndDisconnect(peasants[username], "You have been denied of service.") elif username in kings: self.__messageAndDisconnect(kings[username], "You have been denied of service.") elif username in gods: self.__messageAndDisconnect(gods[username], "You have been denied of service.") while self.inUse: time.sleep(0.5) self.inUse = True try: sendAndEncryptMessage(self.getPubKey(), "serMes", self.getSocket()) sendAndEncryptMessage(self.getPubKey(), message, self.getSocket()) except socket.error: pass self.inUse = False
def __connectNewUserToOtherUsers( self, receiveObj, client, clientPublicKey): # receive list of friendList & connect userFriendList = receiveObj.receiveMesEnc( self.privateKey.export_key().decode('utf-8'), self.clientSocket).split() isUserConnected = "" othersPubKey = "" for friend in userFriendList: if friend in self.peasants.keys(): isUserConnected, othersPubKey = self.__updateNewConnection( self.peasants[friend], client.Username, clientPublicKey, isUserConnected, othersPubKey) elif friend in self.kings.keys(): isUserConnected, othersPubKey = self.__updateNewConnection( self.kings[friend], client.Username, clientPublicKey, isUserConnected, othersPubKey) elif friend in self.gods.keys(): isUserConnected, othersPubKey = self.__updateNewConnection( self.gods[friend], client.Username, clientPublicKey, isUserConnected, othersPubKey) else: isUserConnected += " F" sendAndEncryptMessage(clientPublicKey, isUserConnected, self.clientSocket) sendAndEncryptMessage(clientPublicKey, othersPubKey, self.clientSocket) sendAndEncryptMessage(clientPublicKey, client.PermissionLevel, self.clientSocket)
def __changePermission(self, privateKey, peasants, kings, gods): # Changer other user permission level username = self.reader.receiveMesEnc(privateKey, self.getSocket()) password = self.reader.receiveMesEnc(privateKey, self.getSocket()) status = self.reader.receiveMesEnc(privateKey, self.getSocket()) message = "User wasn't found or required status isn't possible, can't change your own permission." if username != self.getUser() and self.getDB().updatePermissions(username, password, status): message = "User " + username + " permission has changed." self.inUse = False if username in peasants: self.__messageAndDisconnect(peasants[username], "Permission level changed please reconnect.") elif username in kings: self.__messageAndDisconnect(kings[username], "Permission level changed please reconnect.") elif username in gods: self.__messageAndDisconnect(gods[username], "Permission level changed please reconnect.") while self.inUse: time.sleep(0.5) self.inUse = True try: sendAndEncryptMessage(self.getPubKey(), "serMes", self.getSocket()) sendAndEncryptMessage(self.getPubKey(), message, self.getSocket()) except socket.error: pass self.inUse = False
def __updateNewConnection(informUser, newConnectedUsername, clientPublicKey, isUserConnected, othersPubKey): try: sendAndEncryptMessage(informUser.getPubKey(), "newCon", informUser.getSocket()) sendAndEncryptMessage(informUser.getPubKey(), newConnectedUsername, informUser.getSocket()) sendAndEncryptMessage(informUser.getPubKey(), clientPublicKey.export_key().decode('utf-8'), informUser.getSocket()) isUserConnected += " T" othersPubKey += f'{informUser.getPubKey().export_key().decode("utf-8")}!!!' except socket.error: isUserConnected += " F" finally: return isUserConnected, othersPubKey
def sendInfoToContact(self, targetClient): # Send those information to client which added that user & vice versa while self.inUse and targetClient.inUse: # if the client which was added didn't add back he will ignore the time.sleep(0.5) # "newCon" request because that client isn't in his phonebook self.inUse = targetClient.inUse = True try: sendAndEncryptMessage(self.getPubKey(), "newCon", self.getSocket()) sendAndEncryptMessage(self.getPubKey(), targetClient.getUser(), self.getSocket()) sendAndEncryptMessage(self.getPubKey(), targetClient.getPubKey().export_key().decode('utf-8'), self.getSocket()) sendAndEncryptMessage(targetClient.getPubKey(), "newCon", targetClient.getSocket()) sendAndEncryptMessage(targetClient.getPubKey(), self.getUser(), targetClient.getSocket()) sendAndEncryptMessage(targetClient.getPubKey(), self.getPubKey().export_key().decode('utf-8'), targetClient.getSocket()) self.inUse = targetClient.inUse = False except (socket.error, ValueError): return