Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,10 +5,11 @@ import threading
|
|
| 5 |
from queue import Queue
|
| 6 |
import os
|
| 7 |
import shlex
|
|
|
|
| 8 |
from telegram import Update
|
| 9 |
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
| 10 |
|
| 11 |
-
BOT_TOKEN =
|
| 12 |
|
| 13 |
log_queue = Queue()
|
| 14 |
MAX_LOGS = 20000
|
|
@@ -19,6 +20,9 @@ bot_running = False
|
|
| 19 |
bot_thread = None
|
| 20 |
current_dir = os.getcwd()
|
| 21 |
|
|
|
|
|
|
|
|
|
|
| 22 |
# --- Logging ---
|
| 23 |
def add_terminal_log(entry):
|
| 24 |
terminal_logs.append(entry)
|
|
@@ -32,9 +36,21 @@ def add_bot_log(entry):
|
|
| 32 |
|
| 33 |
# --- Command Execution ---
|
| 34 |
def execute_command(cmd):
|
| 35 |
-
global current_dir
|
| 36 |
output = []
|
| 37 |
add_terminal_log(f"$ {cmd}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if cmd.strip().startswith("cd"):
|
| 39 |
parts = shlex.split(cmd)
|
| 40 |
if len(parts) > 1:
|
|
@@ -53,14 +69,15 @@ def execute_command(cmd):
|
|
| 53 |
output.append(msg)
|
| 54 |
add_terminal_log(msg)
|
| 55 |
else:
|
| 56 |
-
|
| 57 |
-
for line in iter(
|
| 58 |
line = line.strip()
|
| 59 |
if line:
|
| 60 |
output.append(line)
|
| 61 |
add_terminal_log(line)
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
return "\n".join(output)
|
| 65 |
|
| 66 |
# --- Telegram Bot Handlers ---
|
|
@@ -83,7 +100,7 @@ async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 83 |
|
| 84 |
# --- Bot Start/Stop Logic ---
|
| 85 |
def start_bot():
|
| 86 |
-
global bot_app, bot_running, bot_thread
|
| 87 |
if bot_running or not BOT_TOKEN:
|
| 88 |
add_bot_log("[Bot] Already running or token missing.")
|
| 89 |
return
|
|
@@ -139,9 +156,9 @@ with gr.Blocks() as demo:
|
|
| 139 |
gr.Markdown("## π₯οΈ Interactive Terminal")
|
| 140 |
|
| 141 |
with gr.Row():
|
| 142 |
-
terminal_output = gr.Textbox(label="π Terminal Output", lines=
|
| 143 |
with gr.Row():
|
| 144 |
-
cmd_input = gr.Textbox(placeholder="Enter shell command", label="Command Input")
|
| 145 |
run_btn = gr.Button("βΆοΈ Run Command")
|
| 146 |
|
| 147 |
run_btn.click(fn=live_terminal, inputs=cmd_input, outputs=terminal_output)
|
|
@@ -151,7 +168,7 @@ with gr.Blocks() as demo:
|
|
| 151 |
gr.Markdown("## π€ Telegram Bot Controls")
|
| 152 |
|
| 153 |
with gr.Row():
|
| 154 |
-
bot_output = gr.Textbox(label="π€ Telegram Bot Logs", lines=
|
| 155 |
with gr.Row():
|
| 156 |
token_box = gr.Textbox(label="Bot Token", placeholder="Enter Telegram Bot Token")
|
| 157 |
with gr.Row():
|
|
|
|
| 5 |
from queue import Queue
|
| 6 |
import os
|
| 7 |
import shlex
|
| 8 |
+
import signal
|
| 9 |
from telegram import Update
|
| 10 |
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes
|
| 11 |
|
| 12 |
+
BOT_TOKEN = ""
|
| 13 |
|
| 14 |
log_queue = Queue()
|
| 15 |
MAX_LOGS = 20000
|
|
|
|
| 20 |
bot_thread = None
|
| 21 |
current_dir = os.getcwd()
|
| 22 |
|
| 23 |
+
# Track current running subprocess for Ctrl+C
|
| 24 |
+
current_proc = None
|
| 25 |
+
|
| 26 |
# --- Logging ---
|
| 27 |
def add_terminal_log(entry):
|
| 28 |
terminal_logs.append(entry)
|
|
|
|
| 36 |
|
| 37 |
# --- Command Execution ---
|
| 38 |
def execute_command(cmd):
|
| 39 |
+
global current_dir, current_proc
|
| 40 |
output = []
|
| 41 |
add_terminal_log(f"$ {cmd}")
|
| 42 |
+
|
| 43 |
+
if cmd.strip() == "ctrl+c":
|
| 44 |
+
if current_proc:
|
| 45 |
+
current_proc.send_signal(signal.SIGINT)
|
| 46 |
+
msg = "[Info] Sent Ctrl+C to running process."
|
| 47 |
+
add_terminal_log(msg)
|
| 48 |
+
return msg
|
| 49 |
+
else:
|
| 50 |
+
msg = "[Info] No running process to interrupt."
|
| 51 |
+
add_terminal_log(msg)
|
| 52 |
+
return msg
|
| 53 |
+
|
| 54 |
if cmd.strip().startswith("cd"):
|
| 55 |
parts = shlex.split(cmd)
|
| 56 |
if len(parts) > 1:
|
|
|
|
| 69 |
output.append(msg)
|
| 70 |
add_terminal_log(msg)
|
| 71 |
else:
|
| 72 |
+
current_proc = subprocess.Popen(cmd, cwd=current_dir, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
| 73 |
+
for line in iter(current_proc.stdout.readline, ''):
|
| 74 |
line = line.strip()
|
| 75 |
if line:
|
| 76 |
output.append(line)
|
| 77 |
add_terminal_log(line)
|
| 78 |
+
current_proc.stdout.close()
|
| 79 |
+
current_proc.wait()
|
| 80 |
+
current_proc = None
|
| 81 |
return "\n".join(output)
|
| 82 |
|
| 83 |
# --- Telegram Bot Handlers ---
|
|
|
|
| 100 |
|
| 101 |
# --- Bot Start/Stop Logic ---
|
| 102 |
def start_bot():
|
| 103 |
+
global bot_app, bot_running, bot_thread, BOT_TOKEN
|
| 104 |
if bot_running or not BOT_TOKEN:
|
| 105 |
add_bot_log("[Bot] Already running or token missing.")
|
| 106 |
return
|
|
|
|
| 156 |
gr.Markdown("## π₯οΈ Interactive Terminal")
|
| 157 |
|
| 158 |
with gr.Row():
|
| 159 |
+
terminal_output = gr.Textbox(label="π Terminal Output", lines=25, interactive=False)
|
| 160 |
with gr.Row():
|
| 161 |
+
cmd_input = gr.Textbox(placeholder="Enter shell command (use 'ctrl+c' to cancel)", label="Command Input")
|
| 162 |
run_btn = gr.Button("βΆοΈ Run Command")
|
| 163 |
|
| 164 |
run_btn.click(fn=live_terminal, inputs=cmd_input, outputs=terminal_output)
|
|
|
|
| 168 |
gr.Markdown("## π€ Telegram Bot Controls")
|
| 169 |
|
| 170 |
with gr.Row():
|
| 171 |
+
bot_output = gr.Textbox(label="π€ Telegram Bot Logs", lines=25, interactive=False)
|
| 172 |
with gr.Row():
|
| 173 |
token_box = gr.Textbox(label="Bot Token", placeholder="Enter Telegram Bot Token")
|
| 174 |
with gr.Row():
|