# app.py from flask import Flask, request, jsonify, render_template, stream_with_context, Response import os import subprocess import tempfile import shutil import sys import logging app = Flask(__name__) logging.basicConfig(level=logging.DEBUG) # Create a temporary directory for operations with full permissions temp_dir = tempfile.mkdtemp() os.chmod(temp_dir, 0o777) @app.route("/") def index(): return render_template("index.html") def execute_shell_command(command): """Executes a shell command and streams output with improved error handling.""" try: # Split the command for better security and handling if command.startswith('git clone'): # Special handling for git clone process = subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, cwd=temp_dir, env={'GIT_TERMINAL_PROMPT': '0'} # Prevent git from prompting ) else: process = subprocess.Popen( command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, cwd=temp_dir ) # Stream output in real-time while True: output = process.stdout.readline() error = process.stderr.readline() if output: yield f"{output}
" if error: yield f"Error: {error}
" # Check if process has finished if output == '' and error == '' and process.poll() is not None: break # Get return code return_code = process.poll() if return_code != 0: yield f"Command failed with return code {return_code}
" else: yield "Command completed successfully.
" except Exception as e: yield f"Error executing command: {str(e)}
" finally: if process: process.stdout.close() process.stderr.close() @app.route("/execute", methods=["POST"]) def execute_code(): command = request.json.get("code", "").strip() if not command: return jsonify({"result": "Error: No command provided."}) try: if command.startswith("!"): shell_command = command[1:] # Log the command being executed app.logger.debug(f"Executing shell command: {shell_command}") return Response( stream_with_context(execute_shell_command(shell_command)), content_type='text/html' ) else: # Handle Python code execution process = subprocess.run( [sys.executable, "-c", command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, cwd=temp_dir ) return jsonify({"result": process.stdout + process.stderr}) except Exception as e: app.logger.error(f"Error executing command: {str(e)}") return jsonify({"result": f"Error: {str(e)}"}) @app.route("/cleanup", methods=["POST"]) def cleanup(): global temp_dir try: if os.path.exists(temp_dir): shutil.rmtree(temp_dir, ignore_errors=True) temp_dir = tempfile.mkdtemp() os.chmod(temp_dir, 0o777) return jsonify({"result": "Temporary files cleaned up."}) except Exception as e: return jsonify({"result": f"Error during cleanup: {str(e)}"}) if __name__ == "__main__": # Ensure the temp directory has proper permissions os.chmod(temp_dir, 0o777) app.run(host="0.0.0.0", port=7860, debug=True)