userbymahadi commited on
Commit
4fd9703
·
verified ·
1 Parent(s): d0bd98c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -27
app.py CHANGED
@@ -2,33 +2,26 @@ import subprocess
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():
14
  with open("index.html", "r", encoding="utf-8") as f:
15
  return HTMLResponse(content=f.read())
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(),
@@ -39,32 +32,48 @@ async def list_files(path: str = "."):
39
  except Exception as e:
40
  return JSONResponse(status_code=500, content={"error": str(e)})
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  @app.websocket("/ws")
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
 
2
  import asyncio
3
  import os
4
  import psutil
5
+ import httpx
6
+ from fastapi import FastAPI, WebSocket, WebSocketDisconnect, UploadFile, File
7
  from fastapi.responses import HTMLResponse, JSONResponse
8
+ from fastapi.staticfiles import StaticFiles
9
+ from typing import List
10
 
11
  app = FastAPI()
12
+ ACCESS_PASSWORD = "admin" # আপনার পাসওয়ার্ড পরিবর্তন করুন
13
 
14
  @app.get("/")
15
  async def get():
16
  with open("index.html", "r", encoding="utf-8") as f:
17
  return HTMLResponse(content=f.read())
18
 
19
+ # --- ফাইল ম্যানেজার API ---
 
 
 
 
 
 
 
 
20
  @app.get("/api/files")
21
  async def list_files(path: str = "."):
22
  try:
 
23
  files = []
24
+ for entry in os.scandir(path):
25
  files.append({
26
  "name": entry.name,
27
  "is_dir": entry.is_dir(),
 
32
  except Exception as e:
33
  return JSONResponse(status_code=500, content={"error": str(e)})
34
 
35
+ @app.post("/api/upload")
36
+ async def upload_file(file: UploadFile = File(...)):
37
+ with open(file.filename, "wb") as buffer:
38
+ shutil.copyfileobj(file.file, buffer)
39
+ return {"message": "Uploaded"}
40
+
41
+ @app.delete("/api/delete")
42
+ async def delete_file(path: str):
43
+ if os.path.isdir(path):
44
+ os.rmdir(path)
45
+ else:
46
+ os.remove(path)
47
+ return {"message": "Deleted"}
48
+
49
+ # --- ওয়েব প্রিভিউ প্রক্সি ---
50
+ @app.get("/proxy")
51
+ async def proxy(url: str = "http://localhost:8080"):
52
+ async with httpx.AsyncClient() as client:
53
+ try:
54
+ response = await client.get(url, timeout=5.0)
55
+ return HTMLResponse(content=response.text)
56
+ except Exception as e:
57
+ return HTMLResponse(content=f"<div style='color:red;padding:20px;'>Error: {str(e)}<br>নিশ্চিত করুন যে আপনার লোকাল সার্ভারটি ৮০৮০ পোর্টে রানিং আছে।</div>")
58
+
59
+ # --- ওয়েব সকেট টার্মিনাল ---
60
  @app.websocket("/ws")
61
  async def websocket_endpoint(websocket: WebSocket):
62
  await websocket.accept()
63
  try:
64
  auth = await websocket.receive_text()
65
  if auth != ACCESS_PASSWORD:
66
+ await websocket.send_text("AUTH_FAILED")
67
  await websocket.close()
68
  return
69
+ await websocket.send_text("AUTH_SUCCESS")
 
 
70
  while True:
71
  command = await websocket.receive_text()
 
 
 
 
 
72
  process = await asyncio.create_subprocess_shell(
73
+ command, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
 
 
74
  )
75
  stdout, stderr = await process.communicate()
76
+ output = stdout.decode().strip() or stderr.decode().strip() or "Executed."
77
  await websocket.send_text(output)
78
  except WebSocketDisconnect:
79
  pass