From: Vincent Le Gallic Date: Fri, 22 Jun 2012 16:55:05 +0000 (+0200) Subject: On peut filer l'IP du serveur en plus des keywords X-Git-Url: http://gitweb.pimeys.fr/?a=commitdiff_plain;h=6127ae01609030b226eacc3f1aa72ee0a817e99d;p=NK2015_Client_Python_Alpha.git On peut filer l'IP du serveur en plus des keywords --- 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 ? "))