File size: 17,955 Bytes
09d0929
 
e8ef6cf
09d0929
 
 
 
 
 
 
 
 
 
 
 
 
e8ef6cf
09d0929
 
 
 
 
e8ef6cf
09d0929
 
 
 
 
 
 
 
 
e8ef6cf
 
 
 
09d0929
 
e8ef6cf
 
 
 
 
 
 
 
 
 
 
09d0929
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ef6cf
 
 
09d0929
 
 
 
 
 
 
 
 
 
e8ef6cf
09d0929
 
 
 
 
 
e8ef6cf
09d0929
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ef6cf
09d0929
 
 
e8ef6cf
09d0929
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ef6cf
 
09d0929
 
 
 
 
e8ef6cf
09d0929
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8ef6cf
09d0929
 
 
 
 
 
 
 
 
 
 
e8ef6cf
09d0929
 
 
 
 
 
 
 
 
e8ef6cf
09d0929
 
 
 
e8ef6cf
 
 
 
 
 
 
 
09d0929
e8ef6cf
09d0929
 
 
 
 
 
 
 
e8ef6cf
 
 
09d0929
 
 
 
 
 
 
 
e8ef6cf
09d0929
 
 
e8ef6cf
 
 
 
09d0929
 
 
 
e8ef6cf
 
09d0929
 
 
e8ef6cf
09d0929
 
e8ef6cf
09d0929
 
e8ef6cf
09d0929
e8ef6cf
09d0929
e8ef6cf
09d0929
e8ef6cf
09d0929
 
 
 
 
e8ef6cf
 
 
 
 
09d0929
 
 
 
 
 
e8ef6cf
09d0929
 
 
 
 
 
 
 
e8ef6cf
09d0929
 
 
e8ef6cf
09d0929
 
 
 
 
 
 
 
 
 
e8ef6cf
 
09d0929
 
 
 
e8ef6cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09d0929
 
 
e8ef6cf
 
09d0929
e8ef6cf
09d0929
e8ef6cf
09d0929
 
 
 
e8ef6cf
09d0929
e8ef6cf
 
 
 
09d0929
 
e8ef6cf
 
09d0929
e8ef6cf
 
09d0929
 
 
e8ef6cf
09d0929
e8ef6cf
 
 
09d0929
 
 
e8ef6cf
09d0929
e8ef6cf
 
 
09d0929
 
e8ef6cf
 
 
 
09d0929
e8ef6cf
 
 
2eb637d
e8ef6cf
 
 
 
 
 
2eb637d
e8ef6cf
 
 
2eb637d
e8ef6cf
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
"""
TreyOS Backend β€” FastAPI
Real Linux filesystem, WebSocket terminal, system stats, Playwright browser
"""

import asyncio
import json
import os
import shutil
import stat
import subprocess
import time
from datetime import datetime
from pathlib import Path

import psutil
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from watchfiles import awatch

app = FastAPI(title="TreyOS", version="1.0.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ── Path handling ─────────────────────────────────────────────────────────────
# TREY_ROOT jails access. Default "/" means full filesystem access.
# The jail check is intentionally lenient β€” on HF the whole container is fair game.
ROOT_JAIL = Path(os.environ.get("TREY_ROOT", "/")).resolve()

def safe_path(requested: str) -> Path:
    """Resolve path. If absolute, use as-is (within jail). If relative, join to ROOT_JAIL."""
    requested = requested.strip()
    if requested.startswith("/"):
        p = Path(requested).resolve()
    else:
        p = (ROOT_JAIL / requested).resolve()
    # Jail check
    try:
        p.relative_to(ROOT_JAIL)
    except ValueError:
        raise HTTPException(403, f"Access denied: {p} is outside jail {ROOT_JAIL}")
    return p

def entry_info(p: Path) -> dict:
    try:
        s = p.stat()
        return {
            "name": p.name,
            "path": str(p),
            "type": "dir" if p.is_dir() else "file",
            "size": s.st_size,
            "modified": datetime.fromtimestamp(s.st_mtime).isoformat(),
            "permissions": oct(stat.S_IMODE(s.st_mode)),
            "extension": p.suffix.lower() if p.is_file() else None,
        }
    except (PermissionError, OSError):
        return {"name": p.name, "path": str(p), "type": "unknown",
                "size": 0, "modified": None, "permissions": None, "extension": None}


# ── Filesystem API ────────────────────────────────────────────────────────────

@app.get("/api/files")
async def list_files(path: str = "/", show_hidden: bool = False):
    p = safe_path(path)
    if not p.exists():
        raise HTTPException(404, f"Path not found: {path}")
    if not p.is_dir():
        raise HTTPException(400, "Not a directory")
    try:
        entries = sorted(p.iterdir(), key=lambda x: (not x.is_dir(), x.name.lower()))
        if not show_hidden:
            entries = [e for e in entries if not e.name.startswith(".")]
        return {
            "path": str(p),
            "parent": str(p.parent) if str(p) != "/" else None,
            "entries": [entry_info(e) for e in entries],
            "count": len(entries),
        }
    except PermissionError:
        raise HTTPException(403, "Permission denied")


@app.get("/api/files/read")
async def read_file(path: str):
    p = safe_path(path)
    if not p.is_file():
        raise HTTPException(404, "File not found")
    try:
        return {"path": str(p), "content": p.read_text(errors="replace"), "size": p.stat().st_size}
    except Exception as e:
        raise HTTPException(500, str(e))


@app.post("/api/files/write")
async def write_file(path: str, content: str = ""):
    p = safe_path(path)
    p.parent.mkdir(parents=True, exist_ok=True)
    p.write_text(content)
    return {"ok": True, "path": str(p), "size": p.stat().st_size}


@app.post("/api/files/mkdir")
async def make_dir(path: str):
    p = safe_path(path)
    p.mkdir(parents=True, exist_ok=True)
    return {"ok": True, "path": str(p)}


@app.delete("/api/files/delete")
async def delete_entry(path: str):
    p = safe_path(path)
    if not p.exists():
        raise HTTPException(404, "Not found")
    if p.is_dir():
        shutil.rmtree(p)
    else:
        p.unlink()
    return {"ok": True, "deleted": str(p)}


@app.post("/api/files/rename")
async def rename_entry(path: str, new_name: str):
    p = safe_path(path)
    dest = p.parent / new_name
    p.rename(dest)
    return {"ok": True, "path": str(dest)}


@app.post("/api/files/copy")
async def copy_entry(src: str, dst: str):
    s, d = safe_path(src), safe_path(dst)
    shutil.copytree(s, d) if s.is_dir() else shutil.copy2(s, d)
    return {"ok": True, "destination": str(d)}


@app.post("/api/files/move")
async def move_entry(src: str, dst: str):
    s, d = safe_path(src), safe_path(dst)
    shutil.move(str(s), str(d))
    return {"ok": True, "destination": str(d)}


@app.post("/api/files/upload")
async def upload_file(path: str, file: UploadFile = File(...)):
    dest = safe_path(path) / file.filename
    dest.parent.mkdir(parents=True, exist_ok=True)
    with open(dest, "wb") as f:
        f.write(await file.read())
    return {"ok": True, "path": str(dest), "size": dest.stat().st_size}


@app.get("/api/files/download")
async def download_file(path: str):
    p = safe_path(path)
    if not p.is_file():
        raise HTTPException(404, "File not found")
    return FileResponse(str(p), filename=p.name)


@app.get("/api/files/search")
async def search_files(path: str = "/", query: str = "", limit: int = 50):
    p = safe_path(path)
    results = []
    try:
        for entry in p.rglob(f"*{query}*"):
            results.append(entry_info(entry))
            if len(results) >= limit:
                break
    except (PermissionError, OSError):
        pass
    return {"query": query, "results": results, "count": len(results)}


# ── System Stats ──────────────────────────────────────────────────────────────

@app.get("/api/system")
async def system_stats():
    disk = psutil.disk_usage("/")
    net  = psutil.net_io_counters()
    uptime_s = int(time.time() - psutil.boot_time())
    h, r = divmod(uptime_s, 3600)
    m = r // 60
    return {
        "cpu":    {"percent": psutil.cpu_percent(interval=0.1), "cores": psutil.cpu_count(),
                   "freq_mhz": psutil.cpu_freq().current if psutil.cpu_freq() else None},
        "memory": {"total_gb": round(psutil.virtual_memory().total/1e9,1),
                   "used_gb":  round(psutil.virtual_memory().used/1e9,1),
                   "percent":  psutil.virtual_memory().percent},
        "disk":   {"total_gb": round(disk.total/1e9,1), "used_gb": round(disk.used/1e9,1),
                   "free_gb":  round(disk.free/1e9,1),  "percent": disk.percent},
        "network":{"bytes_sent_mb": round(net.bytes_sent/1e6,1), "bytes_recv_mb": round(net.bytes_recv/1e6,1)},
        "uptime": f"{h}h {m}m",
        "boot_time": datetime.fromtimestamp(psutil.boot_time()).isoformat(),
        "hostname": os.uname().nodename,
        "os": f"{os.uname().sysname} {os.uname().release}",
    }


@app.get("/api/processes")
async def list_processes(limit: int = 20):
    procs = []
    for p in psutil.process_iter(["pid","name","cpu_percent","memory_percent","status"]):
        try: procs.append(p.info)
        except (psutil.NoSuchProcess, psutil.AccessDenied): pass
    procs.sort(key=lambda x: x.get("cpu_percent", 0), reverse=True)
    return {"processes": procs[:limit]}


# ── WebSocket Terminal ────────────────────────────────────────────────────────

class TerminalSession:
    def __init__(self, cwd: str = "/"):
        self.cwd = cwd if Path(cwd).is_dir() else "/"
        self.env = {**os.environ, "TERM": "xterm-256color"}

    async def run(self, command: str, ws: WebSocket):
        cmd = command.strip()
        # Handle cd internally
        if cmd.startswith("cd"):
            target = cmd[2:].strip() or str(Path.home())
            new_cwd = str((Path(self.cwd) / target).resolve())
            if os.path.isdir(new_cwd):
                self.cwd = new_cwd
            else:
                await ws.send_json({"type":"output","data":f"cd: {target}: No such directory\r\n"})
            await ws.send_json({"type":"prompt","cwd":self.cwd})
            return
        try:
            proc = await asyncio.create_subprocess_shell(
                cmd,
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.STDOUT,
                cwd=self.cwd, env=self.env,
            )
            async for chunk in proc.stdout:
                await ws.send_json({"type":"output","data":chunk.decode(errors="replace")})
            await proc.wait()
            await ws.send_json({"type":"exit","code":proc.returncode,"cwd":self.cwd})
        except Exception as e:
            await ws.send_json({"type":"error","data":str(e)})
        finally:
            await ws.send_json({"type":"prompt","cwd":self.cwd})


@app.websocket("/ws/terminal")
async def terminal_ws(ws: WebSocket, cwd: str = "/"):
    await ws.accept()
    # Use ROOT_JAIL as default cwd if it exists, else /
    default_cwd = str(ROOT_JAIL) if ROOT_JAIL.is_dir() else "/"
    session = TerminalSession(cwd=cwd if Path(cwd).is_dir() else default_cwd)
    await ws.send_json({"type":"welcome","message":"TreyOS Terminal Ready","cwd":session.cwd})
    await ws.send_json({"type":"prompt","cwd":session.cwd})
    try:
        while True:
            msg = await ws.receive_json()
            if msg.get("type") == "command":
                await session.run(msg["data"], ws)
            elif msg.get("type") == "ping":
                await ws.send_json({"type":"pong"})
    except WebSocketDisconnect:
        pass


@app.websocket("/ws/watch")
async def watch_ws(ws: WebSocket, path: str = "/"):
    await ws.accept()
    p = safe_path(path)
    await ws.send_json({"type":"watching","path":str(p)})
    try:
        async for changes in awatch(str(p)):
            events = [{"change": str(c[0].name), "path": c[1]} for c in changes]
            await ws.send_json({"type":"changes","events":events})
    except WebSocketDisconnect:
        pass


@app.websocket("/ws/system")
async def system_ws(ws: WebSocket, interval: float = 2.0):
    await ws.accept()
    try:
        while True:
            stats = await system_stats()
            await ws.send_json({"type":"stats","data":stats})
            await asyncio.sleep(max(interval, 1.0))
    except WebSocketDisconnect:
        pass


# ── Playwright Browser Proxy ──────────────────────────────────────────────────
# Streams a real Chromium screenshot as JPEG for each navigation.
# Install: pip install playwright && playwright install chromium

_browser = None
_playwright = None

async def get_browser():
    global _browser, _playwright
    if _browser is None:
        try:
            from playwright.async_api import async_playwright
            _playwright = await async_playwright().start()
            _browser = await _playwright.chromium.launch(
                headless=True,
                args=["--no-sandbox","--disable-setuid-sandbox","--disable-dev-shm-usage",
                      "--disable-gpu","--single-process"]
            )
        except Exception as e:
            raise HTTPException(503, f"Playwright not available: {e}")
    return _browser


@app.get("/api/browser/screenshot")
async def browser_screenshot(url: str, width: int = 390, height: int = 700):
    """Take a screenshot of a URL and return it as JPEG."""
    if not url.startswith(("http://","https://")):
        url = "https://" + url
    try:
        browser = await get_browser()
        page = await browser.new_page(viewport={"width": width, "height": height})
        await page.goto(url, wait_until="domcontentloaded", timeout=20000)
        screenshot = await page.screenshot(type="jpeg", quality=80, full_page=False)
        await page.close()
        from fastapi.responses import Response
        return Response(content=screenshot, media_type="image/jpeg",
                        headers={"X-Final-URL": page.url if not page.is_closed() else url})
    except HTTPException:
        raise
    except Exception as e:
        raise HTTPException(502, f"Browser error: {e}")


@app.websocket("/ws/browser")
async def browser_ws(ws: WebSocket):
    """
    WebSocket browser session.
    Client sends: {"type":"navigate","url":"https://..."}
    Server sends: {"type":"screenshot","data":"<base64 jpeg>","url":"...","title":"..."}
    """
    await ws.accept()
    await ws.send_json({"type":"status","message":"Browser session ready"})
    page = None
    try:
        browser = await get_browser()
        page = await browser.new_page(viewport={"width":390,"height":700})

        async def send_screenshot(p):
            try:
                shot = await p.screenshot(type="jpeg", quality=75)
                import base64
                await ws.send_json({
                    "type": "screenshot",
                    "data": base64.b64encode(shot).decode(),
                    "url":  p.url,
                    "title": await p.title(),
                })
            except Exception as e:
                await ws.send_json({"type":"error","message":str(e)})

        while True:
            msg = await ws.receive_json()
            if msg.get("type") == "navigate":
                url = msg["url"]
                if not url.startswith(("http://","https://")):
                    url = "https://" + url
                await ws.send_json({"type":"loading","url":url})
                try:
                    await page.goto(url, wait_until="domcontentloaded", timeout=20000)
                    await send_screenshot(page)
                except Exception as e:
                    await ws.send_json({"type":"error","message":str(e)})
            elif msg.get("type") == "click":
                await page.mouse.click(msg.get("x",0), msg.get("y",0))
                await asyncio.sleep(0.5)
                await send_screenshot(page)
            elif msg.get("type") == "scroll":
                await page.mouse.wheel(0, msg.get("delta",200))
                await asyncio.sleep(0.2)
                await send_screenshot(page)
            elif msg.get("type") == "back":
                await page.go_back()
                await send_screenshot(page)
            elif msg.get("type") == "forward":
                await page.go_forward()
                await send_screenshot(page)
            elif msg.get("type") == "refresh":
                await page.reload()
                await send_screenshot(page)
    except WebSocketDisconnect:
        pass
    finally:
        if page and not page.is_closed():
            await page.close()


# ── Docker ────────────────────────────────────────────────────────────────────

def run_docker(*args) -> dict:
    try:
        r = subprocess.run(["docker",*args], capture_output=True, text=True, timeout=10)
        return {"ok": r.returncode==0, "stdout": r.stdout, "stderr": r.stderr}
    except FileNotFoundError:
        return {"ok":False,"error":"Docker not installed"}
    except subprocess.TimeoutExpired:
        return {"ok":False,"error":"Timed out"}


@app.get("/api/docker/containers")
async def docker_containers():
    r = run_docker("ps","-a","--format","json")
    containers = []
    for line in r.get("stdout","").strip().splitlines():
        try: containers.append(json.loads(line))
        except: pass
    return {"containers": containers, "error": r.get("error") or r.get("stderr") if not r["ok"] else None}


@app.post("/api/docker/start/{cid}")
async def docker_start(cid: str): return run_docker("start", cid)

@app.post("/api/docker/stop/{cid}")
async def docker_stop(cid: str): return run_docker("stop", cid)

@app.get("/api/docker/images")
async def docker_images():
    r = run_docker("images","--format","json")
    images = []
    for line in r.get("stdout","").strip().splitlines():
        try: images.append(json.loads(line))
        except: pass
    return {"images": images}


# ── Health ────────────────────────────────────────────────────────────────────

@app.get("/api/health")
async def health():
    return {"status":"ok","name":"TreyOS","version":"1.0.0","root":str(ROOT_JAIL)}


# ── Serve frontend ────────────────────────────────────────────────────────────
# HuggingFace Spaces loads the root URL "/" directly in an iframe, so the
# TreyOS UI must be served there β€” not just at /app β€” or the Space will
# render blank.

frontend_path = Path(__file__).parent / "frontend"
if not frontend_path.exists():
    frontend_path = Path(__file__).parent.parent / "frontend"

if frontend_path.exists():
    index_file = frontend_path / "index.html"

    @app.get("/")
    async def serve_root():
        return FileResponse(str(index_file), media_type="text/html")

    @app.get("/app")
    async def serve_app():
        return FileResponse(str(index_file), media_type="text/html")

    app.mount("/static", StaticFiles(directory=str(frontend_path)), name="static")
else:
    @app.get("/")
    async def serve_root_fallback():
        return JSONResponse({"status":"ok","name":"TreyOS","warning":"frontend/ directory not found"})