Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,39 @@
|
|
| 1 |
-
import os
|
| 2 |
import asyncio
|
| 3 |
-
import
|
| 4 |
-
from fastapi import FastAPI,
|
| 5 |
-
from fastapi.
|
|
|
|
|
|
|
| 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
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
@app.websocket("/ws")
|
| 24 |
-
async def websocket_endpoint(websocket: WebSocket
|
| 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 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|