def handleRequest(self, request, clientSocket): logHelper = LoggingHelper() requestId = request[:3] requestdata = request[3:] if requestId == "001": #chatting for client in self.clients.clientList: if client != dict({self.client: self.username}): for key in client.keys(): key.sendall(self.StringToBytes("001" + requestdata)) elif requestId == "011": #get client informations for client in self.clients.clientList: for key in client.keys(): if client == dict({self.client: "None"}): if key.getpeername()[1] == self.client_address[1]: self.username = requestdata client[self.client] = requestdata else: key.sendall( self.StringToBytes( "411[Client/Error] You are already connected to this server" )) elif requestId == "021": #request name change data = requestdata.split(":") self.clients.updateClientUsername(clientSocket, data[0], data[1]) clientSocket.sendall(self.StringToBytes("201True:" + data[1])) self.username = data[1] else: self.client.sendall( self.StringToBytes("401[Client/Error] Unknown request ID")) logHelper.printAndWriteServerLog("[Server/Error] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " sent unknown request ID") self.handle()
def handleRequest(self, request): logHelper = LoggingHelper() requestId = request[:3] requestdata = request[3:] if requestId == "001": for client in self.clients.clientList: if client != dict({self.client: self.username}): for key in client.keys(): key.sendall(self.StringToBytes("001" + requestdata)) elif requestId == "011": for client in self.clients.clientList: for key in client.keys(): if client == dict({self.client: "None"}): if key.getpeername()[1] == self.client_address[1]: self.username = requestdata client[self.client] = requestdata else: key.sendall( self.StringToBytes( "411[Client/Error] You are already connected to this server" )) else: self.client.sendall( self.StringToBytes("401[Client/Error] Unknown request ID")) logHelper.printAndWriteServerLog("[Server/Error] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " sent unknown request ID") self.handle()
def __init__(self): self.inptHandler = InputHandler() self.logHelper = LoggingHelper() self.ip = str(socket.gethostbyname(socket.gethostname())) self.server = ServerThread((self.ip, self.port), RequestHandler) serverThread = threading.Thread(target=self.server.serve_forever) serverThread.daemon = True serverThread.start() self.logHelper.printAndWriteServerLog("[Server/Info] Started on ip: " + str(self.ip) + " with port: " + str(self.port) + " in " + serverThread.name) self.askForInput()
def handle(self): logHelper = LoggingHelper() fileHelper = FileHelper() self.client = self.request self.clients = Clients() if self.appendClient: logHelper.printAndWriteServerLog("[Server/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " connected to the server") for clientInList in fileHelper.readTXTFile("data/", "banList"): clientInListString = clientInList.split(":") try: banTime = clientInListString[1] except IndexError: var = None #pylint: disable=W0612 if self.client_address[0] + "\n" in clientInList: logHelper.printAndWriteServerLog( "[Server/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " is permanantly banned on the server") self.client.sendall( self.StringToBytes( "405[Client/Info] You are permanantly banned on this server" )) self.client.close() self.appendClient = False elif self.client_address[ 0] + ":" + banTime in clientInList: #FIX_ME_DEPLETED:decrease banntime overtime logHelper.printAndWriteServerLog( "[Server/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " is temporary banned on the server. Remaining Time: " + clientInListString[1] + "Minutes") self.client.sendall( self.StringToBytes( "405[Client/Info] You are temporary banned on this server. Remaining Time: " + str(int(clientInListString[1])) + "Minutes")) self.client.close() self.appendClient = False else: self.clients.addClient(self.client, "None") self.appendClient = False try: self.data = self.BytesToString(self.client.recv(1024).strip()) if len(self.data[6:]) == 0: logHelper.printAndWriteServerLog("[Server/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " sent client informations") self.handleRequest(self.data) else: logHelper.printAndWriteChannelLog("[Server/Channel/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " " + self.data[3:]) self.handleRequest(self.data) except: logHelper.printAndWriteServerLog("[Server/Error] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " closed connection unexpectedly") self.clients.removeClient(self.client, self.username)
def handle(self): logHelper = LoggingHelper() fileHelper = FileHelper() self.client = self.request self.clients = Clients() if self.appendClient: logHelper.printAndWriteServerLog("[Server/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " connected to the server") for clientInList in fileHelper.readTXTFile("data/", "banList"): clientInListString = clientInList.split(":") try: banTime = clientInListString[1] except IndexError: var = None if self.client_address[0] + "\n" in clientInList: logHelper.printAndWriteServerLog( "[Server/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " is permanantly banned on the server") self.client.sendall( self.StringToBytes( "405[Client/Info] You are permanantly banned on this server" )) self.client.close() self.appendClient = False elif self.client_address[0] + ":" + banTime in clientInList: currTime = datetime.datetime.now().timestamp() banTime = banTime[:-1] if (currTime > float(banTime)): print("1") else: print("2") logHelper.printAndWriteServerLog( "[Server/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " is temporary banned on the server. Remaining Time: " + str(int((float(banTime) - currTime) / 60)) + "Minutes") self.client.sendall( self.StringToBytes( "405[Client/Info] You are temporary banned on this server. Remaining Time: " + str(int((float(banTime) - currTime) / 60)) + "Minutes")) self.client.close() self.appendClient = False else: self.clients.addClient(self.client, "None") self.appendClient = False try: self.data = self.BytesToString(self.client.recv(1024).strip()) if len(self.data[6:]) == 0: logHelper.printAndWriteServerLog("[Server/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " sent client informations") self.handleRequest(self.data, self.client) else: logHelper.printAndWriteChannelLog("[Server/Channel/Info] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " " + self.data[3:]) self.handleRequest(self.data, self.client) except: logHelper.printAndWriteServerLog("[Server/Error] " + str(self.client_address[0]) + ":" + str(self.client_address[1]) + " closed connection unexpectedly") self.clients.removeClient(self.client, self.username)
class Server(): inptHandler = None logHelper = None server = None ip = "" #config t = FileHelper().getConfig()[2] port = int(t[5:]) #helper def BytesToString(self, bytes): return str(bytes, "utf-8") def StringToBytes(self, string): return bytes(string, "utf-8") def __init__(self): self.inptHandler = InputHandler() self.logHelper = LoggingHelper() self.ip = str(socket.gethostbyname(socket.gethostname())) self.server = ServerThread((self.ip, self.port), RequestHandler) serverThread = threading.Thread(target=self.server.serve_forever) serverThread.daemon = True serverThread.start() self.logHelper.printAndWriteServerLog("[Server/Info] Started on ip: " + str(self.ip) + " with port: " + str(self.port) + " in " + serverThread.name) self.askForInput() def askForInput(self): while True: try: message = input() except KeyboardInterrupt: self.logHelper.printAndWriteServerLog( "[Server/Info] Gracefully stopping server...") from utils.RequestHandler import Clients #pylint: disable=E0611 if len(Clients().clientList) < 1: self.logHelper.printAndWriteServerLog( "[Server/Info] Gracefully stopped server") break else: for client in Clients().clientList: for key in client.keys(): key.sendall( self.StringToBytes( "403" + "[Client/Info] Server shut down")) self.logHelper.printAndWriteServerLog( "[Server/Info] Gracefully stopped server") break if str(message).startswith("/"): self.inptHandler.handleInput(str(message[1:])) else: self.logHelper.printAndWriteServerLog( "[Server/Error] Unknown command: (" + str(message) + ")") self.logHelper.printAndWriteServerLog( "[Server/Error] type /help for a list of commands")
class InputHandler: CommandList = dict() fileHelper = None logHelper = None clients = None #helper def BytesToString(self, bytes): return str(bytes, "utf-8") def StringToBytes(self, string): return bytes(string, "utf-8") def __init__(self): self.fileHelper = FileHelper() self.logHelper = LoggingHelper() self.clients = Clients() def handleInput(self, command): command = command.split() if command[0] == "clear": os.system('cls' if os.name == 'nt' else 'clear') elif command[0] == "listClients": if len(self.clients.clientList) < 1: self.logHelper.printAndWriteServerLog( "[Server/Error] No clients connected") else: self.logHelper.printAndWriteServerLog( "[Server/Info] Connected clients:") for client in self.clients.clientList: for key in client.keys(): self.logHelper.printAndWriteServerLog( "[Server/Info] " + str(key.getpeername()) + " : " + str(client[key])) elif command[0] == "kick": try: user = command[1] if len(self.clients.clientList) < 1: self.logHelper.printAndWriteServerLog( "[Server/Error] No clients connected") else: for client in self.clients.clientList: for key in client.keys(): if key.getpeername()[0] == user: self.logHelper.printAndWriteServerLog( "[Server/Info] " + user + " : " + client[key] + " got kicked") key.sendall( self.StringToBytes( "402" + "[Client/Info] You got kicked by the console" )) key.close() else: self.logHelper.printAndWriteServerLog( "[Server/Error] No client with ip: " + user) except IndexError: self.logHelper.printAndWriteServerLog( "[Server/Info] /kick <client>") elif command[0] == "ban": try: time = 0 user = command[1] permanantly = True try: time = command[2] except: permanantly = True if int(time) > 0: permanantly = False if len(self.clients.clientList) < 1: self.logHelper.printAndWriteServerLog( "[Server/Error] No clients connected") else: for client in self.clients.clientList: for key in client.keys(): if key.getpeername()[0] == user: if permanantly: self.fileHelper.addClientToBanList(user) self.logHelper.printAndWriteServerLog( "[Server/Info] " + user + " : " + client[key] + " got permanantly banned") key.sendall( self.StringToBytes( "405" + "[Client/Info] You got permanantly banned by the console" )) key.close() else: self.fileHelper.addClientToBanList(user + ":" + time) self.logHelper.printAndWriteServerLog( "[Server/Info] " + user + " : " + client[key] + " got banned for " + str(time) + "minutes") key.sendall( self.StringToBytes( "405" + "[Client/Info] You got banned for " + str(time) + "Minutes by the console")) key.close() else: self.logHelper.printAndWriteServerLog( "[Server/Error] No client with ip: " + user) except IndexError: self.logHelper.printAndWriteServerLog( "[Server/Info] /ban <client> <time>") else: self.logHelper.printAndWriteServerLog( "[Server/Error] Unknown command: (" + str(command) + ")") self.logHelper.printAndWriteServerLog( "[Server/Error] type /help for a list of commands")
def __init__(self): self.fileHelper = FileHelper() self.logHelper = LoggingHelper() self.clients = Clients()