Advertisement
dev017

index.py

Aug 8th, 2023
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import http.server
  2. import socketserver
  3. import os
  4.  
  5. blacklist = {}
  6. packet = b"server|127.0.0.1\nport|17091\ntype|1\n#maint|Mainetrance message GTPS\n\nbeta_server|127.0.0.1\nbeta_port|17091\n\nbeta_type|1\nmeta|localhost\nRTENDMARKERBS1001"
  7. files = {}
  8.  
  9. for file in os.listdir("assets"):
  10.     if not file.endswith(".rrtex"):
  11.         continue
  12.     with open(os.path.join("assets", file), "rb") as f:
  13.         files[file] = f.read()
  14.  
  15. timeout = 10
  16.  
  17. def add_address(address):
  18.     blacklist[address] = timeout + time.time()
  19.  
  20. class RequestHandler(http.server.BaseHTTPRequestHandler):
  21.     def do_POST(self):
  22.         self.process_request()
  23.  
  24.     def do_GET(self):
  25.         self.process_request()
  26.  
  27.     def process_request(self):
  28.         url = self.path.split("/growtopia/")[1]
  29.         if url and url.startswith("server_data.php") and self.command.lower() == "post":
  30.             print(f"Connection from: {self.client_address[0]}")
  31.             if (address := self.client_address[0] + self.path) not in blacklist:
  32.                 print(f"{self.client_address[0]} agora está na lista negra para {timeout} segundos. na rotae: {self.path}")
  33.                 add_address(address)
  34.             else:
  35.                 not_allowed = blacklist[address]
  36.                 if time.time() > not_allowed:
  37.                     print(f"Timeout done for: {self.client_address[0]}")
  38.                     del blacklist[address]
  39.                 else:
  40.                     print(f"Connection blocked: {self.client_address[0]}")
  41.                     self.connection.close()
  42.                     return
  43.  
  44.             print(f"Entered correct route: {self.client_address[0]}")
  45.             self.send_response(200)
  46.             self.send_header('Content-Type', 'text/html')
  47.             self.end_headers()
  48.             self.wfile.write(packet)
  49.  
  50.         elif url and url in files and self.command.lower() == "get":
  51.             print(f"Connection from: {self.client_address[0]}")
  52.             if (address := self.client_address[0] + self.path) not in blacklist:
  53.                 print(f"{self.client_address[0]} agora está na blacklist pata {timeout} segundos. Na rota: {self.path}")
  54.                 add_address(address)
  55.             else:
  56.                 not_allowed = blacklist[address]
  57.                 if time.time() > not_allowed:
  58.                     print(f"Timeout done for: {self.client_address[0]}")
  59.                     del blacklist[address]
  60.                 else:
  61.                     print(f"Connection blocked: {self.client_address[0]}")
  62.                     self.connection.close()
  63.                     return
  64.  
  65.             self.send_response(200)
  66.             self.send_header('Content-Type', 'application/octet-stream')
  67.             self.send_header('Content-Disposition', f'attachment; filename={url + ".rrtex" if not url.endswith(".rrtex") else url}')
  68.             self.end_headers()
  69.             self.wfile.write(files[url])
  70.  
  71.         else:
  72.             print(f"Connection blocked: {self.client_address[0]}")
  73.             self.connection.close()
  74.  
  75. def run(server_class=socketserver.TCPServer, handler_class=RequestHandler, port=80):
  76.     server_address = ('', port)
  77.     httpd = server_class(server_address, handler_class)
  78.     print("HTTP Server now up.")
  79.     httpd.serve_forever()
  80.  
  81. if __name__ == "__main__":
  82.     run()
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement