userbymahadi commited on
Commit
ac2e4c6
·
verified ·
1 Parent(s): d705df7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -50
app.py CHANGED
@@ -1,62 +1,39 @@
1
- import os
2
  import asyncio
3
- import shutil
4
- from fastapi import FastAPI, HTTPException, Query, UploadFile, File, WebSocket, WebSocketDisconnect
5
- from fastapi.middleware.cors import CORSMiddleware
 
 
6
 
7
  app = FastAPI()
8
 
9
- app.add_middleware(
10
- CORSMiddleware,
11
- allow_origins=["*"],
12
- allow_methods=["*"],
13
- allow_headers=["*"],
14
- )
15
-
16
- # আপনার দেওয়া সর্বশেষ টোকেন
17
- SECURITY_TOKEN = os.getenv("SECURITY_TOKEN", "UseRBYMD@Mahadinewarrivepass2026")
18
-
19
  @app.get("/")
20
- async def root():
21
- return {"status": "running"}
 
22
 
23
  @app.websocket("/ws")
24
- async def websocket_endpoint(websocket: WebSocket, token: str = Query(...)):
25
- if token != SECURITY_TOKEN:
26
- print(f"Auth failed: {token}")
27
- await websocket.close(code=1008)
28
- return
29
-
30
  await websocket.accept()
31
  try:
32
  while True:
33
- cmd = await websocket.receive_text()
34
- process = await asyncio.create_subprocess_shell(
35
- cmd,
36
- stdout=asyncio.subprocess.PIPE,
37
- stderr=asyncio.subprocess.PIPE
 
 
 
38
  )
39
-
40
- async def stream(stream_obj):
41
- while True:
42
- line = await stream_obj.readline()
43
- if line:
44
- await websocket.send_text(line.decode())
45
- else:
46
- break
47
-
48
- await asyncio.gather(stream(process.stdout), stream(process.stderr))
49
- await websocket.send_text("\n[Command Finished]\n")
50
- except WebSocketDisconnect:
51
- pass
52
  except Exception as e:
53
- print(f"WS Error: {e}")
54
-
55
- @app.post("/upload")
56
- async def upload_file(token: str = Query(...), file: UploadFile = File(...)):
57
- if token != SECURITY_TOKEN:
58
- raise HTTPException(status_code=403)
59
- file_path = os.path.join(os.getcwd(), file.filename)
60
- with open(file_path, "wb") as f:
61
- shutil.copyfileobj(file.file, f)
62
- return {"message": "Success"}
 
 
1
  import asyncio
2
+ import subprocess
3
+ from fastapi import FastAPI, WebSocket
4
+ from fastapi.responses import HTMLResponse
5
+ from fastapi.staticfiles import StaticFiles
6
+ import os
7
 
8
  app = FastAPI()
9
 
 
 
 
 
 
 
 
 
 
 
10
  @app.get("/")
11
+ async def get():
12
+ with open("index.html", "r", encoding="utf-8") as f:
13
+ return HTMLResponse(content=f.read())
14
 
15
  @app.websocket("/ws")
16
+ async def websocket_endpoint(websocket: WebSocket):
 
 
 
 
 
17
  await websocket.accept()
18
  try:
19
  while True:
20
+ command = await websocket.receive_text()
21
+ # কমান্ড রান করা এবং আউটপুট সংগ্রহ
22
+ process = subprocess.Popen(
23
+ command,
24
+ shell=True,
25
+ stdout=subprocess.PIPE,
26
+ stderr=subprocess.PIPE,
27
+ text=True
28
  )
29
+
30
+ stdout, stderr = process.communicate()
31
+ output = stdout if stdout else stderr
32
+ if not output:
33
+ output = "Command executed (No output)"
34
+
35
+ await websocket.send_text(output)
 
 
 
 
 
 
36
  except Exception as e:
37
+ await websocket.send_text(f"Error: {str(e)}")
38
+ finally:
39
+ await websocket.close()