userbymahadi commited on
Commit
5fc6cc4
·
verified ·
1 Parent(s): 49aaee2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -18
app.py CHANGED
@@ -2,15 +2,12 @@ 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():
@@ -19,25 +16,26 @@ async def get():
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
 
@@ -45,24 +43,28 @@ async def list_files(path: str = "."):
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
 
2
  import asyncio
3
  import os
4
  import psutil
5
+ import shutil
6
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect
7
  from fastapi.responses import HTMLResponse, JSONResponse
 
8
 
9
  app = FastAPI()
10
+ ACCESS_PASSWORD = "admin" # আপনার পাসওয়ার্ড
 
 
11
 
12
  @app.get("/")
13
  async def get():
 
16
 
17
  @app.get("/api/metrics")
18
  async def get_metrics():
 
19
  return {
20
  "cpu": psutil.cpu_percent(),
21
  "ram": psutil.virtual_memory().percent,
22
+ "disk": psutil.disk_usage('/').percent,
23
+ "uptime": shutil.disk_usage("/").total // (1024**3) # Storage limit approx
24
  }
25
 
26
  @app.get("/api/files")
27
  async def list_files(path: str = "."):
 
28
  try:
29
+ abs_path = os.path.abspath(path)
30
  files = []
31
+ for entry in os.scandir(abs_path):
32
  files.append({
33
  "name": entry.name,
34
  "is_dir": entry.is_dir(),
35
+ "path": os.path.join(path, entry.name),
36
+ "size": f"{entry.stat().st_size / 1024:.1f} KB" if entry.is_file() else "-"
37
  })
38
+ return sorted(files, key=lambda x: not x['is_dir'])
39
  except Exception as e:
40
  return JSONResponse(status_code=500, content={"error": str(e)})
41
 
 
43
  async def websocket_endpoint(websocket: WebSocket):
44
  await websocket.accept()
45
  try:
46
+ auth = await websocket.receive_text()
47
+ if auth != ACCESS_PASSWORD:
48
+ await websocket.send_text("[-] AUTH_FAILED")
 
49
  await websocket.close()
50
  return
51
 
52
+ await websocket.send_text("[+] AUTH_SUCCESS")
53
 
54
  while True:
55
  command = await websocket.receive_text()
56
+ # ক্লিয়ার কমান্ড হ্যান্ডলিং
57
+ if command.strip() == "clear":
58
+ await websocket.send_text("\033[H\033[2J")
59
+ continue
60
+
61
  process = await asyncio.create_subprocess_shell(
62
  command,
63
  stdout=asyncio.subprocess.PIPE,
64
  stderr=asyncio.subprocess.PIPE
65
  )
66
  stdout, stderr = await process.communicate()
67
+ output = stdout.decode().strip() or stderr.decode().strip() or "Done."
68
  await websocket.send_text(output)
69
  except WebSocketDisconnect:
70
  pass