Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,44 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
| 2 |
import subprocess
|
| 3 |
-
from playwright.sync_api import sync_playwright
|
| 4 |
-
import base64
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
@app.post("/terminal")
|
| 11 |
-
def
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
page.goto(url, timeout=60000)
|
| 31 |
-
screenshot = page.screenshot()
|
| 32 |
-
browser.close()
|
| 33 |
-
|
| 34 |
-
return {
|
| 35 |
-
"screenshot": base64.b64encode(screenshot).decode()
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
# -------- SCREENSHOT OF SYSTEM --------
|
| 39 |
-
@app.get("/screen")
|
| 40 |
-
def screen():
|
| 41 |
-
os.system("apt-get update && apt-get install -y scrot")
|
| 42 |
-
os.system("scrot screen.png")
|
| 43 |
-
|
| 44 |
-
with open("screen.png","rb") as f:
|
| 45 |
return {
|
| 46 |
-
"
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
import subprocess
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
|
| 8 |
+
@app.get("/")
|
| 9 |
+
def root():
|
| 10 |
+
return {"status": "ok", "message": "Terminal API is running"}
|
| 11 |
+
|
| 12 |
+
|
| 13 |
@app.post("/terminal")
|
| 14 |
+
async def run_terminal(req: Request):
|
| 15 |
+
data = await req.json()
|
| 16 |
+
cmd = data.get("cmd")
|
| 17 |
+
|
| 18 |
+
if not cmd:
|
| 19 |
+
return JSONResponse(
|
| 20 |
+
status_code=400,
|
| 21 |
+
content={"error": "cmd field is required"}
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
result = subprocess.run(
|
| 26 |
+
cmd,
|
| 27 |
+
shell=True,
|
| 28 |
+
capture_output=True,
|
| 29 |
+
text=True,
|
| 30 |
+
timeout=30
|
| 31 |
+
)
|
| 32 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return {
|
| 34 |
+
"command": cmd,
|
| 35 |
+
"stdout": result.stdout,
|
| 36 |
+
"stderr": result.stderr,
|
| 37 |
+
"returncode": result.returncode
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return JSONResponse(
|
| 42 |
+
status_code=500,
|
| 43 |
+
content={"error": str(e)}
|
| 44 |
+
)
|