You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rizzlehash/BF_MultyStationClient.py

141 lines
5.3 KiB

#!/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 <http://www.gnu.org/licenses/>
import socket
import sys
import time
import BF_Utils
from BF_Constants import *
class MultyStationClient:
def __init__(self, serverAdress, serveurPort):
super().__init__()
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serverAdress = serverAdress
self.serveurPort = serveurPort
def connect(self):
self.sock.connect((self.serverAdress, self.serveurPort))
def listen(self):
newSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
newSocket.bind(('',self.serveurPort + 1))
newSocket.listen(1)
chunks = []
bytes_recd = 0
#newSocket.connect(('', self.serveurPort + 1))
while bytes_recd < 5:
conn, addr = newSocket.accept()
#print("test addr: "+ str(addr))
chunk = conn.recv(min(2048 - bytes_recd, 2048))
#if chunk == b'':
# raise RuntimeError("socket connection broken")
chunks.append(chunk)
bytes_recd = bytes_recd + len(chunk)
data = b''.join(chunks).decode("UTF-8")
conn.sendall(bytes(data,"UTF-8"))
if data:
break
return data
def initParamProcessing(self, param):
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_TOTAL.value:
nbCoreTotal = int(value)
elif key == LAN_CONSTANTS.MESSAGE_KEY.INDEX_CORE_MIN.value:
indexCoreMin = int(value)
elif key == LAN_CONSTANTS.MESSAGE_KEY.CRYPTED_PASSWORD.value:
cryptedPassword = value
elif key == LAN_CONSTANTS.MESSAGE_KEY.ENCRYPTION_KEY.value:
encryptionKey = value
elif key == LAN_CONSTANTS.MESSAGE_KEY.PASSWORD_MAX_LENGTH.value:
passwordMaxLength = int(value)
elif key == LAN_CONSTANTS.MESSAGE_KEY.HASH_METHOD.value:
hashMethod = value
return nbCoreTotal, indexCoreMin, cryptedPassword, encryptionKey, passwordMaxLength,hashMethod
def send(self, msg):
totalsent = 0
while totalsent < 5:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError("socket connection broken")
totalsent = totalsent + sent
sent = sent +1
def configToServer(self,nbProcess):
self.connect()
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_CLIENT.value,nbProcess,False)
self.send(bytes(msg,"utf-8"))
def infoToServer(self,nbWord,processTime,word,threadName):
self.connect()
msg = BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.MESSAGE_TYPE.value,LAN_CONSTANTS.MESSAGE_VALUE.TYPE_INFO.value,True)
msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.NB_WORDS.value,nbWord,True)
msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.PROCESS_TIME.value,processTime,True)
msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.WORD.value,word,True)
msg = msg + BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.LOCAL_THREAD_NAME.value,threadName,False)
self.send(bytes(msg,"utf-8"))
def infoToServerList(self,nbWord,processTime,word,threadName):
self.connect()
msg = BF_Utils.createMessage(LAN_CONSTANTS.MESSAGE_KEY.MESSAGE_TYPE.value,LAN_CONSTANTS.MESSAGE_VALUE.TYPE_INFO.value,True)
msg = msg + BF_Utils.createMessageList(LAN_CONSTANTS.MESSAGE_KEY.NB_WORDS.value,nbWord,True)
msg = msg + BF_Utils.createMessageList(LAN_CONSTANTS.MESSAGE_KEY.PROCESS_TIME.value,processTime,True)
msg = msg + BF_Utils.createMessageList(LAN_CONSTANTS.MESSAGE_KEY.WORD.value,word,True)
msg = msg + BF_Utils.createMessageList(LAN_CONSTANTS.MESSAGE_KEY.LOCAL_THREAD_NAME.value,threadName,False)
self.send(bytes(msg,"utf-8"))
#time.sleep(0.1)
#self.receive()
def resultToServer(self,word):
self.connect()
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,word,False)
self.send(bytes(msg,"utf-8"))
def receive(self):
chunks = []
bytes_recd = 0
while bytes_recd < 5:
chunk = self.sock.recv(min(2048 - bytes_recd, 2048))
#if chunk == b'':
# raise RuntimeError("socket connection broken")
chunks.append(chunk)
bytes_recd = bytes_recd + len(chunk)
return b''.join(chunks).decode("UTF-8")
if __name__ == '__main__' :
client = MultyStationClient('127.0.0.1',2323)
client.connect()
client.send(bytes('nbCore:4','UTF-8'))
print(client.receive())
sys.exit()