Esempio n. 1
0
    def listenForPackets(self):
        while True:
            for client in self.clients:
                try:
                    recvPacket = str(user[0].recv(8192), "utf-8")
                    recvPacket = json.loads(recvPacket)
                except:
                    self.users.remove(user)
                    for IpAndName in self.loggedInUsers:
                        if {} == IpAndName:
                            var = None
                        else:
                            IpAndName.pop(str(user[1][0]))
                    self.createLogsUtil.printandwriteserverlog(
                        self.serverPrefix + str(user[1][0]) + ":" +
                        str(user[1][1]) +
                        " lost connection to server unexpectedly")

                if "0" == recvPacket['packetid']:
                    containsIllegalCharachters = self.helperUtils.checkForIllegalCharachters(
                        recvPacket['data'][0] + recvPacket['data'][1])
                    if containsIllegalCharachters:
                        self.sendPacketToClient(
                            packet("20", "IllegalCharachters",
                                   ["ERROR:IllegalCharachters"], user))
                    else:
                        checkRegister = self.helperUtils.checkRegister(
                            recvPacket['data'][0])
                        if checkRegister:
                            successfullyRegistered = self.mysqlHelperUtil.register(
                                recvPacket['data'][0], recvPacket['data'][1],
                                recvPacket['data'][2])
                            if successfullyRegistered:
                                self.sendPacketToClient(
                                    packet("0", "register", ["True"], user))
                                self.createLogsUtil.printandwriteserverlog(
                                    self.serverPrefix + str(user[1][0]) + ":" +
                                    str(user[1][1]) +
                                    " registered a new User: "******"0", "register", ["False"], user))
                        else:
                            self.sendPacketToClient(
                                packet("20", "userAllreadyExists",
                                       ["ERROR:userAllreadyExists"], user))
                else:
                    print("unknown packet id received from " +
                          str(user[1][0]) + ":" + str(user[1][1]))
Esempio n. 2
0
 def execute(self, args):
     print(self._server.serverPrefix + "stopped server")
     for user in self._server.users:
         self._server.sendPacketToClient(
             packet("8", "shutdown", ["True"], user))
         user[0].shutdown(socket.SHUT_RDWR)
         user[0].close()
     print(
         "please stop the server by closing the script ctrl+c or shuting down the machine where the server runs on"
     )
Esempio n. 3
0
 def listenForConnections(self):
     while True:
         self.connection.listen(1)
         client = self.connection.accept()
         if self.mysqlHelperUtil.isBanned(client[1][0]):
             self.sendPacketToClient(packet("4", "ban", ["True"], client))
             client[0].shutdown(socket.SHUT_RDWR)
             client[0].close()
         else:
             self.users.append(client)
             self.createLogsUtil.printandwriteserverlog(self.serverPrefix +
                                                        str(client[1][0]) +
                                                        ":" +
                                                        str(client[1][1]) +
                                                        " connected")
Esempio n. 4
0
 def keepAliveRequestWorker(self):
     while True:
         time.sleep(3)
         for user in self.users:
             if user[1] in self.lastKeepAlivePackets and (
                     time.time() - self.lastKeepAlivePackets[user[1]]) > 8:
                 self.users.remove(user)
                 for IpAndName in self.loggedInUsers:
                     IpAndName.pop(str(user[1][0]))
                 user[0].shutdown(socket.SHUT_RDWR)
                 user[0].close()
                 self.createLogsUtil.printandwriteserverlog(
                     self.serverPrefix + str(user[1][0]) + ":" +
                     str(user[1][1]) + " timed out")
             else:
                 self.sendPacketToClient(
                     packet("7", "keepAlivePackage", ["True"], user))
Esempio n. 5
0
 def execute(self, args):
     if len(self._server.loggedInUsers) is 0:
         print(self._server.serverPrefix + "No clients to kick logged in")
     else:
         success = False
         for user in self._server.loggedInUsers:
             for key, value in user.items():
                 if value == args[0]:
                     for user in self._server.users:
                         if user[1][0] == key:
                             self._server.sendPacketToClient(
                                 packet("5", "kick", ["True"], user))
                     success = True
                     self._server.createLogsUtil.printandwriteserverlog(
                         self._server.serverPrefix + args[0] +
                         " got kicked from the channel")
         if not success:
             print(self._server.serverPrefix + args[0] +
                   " does not exists or is not online")
Esempio n. 6
0
 def execute(self, args):
     if len(self._server.users) is 0:
         print(self._server.serverPrefix +
               "No clients to ban connected to the server")
     else:
         success = False
         for user in self._server.users:
             if user[1][0] == args[0]:
                 self._server.mysqlHelperUtil.ban(args[0])
                 success = True
                 self._server.sendPacketToClient(
                     packet("4", "ban", ["True"], user))
                 user[0].shutdown(socket.SHUT_RDWR)
                 user[0].close()
                 self._server.createLogsUtil.printandwriteserverlog(
                     self._server.serverPrefix + args[0] +
                     " got banned from server")
         if not success:
             print(self._server.serverPrefix + args[0] +
                   " does not exists or is not online")
Esempio n. 7
0
 def listenForConnections(self):
     while True:
         self.connection.listen(1)
         client = self.connection.accept()
         banned = self.readFilesUtil.checkIfBanned(client[1][0])
         if banned:
             self.createLogsUtil.printandwriteserverlog(
                 self.serverPrefix + str(client[1][0]) + ":" +
                 str(client[1][1]) +
                 " got kicked from server because of ban(pls compose new log message this is schelchtes englisch)"
             )
             self.sendPacketToClient(packet("4", "banned", [""], client))
             client[0].shutdown(socket.SHUT_RDWR)
             client[0].close()
         else:
             self.clients.append(client)
             self.createLogsUtil.printandwriteserverlog(self.serverPrefix +
                                                        str(client[1][0]) +
                                                        ":" +
                                                        str(client[1][1]) +
                                                        " connected")
Esempio n. 8
0
    def listenForPackets(self):
        while True:
            for user in self.users:
                try:
                    packet_ = str(user[0].recv(8192), "utf-8")
                    print(packet_)
                    packet_ = json.loads(packet_)
                except:
                    self.users.remove(user)
                    for IpAndName in self.loggedInUsers:
                        if {} == IpAndName:
                            var = None
                        else:
                            IpAndName.pop(str(user[1][0]))
                    self.createLogsUtil.printandwriteserverlog(
                        self.serverPrefix + str(user[1][0]) + ":" +
                        str(user[1][1]) +
                        " lost connection to server unexpectedly")

                if "0" == packet_['packetid']:
                    containsIllegalCharachters = self.helperUtils.checkForIllegalCharachters(
                        packet_['data'][0] + packet_['data'][1])
                    if containsIllegalCharachters:
                        self.sendPacketToClient(
                            packet("0", "register", ["False"], user))
                    else:
                        success = self.mysqlHelperUtil.register(
                            packet_['data'][0], packet_['data'][1],
                            packet_['data'][2])
                        if success:
                            self.sendPacketToClient(
                                packet("0", "register", ["True"], user))
                            self.createLogsUtil.printandwriteserverlog(
                                self.serverPrefix + str(user[1][0]) + ":" +
                                str(user[1][1]) + " registered a new User: "******"0", "register", ["False"], user))
                elif "1" == packet_['packetid']:
                    containsIllegalCharachters = self.helperUtils.checkForIllegalCharachters(
                        packet_['data'][0] + packet_['data'][1])
                    if containsIllegalCharachters:
                        self.sendPacketToClient(
                            packet("1", "login", ["False"], user))
                        self.loggedIn = False
                    else:

                        #for loggedInUser in self.loggedInUsers:
                        #	if {} == loggedInUser:
                        #		self.loggedIn = False
                        #		break
                        #	for ip, loggedInUser1 in loggedInUser.items():
                        #		print("test1.5")
                        #		if loggedInUser1 == packet_['data'][0]:
                        #			print("test2")
                        #			self.sendPacketToClient(packet("1", "login", ["False"], user))
                        #			self.loggedIn = True
                        #		else:
                        #			self.loggedIn = False

                        #if self.loggedIn:
                        #	print("the user is allready logged in")
                        #else:
                        success = self.mysqlHelperUtil.checkLogin(
                            packet_['data'][0], packet_['data'][1])
                        if success:
                            self.mysqlHelperUtil.updateAddress(
                                packet_['data'][0], str(user[1][0]))
                            self.loggedInUsers.append(
                                {str(user[1][0]): packet_['data'][0]})
                            self.createLogsUtil.printandwriteserverlog(
                                self.serverPrefix + str(user[1][0]) + ":" +
                                str(user[1][1]) + " logged in as: " +
                                packet_['data'][0])
                            self.sendPacketToClient(
                                packet("1", "login", ["True"], user))
                        else:
                            self.sendPacketToClient(
                                packet("1", "login", ["False"], user))

                elif "2" == packet_['packetid']:
                    channels = self.mysqlHelperUtil.getChannels()
                    self.sendPacketToClient(
                        packet("2", "listChannels", [channels], user))
                    self.createLogsUtil.printandwriteserverlog(
                        self.serverPrefix + str(user[1][0]) + ":" +
                        str(user[1][1]) + " requested channels")

                elif "3" == packet_['packetid']:
                    channels = self.mysqlHelperUtil.getChannels()
                    if packet_['data'][0] in channels:
                        self.sendPacketToClient(
                            packet("3", "changeChannel", ["True"], user))
                        #send user join the channel to all other users in the channel
                        #assign the user the channel he joined
                        self.createLogsUtil.printandwriteserverlog(
                            self.serverPrefix + str(user[1][0]) + ":" +
                            str(user[1][1]) + " changed channel to " +
                            packet_['data'][0])
                    else:
                        self.sendPacketToClient(
                            packet("3", "changeChannel", ["False"], user))

                elif "4" == packet_['packetid']:
                    var = None
                elif "5" == packet_['packetid']:
                    var = None
                elif "6" == packet_['packetid']:
                    var = None
                elif "7" == packet_['packetid']:
                    if (time.time() -
                            int(packet_['data'][0])) > self.maxClientPing:
                        self.sendPacketToClient(
                            packet("7", "keepAlivePackage", ["False"], user))
                        self.users.remove(user)
                        for IpAndName in self.loggedInUsers:
                            IpAndName.pop(str(user[1][0]))
                        user[0].shutdown(socket.SHUT_RDWR)
                        user[0].close()
                        self.createLogsUtil.printandwriteserverlog(
                            self.serverPrefix + str(user[1][0]) + ":" +
                            str(user[1][1]) + " timed out due to high ping")
                    self.lastKeepAlivePackets[user[1]] = time.time()
                elif "10" == packet_['packetid']:
                    for userToSendTheMessage in self.users:
                        if userToSendTheMessage == user:
                            print(
                                "user who sent the message cant be the receiver"
                            )
                        else:
                            #check if users are in the same channel
                            self.sendPacketToClient(
                                packet("10", "chatMsg", [packet_['data']],
                                       userToSendTheMessage))
                else:
                    print("unknown packet received from client" +
                          str(user[1][0]) + ":" + str(user[1][1]))