Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1187,40 +1187,52 @@ async def websocket_endpoint(ws: WebSocket):
|
|
| 1187 |
active_connections.append(ws)
|
| 1188 |
|
| 1189 |
# ── عزل كامل: إنشاء Xvfb + متصفح خاصين بهذا الاتصال فقط ──
|
| 1190 |
-
# كل اتصال WebSocket يحصل على بيئة كمبيوتر افتراضي مستقلة تماماً عن أي اتصال آخر
|
| 1191 |
-
#
|
| 1192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1193 |
_current_display.set(session_display) # يُربط بـ task هذا الاتصال — لا يؤثر على اتصالات أخرى
|
| 1194 |
|
| 1195 |
-
|
| 1196 |
-
|
| 1197 |
-
"type": "connected",
|
| 1198 |
-
"screen_width": w,
|
| 1199 |
-
"screen_height": h,
|
| 1200 |
-
"browser": BROWSER,
|
| 1201 |
-
"display": session_display,
|
| 1202 |
-
"msg": f"Z Computer Mode v7 — Smart Control | Browser: {BROWSER} | Screen: {w}x{h} | Isolated session: {session_display}",
|
| 1203 |
-
"commands": [
|
| 1204 |
-
"screenshot", "terminal {cmd}", "mouse_move {x,y}", "mouse_click {x,y,button}",
|
| 1205 |
-
"keyboard_type {text}", "keyboard_hotkey {keys}", "scroll {x,y,clicks}",
|
| 1206 |
-
"open_app {cmd}", "open_browser {url}", "simple_command {cmd}",
|
| 1207 |
-
"Simple cmds: 'open firefox', 'mouse go x y', 'click x y', 'type TEXT', 'key enter'"
|
| 1208 |
-
]
|
| 1209 |
-
}, ensure_ascii=False))
|
| 1210 |
-
# أرسل screenshot أولية مع grid (لشاشة هذه الجلسة المعزولة فقط)
|
| 1211 |
-
result = await asyncio.to_thread(capture_screen_with_grid, scale=0.65, quality=72)
|
| 1212 |
-
if result["data"]:
|
| 1213 |
await ws.send_text(json.dumps({
|
| 1214 |
-
"type": "
|
| 1215 |
-
"
|
| 1216 |
-
"
|
| 1217 |
-
"
|
| 1218 |
-
"
|
| 1219 |
-
"
|
| 1220 |
-
"
|
| 1221 |
-
|
| 1222 |
-
|
|
|
|
|
|
|
|
|
|
| 1223 |
}, ensure_ascii=False))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1224 |
try:
|
| 1225 |
while True:
|
| 1226 |
raw = await ws.receive_text()
|
|
|
|
| 1187 |
active_connections.append(ws)
|
| 1188 |
|
| 1189 |
# ── عزل كامل: إنشاء Xvfb + متصفح خاصين بهذا الاتصال فقط ──
|
| 1190 |
+
# كل اتصال WebSocket يحصل على بيئة كمبيوتر افتراضي مستقلة تماماً عن أي اتصال آخر.
|
| 1191 |
+
# إذا فشل إنشاء جلسة معزولة لأي سبب (صلاحيات، موارد، Xvfb غير متاح...)، نرجع فوراً
|
| 1192 |
+
# للوضع المشترك القديم (DISPLAY الأساسي) بدل أن ينهار الاتصال بصمت بالكامل.
|
| 1193 |
+
session_display = _BASE_DISPLAY
|
| 1194 |
+
try:
|
| 1195 |
+
session_display = await asyncio.wait_for(create_session_for(ws), timeout=8.0)
|
| 1196 |
+
except asyncio.TimeoutError:
|
| 1197 |
+
print("[session] ⚠️ session creation timed out (>8s), falling back to shared display")
|
| 1198 |
+
session_display = _BASE_DISPLAY
|
| 1199 |
+
except Exception as e:
|
| 1200 |
+
print(f"[session] ⚠️ failed to create isolated session, falling back to shared display: {e}")
|
| 1201 |
+
session_display = _BASE_DISPLAY
|
| 1202 |
_current_display.set(session_display) # يُربط بـ task هذا الاتصال — لا يؤثر على اتصالات أخرى
|
| 1203 |
|
| 1204 |
+
try:
|
| 1205 |
+
w, h = await asyncio.to_thread(_get_screen_size)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1206 |
await ws.send_text(json.dumps({
|
| 1207 |
+
"type": "connected",
|
| 1208 |
+
"screen_width": w,
|
| 1209 |
+
"screen_height": h,
|
| 1210 |
+
"browser": BROWSER,
|
| 1211 |
+
"display": session_display,
|
| 1212 |
+
"msg": f"Z Computer Mode v7 — Smart Control | Browser: {BROWSER} | Screen: {w}x{h} | Isolated session: {session_display}",
|
| 1213 |
+
"commands": [
|
| 1214 |
+
"screenshot", "terminal {cmd}", "mouse_move {x,y}", "mouse_click {x,y,button}",
|
| 1215 |
+
"keyboard_type {text}", "keyboard_hotkey {keys}", "scroll {x,y,clicks}",
|
| 1216 |
+
"open_app {cmd}", "open_browser {url}", "simple_command {cmd}",
|
| 1217 |
+
"Simple cmds: 'open firefox', 'mouse go x y', 'click x y', 'type TEXT', 'key enter'"
|
| 1218 |
+
]
|
| 1219 |
}, ensure_ascii=False))
|
| 1220 |
+
# أرسل screenshot أولية مع grid (لشاشة هذه الجلسة المعزولة فقط)
|
| 1221 |
+
result = await asyncio.to_thread(capture_screen_with_grid, scale=0.65, quality=72)
|
| 1222 |
+
if result["data"]:
|
| 1223 |
+
await ws.send_text(json.dumps({
|
| 1224 |
+
"type": "screenshot",
|
| 1225 |
+
"data": result["data"],
|
| 1226 |
+
"ts": int(time.time() * 1000),
|
| 1227 |
+
"label": "الشاشة الأولية",
|
| 1228 |
+
"screen_width": result["width"],
|
| 1229 |
+
"screen_height": result["height"],
|
| 1230 |
+
"mouse_x": result["mouse_x"],
|
| 1231 |
+
"mouse_y": result["mouse_y"],
|
| 1232 |
+
"has_grid": True,
|
| 1233 |
+
}, ensure_ascii=False))
|
| 1234 |
+
except Exception as e:
|
| 1235 |
+
print(f"[ws] ⚠️ error sending initial messages: {e}")
|
| 1236 |
try:
|
| 1237 |
while True:
|
| 1238 |
raw = await ws.receive_text()
|