#!/usr/bin/python3.4 # -*- coding: utf-8 -*- # Copyright © 2015 Matthieu DUVAL, Rudy DUCHE # This file is part of RizzleHash. # RizzleHash is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # RizzleHash is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with RizzleHash. If not, see import socketserver import socket import sys import time import signal import BF_Utils from BF_Constants import * from BF_GUI import * COEUR_CONNECTE = 0 IP_CONNECTE = list() CLIENT = list() NB_COEUR = 0 PASSWORD_FOUND = "" FOUND = False class MultyStationServerHandler(socketserver.BaseRequestHandler): """ The RequestHandler class for our server. It is instantiated once per connection to the server, and must override the handle() method to implement communication to the client. """ def __init__(self, request, client_address, server): self.request = request self.client_address = client_address self.server = server self.setup() self.coreClient = 0 self.client = list() self.GUI = GUI() self.messageType = LAN_CONSTANTS.MESSAGE_VALUE.TYPE_DEFAULT.value global COEUR_CONNECTE global CLIENT try: self.handle() COEUR_CONNECTE = COEUR_CONNECTE + self.coreClient CLIENT = CLIENT + self.client finally: self.finish() def handle(self): # self.request is the TCP socket connected to the client self.data = self.request.recv(1024).strip() #self.GUI.displayMessage("".join(self.client_address[0]),self.data.decode('utf-8')) errorCode, errorMessage = self.traitementResponse(self.data.decode('utf-8'),self.client_address[0]) if self.messageType == LAN_CONSTANTS.MESSAGE_VALUE.TYPE_CONFIG.value: self.sendResponse(errorCode, errorMessage) elif self.messageType == LAN_CONSTANTS.MESSAGE_VALUE.TYPE_INFO.value: self.managmentInfo(self.data.decode('utf-8'),"".join(self.client_address[0])) elif self.messageType == LAN_CONSTANTS.MESSAGE_VALUE.TYPE_RESULT.value: if self.messageState == LAN_CONSTANTS.MESSAGE_VALUE.STATE_OK.value: #print(self.data.decode('utf-8')) global FOUND FOUND = True #self.finish() return def setup(self): pass def finish(self): pass def managmentInfo(self,data,clientAdress): argvArray = data.split(LAN_CONSTANTS.MESSAGE_SPECIAL_CHAR.SEPARATOR.value) processName = list() WordsSeconds = list() curentWord = list() processTime = list() nbWord = list() for object in argvArray: key,value = BF_Utils.getValueWithKey(object) if LAN_CONSTANTS.MESSAGE_SPECIAL_CHAR.SAME_VALUE_SEPRATOR.value in value: contentList = value.split(LAN_CONSTANTS.MESSAGE_SPECIAL_CHAR.SAME_VALUE_SEPRATOR.value) if key == LAN_CONSTANTS.MESSAGE_KEY.LOCAL_THREAD_NAME.value : for content in contentList: processName.append(content) elif key == LAN_CONSTANTS.MESSAGE_KEY.WORD.value: for content in contentList: curentWord.append(content) elif key == LAN_CONSTANTS.MESSAGE_KEY.PROCESS_TIME.value: for content in contentList: processTime.append(float(content)) elif key == LAN_CONSTANTS.MESSAGE_KEY.NB_WORDS.value: for content in contentList: nbWord.append(int(content)) for i in range(0,len(nbWord)): WordsSeconds.append(round(nbWord[i]/processTime[i])) self.GUI.displayProcessPerfsList( clientAdress, processName, WordsSeconds, curentWord) """else: if key == LAN_CONSTANTS.MESSAGE_KEY.LOCAL_THREAD_NAME.value : processName = value elif key == LAN_CONSTANTS.MESSAGE_KEY.WORD.value: curentWord = value elif key == LAN_CONSTANTS.MESSAGE_KEY.PROCESS_TIME.value: processTime = float(value) elif key == LAN_CONSTANTS.MESSAGE_KEY.NB_WORDS.value: nbWord = int(value) WordsSeconds = round(nbWord/processTime) self.GUI.displayProcessPerfs( clientAdress, processName, WordsSeconds, curentWord)""" def sendResponse(self, errorCode, errorMessage): message = list() for i in range(0,len(errorCode)): if not i == (len(errorCode) -1): message.append(errorCode[i]+':'+errorMessage[i]+',') else: message.append(errorCode[i]+':'+errorMessage[i]) self.request.sendall(bytes("".join(message),'utf-8')) def traitementResponse(self,param,clientAdress): errorMessage = list() errorCode = list() argvArray = param.split(LAN_CONSTANTS.MESSAGE_SPECIAL_CHAR.SEPARATOR.value) for object in argvArray: key,value = BF_Utils.getValueWithKey(object) if key == LAN_CONSTANTS.MESSAGE_KEY.NB_CORE_CLIENT.value : code, message = self.coreProcessing(value,clientAdress) errorCode = errorCode + code errorMessage = errorMessage + message elif key == LAN_CONSTANTS.MESSAGE_KEY.MESSAGE_TYPE.value: self.messageType = value elif key == LAN_CONSTANTS.MESSAGE_KEY.RETURN_STATE.value: self.messageState = value elif key == LAN_CONSTANTS.MESSAGE_KEY.WORD.value: global PASSWORD_FOUND PASSWORD_FOUND = value else: errorCode.append(ANSWER_STATES.MESSAGE_STATE.STATE_KO.value) errorMessage.append('protocol key not recognize') return errorCode,errorMessage def coreProcessing(self,value,clientAdress): global IP_CONNECTE errorMessage = list() errorCode = list() if BF_Utils.is_int(value): if clientAdress not in IP_CONNECTE: coeurClient = int(value) if coeurClient > 0: if coeurClient <= NB_COEUR: self.coreClient = coeurClient IP_CONNECTE.append(clientAdress) self.client.append((clientAdress,coeurClient)) errorMessage.append("core added") errorCode.append(ANSWER_STATES.MESSAGE_STATE.STATE_OK.value) else: errorCode.append(ANSWER_STATES.MESSAGE_STATE.STATE_KO.value) errorMessage.append("Please insert " + str(NB_COEUR - self.coreClient)+ " core max") else: errorCode.append(ANSWER_STATES.MESSAGE_STATE.STATE_KO.value) errorMessage.append("Please insert at least 1 core") else: errorCode.append(ANSWER_STATES.MESSAGE_STATE.STATE_KO.value) errorMessage.append("client already conected") else: errorCode.append(ANSWER_STATES.MESSAGE_STATE.STATE_KO.value) errorMessage.append("Value is not integer") return errorCode,errorMessage class MultyStationServer(): def __init__(self,host,port,nbCoeur,password,key,passwordLength,hashMethod): global NB_COEUR self.nbCoeur=nbCoeur NB_COEUR = nbCoeur self.serverTime = time.time() self.host = host self.port = port self.coreConnected = 0 self.password = password self.passwordLength = passwordLength self.hashMethod=hashMethod self.key = key self.GUI = GUI() signal.signal(signal.SIGINT, self.signal_handler) return def signal_handler(self, signal, frame): self.serverHandler.shutdown() sys.exit(0) def connect(self): self.serverHandler = socketserver.TCPServer((self.host, self.port ), MultyStationServerHandler) return def run(self): global IP_CONNECTE global FOUND if NB_COEUR < 1: self.GUI.displayError("127.0.0.1","INIT","too low processor core") sys.exit() while COEUR_CONNECTE < NB_COEUR: self.serverHandler.handle_request() self.GUI.displayMessage(IP_CONNECTE[len(IP_CONNECTE) -1],str(COEUR_CONNECTE) +"/" +str(NB_COEUR)) time.sleep(0.1) indexCore = 0 for pc in CLIENT: #print("ip : " + pc[0] + "\t Nombre de core offert : " + str(pc[1])) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((pc[0],self.port+1)) msg = BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.MESSAGE_TYPE.value,LAN_CONSTANTS.MESSAGE_VALUE.TYPE_CONFIG.value,True) msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.NB_CORE_TOTAL.value, str(self.nbCoeur),True) msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.INDEX_CORE_MIN.value, str(indexCore),True) msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.CRYPTED_PASSWORD.value, str(self.password),True) msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.ENCRYPTION_KEY.value, str(self.key),True) msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.HASH_METHOD.value, str(self.hashMethod),True) msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.PASSWORD_MAX_LENGTH.value, str(self.passwordLength),False) #self.GUI.displayMessage("127.0.0.1","Message envoyé : "+msg) sock.send(bytes(msg,"UTF-8")) indexCore = indexCore +pc[1] sock.recv(min(2048, 2048)) sock.close() self.serverTime = time.time() self.GUI.displayMessage("SERVER","Start ! ") global FOUND while not FOUND: self.serverHandler.handle_request() self.GUI.displayMessage("SERVER","Found ! ") self.GUI.displayResult(PASSWORD_FOUND,self.serverTime ) for pc in CLIENT: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((pc[0],self.port+1)) msg = BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.MESSAGE_TYPE.value,LAN_CONSTANTS.MESSAGE_VALUE.TYPE_RESULT.value,True) msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.RETURN_STATE.value,LAN_CONSTANTS.MESSAGE_VALUE.STATE_OK.value,True) msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.WORD.value,PASSWORD_FOUND,False) sock.send(bytes(msg,"utf-8")) sock.recv(min(2048, 2048)) sock.close() for pc in range(0,len(CLIENT)-1): self.serverHandler.handle_request() if __name__ == "__main__": HOST = '' PORT = 2323 nbCoeur = 8 server = MultyStationServer(HOST,PORT,nbCoeur,"azert","",6) server.connect() server.run() sys.exit()