def chercherFichier(fileName): # Fonction qui regarde dans sa BDD si il y a le file en question retour = 0 retour = chercherInfo("Fichiers", fileName) if retour != 0 and str(retour) != "None": # Le noeud héberge le file demandé # Donc on retourne son IP return autresFonctions.connaitreIP() + ":" + str( config.readConfFile("defaultPort")) retour = chercherInfo("FichiersExt", fileName) # ATTENTION ! Si le file n'est pas connu, 0 est retourné return retour
def mini(): # Affiche une mini doc (help) print("Web Transfer Protocol V" + str(config.readConfFile("Version"))) print("Here are the main functions:") print("update Check for updates") print("stats Shows your peer statistics") print("config Edit the configuration") print("exit Stop WTP") print("dns Edit the VPN configuration") print("doc How to use wtp") print("checkFiles Check the ADDFILES and HOSTEDFILES") print("delAll Delete all : Config, DB, Files.") print("majDNS Update the DNS database") print("client Use WTP in console") print("blacklist Edit the BlackList configuration") print("For more informations, enter doc")
def portLibre(premierPort): # Fonction qui cherche les ports de la machine qui sont libres # Et renvoie le premier trouvé, en partant du port initial, donné. port = True while port: clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: clientsocket.connect(('127.0.0.1', int(premierPort))) except ConnectionRefusedError: # Le port est occupé ! clientsocket.close() premierPort += 1 else: # Le port est libre ! clientsocket.close() premierPort += 1 if premierPort < int(config.readConfFile("MaxPort")): return premierPort logs.addLogs("ERROR: All ports already in use")
def run(self): while self.allume: requete = self.getMessage() print("Youpi : " + requete) if requete[:19] == "=cmd rechercher nom": # =cmd rechercher nom SHA256.ext sortie = search.rechercheFichierEntiere(requete[20:]) ipport = sortie[:sortie.find(";")] sha = sortie[sortie.find(";") + 1:] if re.findall(r'[0-9]+(?:\.[0-9]+){3}:[0-9]+', ipport): # C'est un IPPort # On envoi vers le thread qui télécharge le file error += fctsClient.CmdDemandeFichier( ipport[:ipport.find(":")], int(ipport[ipport.find(":") + 1:]), sha) if error == 0: resultat = "=cmd SUCCESS : " + str( config.readConfFile( "Path")) + "/HOSTEDFILES/" + sha elif error == 5: resultat = "=cmd ERROR NO FILE" else: resultat = "=cmd ERROR" else: # C'est une erreur print("AHH") print(sortie) print(ipport) resultat = sortie elif requete[:15] == "=cmd VPN noeud " and requete.find( " request =cmd DemandeFichier") != -1: # =cmd VPN noeud 127.0.0.1:5555 request =cmd DemandeFichier ipport = requete[requete.find("=cmd VPN noeud ") + 15:requete.find(" request ")] resultat = requete[requete.find(" request ") + 10:] else: resultat = "=cmd CommandeInconnue :" + requete + ":" print(resultat) logs.addLogs(resultat) self.sendMessage(self.encodeMessage(resultat))
def run(self): status = loader("The DNS service start") status.start() host = '127.0.0.1' port = int(config.readConfFile("VPNPort")) logs.addLogs( "INFO : The VPN service has started, he is now listening to the port " + str(port)) try: try: tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #tcpsock = ssl.wrap_socket(tcpsock, ssl_version=ssl.PROTOCOL_TLSv1, ciphers="ADH-AES256-SHA") tcpsock.bind((host, port)) tcpsock.settimeout(5) except OSError as e: logs.addLogs("ERROR : In vpn.py : " + str(e)) logs.addLogs("FATAL : Shutting down...") print( c("red") + "An error occured. Please restart WTP." + c("")) else: logs.addLogs( "INFO : The VPN service has started, he is now listening to the port " + str(port)) status.stop() status.join() while self.serveur_lance: try: tcpsock.listen(10) (clientsocket, (ip, port)) = tcpsock.accept() except socket.timeout: pass else: newthread = ClientThread(ip, port, clientsocket) newthread.start() except KeyboardInterrupt: print(" pressed: Shutting down ...") print("") logs.addLogs("INFO : WTP service has been stoped successfully.")
def maj(ipport=""): # Fonction pour noeud DNS qui permet de mettre à jour toute sa base avec un autre noeud BDD.verifExistBDD() error = 0 if ipport == "": ipport = str(config.readConfFile("Blacklist")) else: if ipport != "": connexion_avec_serveur = autresFonctions.connectionClient(ipport) if str(connexion_avec_serveur) != "=cmd ERROR": sendCmd = b"" sendCmd = "=cmd BlackList sync" connexion_avec_serveur.send(sendCmd.encode()) rcvCmd = connexion_avec_serveur.recv(1024) rcvCmd = rcvCmd.decode() connexion_avec_serveur.close() # Le noeud renvoie le nom du fichier à télécharger # Et son IP:PortPrincipal, sinon =cmd ERROR if rcvCmd != "=cmd ERROR": # =cmd Filename ****** ipport ****** filename = rcvCmd[14:rcvCmd.find(" ipport ")] ipport = rcvCmd[rcvCmd.find(" ipport ") + 8:] ip = ipport[:ipport.find(":")] port = ipport[ipport.find(":") + 1:] error += fctsClient.CmdDemandeFichier(ip, port, filename) if error == 0: echangeListes.filetoTable(filename, "BlackList") else: error += 1 else: error += 1 else: error += 1 else: error += 1 logs.addLogs("ERROR : No peer ip was found in Blacklist.maj()") return error
def lsteFichiers(filesExternes=0): # ATTENTION !! IL FAUT CONNAITRE SON IP EXTERNE POUR POUVOIR L'AJOUTER EN FIN DE LIGNE # CAR CHAQUE LIGNE EST DE TYPE SHA256file.ext @ IP:Port # Fonction qui retourne un file qui contient tous les # noms des fichers hébergés par le noeud, # et qui sont dans la BDD, un par ligne # Si filesExternes = 1, on doit faire la liste des focihers externes connus et non des files hébergés BDD.verifExistBDD() try: os.makedirs("HOSTEDFILES") except OSError: if not os.path.isdir("HOSTEDFILES"): raise conn = sqlite3.connect('WTP.db') cursor = conn.cursor() if filesExternes == 0: cursor.execute("""SELECT Nom FROM Fichiers WHERE 1""") else: cursor.execute("""SELECT Nom FROM FichiersExt WHERE 1""") rows = cursor.fetchall() fileDir = "HOSTEDFILES/TEMP" + str(time.time()) # On vide le file au cas où vidage = open(fileDir, "wb") vidage.write("") vidage.close() # On cherche notre IP monIP = connaitreIP() # Puis on commence l'importation fluxEcriture = open(fileDir, "ab") for row in rows: if filesExternes == 0: fluxEcriture.write(row[0] + " @ " + monIP + ":" + str(config.readConfFile("defaultPort")) + "\n") else: fluxEcriture.write(row[0] + " @ " + row[1] + "\n") fluxEcriture.close() return fileName
def searchBlackList(name, Isrank=False): rank = 0 rank = search.chercherInfo("Blacklist", name) if rank == 0: ipport = str(config.readConfFile("Blacklist")) connexion_avec_serveur = autresFonctions.connectionClient(ipport) if str(connexion_avec_serveur) != "=cmd ERROR": sendCmd = "=cmd BlackList name " + str(name) connexion_avec_serveur.send(sendCmd.encode()) rcvCmd = connexion_avec_serveur.recv(1024) rcvCmd = rcvCmd.decode() connexion_avec_serveur.close() rank = rcvCmd # Le noeud renvoie le nom du fichier à télécharger # Et son IP:PortPrincipal, sinon =cmd ERROR else: logs.addLogs("ERROR : Can not check the " + str(name) + " item in the blacklist") return 0 if Isrank: return rank if rank == 0: return 0 return 1
def CmdDemandeFichier(ip, port, file): if blacklist.searchBlackList(file) != 0: logs.addLogs("ERROR : This file is part of the Blacklist : " + str(file)) return 1 error = 0 connNoeud = autresFonctions.connectionClient(ip, port) if str(connNoeud) == "=cmd ERROR": error += 1 else: logs.addLogs( "INFO : Connection with peer etablished on port {}".format(port)) # Il faut trouver un port libre pour que le noeud donneur # puisse se connecter à ce noeud sur le bon port freePort = autresFonctions.portLibre( int(config.readConfFile("miniPort"))) temp = str(time.time()) thrdDown = echangeFichiers.downFile(file, int(freePort), temp) thrdDown.start() newIPPort = str(autresFonctions.connaitreIP()) + ":" + str(freePort) sendCmd = "=cmd DemandeFichier nom " + str(file) + " ipport " + str( newIPPort) connNoeud.send(sendCmd.encode()) connNoeud.close() thrdDown.join() # Lire le fichier temporaire pour savoir si il y a eut des erreurs try: f = open(".TEMP/" + temp, "r") error += int(f.read()) f.close() os.remove(".TEMP/" + temp) except Exception as e: error += 1 logs.addLogs("ERROR : An error occured in CmdDemandeFichier : " + str(e)) return error
def run(self): rcvCmd = self.clientsocket.recv(1024) rcvCmd = rcvCmd.decode() # Maintenant, vérifions la demande du client. if rcvCmd != '': if rcvCmd[:19] == "=cmd DemandeFichier": # =cmd DemandeFichier nom sha256.ext ipport IP:PORT # Dirriger vers la fonction UploadFichier() # Le noeud distant demande le file, donc on lui envoi, Up ! # On va chercher les deux informations qu'il nous faut : # Le nom du file (sous forme sha256.ext) # L'IP et le port du noeud qui a fait la demande (sous forme IP:PORT) fileName = rcvCmd[rcvCmd.find(" nom ") + 5:rcvCmd.find(" ipport ")] IppeerPort = rcvCmd[rcvCmd.find(" ipport ") + 8:] if BDD.verifFichier(fileName): error = 0 # Le file est présent dans la BDD, on peut l'envoyer temp = str(time.time()) thrdUp = echangeFichiers.upFile(IppeerPort, fileName, temp) thrdUp.start() thrdUp.join() # Lire le fichier temporaire pour savoir si il y a eut des erreurs try: f = open(".TEMP/" + temp, "r") error += int(f.read()) f.close() os.remove(".TEMP/" + temp) except Exception as e: error += 1 logs.addLogs( c("red") + "ERROR : An error occured in CmdDemandeFichier : " + str(e) + c("")) os.remove(".TEMP/" + temp) if error != 0: stats.modifStats("NbEnvsFichiers") else: logs.addLogs( c("red") + "ERROR : An error occured for upload " + str(fileName) + " in thrdLnch.py (" + str(error) + ")" + c("")) else: print( c("red") + "This file isn't on this computer : '" + str(fileName) + "'" + c("")) elif rcvCmd[:17] == "=cmd DemandeNoeud": # Fonction Serveur # Dirriger vers la fonction EnvoiNoeuds() # Le noeud distant a demandé les noeuds, on lui envoi ! # On trouve le premier port qui peut être attribué IppeerPort = "127.0.0.1:" + str( autresFonctions.portLibre( int(config.readConfFile("miniPort")))) sendCmd = IppeerPort # On envoie au demandeur l'adresse à contacter self.clientsocket.send(sendCmd.encode()) echangeNoeuds.EnvoiNoeuds(IppeerPort) stats.modifStats("NbEnvsLstNoeuds") elif rcvCmd[:25] == "=cmd DemandeListeFichiers": # On va récuperer le nom du file qui contient la liste # Ensuite, on la transmet au noeud distant pour qu'il puisse # faire la demande de réception du file pour qu'il puisse l'analyser if rcvCmd[:28] == "=cmd DemandeListeFichiersExt": file = autresFonctions.lsteFichiers(1) stats.modifStats("NbEnvsLstFichiers") else: file = autresFonctions.lsteFichiers() stats.modifStats("NbEnvsLstFichiersExt") self.clientsocket.send(file.encode()) elif rcvCmd[:23] == "=cmd DemandeListeNoeuds": # On va récuperer le nom du file qui contient la liste # Ensuite, on la transmet au noeud distant pour qu'il puisse # faire la demande de réception du file pour qu'il puisse l'analyser file = autresFonctions.lsteNoeuds() self.clientsocket.send(file.encode()) stats.modifStats("NbEnvsLstNoeuds") elif rcvCmd[:20] == "=cmd DemandePresence": # C'est tout bête, pas besoin de fonction # Il suffit de renvoyer la request informant que l'on est connecté au réseau. sendCmd = "=cmd Present" self.clientsocket.send(sendCmd.encode()) stats.modifStats("NbPresence") elif rcvCmd[:15] == "=cmd rechercher": # =cmd rechercher nom SHA256.ext # Renvoie le retour de la fonction, qui elle même retourne une IP+Port ou 0 # Chercher le nom du file donnee = rcvCmd[20:] sendCmd = str(search.rechercheFichierEntiere(donnee)) self.clientsocket.send(sendCmd.encode()) elif rcvCmd[:11] == "=cmd status": # On demande le statut du noeud (Simple, Parser, DNS, VPN, Main) if (config.readConfFile("Parser") == "Oui"): sendCmd = "=cmd Parser" self.clientsocket.send(sendCmd.encode()) else: sendCmd = "=cmd Simple" self.clientsocket.send(sendCmd.encode()) elif rcvCmd[:25] == "=cmd newFileNetwork name ": # =cmd newFileNetwork name ****** ip ****** # Un nouveau file est envoyé sur le réseau fileName = rcvCmd[25:] ipport = rcvCmd[rcvCmd.find(" ip ") + 4:] if (config.readConfFile("Parser") == "Oui"): # On prend en charge l'import de files, # On l'ajoute à la base de données et on le télécharge BDD.ajouterEntree("FichiersExt", fileName, ipport) sendCmd = "=cmd fileAdded" self.clientsocket.send(sendCmd.encode()) ip = ipport[:ipport.find(":")] port = ipport[ipport.find(":") + 1:] fctsClient.CmdDemandeFichier(ip, port, fileName) else: # On ne prend pas en charge l'import de files, # Mais on l'ajoute quand même à la base de données BDD.ajouterEntree("FichiersExt", fileName, ipport) sendCmd = "=cmd noParser" self.clientsocket.send(sendCmd.encode()) elif rcvCmd[:23] == "=cmd newPeerNetwork ip ": # =cmd newPeerNetwork ip ****** # Un nouveau peer est envoyé sur le réseau ipport = rcvCmd[23:] if (config.readConfFile("Parser") == "True"): # On prend en charge l'ajout de peers # On l'ajoute à la base de données BDD.ajouterEntree("Noeuds", ipport) sendCmd = "=cmd peerAdded" self.clientsocket.send(sendCmd.encode()) else: # On ne prend pas en charge l'import de files, # Mais on l'ajoute quand même à la base de données BDD.ajouterEntree("FichiersExt", fileName, ipport) sendCmd = "=cmd noParser" self.clientsocket.send(sendCmd.encode()) elif rcvCmd[10:] == "BlackList ": # Les fonctionnalitées de la BlackList rcvCmd = rcvCmd[10:] if rcvCmd[:4] == "name": # Le peer veut savoir si cet élément fait partie de la BlackList, # et si oui quel est son rang (de 0 à 5) rank = search.chercherInfo("BlackList", rcvCmd[4:]) self.clientsocket.send(rank.encode()) if rcvCmd[4:] == "sync": # Le peer veut recevoir l'intégralité de cette base # On va donc la mettre dans un fichier filename = echangeListes.tableToFile("BlackList") try: filename = int(filename) except ValueError: # Si c'est un chiffre, c'est un code d'erreur sendCmd = "=cmd Filename " + str( filename) + "ipport " + config.readConfFile( "MyIP") + ":" + config.readConfFile( "defaultPort") else: sendCmd = "=cmd ERROR" self.clientsocket.send(sendCmd.encode()) else: # Oups... Demande non-reconnue... logs.addLogs("ERROR : Unknown request : " + str(rcvCmd)) self.clientsocket.send("=cmd UNKNOW".encode())
from parser import * from serveurDNS import ServDNS from vpn import ServVPN #from bridge import Bridge import config from thrdLnch import ThreadLauncher import cmdLauncher from color import c #import ssl logs.addLogs("\n\n\n") # On vérifie que les sources sont correctes et pas modifiées #maj.verifSources() config.verifConfig() host = '127.0.0.1' port = int(config.readConfFile("defaultPort")) status = loader("Start Up") # Ici et pas avant car si le fichier n'existe pas status.start() # L'assistant est invisible. class ServeurThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.serveur_lance = True self.ip = "" self.port = 0 self.clientsocket = "" def run(self): while self.serveur_lance: tcpsock.listen(10)
def run(self): print("Welcome in the Blacklist center !\nWhat do you want to do ?") action = autresFonctions.ask("1 : Add something in the BlackList\n2 : Delete something in the BlackList\n3 : Update the Blacklist with an other peer\n4 : Change the BlackList default peer") try: action = int(action) except ValueError as e: print(str(c("red")+"Your input isn't correct (between 1 and 4)\nError : "+str(e)+""+str(c("")))) if action == 1: entree = autresFonctions.ask("What do you want to add in your BlackList ?") while 1: rank = autresFonctions.ask("What is the rank of this new entry ?") try: if int(rank) > 0 and int(rank) <= 5: break except ValueError as e: print(str(c("red")+"Your input isn't correct. (between 1 and 5)\nError : "+str(e)+""+str(c("")))) error = BDD.ajouterEntree("BlackList", entree, rank) if error == 0: print(str(c("yellow")+""+str(entree) + " has been added in the BlackList with the rank " + str(rank)+""+str(c("")))) else: print(str(c("red")+"An error occured. Look at the logs for more informations. ("+str(error)+")"+str(c("")))) elif action == 2: entree = autresFonctions.ask("What do you want to delete in your BlackList ?") error = BDD.supprEntree("BlackList", entree) if error == 0: print(str(c("yellow")+""+str(entree) + " has been deleted in the BlackList"+str(c("")))) else: print(str(c("red")+"An error occured. Look at the logs for more informations. ("+str(error)+")"+str(c("")))) elif action == 3: ipport = str(autresFonctions.ask("With which peer do you want to update? (Press ENTER to update with the default peer : "+str(config.readConfFile("BlackList"))+")")) if ipport == "": status = loader("Work in progress") status.start() blacklist.maj() status.stop() status.join() elif autresFonctions.verifIPPORT(IppeerPort): # Si ipport est un ip:port status = loader("Work in progress") status.start() blacklist.maj(ipport) status.stop() status.join() else: print(str(c("red")+"This is not a IP:PORT"+str(c("")))) elif action == 4: value = str(autresFonctions.ask("Which peer do you want to choose to be your default blacklist peer ? (The actual peer is "+str(config.readConfFile("Blacklist")))) if autresFonctions.verifIPPORT(value): config.modifConfigFile("Blacklist", value) else: print(str(c("red")+"Your input isn't a IP:PORT"+str(c("")))) else: print(str(c("red")+"Your input isn't correct."+str(c(""))))
def run(self): rcvCmd = self.clientsocket.recv(1024) rcvCmd = rcvCmd.decode() status = loader("Connection with a peer") status.start() if rcvCmd[:9] == "=cmd DNS ": # =cmd DNS ... request = rcvCmd[9:] if request[:7] == "AddNDD ": # =cmd DNS AddNDD sha ******* ndd ******* pass ******* sha256 = request[11:request.find(" ndd ")] ndd = request[request.find(" ndd ")+5:request.find(" pass ")] password = request[request.find(" pass ")+6:] erreur = dns.ajouterEntree("DNS", ndd, sha256, password) if erreur != 0: # Une erreur s'est produite et le nom de domaine n'a pas été ajouté if erreur == 5: # Le nom de domaine ets déjà pris sendCmd = "=cmd NDDDejaUtilise" else: sendCmd = "=cmd ERROR" else: sendCmd = "=cmd SUCCESS" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) print(erreur) elif request[:9] == "AddDNSExt": # =cmd DNS AddDNSExt ipport ****** erreur = dns.ajouterEntree("DNSExt", request[17:]) if erreur != 0: # Une erreur s'est produite et le noeud DNS n'a pas été ajouté if erreur == 5: # Le noeud est déjà connu sendCmd = "=cmd IPPORTDejaUtilise" else: sendCmd = "=cmd ERROR" else: sendCmd = "=cmd SUCCESS" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) print(erreur) elif request[:8] == "modifNDD": # =cmd DNS modifNDD ndd ****** adress ****** pass ****** ndd = request[13:request.find(" adress ")] adress = request[request.find(" adress ")+8:request.find(" pass ")] password = request[request.find(" pass ")+6:] erreur = dns.modifEntree("DNS", adress, ndd, password) if erreur > 0: if erreur == 5: sendCmd = "=cmd MDPInvalide" elif erreur == 8: sendCmd = "=cmd NDDInconnuCree" else: sendCmd = "=cmd ERROR" else: sendCmd = "=cmd SUCCESS" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) print(erreur) elif request[:8] == "supprNDD": # =cmd DNS supprNDD ndd ****** pass ****** ndd = request[22:request.find(" pass ")] password = request[request.find(" pass ")+6:] erreur = dns.supprEntree("DNS", ndd, password) if erreur > 0: if erreur == 5: sendCmd = "=cmd MDPInvalide" else: sendCmd = "=cmd ERROR" else: sendCmd = "=cmd SUCCESS" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) print(erreur) elif request[:9] == "searchSHA": # =cmd DNS searchSHA ndd ****** sortie = str(search.searchSHA(request[14:])) if len(sortie) >= 64: sendCmd = sortie else: if sortie == "INCONNU": sendCmd = "=cmd NNDInconnu" else: sendCmd = "=cmd ERROR" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) elif request[:11] == "=cmd status": # On demande le statut du noeud (Simple, Parser, DNS, VPN, Main) sendCmd = "=cmd DNS" self.clientsocket.send(sendCmd.encode()) elif request == "syncBase": # Le noeud demande à recevoir la BDD DNS de ce noeud filename = echangeListes.tableToFile("DNS") try: filename = int(filename) except ValueError: sendCmd = "=cmd Filename " + str(filename) + "ipport " + config.readConfFile("MyIP") + ":" + config.readConfFile("defaultPort") else: sendCmd = "=cmd ERROR" self.clientsocket.send(sendCmd.encode()) else: sendCmd = "=cmd Inconnu" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) elif rcvCmd == "=cmd DemandePresence": sendCmd = "=cmd Present" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) else: # Oups... Demande non-reconnue # On envoie le port par défaut du noeud if rcvCmd != '': logs.addLogs("ERROR : Unknown error (serveurDNS.py) : " + str(rcvCmd)) sendCmd = "=cmd ERROR DefaultPort "+str(config.readConfFile("defaultPort")) sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) status.stop() status.join()
def creerFichier(): # Fonction qui va s'executer via la maintenance assez régulièrement # Elle regarde dans le dossier ADDFILES si il y a des files # Si oui, elle les copie dans le dossier HOSTEDFILES, les renomme de type SHA256.extention # Elle envoie vers la fonction contenue dans BDD.py qui va ajouter les files à la base de données # Et pour finir, elle supprime les files ajoutés de façon à ce que le dossier ADDFILES soit vide. repertoire = "ADDFILES" # Vérifier si le dossier ADDFILES existe, sinon le créer try: os.makedirs("ADDFILES") except OSError: if not os.path.isdir("ADDFILES"): raise # Vérifier si le dossier HOSTEDFILES existe, sinon le créer try: os.makedirs("HOSTEDFILES") except OSError: if not os.path.isdir("HOSTEDFILES"): raise dirs = os.listdir(repertoire) # This would print all the files and directories for file in dirs: file = repertoire + "/" + file if os.path.isfile(file): # L'élément est un file, c'est bon (et pas un dossier) try: with open(file, "rb") as fluxLecture: contenu = fluxLecture.read() fluxLecture.close() except UnicodeDecodeError: logs.addLogs("ERROR : The file is not supported : " + str(file)) os.remove(file) else: shaFichier = hashlib.sha256(str(contenu).encode()).hexdigest() osef, extention = os.path.splitext(file) filename = shaFichier + extention fileDir = "HOSTEDFILES/" + filename fluxEcriture = open(fileDir, "wb") fluxEcriture.write(contenu) fluxEcriture.close() os.remove(file) # L'ajouter à la BDD BDD.ajouterEntree("Fichiers", filename) logs.addLogs( "INFO : A new hosted file has been added successfully : " + filename) # On transmet à quelques noeuds l'information tableau = BDD.aleatoire("Noeuds", "IP", 15, "Parser") if isinstance(tableau, list) and len(tableau) == 15: # On envoi la request à chaque noeud sélectionné for peerIP in tableau: connNoeud = autresFonctions.connectionClient(peerIP) if str(connNoeud) != "=cmd ERROR": logs.addLogs( "INFO : Connection with peer etablished") request = "=cmd newFileNetwork name " + filename + " ip " + str( config.readConfFile("MyIP")) + str( config.readConfFile("defaultPort")) request = request.encode() connNoeud.send(request) rcvCmd = connNoeud.recv(1024) connNoeud.close() if rcvCmd == "=cmd noParser": # Il faut changer le paramètre du noeud, il n'est pas parseur mais simple BDD.supprEntree("Noeuds", peerIP) BDD.ajouterEntree("Noeuds", peerIP) elif rcvCmd != "=cmd fileAdded": # Une erreur s'est produite logs.addLogs( "ERROR : The request was not recognized in creerFichier() : " + str(rcvCmd)) else: logs.addLogs( "ERROR : An error occured in creerFichier() : " + str(connNoeud)) else: # Une erreur s'est produite logs.addLogs( "ERROR : There is not enough IP in creerFichier() : " + str(tableau))
def searchFile(fileName): # Fonction qui a pour but de chercher sur le réseau un file # Il faut d'abord chercher dans la BDD, et si il n'y est pas on cherche plus spécifiquement # IPpeerPort est la variable qui contient l'IP Port du noeud qui possède le file BDD.verifExistBDD() IPpeerPort = "" conn = sqlite3.connect('WTP.db') cursor = conn.cursor() # On va chercher dans les files hébergés try: cursor.execute("""SELECT id FROM Fichiers WHERE Nom = ?""", (fileName, )) except Exception as e: logs.addLogs("ERROR : Problem with database (searchFile()):" + str(e)) rows = cursor.fetchall() for row in rows: boucle += 1 # Le file est hébergé par le noeud qui le cherche ipPortIci = "127.0.0.1:" + str(config.readConfFile("defaultPort")) IPpeerPort = ipPortIci # Si le file n'a pas été trouvé # Il faut chercher dans les files connus externes try: cursor.execute("""SELECT IP FROM FichiersExt WHERE Nom = ?""", (fileName, )) except Exception as e: logs.addLogs("ERROR : Problem with database (searchFile()):" + str(e)) else: rows = cursor.fetchall() for row in rows: # Le file est hébergé par un noeud connu IPpeerPort = str(row) if IPpeerPort == "": # Il faut demander aux noeuds que l'on connait tblNoeuds = BDD.aleatoire("Noeuds", "IP", 10) # On a 10 noeuds dans le tableau # Maintenant, il faut vérifier que au moins la moitiée des noeuds choisis aléatoirement # sont des noeuds "simple", puis mettre les noeuds "simple" au début du tableau tableauNoeudsSimple = [] tableauSuperNoeuds = [] for noeud in tblNoeuds: # On verifie chaque noeud fonctionNoeud = chercherInfo("Noeuds", noeud) if fonctionNoeud == "simple": tableauNoeudsSimple.append(noeud) if fonctionNoeud == "supernoeud" or fonctionNoeud == "DNS": tableauSuperNoeuds.append(noeud) if len(tableauNoeudsSimple) < 6: # Il n'y a pas assez de noeuds simples dans la liste, # Il faut aller en rechercher logs.addLogs("WARNING : Not enough simple peers") # On ajoute les super noeuds après les noeuds simples tblNoeuds = tableauNoeudsSimple + tableauSuperNoeuds for noeudActuel in tblNoeuds: # À chaque noeud on demande si il a le file ou s'il connait un noeud qui l'a # Maintenant on se connecte au noeud error = 0 connexion_avec_serveur = autresFonctions.connectionClient( noeudActuel) if str(connexion_avec_serveur) == "=cmd ERROR": error += 1 else: sendCmd = b"" sendCmd = "=cmd rechercherFichier nom " + fileName sendCmd = sendCmd.encode() connexion_avec_serveur.send(sendCmd) rcvCmd = connexion_avec_serveur.recv(1024) rcvCmd = rcvCmd.decode() connexion_avec_serveur.close() if rcvCmd != "0": IPpeerPort = rcvCmd break return IPpeerPort # Peut retourner "" si il ne trouve pas d'hébergeur
def run(self): status = loader("Connection with a peer") status.start() rcvCmd = self.clientsocket.recv(1024) rcvCmd = rcvCmd.decode() if rcvCmd[:15] == "=cmd VPN noeud ": # =cmd VPN noeud 127.0.0.1:5555 request =cmd DemandeFichier ipport = rcvCmd[15:rcvCmd.find(" request ")] request = rcvCmd[rcvCmd.find(" request ") + 10:] ip = ipport[:ipport.find(":")] port = ipport[ipport.find(":") + 1:] if request[:19] == "=cmd DemandeFichier": fctsClient.CmdDemandeFichier(ip, port, request[24:]) # Manque du code pour transmettre les infos au noeud qui les demandait sendCmd = "=cmd TravailFini" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) elif request[:17] == "=cmd DemandeNoeud": fctsClient.CmdDemandeNoeud(ip, port) # Manque du code pour transmettre les infos au noeud qui les demandait sendCmd = "=cmd TravailFini" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) elif request[:28] == "=cmd DemandeListeFichiersExt": fctsClient.CmdDemandeListeFichiers(ip, port, 1) # Manque du code pour transmettre les infos au noeud qui les demandait sendCmd = "=cmd TravailFini" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) elif request[:25] == "=cmd DemandeListeFichiers": fctsClient.CmdDemandeListeFichiers(ip, port) # Manque du code pour transmettre les infos au noeud qui les demandait sendCmd = "=cmd TravailFini" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) elif request[:23] == "=cmd DemandeListeNoeuds": fctsClient.CmdDemandeListeNoeuds(ip, port) # Manque du code pour transmettre les infos au noeud qui les demandait sendCmd = "=cmd TravailFini" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) elif request[:22] == "=cmd rechercher": # =cmd rechercher nom ****** search.rechercheFichierEntiere(request[20:]) # Manque du code pour transmettre les infos au noeud qui les demandait sendCmd = "=cmd TravailFini" sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) elif request[:11] == "=cmd status": # On demande le statut du noeud (Simple, Parser, DNS, VPN, Main) sendCmd = "=cmd VPN" self.clientsocket.send(sendCmd.encode()) else: logs.addLogs("ERROR : Unknown request (vpn.py) : " + str(request)) else: # Oups... Demande non-reconnue # On envoie le port par défaut du noeud if rcvCmd != '': logs.addLogs("ERROR : Unknown request (vpn.py) : " + str(rcvCmd)) sendCmd = "=cmd ERROR DefaultPort " + str( config.readConfFile("defaultPort")) sendCmd = sendCmd.encode() self.clientsocket.send(sendCmd) status.stop() status.join()
def maxi(): # Affiche la grande documentation interactive (doc) print("Welcome to the WTP documentation.") print(str(c("highlighted")) + "1. Basics" + str(c(""))) print( "To be able to use WTP from your browser, you need to install the browser extension. Link : " + str(c("underline")) + "https://github.com/Torzivalds/WTP/tree/master/Extention%20Firefox" + str(c("")) + " A configuration wizard is included.\n") print( "To add files to the network, you must go to the file named ADDFILES and copy / paste them. Path : " + str(os.getcwd()) + "/ADDFILES WTP will automatically add them. \n" + str(c("bold")) + "Pro tip : WTP checks this folder every 5 minutes. If you are in a hurry, you can enter this request : checkFiles." + str(c("")) + " \n" + str(c("red")) + "DISCLAMER : A file can not be deleted from the network. Be careful when you add some." + str(c(""))) print( "For more information on the possible commands here, enter the name of the command below.\n" ) print(str(c("highlighted")) + "2. Advenced" + str(c(""))) print("You found a bug or improvement ? Contact us on our website :\n" + str(c("underline")) + "https://myrasp.fr/WTP" + str(c(""))) print("Developer ressources are in the Wiki part of our GitHub.\n" + str(c("underline")) + "https://github.com/Torzivalds/WTP/wiki" + str(c(""))) print(str(c("highlighted")) + "3. Contribute" + str(c(""))) print( "You can contribute by improving the code with us, by talking about Web Transfer Protocol to your friends, your family, your colleagues, and if you can not contribute in these ways, you can donate via PayPal.\nYour contributions make us happy and help us to move forward, thank you!\n" ) print(str(c("highlighted")) + "4. External ressources" + str(c(""))) print("GitHub : " + str(c("underline")) + "https://github.com/Torzivalds/WTP" + str(c("")) + "\nPayPal : " + str(c("underline")) + "https://paypal.me/torzivalds" + str(c("")) + "\nWebsite : " + str(c("underline")) + "https://myrasp.fr/WTP" + str(c("")) + "\nFirefox Extension : " + str(c("underline")) + "https://github.com/Torzivalds/WTP/tree/master/Extention%20Firefox" + str(c(""))) while 1: print( str(c("bold")) + "Enter an order, press enter or enter exit" + str(c(""))) cmd = str(input(str(c("highlighted")) + ">> ")) print(str(c("")) + "", end="") if cmd == "exit": break elif cmd == "update": print( "This function allows you to update the source code of WTP, which allows you to add new features and fix bugs. A check is made every 24 hours, and the update is installed as soon as it is detected." ) print(str(c("yellow")) + "A restart may be required" + str(c(""))) elif cmd == "stats": print( "This command allows to know some statistics of his peer, which allows to have some information about the use of peer by the network. They are for informational purposes only, they are not necessarily exact, because it is very easy to modify them via the database (WTP.db which is a SQlite database)" ) elif cmd == "config": print( "This function allows you to modify the wtp.conf configuration file, accessible via this path : " + str(os.getcwd()) + "/wtp.conf") print("You can also edit it with your favorite text editor.") print( str(c("bold")) + "Think after changing settings to restart WTP for these to take effect." + str(c(""))) elif cmd == "dns": print( "The DNS is a system for replacing the names of files that are very long with domain names that are much shorter and easier to remember.\nThis command is used to access a program that allows you to add, modify and delete DNS entries. You can do this on your peer (127.0.0.1) or on a remote peer. So you can add your own domain name, for free and very easily.\n" + str(c("yellow")) + "Be careful, when you add a new domain name, manually check that it does not already exist!" + str(c(""))) elif cmd == "vpn": print("This section is being created.") elif cmd == "wtp": print("Web Transfer Protocol V" + str(config.readConfFile("Version"))) print( "WTP is a new peer to peer network for the web under the GPL v3 lisence coded by a student on his free time." ) print( "This version is not stable. WTP is being created, it's taking time. You can contribute by improving the code with us, by talking about Web Transfer Protocol to your friends, your family, your colleagues, and if you can not contribute in these ways, you can donate via PayPal.Your contributions make us happy and help us to move forward, thank you!" ) elif cmd == "license": print( "WTP is free software, and is under GPL v3 license.This means that you can use, share, modify, redistribute as much as you want WTP. For more information, you can consult the license on the official GNU website :" ) print( str(c("underline")) + "https://www.gnu.org/licenses/gpl.html" + str(c(""))) elif cmd == "blacklist": print( "The blacklist is a system to block the files you want, such as advertisements and explicit documents. You can add or delete files from this list locally, and you can also use the list of a remote peer. This command allows you to modify the BlackList easily." ) elif cmd[:6] == "client": print( str(c("yellow")) + "This command is used to send orders by hand to other peers or yours (IP: 127.0.0.1). Most users should not use this command, but it may be useful in some cases." + str(c(""))) cmd = cmd[7:] if cmd == "DemandePresence": print( "This command verifies that a peer is still connected to the network. It is done automatically every day. If the peer does not respond, it is moved into a sort of quarantine in the database, and after 10 days, it is permanently deleted." ) elif cmd == "DemandeNoeud": print( "This command requests 48 peers IP addresses from the network to a known peer to add them to the database. If you want to receive all the peers in the remote peer's database, prefer to use =cmd DemandeListeNoeuds" ) elif cmd == "DemandeFichier": print( "This command is used to download a file. Takes into parameters the ip and the port of the peer, and the name of the file which one wishes to download. If you do not know which peer is hosting the file, use =cmd rechercher" ) elif cmd == "DemandeListeNoeuds": print( "This command allows to receive all the peers known by another peer. \n" + str(c("yellow")) + "It can be very long if the peer knows a lot of peers." + str(c("")) + "\nIf you want to know only a few peers (48 to be precise), use =cmd DemandeNoeud" ) elif cmd == "DemandeListeFichiers": print( "This command asks a peer for a list of all the files it hosts. The received list is processed and the files are added to the database. \n" + str(c("yellow")) + "Attention, this action could take time if the peer hosts a lot of files." + str(c(""))) elif cmd == "rechercher": print( "This function is used to search for a file on the network. It takes as parameter a domain name, or the exact name of the file. If it finds one of the file locations on the network, it is downloaded." ) else: print( str(c("red")) + "Unknown command." + str(c("")) + " Enter client then :") print("DemandePresence DemandeNoeud DemandeFichier") print("DemandeListeNoeuds DemandeListeFichiers rechercher") elif cmd == "majDNS": print( "This command makes it easy to update your peer's DNS entries with a trusted remote peer " + str(c("yellow")) + "(Beware of phishing)" + str(c("")) + ". The DNS is a system for replacing the names of files that are very long with domain names that are much shorter and easier to remember." ) elif cmd == "delAll": print( str(c("yellow")) + "This function deletes all settings, all files, the entire WTP database. It's irreversible." + str(c(""))) print( "To be precise, it removes the following folders : .TEMP, ADDFILES, HOSTEDFILES, and the folloing files : .TempMaintenance24H, .TempMaintenance5M, logs, wtp.conf and the database : WTP.db" ) elif cmd == "checkFiles": print( "This command is used to check if there are new files in ADDFILES. If so, they will be added to the most deleted network from this folder. This command also makes it possible to check that all the files in HOSTEDFILES are well known, otherwise they are sent to ADDFILES so that they are added to the network. And finally, this removes all the old temporary files in .TEMP" ) elif cmd == "folder tree": print("WTP is on this path : " + str(os.getcwd())) print("In there, you can find :") print( "WTP source files that allow it to work. If you modify or delete it, WTP will not work properly." ) print(str(c("bold") + ".TEMP" + str(c("")))) print( "This folder contains a number of small temporary files. Normally, they are very quickly removed." ) print( str(c("yellow")) + "If you delete the folder or files inside, WTP may not be doing what it is doing or even crashing." + str(c(""))) print(str(c("bold") + "ADDFILES" + str(c("")))) print( "This is a folder for you! This is the folder in which you can put all the files you want. After a few minutes, they will be available on the network. If you do not want to wait, you can start the checkFiles command. \n" + str(c("bold")) + "Once the files on the network, they are deleted from this folder. Remember to save them elsewhere! Attention, so that the files are sent on the network, they must not be in a subfolder of ADDFILES, otherwise they will be removed." + str(c("")) + " \n" + str(c("red")) + "Once a file is on the network, no one can delete it. Pay attention !" + str(c(""))) print(str(c("bold") + "HOSTEDFILES" + str(c("")))) print( "This folder contains all the files that your peer knows. If you delete them, WTP will have to download them again if you need them. Deleting the contents of this folder frees disk space, but it takes longer if you want to access the file again. If you delete files, they will most certainly be accessible on the network thanks to other peers." ) else: # On affiche toutes les commandes possibles ici # Chaque commande permet d'afficher une documentation précise, plus que help print("Here are all the possible commands here :") print("update vpn majDNS") print("stats wtp delAll") print("config license checkFiles") print("dns blacklist client <cmd>") print("folder tree")