Advertisement
Guest User

HD2Web 1.0

a guest
Aug 3rd, 2011
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.66 KB | None | 0 0
  1. import string,cgi,socket,sys
  2. from os import curdir,sep,listdir,path,makedirs,pathsep,name
  3. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  4.  
  5. ricorda = []
  6. passw = ""
  7. loggato = 0
  8. ip_acc = []
  9.  
  10.  
  11. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  12. s.connect(("google.com",80))
  13. ip = s.getsockname()
  14.  
  15. def ce():
  16.     for i in ip_acc:
  17.         if ip[0] == i:
  18.             return 1
  19.     return 0
  20.  
  21. def search_file(filename, search_path):
  22.     file_found = 0
  23.     paths = string.split(search_path, pathsep)
  24.     for pathz in paths:
  25.         if path.exists(path.join(pathz, filename)):
  26.             file_found = 1
  27.             break
  28.     if file_found:
  29.         return path.abspath(path.join(pathz, filename))
  30.     else:
  31.          return 0
  32.  
  33. class MyHandler(BaseHTTPRequestHandler):
  34.     def do_GET(self):
  35.         try:
  36.             global ricorda
  37.             self.path = self.path.replace("%20"," ")
  38.             if self.path != "/favicon.ico":
  39.                 ricorda.append(self.path)
  40.             if loggato == 0 and ce() == 0 and passw != "":
  41.                 self.wfile.write("<html><form method='POST' enctype='multipart/form-data' action=''><b>Stai cercando di accedere ad un computer protetto da password. Inseriscila per continuare:</b>"+'<br><input type="password" name=pw><br><input type=submit value=OK></form></html>')
  42.             else:
  43.                 if path.isfile(self.path):
  44.                     if self.path == "/hd2web/upload.html":
  45.                         self.wfile.write("<HTML><BODY><form method='POST' enctype='multipart/form-data' action=''>File da uploadare: <input type=file name=file><br><br>"+'Nome del file: <input type="text" name=nome><br><br><input type=submit value=Premi> per uploadare il file</form></body></html><br><br>'+'<a href="'+ricorda[len(ricorda)-2]+'">Indietro</a>')
  46.                         return
  47.                     elif self.path == "/hd2web/about.html":
  48.                         self.wfile.write('<html><p align="center"><h1>HD2Web</h1></p>')
  49.                         self.wfile.write("<b>Descrizione:</b> rende accessibili tutti i tuoi file da remoto, connettendosi via browser<br>")
  50.                         self.wfile.write("<b>Autore:</b> I.Ren<br>")
  51.                         self.wfile.write("<b>Licenza:</b> <a href=http://www.gnu.org/copyleft/gpl.html>GNU GPL</a><br>")
  52.                         self.wfile.write("<b>Versione:</b> 1.0 ipv4<br>")
  53.                         self.wfile.write("<b>Visita:</b> <a href=http://autoit.forumcommunity.net>autoit.forumcommunity.net</a>")
  54.                         self.wfile.write("<br><br><font size=2><b>History:</b>")
  55.                         self.wfile.write("<br>26/07/2011: rilasciata versione 0.9")
  56.                         self.wfile.write("<br>03/08/2011: rilasciata versione 1.0. Aggiornamenti:<ul><li>Bug fixati<li>Aggiunta compatiblita' con windows<li>Aggiunta possibilita' di proteggere con password<li>Aggiuna la possibilita' di cercare cartelleo files</ul>")
  57.                         self.wfile.write('</font><br><a href="'+ricorda[len(ricorda)-2]+'">Indietro</a>')
  58.                         return
  59.                     else:
  60.                         self.send_response(200)
  61.                         self.send_header('Content-Type', 'application/zip')
  62.                         self.send_header('Content-Disposition', 'attachment;'
  63.                                 'filename='+self.path[self.path.rfind("/")+1:len(self.path)])
  64.                         self.send_header("Content-Transfer-Encoding","binary")
  65.                         self.end_headers()
  66.                         o = open(self.path)
  67.                         s = ""
  68.                         for line in o:
  69.                             s += line
  70.                         o.close()
  71.                         self.wfile.write(s)
  72.                         return
  73.                 else:
  74.                     lista = listdir(self.path)
  75.                     self.send_response(200)
  76.                     self.send_header('Content-type',    'text/html')
  77.                     self.end_headers()
  78.                     self.wfile.write("<form method='POST' enctype='multipart/form-data action=''><b><a href=/hd2web/upload.html>Upload</a></b> <b><a href=/hd2web/about.html>About</a></b>&emsp;File/cartella da cercare:"+' <input type="text" name=cercafile> <input type=submit value=cerca></form>')
  79.                     if self.path != "/":
  80.                         self.wfile.write('<a href="'+self.path[0:self.path.find(self.path.split("/")[len(self.path.split("/"))-2])]+'">..</a><br>')
  81.                     for i in sorted(lista):
  82.                         if i != "hd2web":
  83.                             if path.isfile(self.path+i) == False:
  84.                                 self.wfile.write('DIR:&emsp; <a href="'+self.path+i+'/">'+i+'</a><BR>')
  85.                     for i in sorted(lista):
  86.                         if path.isfile(self.path+i):
  87.                             self.wfile.write("FILE:&emsp;<a href="+self.path+i+">"+i+"</a><BR>")
  88.                 return
  89.        
  90.         except:
  91.             self.send_error(404,"File not found: "+self.path)
  92.    
  93.     def do_POST(self):
  94.         global rootnode
  95.         global loggato
  96.         self.path = self.path.replace("%20"," ")
  97.         ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
  98.         if ctype == 'multipart/form-data':
  99.             query = cgi.parse_multipart(self.rfile,pdict)
  100.         self.send_response(200)
  101.         self.end_headers()
  102.         try:
  103.             nome = query.get("nome")
  104.             data = query.get("file")
  105.             dove = ricorda[len( ricorda)-2]
  106.             o = open(dove+nome[0],"w")
  107.             o.write(data[0])
  108.             o.close()
  109.             self.wfile.write('<html><b>Upload effettuato con successo.</b><br><br><a href="'+dove+'">Indietro</a></html>')
  110.         except:
  111.             try:
  112.                 g_pass = query.get("pw")
  113.                 if g_pass[0] == passw:
  114.                     self.wfile.write("<b>Password corretta.</b><br><br><a href=http://"+ip[0]+">Entra</a>")
  115.                     loggato = 1
  116.                     ip_acc.append(ip[0])
  117.                 else:
  118.                     self.wfile.write("<b>Password errata.</b><br><br><a href=http://"+ip[0]+">Riprova</a>")
  119.             except:
  120.                 nome_fc = query.get("cercafile")
  121.                 cerca = search_file(nome_fc[0],self.path)
  122.                 self.wfile.write("<html><b>Risultati ricerca</b><br><br>")
  123.                 print "---PATH\t" + self.path
  124.                 if cerca != 0:
  125.                     if name == "nt":
  126.                         if path.isfile(cerca[2:len(cerca)]) == False:
  127.                             self.wfile.write('<a href="'+cerca[2:len(cerca)]+'/">'+cerca+"</a></html>")
  128.                         else:
  129.                             self.wfile.write('<a href="'+cerca[2:len(cerca)]+'">'+cerca+"</a></html>")
  130.                     else:
  131.                         if path.isfile(cerca) == False:
  132.                             self.wfile.write('<a href="'+cerca+'/">'+cerca+"</a></html>")
  133.                         else:
  134.                             self.wfile.write('<a href="'+cerca+'">'+cerca+"</a></html>")
  135.                 else:
  136.                     self.wfile.write("Nessun file o cartella trovato.")
  137.                 if self.path == "/":
  138.                     self.wfile.write('<br><br><a href="'+ricorda[len(ricorda)-2]+'">Indietro</a>')
  139.                 else:
  140.                     self.wfile.write('<br><br><a href="'+ricorda[len(ricorda)-1]+'">Indietro</a>')
  141.  
  142. def main():
  143.     global passw
  144.     if not path.exists("/hd2web/"):
  145.         makedirs("/hd2web")
  146.     if not path.exists("/hd2web/upload.html"):
  147.         open("/hd2web/upload.html","w").close()
  148.     if not path.exists("/hd2web/about.html"):
  149.         open("/hd2web/about.html","w").close()
  150.     if not path.exists("/hd2web/cerca.html"):
  151.         open("/hd2web/cerca.html","w").close()
  152.     try:
  153.         if len(sys.argv) > 1:
  154.             porta = sys.argv[1]
  155.         else:
  156.             porta = 80
  157.  
  158.         if len(sys.argv) == 3:
  159.             porta = sys.argv[1]
  160.             passw = sys.argv[2]
  161.        
  162.         server = HTTPServer((ip[0],int(porta)),MyHandler)
  163.         print "Server creato su: http://"+ip[0]+":"+str(porta)
  164.         server.serve_forever()
  165.     except KeyboardInterrupt:
  166.         print "^C ricevuto, chiudo il server"
  167.         server.socket.close()
  168.  
  169. if __name__ == "__main__":
  170.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement