Samiran757 commited on
Commit
8cd8930
·
verified ·
1 Parent(s): 0640999

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -41
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
- # -------- TERMINAL TOOL --------
 
 
 
 
 
10
  @app.post("/terminal")
11
- def terminal(cmd: str):
12
- result = subprocess.run(
13
- cmd,
14
- shell=True,
15
- capture_output=True,
16
- text=True
17
- )
18
- return {
19
- "stdout": result.stdout,
20
- "stderr": result.stderr,
21
- "code": result.returncode
22
- }
23
-
24
- # -------- BROWSER TOOL --------
25
- @app.get("/browse")
26
- def browse(url: str):
27
- with sync_playwright() as p:
28
- browser = p.chromium.launch(headless=True)
29
- page = browser.new_page()
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
- "image": base64.b64encode(f.read()).decode()
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
+ )