wop commited on
Commit
b0ab72e
·
verified ·
1 Parent(s): 43006ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -74
app.py CHANGED
@@ -1,86 +1,52 @@
1
  import os
2
- from flask import Flask, request, jsonify
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
- # Database connection function
17
- def get_db_connection():
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 jsonify({"error": "Invalid password!"}), 401
39
-
40
- # Validate SQL command input
41
- if not command:
42
- return jsonify({"error": "No SQL command provided!"}), 400
43
-
44
- # Execute the SQL command
45
- conn = None
46
- result = None
47
  try:
48
- # Connect to the database
49
- conn = get_db_connection()
50
- cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
51
-
52
- # Execute the SQL command (this uses parameterized queries for security)
53
- cursor.execute(command)
54
- if command.strip().lower().startswith("select"):
55
- result = cursor.fetchall() # If the command is a SELECT, fetch the results
56
- else:
57
- conn.commit() # For non-SELECT commands, commit the changes
58
- result = {"message": "Command executed successfully!"}
 
59
  except Exception as e:
60
- result = {"error": f"Error executing command: {str(e)}"}
61
- finally:
62
- if conn:
63
- conn.close()
64
-
65
- return jsonify(result)
66
 
67
  # Define Gradio interface
68
- def gradio_interface():
69
- def greet(name):
70
- return f"Hello {name}!"
71
-
72
- interface = gr.Interface(fn=greet, inputs="text", outputs="text")
73
- interface.launch(share=True, inline=True)
74
-
75
- # Starting Flask app in a separate thread
76
- def run_flask():
77
- app.run(host="0.0.0.0", port=5050)
78
-
79
- # Run Flask and Gradio in parallel using threading
 
 
 
 
80
  if __name__ == "__main__":
81
- flask_thread = threading.Thread(target=run_flask)
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)