Spaces:
Sleeping
Sleeping
| import os, subprocess, threading, time, psutil | |
| from flask import Flask, render_template_string, request, jsonify | |
| app = Flask(__name__) | |
| # دالة لجلب معلومات النظام (RAM, CPU, Network) | |
| def get_system_stats(): | |
| try: | |
| ram = psutil.virtual_memory() | |
| # [span_1](start_span)حساب الرام المتوفرة والمستخدمة[span_1](end_span) | |
| ram_usage = f"{ram.used / (1024**3):.2f}/{ram.total / (1024**3):.2f} GB ({ram.percent}%)" | |
| cpu_usage = f"{psutil.cpu_percent(interval=None)}%" | |
| # [span_2](start_span)قياس سرعة الشبكة في السيرفر[span_2](end_span) | |
| net_io_1 = psutil.net_io_counters() | |
| time.sleep(0.2) | |
| net_io_2 = psutil.net_io_counters() | |
| download_speed = (net_io_2.bytes_recv - net_io_1.bytes_recv) * 5 / (1024 * 1024) # MB/s | |
| upload_speed = (net_io_2.bytes_sent - net_io_1.bytes_sent) * 5 / (1024 * 1024) # MB/s | |
| return { | |
| "ram": ram_usage, | |
| "cpu": cpu_usage, | |
| "down": f"{download_speed:.2f} MB/s", | |
| "up": f"{upload_speed:.2f} MB/s" | |
| } | |
| except: | |
| return {"ram": "N/A", "cpu": "N/A", "down": "0 MB/s", "up": "0 MB/s"} | |
| def index(): | |
| return render_template_string(""" | |
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>AJIJ Terminal V3 - Pro</title> | |
| <style> | |
| body { background: #0a0a0a; color: #00ff41; font-family: 'Consolas', monospace; padding: 20px; margin: 0; } | |
| .stats-bar { | |
| display: flex; gap: 25px; background: #161616; padding: 15px; | |
| border: 1px solid #333; border-radius: 8px; margin-bottom: 20px; font-size: 0.9em; | |
| box-shadow: 0 4px 15px rgba(0,0,0,0.5); | |
| } | |
| .stat-item { color: #888; text-transform: uppercase; letter-spacing: 1px; } | |
| .stat-value { color: #00ff41; font-weight: bold; } | |
| #terminal { | |
| background: #000; border: 1px solid #444; height: 500px; | |
| overflow-y: auto; padding: 20px; border-radius: 10px; | |
| box-shadow: inset 0 0 10px #000; margin-bottom: 20px; | |
| line-height: 1.5; | |
| } | |
| .input-box { display: flex; gap: 12px; background: #111; padding: 10px; border-radius: 8px; border: 1px solid #333; } | |
| input { | |
| background: transparent; border: none; color: #fff; | |
| flex-grow: 1; padding: 10px; outline: none; font-size: 1.1em; font-family: inherit; | |
| } | |
| button { | |
| background: #00ff41; color: #000; border: none; | |
| padding: 0 30px; cursor: pointer; font-weight: bold; border-radius: 5px; | |
| transition: 0.3s; | |
| } | |
| button:hover { background: #00cc33; transform: scale(1.05); } | |
| .cmd { color: #00b8ff; margin-top: 15px; font-weight: bold; } | |
| .res { color: #f0f0f0; white-space: pre-wrap; margin-bottom: 20px; padding-left: 15px; border-left: 1px solid #333; font-size: 0.95em; } | |
| .sys-msg { color: #ffae00; font-style: italic; } | |
| </style> | |
| </head> | |
| <body> | |
| <div class="stats-bar"> | |
| <div class="stat-item">RAM: <span id="ram" class="stat-value">--</span></div> | |
| <div class="stat-item">CPU: <span id="cpu" class="stat-value">--</span></div> | |
| <div class="stat-item">Net Down: <span id="down" class="stat-value">0 MB/s</span></div> | |
| <div class="stat-item">Net Up: <span id="up" class="stat-value">0 MB/s</span></div> | |
| </div> | |
| <h2 style="margin: 0 0 15px 5px;">💻 AJIJ SUPER TERMINAL <span style="color:#888; font-size:0.5em;">v3.0.1</span></h2> | |
| <div id="terminal"> | |
| <div class="sys-msg">Connecting to 16GB RAM Cluster... [OK]</div> | |
| [span_3](start_span)<div class="sys-msg">Environment: Python 3.10-slim / Debian[span_3](end_span)</div> | |
| <div class="res">Type 'help' or any Linux command to start.</div> | |
| </div> | |
| <div class="input-box"> | |
| <span style="padding-top:10px; color:#888;">$</span> | |
| <input type="text" id="command" placeholder="Enter Linux command (e.g. ls, pip, curl)..." autofocus> | |
| <button onclick="runCmd()">RUN</button> | |
| </div> | |
| <script> | |
| function updateStats() { | |
| fetch('/stats').then(res => res.json()).then(data => { | |
| document.getElementById('ram').innerText = data.ram; | |
| document.getElementById('cpu').innerText = data.cpu; | |
| document.getElementById('down').innerText = data.down; | |
| document.getElementById('up').innerText = data.up; | |
| }); | |
| } | |
| setInterval(updateStats, 2000); | |
| function runCmd() { | |
| const cmdInput = document.getElementById('command'); | |
| const cmd = cmdInput.value; | |
| if(!cmd) return; | |
| const term = document.getElementById('terminal'); | |
| term.innerHTML += `<div class="cmd">admin@ajij:~# ${cmd}</div>`; | |
| fetch('/exec', { | |
| method: 'POST', | |
| headers: {'Content-Type': 'application/json'}, | |
| body: JSON.stringify({command: cmd}) | |
| }) | |
| .then(res => res.json()) | |
| .then(data => { | |
| term.innerHTML += `<div class="res">${data.output}</div>`; | |
| term.scrollTop = term.scrollHeight; | |
| cmdInput.value = ''; | |
| }); | |
| } | |
| document.getElementById('command').addEventListener('keypress', (e) => { if (e.key === 'Enter') runCmd(); }); | |
| </script> | |
| </body> | |
| </html> | |
| """) | |
| def stats(): | |
| return jsonify(get_system_stats()) | |
| def execute(): | |
| data = request.json | |
| command = data.get('command') | |
| # حماية من الأوامر الفارغة | |
| if not command.strip(): | |
| return jsonify({'output': ''}) | |
| try: | |
| # [span_4](start_span)تنفيذ الأمر في بيئة السيرفر مع دمج الأخطاء والمخرجات[span_4](end_span) | |
| process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) | |
| output, _ = process.communicate(timeout=60) # مهلة دقيقة للأوامر الطويلة | |
| except subprocess.TimeoutExpired: | |
| output = "Error: Command timed out (60s limit)." | |
| except Exception as e: | |
| output = str(e) | |
| if not output: output = "(Done / No Output)" | |
| return jsonify({'output': output}) | |
| if __name__ == "__main__": | |
| # [span_5](start_span)تشغيل السيرفر على البورت الخاص بـ Hugging Face[span_5](end_span) | |
| app.run(host='0.0.0.0', port=7860) | |