Update app.py
Browse files
app.py
CHANGED
|
@@ -1,86 +1,52 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 3 |
-
from psycopg2 import connect, sql
|
| 4 |
-
import psycopg2.extras
|
| 5 |
import gradio as gr
|
| 6 |
-
import threading
|
| 7 |
|
| 8 |
-
# Fetch environment variables
|
| 9 |
-
DB_NAME = os.getenv("DB_NAME")
|
| 10 |
-
DB_USER = os.getenv("DB_USER")
|
| 11 |
-
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
| 12 |
-
DB_HOST = os.getenv("DB_HOST")
|
| 13 |
-
DB_PORT = os.getenv("DB_PORT")
|
| 14 |
APP_PASSWORD = os.getenv("APP_PASSWORD")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
-
return connect(
|
| 19 |
-
dbname=DB_NAME,
|
| 20 |
-
user=DB_USER,
|
| 21 |
-
password=DB_PASSWORD,
|
| 22 |
-
host=DB_HOST,
|
| 23 |
-
port=DB_PORT
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
# Create Flask app
|
| 27 |
-
app = Flask(__name__)
|
| 28 |
-
|
| 29 |
-
# API endpoint for running SQL commands
|
| 30 |
-
@app.route("/run_sql", methods=["POST"])
|
| 31 |
-
def run_sql():
|
| 32 |
-
# Get the password and command from the request
|
| 33 |
-
password = request.json.get("password")
|
| 34 |
-
command = request.json.get("command")
|
| 35 |
-
|
| 36 |
# Check if the password is correct
|
| 37 |
if password != APP_PASSWORD:
|
| 38 |
-
return
|
| 39 |
-
|
| 40 |
-
# Validate
|
| 41 |
-
if not
|
| 42 |
-
return
|
| 43 |
-
|
| 44 |
-
# Execute the SQL command
|
| 45 |
-
conn = None
|
| 46 |
-
result = None
|
| 47 |
try:
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
-
|
| 61 |
-
finally:
|
| 62 |
-
if conn:
|
| 63 |
-
conn.close()
|
| 64 |
-
|
| 65 |
-
return jsonify(result)
|
| 66 |
|
| 67 |
# Define Gradio interface
|
| 68 |
-
def gradio_interface():
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
if __name__ == "__main__":
|
| 81 |
-
|
| 82 |
-
flask_thread.daemon = True # Make sure it will close when the main thread exits
|
| 83 |
-
flask_thread.start()
|
| 84 |
-
|
| 85 |
-
# Run Gradio in the main thread
|
| 86 |
-
gradio_interface()
|
|
|
|
| 1 |
import os
|
| 2 |
+
import subprocess
|
|
|
|
|
|
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
+
# Fetch the app password from environment variables
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
APP_PASSWORD = os.getenv("APP_PASSWORD")
|
| 7 |
|
| 8 |
+
# Function to execute shell commands
|
| 9 |
+
def execute_command(password, script):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Check if the password is correct
|
| 11 |
if password != APP_PASSWORD:
|
| 12 |
+
return {"error": "Invalid password!"}
|
| 13 |
+
|
| 14 |
+
# Validate the script input
|
| 15 |
+
if not script:
|
| 16 |
+
return {"error": "No script provided!"}
|
| 17 |
+
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
+
# Execute the command using subprocess
|
| 20 |
+
process = subprocess.Popen(
|
| 21 |
+
script, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
| 22 |
+
)
|
| 23 |
+
stdout, stderr = process.communicate()
|
| 24 |
+
|
| 25 |
+
# Return the output and errors
|
| 26 |
+
return {
|
| 27 |
+
"stdout": stdout.decode("utf-8"),
|
| 28 |
+
"stderr": stderr.decode("utf-8"),
|
| 29 |
+
"exit_code": process.returncode
|
| 30 |
+
}
|
| 31 |
except Exception as e:
|
| 32 |
+
return {"error": f"Error executing script: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Define Gradio interface
|
| 35 |
+
def gradio_interface(password, script):
|
| 36 |
+
return execute_command(password, script)
|
| 37 |
+
|
| 38 |
+
# Create Gradio app
|
| 39 |
+
interface = gr.Interface(
|
| 40 |
+
fn=gradio_interface,
|
| 41 |
+
inputs=[
|
| 42 |
+
gr.Textbox(label="Password", type="password"),
|
| 43 |
+
gr.Textbox(label="Script", lines=5, placeholder="Enter your script here...")
|
| 44 |
+
],
|
| 45 |
+
outputs="json",
|
| 46 |
+
title="Command Executor",
|
| 47 |
+
description="Provide a password and a shell script to execute commands remotely."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Launch the Gradio interface
|
| 51 |
if __name__ == "__main__":
|
| 52 |
+
interface.launch(share=True, inline=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|