Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from fastapi import FastAPI, Request, Form
|
| 5 |
+
from fastapi.responses import HTMLResponse
|
| 6 |
+
from fastapi.staticfiles import StaticFiles
|
| 7 |
+
from fastapi.templating import Jinja2Templates
|
| 8 |
+
from e2b_desktop import Sandbox
|
| 9 |
+
|
| 10 |
+
load_dotenv() # load .env if present
|
| 11 |
+
|
| 12 |
+
E2B_API_KEY = os.environ.get("E2B_API_KEY")
|
| 13 |
+
if not E2B_API_KEY:
|
| 14 |
+
raise ValueError("E2B_API_KEY is not set. See env.example.")
|
| 15 |
+
|
| 16 |
+
app = FastAPI()
|
| 17 |
+
templates = Jinja2Templates(directory="templates")
|
| 18 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 19 |
+
|
| 20 |
+
desktop = None
|
| 21 |
+
vnc_url = None
|
| 22 |
+
vnc_host_port = None
|
| 23 |
+
|
| 24 |
+
def extract_host_port(url):
|
| 25 |
+
m = re.search(r"https?://([^/:]+)(?::(\d+))?", url)
|
| 26 |
+
if m:
|
| 27 |
+
host = m.group(1)
|
| 28 |
+
port = m.group(2) or "5900"
|
| 29 |
+
return f"{host}:{port}"
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
@app.get("/", response_class=HTMLResponse)
|
| 33 |
+
async def index(request: Request):
|
| 34 |
+
return templates.TemplateResponse("index.html", {
|
| 35 |
+
"request": request,
|
| 36 |
+
"vnc_url": vnc_url or "",
|
| 37 |
+
"vnc_host_port": vnc_host_port or "",
|
| 38 |
+
"status_msg": "Idle" if not desktop else f"Running: {vnc_host_port}"
|
| 39 |
+
})
|
| 40 |
+
|
| 41 |
+
@app.post("/start")
|
| 42 |
+
async def start():
|
| 43 |
+
global desktop, vnc_url, vnc_host_port
|
| 44 |
+
if desktop is None:
|
| 45 |
+
desktop = Sandbox(api_key=E2B_API_KEY)
|
| 46 |
+
desktop.stream.start(require_auth=False)
|
| 47 |
+
vnc_url = desktop.stream.get_url()
|
| 48 |
+
vnc_host_port = extract_host_port(vnc_url)
|
| 49 |
+
return {"iframe": vnc_url, "status": f"✅ VNC Started — {vnc_host_port}"}
|
| 50 |
+
return {"iframe": vnc_url, "status": f"⚠️ Already Running — {vnc_host_port}"}
|
| 51 |
+
|
| 52 |
+
@app.post("/stop")
|
| 53 |
+
async def stop():
|
| 54 |
+
global desktop, vnc_url, vnc_host_port
|
| 55 |
+
if desktop:
|
| 56 |
+
desktop.kill()
|
| 57 |
+
desktop = None
|
| 58 |
+
vnc_url = None
|
| 59 |
+
vnc_host_port = None
|
| 60 |
+
return {"iframe": "", "status": "✅ VNC Stopped"}
|
| 61 |
+
return {"iframe": "", "status": "⚠️ Nothing to Stop"}
|
| 62 |
+
|
| 63 |
+
@app.post("/send_text")
|
| 64 |
+
async def send_text(teks: str = Form(...)):
|
| 65 |
+
global desktop
|
| 66 |
+
if not teks.strip():
|
| 67 |
+
return {"msg": "⚠️ Teks kosong"}
|
| 68 |
+
if not desktop:
|
| 69 |
+
return {"msg": "❌ VNC belum berjalan"}
|
| 70 |
+
try:
|
| 71 |
+
desktop.write(teks)
|
| 72 |
+
desktop.press("enter")
|
| 73 |
+
return {"msg": f"✅ Teks '{teks}' berhasil dikirim"}
|
| 74 |
+
except Exception as e:
|
| 75 |
+
return {"msg": f"Error: {e}"}
|