From 6127ae01609030b226eacc3f1aa72ee0a817e99d Mon Sep 17 00:00:00 2001 From: Vincent Le Gallic Date: Fri, 22 Jun 2012 18:55:05 +0200 Subject: [PATCH] On peut filer l'IP du serveur en plus des keywords --- client.py | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/client.py b/client.py index 42668ec..50da240 100755 --- a/client.py +++ b/client.py @@ -19,10 +19,23 @@ from pprint import pprint sys.path.append(client_config.rsa_path) import monRSA -def connect(sock,where="cerebro",port=None): +def full_read(sock): + """Lit sur la socket jusqu'à ce que l'output soit déJSON-izable""" + output="" + while True: + output+=sock.read() + try: + return json.loads(output) + except: + pass + +def connect(sock,where,port=None): if port==None: port=client_config.port - ip={"ici":"localhost","vent":"vent.crans.org","eva":"eva.crans.org","cerebro":"cerebro.crans.org"}[where] + try: + ip={"ici":"localhost","vent":"vent.crans.org","eva":"eva.crans.org","cerebro":"cerebro.crans.org"}[where] + except: + ip=where sock.connect((ip,port)) return ssl.wrap_socket(sock,ca_certs=client_config.ca_certfile) # cert_reqs=ssl.CERT_NONE,ciphers="AES256-SHA") @@ -60,12 +73,11 @@ def login(socket,user,mdp="",typ="bdd"): def search(socket,term): a.write("search %s"%(term)) - return json.loads(a.read()) + return lit(a) def lit(socket): try: - t=a.read(4096) - obj=json.loads(t) + obj=full_read(socket) if obj["retcode"]==0: return obj['msg'] else: @@ -76,22 +88,6 @@ def lit(socket): except ValueError: return "Serveur mort ?" -def challenge(a): - _,pub=monRSA.litcles(None,client_config.server_rsa_pub_key) - chall=str(time.time()) - chall+="".join(["azertyuiopqsdfghjklmwxcvbn"[random.randrange(0,26)] for i in range(20)]) - coded=monRSA.crypte(chall,pub) - a.send('challenge "%s"'%(coded)) - a.setblocking(True) - result=a.read() - a.settimeout(0.5) - if json.loads(result)==chall: - print "Recognized" - elif json.loads(result)==u"Challenge tenté il y a moins de 10 minutes ! =p": - print "Top tôt. Réessaye plus tard." - else: - print "Imposteur !" - if __name__=="__main__": where=raw_input("quel serveur ?") #port=int(raw_input("port ? ")) -- 2.39.2