Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,68 @@
|
|
| 1 |
import subprocess
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
<!DOCTYPE html>
|
| 10 |
-
<html lang="bn">
|
| 11 |
-
<head>
|
| 12 |
-
<meta charset="UTF-8">
|
| 13 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 14 |
-
<title>Modern Terminal</title>
|
| 15 |
-
<script src="https://cdn.tailwindcss.com"></script>
|
| 16 |
-
<style>
|
| 17 |
-
.glass { background: rgba(30, 41, 59, 0.8); backdrop-filter: blur(12px); border: 1px solid rgba(255,255,255,0.1); }
|
| 18 |
-
body { background: linear-gradient(135deg, #0f172a 0%, #1e293b 100%); min-height: 100vh; color: #f8fafc; }
|
| 19 |
-
</style>
|
| 20 |
-
</head>
|
| 21 |
-
<body class="p-4 md:p-10">
|
| 22 |
-
<div class="max-w-5xl mx-auto glass rounded-2xl shadow-2xl overflow-hidden">
|
| 23 |
-
<div class="bg-slate-800/50 p-4 border-b border-slate-700 flex justify-between items-center">
|
| 24 |
-
<div class="flex gap-2 items-center">
|
| 25 |
-
<div class="w-3 h-3 bg-red-500 rounded-full"></div>
|
| 26 |
-
<div class="w-3 h-3 bg-yellow-500 rounded-full"></div>
|
| 27 |
-
<div class="w-3 h-3 bg-green-500 rounded-full"></div>
|
| 28 |
-
<span class="ml-2 font-mono text-sm opacity-70">Terminal System v2.0</span>
|
| 29 |
-
</div>
|
| 30 |
-
</div>
|
| 31 |
-
|
| 32 |
-
<div id="output" class="h-[500px] overflow-y-auto p-6 font-mono text-sm space-y-3">
|
| 33 |
-
<div class="text-blue-400">● System Ready. Type your command below.</div>
|
| 34 |
-
</div>
|
| 35 |
-
|
| 36 |
-
<div class="p-4 bg-slate-900/50 border-t border-slate-700">
|
| 37 |
-
<div class="flex gap-2">
|
| 38 |
-
<span class="text-green-500 font-bold self-center">❯</span>
|
| 39 |
-
<input type="text" id="cmd" autofocus
|
| 40 |
-
class="w-full bg-transparent border-none outline-none focus:ring-0 text-blue-300 font-mono"
|
| 41 |
-
placeholder="ls -la / echo 'Hello World'">
|
| 42 |
-
<button onclick="send()" class="bg-blue-600 hover:bg-blue-500 px-5 py-2 rounded-lg text-sm font-bold transition-all">RUN</button>
|
| 43 |
-
</div>
|
| 44 |
-
</div>
|
| 45 |
-
</div>
|
| 46 |
-
|
| 47 |
-
<script>
|
| 48 |
-
// অটো প্রোটোকল ডিটেকশন (ws বা wss)
|
| 49 |
-
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
|
| 50 |
-
const socket = new WebSocket(`${protocol}//${window.location.host}/ws`);
|
| 51 |
-
|
| 52 |
-
socket.onmessage = function(event) {
|
| 53 |
-
const out = document.getElementById('output');
|
| 54 |
-
const entry = document.createElement('div');
|
| 55 |
-
entry.className = "bg-black/20 p-3 rounded border-l-2 border-blue-500 whitespace-pre-wrap font-mono text-gray-300";
|
| 56 |
-
entry.innerText = event.data;
|
| 57 |
-
out.appendChild(entry);
|
| 58 |
-
out.scrollTop = out.scrollHeight;
|
| 59 |
-
};
|
| 60 |
-
|
| 61 |
-
function send() {
|
| 62 |
-
const input = document.getElementById('cmd');
|
| 63 |
-
if (input.value) {
|
| 64 |
-
const myCmd = document.createElement('div');
|
| 65 |
-
myCmd.className = "text-green-400 font-bold mt-4";
|
| 66 |
-
myCmd.innerText = "> " + input.value;
|
| 67 |
-
document.getElementById('output').appendChild(myCmd);
|
| 68 |
-
|
| 69 |
-
socket.send(input.value);
|
| 70 |
-
input.value = '';
|
| 71 |
-
}
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
document.getElementById('cmd').addEventListener("keypress", e => {
|
| 75 |
-
if (e.key === "Enter") send();
|
| 76 |
-
});
|
| 77 |
-
</script>
|
| 78 |
-
</body>
|
| 79 |
-
</html>
|
| 80 |
-
"""
|
| 81 |
|
| 82 |
@app.get("/")
|
| 83 |
async def get():
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
@app.websocket("/ws")
|
| 87 |
async def websocket_endpoint(websocket: WebSocket):
|
| 88 |
await websocket.accept()
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
await websocket.send_text(output)
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
break
|
|
|
|
| 1 |
import subprocess
|
| 2 |
+
import asyncio
|
| 3 |
+
import os
|
| 4 |
+
import psutil
|
| 5 |
+
import json
|
| 6 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException, Depends
|
| 7 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 8 |
+
from fastapi.staticfiles import StaticFiles
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# সিকিউরিটি: এখানে আপনার নিজের পাসওয়ার্ড সেট করুন
|
| 13 |
+
ACCESS_PASSWORD = "admin"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@app.get("/")
|
| 16 |
async def get():
|
| 17 |
+
with open("index.html", "r", encoding="utf-8") as f:
|
| 18 |
+
return HTMLResponse(content=f.read())
|
| 19 |
+
|
| 20 |
+
@app.get("/api/metrics")
|
| 21 |
+
async def get_metrics():
|
| 22 |
+
# সিস্টেম রিসোর্স ডাটা
|
| 23 |
+
return {
|
| 24 |
+
"cpu": psutil.cpu_percent(),
|
| 25 |
+
"ram": psutil.virtual_memory().percent,
|
| 26 |
+
"disk": psutil.disk_usage('/').percent
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
@app.get("/api/files")
|
| 30 |
+
async def list_files(path: str = "."):
|
| 31 |
+
# ফাইল এক্সপ্লোরার ডাটা
|
| 32 |
+
try:
|
| 33 |
+
files = []
|
| 34 |
+
for entry in os.scandir(path):
|
| 35 |
+
files.append({
|
| 36 |
+
"name": entry.name,
|
| 37 |
+
"is_dir": entry.is_dir(),
|
| 38 |
+
"size": entry.stat().st_size if entry.is_file() else "-"
|
| 39 |
+
})
|
| 40 |
+
return files
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|
| 43 |
|
| 44 |
@app.websocket("/ws")
|
| 45 |
async def websocket_endpoint(websocket: WebSocket):
|
| 46 |
await websocket.accept()
|
| 47 |
+
try:
|
| 48 |
+
# প্রথম মেসেজটি পাসওয়ার্ড হতে হবে
|
| 49 |
+
auth_data = await websocket.receive_text()
|
| 50 |
+
if auth_data != ACCESS_PASSWORD:
|
| 51 |
+
await websocket.send_text("AUTH_FAILED")
|
| 52 |
+
await websocket.close()
|
| 53 |
+
return
|
| 54 |
+
|
| 55 |
+
await websocket.send_text("AUTH_SUCCESS")
|
| 56 |
+
|
| 57 |
+
while True:
|
| 58 |
+
command = await websocket.receive_text()
|
| 59 |
+
process = await asyncio.create_subprocess_shell(
|
| 60 |
+
command,
|
| 61 |
+
stdout=asyncio.subprocess.PIPE,
|
| 62 |
+
stderr=asyncio.subprocess.PIPE
|
| 63 |
+
)
|
| 64 |
+
stdout, stderr = await process.communicate()
|
| 65 |
+
output = stdout.decode().strip() or stderr.decode().strip() or "Success (No Output)"
|
| 66 |
await websocket.send_text(output)
|
| 67 |
+
except WebSocketDisconnect:
|
| 68 |
+
pass
|
|
|