1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| from flask import Flask, request, render_template_string import socket import threading import html
app = Flask(__name__)
@app.route('/', methods=["GET"]) def source(): with open(__file__, 'r', encoding='utf-8') as f: return '<pre>'+html.escape(f.read())+'</pre>'
@app.route('/', methods=["POST"]) def template(): template_code = request.form.get("code") blacklist = ['__', 'import', 'os', 'sys', 'eval', 'subprocess', 'popen', 'system', '\r', '\n'] for black in blacklist: if black in template_code: return "Forbidden content detected!" result = render_template_string(template_code) print(result) return 'ok' if result is not None else 'error'
class HTTPProxyHandler: def __init__(self, target_host, target_port): self.target_host = target_host self.target_port = target_port
def handle_request(self, client_socket): try: request_data = b"" while True: chunk = client_socket.recv(4096) request_data += chunk if len(chunk) < 4096: break
if not request_data: client_socket.close() return
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as proxy_socket: proxy_socket.connect((self.target_host, self.target_port)) proxy_socket.sendall(request_data)
response_data = b"" while True: chunk = proxy_socket.recv(4096) if not chunk: break response_data += chunk
header_end = response_data.rfind(b"\r\n\r\n") if header_end != -1: body = response_data[header_end + 4:] else: body = response_data response_body = body response = b"HTTP/1.1 200 OK\r\n" \ b"Content-Length: " + str(len(response_body)).encode() + b"\r\n" \ b"Content-Type: text/html; charset=utf-8\r\n" \ b"\r\n" + response_body
client_socket.sendall(response) except Exception as e: print(f"Proxy Error: {e}") finally: client_socket.close()
def start_proxy_server(host, port, target_host, target_port): proxy_handler = HTTPProxyHandler(target_host, target_port) server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_socket.bind((host, port)) server_socket.listen(100) print(f"Proxy server is running on {host}:{port} and forwarding to {target_host}:{target_port}...")
try: while True: client_socket, addr = server_socket.accept() print(f"Connection from {addr}") thread = threading.Thread(target=proxy_handler.handle_request, args=(client_socket,)) thread.daemon = True thread.start() except KeyboardInterrupt: print("Shutting down proxy server...") finally: server_socket.close()
def run_flask_app(): app.run(debug=False, host='127.0.0.1', port=5000)
if __name__ == "__main__": proxy_host = "0.0.0.0" proxy_port = 5001 target_host = "127.0.0.1" target_port = 5000
proxy_thread = threading.Thread(target=start_proxy_server, args=(proxy_host, proxy_port, target_host, target_port)) proxy_thread.daemon = True proxy_thread.start()
print("Starting Flask app...") run_flask_app()
|