File size: 7,250 Bytes
1e0c9d6
 
ba34f84
 
17ce298
0a26dc5
17ce298
fad3637
3b4e567
1e0c9d6
 
7ce64c9
fad3637
0a26dc5
1e0c9d6
0a26dc5
7723ce0
 
0a26dc5
 
 
17ce298
3b4e567
 
1e0c9d6
3b4e567
 
 
 
 
 
 
 
 
fad3637
7723ce0
 
3b4e567
7723ce0
17ce298
7723ce0
 
 
 
17ce298
 
3b4e567
17ce298
fad3637
17ce298
 
fad3637
 
 
 
 
 
 
 
 
 
 
 
17ce298
 
 
3b4e567
 
 
 
 
 
 
 
 
 
 
 
 
17ce298
3b4e567
17ce298
 
 
 
 
 
 
3b4e567
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9261eb
3b4e567
6918ea1
3b4e567
e9261eb
 
 
 
17ce298
 
 
 
 
 
6918ea1
17ce298
6918ea1
7723ce0
17ce298
7723ce0
17ce298
 
 
0a26dc5
fad3637
0a26dc5
17ce298
0a26dc5
 
 
6918ea1
 
0a26dc5
e9261eb
0a26dc5
6918ea1
 
 
e9261eb
0a26dc5
 
e9261eb
6918ea1
e9261eb
 
6918ea1
 
0a26dc5
 
 
6918ea1
0a26dc5
 
 
e9261eb
17ce298
0a26dc5
eceb86e
0a26dc5
e9261eb
6918ea1
17ce298
e9261eb
 
67cfbd5
e9261eb
67cfbd5
3b4e567
6918ea1
 
3b4e567
7723ce0
 
0a26dc5
17ce298
 
 
 
 
 
 
 
 
3b4e567
9f65335
17ce298
 
1e0c9d6
17ce298
fad3637
17ce298
fad3637
17ce298
0a26dc5
17ce298
68e716f
0a26dc5
17ce298
 
1e0c9d6
17ce298
fad3637
17ce298
 
 
 
 
7723ce0
17ce298
 
 
 
 
1e0c9d6
17ce298
ba34f84
17ce298
 
 
ba34f84
17ce298
68e716f
ba34f84
3b4e567
ba34f84
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import gradio as gr
import subprocess
import asyncio
import threading
from queue import Queue
import os
import shlex
import signal
import glob
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes

BOT_TOKEN = ""

log_queue = Queue()
MAX_LOGS = 20000
terminal_logs = []
bot_logs = []
bot_app = None
bot_running = False
bot_thread = None
current_dir = os.getcwd()
current_proc = None  # Track running process
LOG_FILE = "terminal_logs.txt"

# Load previous logs if available
if os.path.exists(LOG_FILE):
    with open(LOG_FILE, "r") as f:
        terminal_logs = f.read().splitlines()[-MAX_LOGS:]

# Logging
def save_log_to_file(entry):
    with open(LOG_FILE, "a") as f:
        f.write(entry + "\n")

def add_terminal_log(entry):
    terminal_logs.append(entry)
    save_log_to_file(entry)
    if len(terminal_logs) > MAX_LOGS:
        terminal_logs.pop(0)

def add_bot_log(entry):
    bot_logs.append(entry)
    if len(bot_logs) > MAX_LOGS:
        bot_logs.pop(0)

# Command Execution
def execute_command(cmd):
    global current_dir, current_proc
    output = []
    add_terminal_log(f"$ {cmd}")

    if cmd.strip() == "ctrl+c":
        if current_proc:
            current_proc.send_signal(signal.SIGINT)
            msg = "[Info] Sent Ctrl+C to running process."
            add_terminal_log(msg)
            return msg
        else:
            msg = "[Info] No running process to interrupt."
            add_terminal_log(msg)
            return msg

    if cmd.strip().startswith("cd"):
        parts = shlex.split(cmd)
        if len(parts) > 1:
            pattern = os.path.join(current_dir, parts[1])
            matches = glob.glob(pattern)
            if matches:
                new_path = os.path.abspath(matches[0])
                if os.path.isdir(new_path):
                    current_dir = new_path
                    msg = f"Changed directory to {current_dir}"
                    output.append(msg)
                    add_terminal_log(msg)
                else:
                    msg = f"Not a directory: {new_path}"
                    output.append(msg)
                    add_terminal_log(msg)
            else:
                msg = f"No matching path for: {parts[1]}"
                output.append(msg)
                add_terminal_log(msg)
        else:
            msg = "Usage: cd <path>"
            output.append(msg)
            add_terminal_log(msg)
    else:
        try:
            current_proc = subprocess.Popen(
                cmd,
                cwd=current_dir,
                shell=True,
                stdout=subprocess.PIPE,
                stderr=subprocess.STDOUT,
                text=True,
                bufsize=1
            )
            for line in iter(current_proc.stdout.readline, ''):
                line = line.strip()
                if line:
                    output.append(line)
                    add_terminal_log(line)
            current_proc.stdout.close()
            current_proc.wait()
        except Exception as e:
            error_msg = f"[Error] {e}"
            output.append(error_msg)
            add_terminal_log(error_msg)
        finally:
            current_proc = None

    return "\n".join(output)

# Telegram Bot
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    await update.message.reply_text("Hello! I'm your terminal bot.\nUse /bash <command> to execute shell commands.")
    add_bot_log("[Bot] Received /start command.")

async def bash_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    cmd = ' '.join(context.args)
    if not cmd:
        await update.message.reply_text("Usage: /bash <command>")
        return

    add_bot_log(f"[Bot] $ {cmd}")
    try:
        result = execute_command(cmd)
    except Exception as e:
        result = f"Error: {e}"

    add_bot_log(result)
    await update.message.reply_text(f"$ {cmd}\n{result}")

def start_bot():
    global bot_app, bot_running, bot_thread, BOT_TOKEN
    if bot_running or not BOT_TOKEN:
        add_bot_log("[Bot] Already running or token missing.")
        return

    async def run_bot():
        global bot_app, bot_running
        add_bot_log("[Bot] Starting bot...")
        bot_app = ApplicationBuilder().token(BOT_TOKEN).build()
        bot_app.add_handler(CommandHandler("start", start_command))
        bot_app.add_handler(CommandHandler("bash", bash_command))
        bot_running = True
        await bot_app.run_polling()
        add_bot_log("[Bot] Bot stopped.")
        bot_running = False

    def runner():
        try:
            asyncio.run(run_bot())
        except Exception as e:
            add_bot_log(f"[Bot Error] {e}")
            global bot_running
            bot_running = False

    bot_thread = threading.Thread(target=runner, daemon=True)
    bot_thread.start()
    add_bot_log("[Bot] Bot thread started.")

def stop_bot():
    global bot_app, bot_running
    if not bot_running or not bot_app:
        add_bot_log("[Bot] Bot not running.")
        return

    async def shutdown():
        await bot_app.shutdown()
        await bot_app.stop()
        add_bot_log("[Bot] Bot stopped.")
        global bot_running
        bot_running = False

    threading.Thread(target=lambda: asyncio.run(shutdown()), daemon=True).start()

# UI Hooks
def update_bot_logs():
    return "\n".join(bot_logs[-100:])

def update_terminal_logs():
    return "\n".join(terminal_logs[-100:])

def live_terminal(cmd):
    result = [f"$ {cmd}"]
    try:
        result.append(execute_command(cmd))
    except Exception as e:
        result.append(f"[Error] {e}")
    result.append(f"$ {current_dir} >")
    return "\n".join(result)

# Gradio UI
with gr.Blocks() as demo:
    with gr.Tab("πŸ’» Terminal"):
        gr.Markdown("## πŸ–₯️ Interactive Terminal")

        with gr.Row():
            terminal_output = gr.Textbox(label="πŸ“Ÿ Terminal Output", lines=25, interactive=False)
        with gr.Row():
            cmd_input = gr.Textbox(placeholder="Enter shell command (use 'ctrl+c' to cancel)", label="Command Input")
            run_btn = gr.Button("▢️ Run Command")

        run_btn.click(fn=live_terminal, inputs=cmd_input, outputs=terminal_output)
        demo.load(fn=update_terminal_logs, outputs=terminal_output)

    with gr.Tab("πŸ€– Telegram Bot"):
        gr.Markdown("## πŸ€– Telegram Bot Controls")

        with gr.Row():
            bot_output = gr.Textbox(label="πŸ€– Telegram Bot Logs", lines=25, interactive=False)
        with gr.Row():
            token_box = gr.Textbox(label="Bot Token", placeholder="Enter Telegram Bot Token")
        with gr.Row():
            start_btn = gr.Button("πŸš€ Start Telegram Bot")
            stop_btn = gr.Button("πŸ›‘ Stop Telegram Bot")

        def set_token_and_start(token):
            global BOT_TOKEN
            BOT_TOKEN = token
            start_bot()
            return update_bot_logs()

        start_btn.click(fn=set_token_and_start, inputs=token_box, outputs=bot_output)

        def handle_stop():
            stop_bot()
            return update_bot_logs()

        stop_btn.click(fn=handle_stop, outputs=bot_output)
        demo.load(fn=update_bot_logs, outputs=bot_output)

# Launch App
demo.launch()