Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
"""
|
| 2 |
-
THE Z AI — Computer Mode Server
|
| 3 |
-
====================================================
|
| 4 |
-
الإصلاحات الجوهرية في
|
| 5 |
-
1.
|
| 6 |
-
2.
|
| 7 |
-
3.
|
| 8 |
-
4.
|
| 9 |
-
5.
|
| 10 |
-
6.
|
| 11 |
"""
|
| 12 |
|
| 13 |
import asyncio
|
|
@@ -28,24 +28,18 @@ import uvicorn
|
|
| 28 |
DISPLAY = os.environ.get("DISPLAY", ":1")
|
| 29 |
os.environ["DISPLAY"] = DISPLAY
|
| 30 |
|
| 31 |
-
# ─── اكتشاف المتصفح
|
| 32 |
def _detect_browser() -> str:
|
| 33 |
-
"""يكتشف المتصفح المتاح ويُنشئ symlink لـ firefox-esr إذا لزم"""
|
| 34 |
-
env_browser = os.environ.get("DEFAULT_BROWSER", "")
|
| 35 |
-
|
| 36 |
candidates = ["firefox", "firefox-esr", "chromium-browser", "chromium", "google-chrome"]
|
| 37 |
found = None
|
| 38 |
-
|
| 39 |
for c in candidates:
|
| 40 |
r = subprocess.run(["which", c], capture_output=True, text=True)
|
| 41 |
if r.returncode == 0 and r.stdout.strip():
|
| 42 |
found = c
|
| 43 |
break
|
| 44 |
-
|
| 45 |
if not found:
|
| 46 |
return "firefox"
|
| 47 |
-
|
| 48 |
-
# تأكد من وجود firefox-esr كـ symlink لأن الـ AI يستدعيه دائماً
|
| 49 |
esr_check = subprocess.run(["which", "firefox-esr"], capture_output=True, text=True)
|
| 50 |
if esr_check.returncode != 0:
|
| 51 |
real_path = subprocess.run(["which", found], capture_output=True, text=True).stdout.strip()
|
|
@@ -55,13 +49,12 @@ def _detect_browser() -> str:
|
|
| 55 |
print(f"✅ Created firefox-esr symlink → {real_path}")
|
| 56 |
except Exception as e:
|
| 57 |
print(f"⚠️ Could not create firefox-esr symlink: {e}")
|
| 58 |
-
|
| 59 |
return found
|
| 60 |
|
| 61 |
BROWSER = _detect_browser()
|
| 62 |
print(f"🌐 Browser detected: {BROWSER}")
|
| 63 |
|
| 64 |
-
app = FastAPI(title="Z-Computer-Mode API
|
| 65 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True,
|
| 66 |
allow_methods=["*"], allow_headers=["*"])
|
| 67 |
|
|
@@ -153,54 +146,187 @@ def _is_empty_result(stdout: str) -> bool:
|
|
| 153 |
|
| 154 |
# ─── Screen Capture ─────────────────────────────────
|
| 155 |
|
| 156 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
tmp = f"/tmp/zs_{int(time.time()*1000)}.png"
|
| 159 |
-
r = subprocess.run(["scrot", "-q", "
|
| 160 |
env={**os.environ, "DISPLAY": DISPLAY},
|
| 161 |
timeout=8, capture_output=True)
|
| 162 |
if r.returncode != 0 or not os.path.exists(tmp):
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
if scale < 1.0:
|
| 168 |
-
img = img.resize((int(
|
| 169 |
buf = io.BytesIO()
|
| 170 |
-
img.
|
| 171 |
return base64.b64encode(buf.getvalue()).decode()
|
| 172 |
except Exception as e:
|
| 173 |
print(f"[capture] {e}")
|
| 174 |
return ""
|
| 175 |
|
| 176 |
|
| 177 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
try:
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
buf = io.BytesIO()
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
|
| 200 |
# ─── Command Runner ──────────────────────────────────
|
| 201 |
|
| 202 |
def run_raw_command(cmd: str, timeout: int = 60) -> dict:
|
| 203 |
-
"""ينفذ أمر bash خام"""
|
| 204 |
env = {**os.environ, "DISPLAY": DISPLAY,
|
| 205 |
"PYTHONIOENCODING": "utf-8", "LANG": "en_US.UTF-8"}
|
| 206 |
try:
|
|
@@ -219,9 +345,6 @@ def run_raw_command(cmd: str, timeout: int = 60) -> dict:
|
|
| 219 |
|
| 220 |
|
| 221 |
def run_command_smart(cmd: str, timeout: int = 60) -> dict:
|
| 222 |
-
"""
|
| 223 |
-
ينفذ الأمر — إذا كان curl بحث وفشل/فرغ → يجرب مصادر بديلة تلقائياً
|
| 224 |
-
"""
|
| 225 |
res = run_raw_command(cmd, timeout=timeout)
|
| 226 |
stdout = res["stdout"].strip()
|
| 227 |
|
|
@@ -256,12 +379,8 @@ def run_command_smart(cmd: str, timeout: int = 60) -> dict:
|
|
| 256 |
src_out = src_res["stdout"].strip()
|
| 257 |
|
| 258 |
if not _is_empty_result(src_out):
|
| 259 |
-
combined_header =
|
| 260 |
-
f"[مصدر بديل: {src_name}]\n"
|
| 261 |
-
f"{'='*50}\n"
|
| 262 |
-
)
|
| 263 |
all_results.append(src_out)
|
| 264 |
-
# جرب مصدراً إضافياً آخر لإثراء النتائج
|
| 265 |
for source2 in sources:
|
| 266 |
if source2["name"] != src_name:
|
| 267 |
s2 = run_raw_command(source2["cmd"], timeout=20)
|
|
@@ -269,7 +388,6 @@ def run_command_smart(cmd: str, timeout: int = 60) -> dict:
|
|
| 269 |
if not _is_empty_result(s2_out):
|
| 270 |
all_results.append(f"\n[مصدر إضافي: {source2['name']}]\n{s2_out}")
|
| 271 |
break
|
| 272 |
-
|
| 273 |
final_out = combined_header + "\n\n".join(all_results)
|
| 274 |
return {
|
| 275 |
"stdout": final_out[:15000],
|
|
@@ -294,6 +412,166 @@ def xdo(args: list, timeout=10) -> dict:
|
|
| 294 |
return {"rc": r.returncode, "out": r.stdout, "err": r.stderr}
|
| 295 |
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
async def broadcast(msg: dict):
|
| 298 |
txt = json.dumps(msg, ensure_ascii=False)
|
| 299 |
dead = []
|
|
@@ -327,18 +605,59 @@ async def handle_action(ws: WebSocket, msg: dict):
|
|
| 327 |
async def send(obj):
|
| 328 |
await ws.send_text(json.dumps(obj, ensure_ascii=False))
|
| 329 |
|
| 330 |
-
async def
|
|
|
|
| 331 |
await asyncio.sleep(delay)
|
| 332 |
-
|
| 333 |
-
if
|
| 334 |
-
await send({
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
if action == "screenshot":
|
| 339 |
-
|
| 340 |
-
await send({
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
elif action == "terminal":
|
| 343 |
cmd = data.get("cmd", "")
|
| 344 |
timeout = int(data.get("timeout", 60))
|
|
@@ -346,9 +665,7 @@ async def handle_action(ws: WebSocket, msg: dict):
|
|
| 346 |
await send({"type": "terminal_result", "cmd": "", "stdout": "",
|
| 347 |
"stderr": "no command", "returncode": -1})
|
| 348 |
return
|
| 349 |
-
|
| 350 |
res = run_command_smart(cmd, timeout=timeout)
|
| 351 |
-
|
| 352 |
await send({
|
| 353 |
"type": "terminal_result",
|
| 354 |
"cmd": cmd,
|
|
@@ -358,58 +675,77 @@ async def handle_action(ws: WebSocket, msg: dict):
|
|
| 358 |
"fallback_used": res.get("_fallback_used", None),
|
| 359 |
"sources_tried": res.get("_sources_tried", []),
|
| 360 |
})
|
|
|
|
| 361 |
|
| 362 |
-
|
| 363 |
-
|
| 364 |
elif action == "mouse_move":
|
| 365 |
x, y = int(data.get("x", 0)), int(data.get("y", 0))
|
| 366 |
xdo(["mousemove", str(x), str(y)])
|
| 367 |
-
await send({"type": "ack", "action": "mouse_move"})
|
|
|
|
|
|
|
| 368 |
|
|
|
|
| 369 |
elif action == "mouse_click":
|
| 370 |
x, y = int(data.get("x", 0)), int(data.get("y", 0))
|
| 371 |
-
btn_num = {"left": "1", "middle": "2", "right": "3"}.get(data.get("button","left"), "1")
|
| 372 |
xdo(["mousemove", str(x), str(y)])
|
| 373 |
-
await asyncio.sleep(0.
|
| 374 |
if data.get("double"):
|
| 375 |
xdo(["click", "--repeat", "2", "--delay", "100", btn_num])
|
| 376 |
else:
|
| 377 |
xdo(["click", btn_num])
|
| 378 |
-
|
| 379 |
-
await
|
|
|
|
| 380 |
|
|
|
|
| 381 |
elif action == "keyboard_type":
|
| 382 |
text = data.get("text", "")
|
| 383 |
if text:
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
|
|
|
|
|
|
|
|
|
| 387 |
|
|
|
|
| 388 |
elif action == "keyboard_hotkey":
|
| 389 |
keys = data.get("keys", [])
|
| 390 |
if keys:
|
| 391 |
xdo(["key", "--clearmodifiers", "+".join(keys)])
|
| 392 |
-
await send({"type": "ack", "action": "keyboard_hotkey"})
|
| 393 |
-
await
|
| 394 |
|
|
|
|
| 395 |
elif action == "keyboard_press":
|
| 396 |
key = data.get("key", "")
|
| 397 |
if key:
|
| 398 |
xdo(["key", "--clearmodifiers", key])
|
| 399 |
await send({"type": "ack", "action": "keyboard_press"})
|
| 400 |
-
await
|
| 401 |
|
|
|
|
| 402 |
elif action == "clipboard_write":
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
|
|
|
|
| 409 |
elif action == "clipboard_read":
|
| 410 |
res = run_raw_command("xclip -selection clipboard -o", timeout=5)
|
| 411 |
await send({"type": "clipboard_content", "text": res["stdout"]})
|
| 412 |
|
|
|
|
| 413 |
elif action == "scroll":
|
| 414 |
x, y = int(data.get("x", 0)), int(data.get("y", 0))
|
| 415 |
clicks = int(data.get("clicks", 3))
|
|
@@ -417,39 +753,50 @@ async def handle_action(ws: WebSocket, msg: dict):
|
|
| 417 |
xdo(["mousemove", str(x), str(y)])
|
| 418 |
for _ in range(abs(clicks)):
|
| 419 |
xdo(["click", btn])
|
| 420 |
-
|
| 421 |
-
await
|
|
|
|
| 422 |
|
|
|
|
| 423 |
elif action == "open_app":
|
| 424 |
app_cmd = data.get("cmd", "")
|
| 425 |
if app_cmd:
|
| 426 |
-
# إصلاح: استبدل firefox-esr بالمتصفح الحقيقي إذا لم يكن موجوداً
|
| 427 |
fixed_cmd = app_cmd
|
|
|
|
| 428 |
if "firefox-esr" in app_cmd:
|
| 429 |
esr_check = subprocess.run(["which", "firefox-esr"], capture_output=True, text=True)
|
| 430 |
if esr_check.returncode != 0:
|
| 431 |
fixed_cmd = app_cmd.replace("firefox-esr", BROWSER)
|
| 432 |
-
|
| 433 |
subprocess.Popen(fixed_cmd, shell=True,
|
| 434 |
env={**os.environ, "DISPLAY": DISPLAY},
|
| 435 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 436 |
await send({"type": "ack", "action": "open_app", "cmd": fixed_cmd})
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
wait_time = 5.0 # انتظر 5 ثواني لفتح المتصفح
|
| 440 |
-
if any(x in app_cmd for x in ["firefox", "chromium", "chrome"]):
|
| 441 |
-
wait_time = 7.0 # المتصفح يحتاج وقتاً أطول
|
| 442 |
-
|
| 443 |
await asyncio.sleep(2.0)
|
| 444 |
-
await
|
| 445 |
await asyncio.sleep(wait_time - 2.0)
|
| 446 |
-
await
|
| 447 |
else:
|
| 448 |
await send({"type": "ack", "action": "open_app"})
|
| 449 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
elif action == "mouse_drag":
|
| 451 |
-
x1,y1 = int(data.get("x1",0)), int(data.get("y1",0))
|
| 452 |
-
x2,y2 = int(data.get("x2",0)), int(data.get("y2",0))
|
| 453 |
xdo(["mousemove", str(x1), str(y1)])
|
| 454 |
xdo(["mousedown", "1"])
|
| 455 |
await asyncio.sleep(0.1)
|
|
@@ -457,8 +804,9 @@ async def handle_action(ws: WebSocket, msg: dict):
|
|
| 457 |
await asyncio.sleep(0.1)
|
| 458 |
xdo(["mouseup", "1"])
|
| 459 |
await send({"type": "ack", "action": "mouse_drag"})
|
| 460 |
-
await
|
| 461 |
|
|
|
|
| 462 |
elif action == "start_stream":
|
| 463 |
global stream_active, stream_fps, stream_quality, stream_scale
|
| 464 |
stream_fps = int(data.get("fps", 3))
|
|
@@ -469,23 +817,43 @@ async def handle_action(ws: WebSocket, msg: dict):
|
|
| 469 |
asyncio.create_task(screen_stream_loop())
|
| 470 |
await send({"type": "ack", "action": "start_stream"})
|
| 471 |
|
|
|
|
| 472 |
elif action == "stop_stream":
|
| 473 |
stream_active = False
|
| 474 |
await send({"type": "ack", "action": "stop_stream"})
|
| 475 |
|
|
|
|
| 476 |
elif action == "screen_info":
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 486 |
|
|
|
|
| 487 |
else:
|
| 488 |
-
await send({
|
|
|
|
|
|
|
|
|
|
| 489 |
|
| 490 |
|
| 491 |
# ─── WebSocket ───────────────────────────────────────
|
|
@@ -494,24 +862,42 @@ async def handle_action(ws: WebSocket, msg: dict):
|
|
| 494 |
async def websocket_endpoint(ws: WebSocket):
|
| 495 |
await ws.accept()
|
| 496 |
active_connections.append(ws)
|
| 497 |
-
|
| 498 |
-
try: parts = res["stdout"].strip().split(); w,h = int(parts[0]),int(parts[1])
|
| 499 |
-
except: w,h = 1920,1080
|
| 500 |
await ws.send_text(json.dumps({
|
| 501 |
-
"type": "connected",
|
|
|
|
|
|
|
| 502 |
"browser": BROWSER,
|
| 503 |
-
"msg": f"Z Computer Mode
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
}, ensure_ascii=False))
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
try:
|
| 510 |
while True:
|
| 511 |
raw = await ws.receive_text()
|
| 512 |
await handle_action(ws, json.loads(raw))
|
| 513 |
-
except WebSocketDisconnect:
|
| 514 |
-
|
|
|
|
|
|
|
| 515 |
finally:
|
| 516 |
if ws in active_connections:
|
| 517 |
active_connections.remove(ws)
|
|
@@ -521,25 +907,48 @@ async def websocket_endpoint(ws: WebSocket):
|
|
| 521 |
|
| 522 |
@app.get("/screenshot")
|
| 523 |
async def rest_screenshot():
|
| 524 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
|
| 526 |
@app.post("/terminal")
|
| 527 |
async def rest_terminal(body: dict):
|
| 528 |
-
return JSONResponse(run_command_smart(body.get("cmd",""), body.get("timeout",60)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
|
| 530 |
@app.get("/health")
|
| 531 |
async def health():
|
| 532 |
-
|
| 533 |
-
|
| 534 |
browser_check = subprocess.run(["which", "firefox-esr"], capture_output=True, text=True)
|
| 535 |
return {
|
| 536 |
"status": "ok",
|
| 537 |
-
"version": "
|
| 538 |
"browser": BROWSER,
|
| 539 |
"firefox_esr_available": browser_check.returncode == 0,
|
| 540 |
-
"
|
| 541 |
-
"
|
| 542 |
-
"
|
|
|
|
| 543 |
}
|
| 544 |
|
| 545 |
@app.get("/search/{query}")
|
|
|
|
| 1 |
"""
|
| 2 |
+
THE Z AI — Computer Mode Server v7 — SMART CONTROL
|
| 3 |
+
====================================================
|
| 4 |
+
الإصلاحات الجوهرية في v7:
|
| 5 |
+
1. capture_screen_with_grid() — صورة مزدوجة: يمين نظيفة + يسار بشبكة احداثيات شفافة
|
| 6 |
+
2. أوامر بسيطة: open pc, open firefox, mouse go x y, click x y, rclick x y, type TEXT, key ENTER ...
|
| 7 |
+
3. إصلاح الكتابة العربية: xclip clipboard + xdotool key ctrl+v
|
| 8 |
+
4. إرسال احداثيات الشاشة الحقيقية + موقع الماوس مع كل screenshot
|
| 9 |
+
5. auto_shot_with_grid بعد كل خطوة
|
| 10 |
+
6. simple_command parser: يفهم أوامر بسيطة ويحولها لأفعال WebSocket
|
| 11 |
"""
|
| 12 |
|
| 13 |
import asyncio
|
|
|
|
| 28 |
DISPLAY = os.environ.get("DISPLAY", ":1")
|
| 29 |
os.environ["DISPLAY"] = DISPLAY
|
| 30 |
|
| 31 |
+
# ─── اكتشاف المتصفح ──────────────────────────────────
|
| 32 |
def _detect_browser() -> str:
|
|
|
|
|
|
|
|
|
|
| 33 |
candidates = ["firefox", "firefox-esr", "chromium-browser", "chromium", "google-chrome"]
|
| 34 |
found = None
|
|
|
|
| 35 |
for c in candidates:
|
| 36 |
r = subprocess.run(["which", c], capture_output=True, text=True)
|
| 37 |
if r.returncode == 0 and r.stdout.strip():
|
| 38 |
found = c
|
| 39 |
break
|
|
|
|
| 40 |
if not found:
|
| 41 |
return "firefox"
|
| 42 |
+
# تأكد من وجود firefox-esr كـ symlink
|
|
|
|
| 43 |
esr_check = subprocess.run(["which", "firefox-esr"], capture_output=True, text=True)
|
| 44 |
if esr_check.returncode != 0:
|
| 45 |
real_path = subprocess.run(["which", found], capture_output=True, text=True).stdout.strip()
|
|
|
|
| 49 |
print(f"✅ Created firefox-esr symlink → {real_path}")
|
| 50 |
except Exception as e:
|
| 51 |
print(f"⚠️ Could not create firefox-esr symlink: {e}")
|
|
|
|
| 52 |
return found
|
| 53 |
|
| 54 |
BROWSER = _detect_browser()
|
| 55 |
print(f"🌐 Browser detected: {BROWSER}")
|
| 56 |
|
| 57 |
+
app = FastAPI(title="Z-Computer-Mode API v7")
|
| 58 |
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True,
|
| 59 |
allow_methods=["*"], allow_headers=["*"])
|
| 60 |
|
|
|
|
| 146 |
|
| 147 |
# ─── Screen Capture ─────────────────────────────────
|
| 148 |
|
| 149 |
+
def _get_screen_size() -> tuple[int, int]:
|
| 150 |
+
"""يُعيد حجم الشاشة الحقيقي"""
|
| 151 |
+
r = subprocess.run(["xdotool", "getdisplaygeometry"],
|
| 152 |
+
env={**os.environ, "DISPLAY": DISPLAY},
|
| 153 |
+
capture_output=True, text=True, timeout=5)
|
| 154 |
+
try:
|
| 155 |
+
parts = r.stdout.strip().split()
|
| 156 |
+
return int(parts[0]), int(parts[1])
|
| 157 |
+
except:
|
| 158 |
+
return 1920, 1080
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
def _get_mouse_pos() -> tuple[int, int]:
|
| 162 |
+
"""يُعيد موقع الماوس الحالي"""
|
| 163 |
+
r = subprocess.run(["xdotool", "getmouselocation"],
|
| 164 |
+
env={**os.environ, "DISPLAY": DISPLAY},
|
| 165 |
+
capture_output=True, text=True, timeout=5)
|
| 166 |
try:
|
| 167 |
+
mx = int(re.search(r"x:(\d+)", r.stdout).group(1))
|
| 168 |
+
my = int(re.search(r"y:(\d+)", r.stdout).group(1))
|
| 169 |
+
return mx, my
|
| 170 |
+
except:
|
| 171 |
+
return 0, 0
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def capture_screen_raw() -> tuple[object, int, int]:
|
| 175 |
+
"""يلتقط الشاشة ويُعيد (PIL Image, width, height) أو (None, 0, 0)"""
|
| 176 |
+
try:
|
| 177 |
+
from PIL import Image
|
| 178 |
tmp = f"/tmp/zs_{int(time.time()*1000)}.png"
|
| 179 |
+
r = subprocess.run(["scrot", "-q", "95", tmp],
|
| 180 |
env={**os.environ, "DISPLAY": DISPLAY},
|
| 181 |
timeout=8, capture_output=True)
|
| 182 |
if r.returncode != 0 or not os.path.exists(tmp):
|
| 183 |
+
# fallback xwd
|
| 184 |
+
xwd = tmp.replace(".png", ".xwd")
|
| 185 |
+
subprocess.run(["xwd", "-root", "-silent", "-out", xwd],
|
| 186 |
+
env={**os.environ, "DISPLAY": DISPLAY}, timeout=8, capture_output=True)
|
| 187 |
+
subprocess.run(["convert", xwd, tmp], timeout=8, capture_output=True)
|
| 188 |
+
try: os.unlink(xwd)
|
| 189 |
+
except: pass
|
| 190 |
+
if not os.path.exists(tmp):
|
| 191 |
+
return None, 0, 0
|
| 192 |
+
img = Image.open(tmp).convert("RGB")
|
| 193 |
+
w, h = img.size
|
| 194 |
+
try: os.unlink(tmp)
|
| 195 |
+
except: pass
|
| 196 |
+
return img, w, h
|
| 197 |
+
except Exception as e:
|
| 198 |
+
print(f"[capture_raw] {e}")
|
| 199 |
+
return None, 0, 0
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def capture_screen(scale=0.6, quality=70) -> str:
|
| 203 |
+
"""يلتقط الشاشة العادية بدون grid"""
|
| 204 |
+
img, w, h = capture_screen_raw()
|
| 205 |
+
if img is None:
|
| 206 |
+
return ""
|
| 207 |
+
try:
|
| 208 |
if scale < 1.0:
|
| 209 |
+
img = img.resize((int(w * scale), int(h * scale)), img.LANCZOS)
|
| 210 |
buf = io.BytesIO()
|
| 211 |
+
img.save(buf, format="JPEG", quality=quality, optimize=True)
|
| 212 |
return base64.b64encode(buf.getvalue()).decode()
|
| 213 |
except Exception as e:
|
| 214 |
print(f"[capture] {e}")
|
| 215 |
return ""
|
| 216 |
|
| 217 |
|
| 218 |
+
def capture_screen_with_grid(scale=0.65, quality=72) -> dict:
|
| 219 |
+
"""
|
| 220 |
+
يلتقط الشاشة ويُعيد صورة مزدوجة:
|
| 221 |
+
- يسار: صورة نظيفة بدون خطوط
|
| 222 |
+
- يمين: نفس الصورة مع شبكة احداثيات شفافة وأرقام
|
| 223 |
+
يُعيد أيضاً: احداثيات الشاشة الحقيقية + موقع الماوس
|
| 224 |
+
"""
|
| 225 |
try:
|
| 226 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 227 |
+
img, orig_w, orig_h = capture_screen_raw()
|
| 228 |
+
if img is None:
|
| 229 |
+
return {"data": "", "width": 1920, "height": 1080, "mouse_x": 0, "mouse_y": 0}
|
| 230 |
+
|
| 231 |
+
mx, my = _get_mouse_pos()
|
| 232 |
+
|
| 233 |
+
# حجم كل نصف بعد الـ scale
|
| 234 |
+
sw = int(orig_w * scale)
|
| 235 |
+
sh = int(orig_h * scale)
|
| 236 |
+
clean = img.resize((sw, sh), Image.LANCZOS)
|
| 237 |
+
|
| 238 |
+
# نسخة مع grid
|
| 239 |
+
grid_img = clean.copy()
|
| 240 |
+
draw = ImageDraw.Draw(grid_img, "RGBA")
|
| 241 |
+
|
| 242 |
+
# شبكة كل 100px على الأصل (مقاسة للـ scale)
|
| 243 |
+
step_x_orig = 100
|
| 244 |
+
step_y_orig = 100
|
| 245 |
+
step_x = int(step_x_orig * scale)
|
| 246 |
+
step_y = int(step_y_orig * scale)
|
| 247 |
+
|
| 248 |
+
grid_color = (255, 255, 255, 35) # شبكة شفافة جداً
|
| 249 |
+
label_color = (0, 255, 200, 120) # أرقام بلون سماوي شفاف
|
| 250 |
+
|
| 251 |
+
# خطوط عمودية + أرقام X
|
| 252 |
+
x_pos = step_x
|
| 253 |
+
x_orig = step_x_orig
|
| 254 |
+
while x_pos < sw:
|
| 255 |
+
draw.line([(x_pos, 0), (x_pos, sh)], fill=grid_color, width=1)
|
| 256 |
+
draw.text((x_pos + 2, 2), str(x_orig), fill=label_color)
|
| 257 |
+
x_pos += step_x
|
| 258 |
+
x_orig += step_x_orig
|
| 259 |
+
|
| 260 |
+
# خطوط أفقية + أرقام Y
|
| 261 |
+
y_pos = step_y
|
| 262 |
+
y_orig = step_y_orig
|
| 263 |
+
while y_pos < sh:
|
| 264 |
+
draw.line([(0, y_pos), (sw, y_pos)], fill=grid_color, width=1)
|
| 265 |
+
draw.text((2, y_pos + 2), str(y_orig), fill=label_color)
|
| 266 |
+
y_pos += step_y
|
| 267 |
+
y_orig += step_y_orig
|
| 268 |
+
|
| 269 |
+
# موقع الماوس على الصورة المُقلّصة
|
| 270 |
+
mouse_sx = int(mx * scale)
|
| 271 |
+
mouse_sy = int(my * scale)
|
| 272 |
+
# دائرة حمراء للماوس
|
| 273 |
+
r_size = 8
|
| 274 |
+
draw.ellipse(
|
| 275 |
+
[(mouse_sx - r_size, mouse_sy - r_size), (mouse_sx + r_size, mouse_sy + r_size)],
|
| 276 |
+
outline=(255, 50, 50, 220), width=2
|
| 277 |
+
)
|
| 278 |
+
draw.line([(mouse_sx - 12, mouse_sy), (mouse_sx + 12, mouse_sy)], fill=(255, 50, 50, 180), width=1)
|
| 279 |
+
draw.line([(mouse_sx, mouse_sy - 12), (mouse_sx, mouse_sy + 12)], fill=(255, 50, 50, 180), width=1)
|
| 280 |
+
|
| 281 |
+
# أنشئ صورة مزدوجة جنباً إلى جنب
|
| 282 |
+
gap = 4
|
| 283 |
+
combined_w = sw * 2 + gap
|
| 284 |
+
combined_h = sh
|
| 285 |
+
combined = Image.new("RGB", (combined_w, combined_h), (15, 15, 15))
|
| 286 |
+
|
| 287 |
+
# يسار = نظيفة، يمين = grid
|
| 288 |
+
combined.paste(clean, (0, 0))
|
| 289 |
+
combined.paste(grid_img.convert("RGB"), (sw + gap, 0))
|
| 290 |
+
|
| 291 |
+
# شريط فاصل
|
| 292 |
+
draw2 = ImageDraw.Draw(combined)
|
| 293 |
+
draw2.line([(sw, 0), (sw, sh)], fill=(60, 60, 60), width=gap)
|
| 294 |
+
|
| 295 |
+
# تسميات صغيرة في الأعلى
|
| 296 |
+
draw2.rectangle([(0, 0), (sw, 16)], fill=(0, 0, 0, 180))
|
| 297 |
+
draw2.text((4, 2), "CLEAN VIEW", fill=(150, 150, 150))
|
| 298 |
+
draw2.rectangle([(sw + gap, 0), (combined_w, 16)], fill=(0, 0, 0, 180))
|
| 299 |
+
draw2.text((sw + gap + 4, 2), f"GRID VIEW mouse:({mx},{my})", fill=(0, 220, 180))
|
| 300 |
+
|
| 301 |
+
# معلومات الشاشة في الزاوية السفلية اليمنى
|
| 302 |
+
info_text = f"Screen: {orig_w}x{orig_h} | Mouse: ({mx},{my})"
|
| 303 |
+
draw2.rectangle([(combined_w - 280, combined_h - 18), (combined_w, combined_h)], fill=(0, 0, 0))
|
| 304 |
+
draw2.text((combined_w - 278, combined_h - 16), info_text, fill=(100, 200, 100))
|
| 305 |
+
|
| 306 |
buf = io.BytesIO()
|
| 307 |
+
combined.save(buf, format="JPEG", quality=quality, optimize=True)
|
| 308 |
+
data = base64.b64encode(buf.getvalue()).decode()
|
| 309 |
+
|
| 310 |
+
return {
|
| 311 |
+
"data": data,
|
| 312 |
+
"width": orig_w,
|
| 313 |
+
"height": orig_h,
|
| 314 |
+
"mouse_x": mx,
|
| 315 |
+
"mouse_y": my,
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
except Exception as e:
|
| 319 |
+
print(f"[capture_grid] {e}")
|
| 320 |
+
# fallback
|
| 321 |
+
plain = capture_screen(scale=scale, quality=quality)
|
| 322 |
+
mx, my = _get_mouse_pos()
|
| 323 |
+
w, h = _get_screen_size()
|
| 324 |
+
return {"data": plain, "width": w, "height": h, "mouse_x": mx, "mouse_y": my}
|
| 325 |
|
| 326 |
|
| 327 |
# ─── Command Runner ──────────────────────────────────
|
| 328 |
|
| 329 |
def run_raw_command(cmd: str, timeout: int = 60) -> dict:
|
|
|
|
| 330 |
env = {**os.environ, "DISPLAY": DISPLAY,
|
| 331 |
"PYTHONIOENCODING": "utf-8", "LANG": "en_US.UTF-8"}
|
| 332 |
try:
|
|
|
|
| 345 |
|
| 346 |
|
| 347 |
def run_command_smart(cmd: str, timeout: int = 60) -> dict:
|
|
|
|
|
|
|
|
|
|
| 348 |
res = run_raw_command(cmd, timeout=timeout)
|
| 349 |
stdout = res["stdout"].strip()
|
| 350 |
|
|
|
|
| 379 |
src_out = src_res["stdout"].strip()
|
| 380 |
|
| 381 |
if not _is_empty_result(src_out):
|
| 382 |
+
combined_header = f"[مصدر بديل: {src_name}]\n{'='*50}\n"
|
|
|
|
|
|
|
|
|
|
| 383 |
all_results.append(src_out)
|
|
|
|
| 384 |
for source2 in sources:
|
| 385 |
if source2["name"] != src_name:
|
| 386 |
s2 = run_raw_command(source2["cmd"], timeout=20)
|
|
|
|
| 388 |
if not _is_empty_result(s2_out):
|
| 389 |
all_results.append(f"\n[مصدر إضافي: {source2['name']}]\n{s2_out}")
|
| 390 |
break
|
|
|
|
| 391 |
final_out = combined_header + "\n\n".join(all_results)
|
| 392 |
return {
|
| 393 |
"stdout": final_out[:15000],
|
|
|
|
| 412 |
return {"rc": r.returncode, "out": r.stdout, "err": r.stderr}
|
| 413 |
|
| 414 |
|
| 415 |
+
def type_text_smart(text: str) -> dict:
|
| 416 |
+
"""
|
| 417 |
+
كتابة نص ذكية — تدعم العربية والإنجليزية:
|
| 418 |
+
- للنصوص الإنجليزية: xdotool type مباشرة
|
| 419 |
+
- للنصوص العربية أو المختلطة: xclip clipboard ثم ctrl+v
|
| 420 |
+
"""
|
| 421 |
+
has_arabic = bool(re.search(r'[\u0600-\u06FF]', text))
|
| 422 |
+
|
| 423 |
+
if has_arabic:
|
| 424 |
+
# طريقة Clipboard لضمان كتابة العربية بشكل صحيح
|
| 425 |
+
try:
|
| 426 |
+
proc = subprocess.Popen(
|
| 427 |
+
["xclip", "-selection", "clipboard"],
|
| 428 |
+
stdin=subprocess.PIPE,
|
| 429 |
+
env={**os.environ, "DISPLAY": DISPLAY}
|
| 430 |
+
)
|
| 431 |
+
proc.communicate(text.encode("utf-8"))
|
| 432 |
+
time.sleep(0.15)
|
| 433 |
+
# Focus + paste
|
| 434 |
+
xdo(["key", "--clearmodifiers", "ctrl+v"])
|
| 435 |
+
return {"success": True, "method": "clipboard+paste", "arabic": True}
|
| 436 |
+
except Exception as e:
|
| 437 |
+
# fallback: xdotool type
|
| 438 |
+
r = xdo(["type", "--clearmodifiers", "--delay", "50", text])
|
| 439 |
+
return {"success": r["rc"] == 0, "method": "xdotool_fallback", "error": r["err"]}
|
| 440 |
+
else:
|
| 441 |
+
# إنجليزية: xdotool type مباشرة
|
| 442 |
+
r = xdo(["type", "--clearmodifiers", "--delay", "30", text])
|
| 443 |
+
return {"success": r["rc"] == 0, "method": "xdotool_direct"}
|
| 444 |
+
|
| 445 |
+
|
| 446 |
+
def open_browser_smart(url: str = "") -> str:
|
| 447 |
+
"""يفتح المتصفح بطريقة ذكية مع fallback"""
|
| 448 |
+
browser_cmd = BROWSER
|
| 449 |
+
if not url:
|
| 450 |
+
url = "about:blank"
|
| 451 |
+
|
| 452 |
+
# جرب أولاً: BROWSER العادي
|
| 453 |
+
cmd = f"{browser_cmd} --new-window '{url}' &"
|
| 454 |
+
proc = subprocess.Popen(cmd, shell=True,
|
| 455 |
+
env={**os.environ, "DISPLAY": DISPLAY},
|
| 456 |
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 457 |
+
return f"{browser_cmd} → {url}"
|
| 458 |
+
|
| 459 |
+
|
| 460 |
+
# ─── Simple Command Parser ─────────────────────────────
|
| 461 |
+
# يفهم أوامر بسيطة ويحولها لأفعال داخلية
|
| 462 |
+
|
| 463 |
+
def parse_simple_command(raw: str) -> dict | None:
|
| 464 |
+
"""
|
| 465 |
+
يُحلّل الأوامر البسيطة النصية وتُعيد action dict
|
| 466 |
+
أمثلة:
|
| 467 |
+
open pc → {"action":"screenshot"}
|
| 468 |
+
open firefox → {"action":"open_browser","url":""}
|
| 469 |
+
open chrome → {"action":"open_browser","url":""}
|
| 470 |
+
open browser → {"action":"open_browser","url":""}
|
| 471 |
+
open url https://google.com → {"action":"open_browser","url":"https://..."}
|
| 472 |
+
mouse go x500 y300 → {"action":"mouse_move","x":500,"y":300}
|
| 473 |
+
mouse go 500 300 → {"action":"mouse_move","x":500,"y":300}
|
| 474 |
+
click x500 y300 → {"action":"mouse_click","x":500,"y":300,"button":"left"}
|
| 475 |
+
click 500 300 → {"action":"mouse_click","x":500,"y":300,"button":"left"}
|
| 476 |
+
rclick x500 y300 → {"action":"mouse_click","x":500,"y":300,"button":"right"}
|
| 477 |
+
dclick x500 y300 → {"action":"mouse_click","x":500,"y":300,"double":true}
|
| 478 |
+
type hello world → {"action":"keyboard_type","text":"hello world"}
|
| 479 |
+
key enter → {"action":"keyboard_hotkey","keys":["Return"]}
|
| 480 |
+
key ctrl+c → {"action":"keyboard_hotkey","keys":["ctrl","c"]}
|
| 481 |
+
scroll up → {"action":"scroll","x":960,"y":540,"clicks":3}
|
| 482 |
+
scroll down → {"action":"scroll","x":960,"y":540,"clicks":-3}
|
| 483 |
+
screenshot → {"action":"screenshot"}
|
| 484 |
+
screen info → {"action":"screen_info"}
|
| 485 |
+
"""
|
| 486 |
+
s = raw.strip().lower()
|
| 487 |
+
|
| 488 |
+
# open pc / open computer / open screen
|
| 489 |
+
if re.match(r'^open\s+(pc|computer|screen|desktop)$', s):
|
| 490 |
+
return {"action": "screenshot"}
|
| 491 |
+
|
| 492 |
+
# open firefox / chrome / browser / browser url
|
| 493 |
+
m = re.match(r'^open\s+(firefox|chrome|chromium|browser|web|internet)(\s+(.+))?$', s)
|
| 494 |
+
if m:
|
| 495 |
+
url = (m.group(3) or "").strip()
|
| 496 |
+
if url and not url.startswith("http"):
|
| 497 |
+
url = "https://" + url
|
| 498 |
+
return {"action": "open_browser", "url": url or ""}
|
| 499 |
+
|
| 500 |
+
# open url <url>
|
| 501 |
+
m = re.match(r'^open\s+url\s+(\S+)$', s)
|
| 502 |
+
if m:
|
| 503 |
+
url = m.group(1)
|
| 504 |
+
if not url.startswith("http"):
|
| 505 |
+
url = "https://" + url
|
| 506 |
+
return {"action": "open_browser", "url": url}
|
| 507 |
+
|
| 508 |
+
# mouse go x500 y300 OR mouse go 500 300
|
| 509 |
+
m = re.match(r'^mouse\s+(?:go|move|to)\s+x?(\d+)\s+y?(\d+)$', s)
|
| 510 |
+
if m:
|
| 511 |
+
return {"action": "mouse_move", "x": int(m.group(1)), "y": int(m.group(2))}
|
| 512 |
+
|
| 513 |
+
# click x500 y300 OR click 500 300
|
| 514 |
+
m = re.match(r'^click\s+x?(\d+)\s+y?(\d+)$', s)
|
| 515 |
+
if m:
|
| 516 |
+
return {"action": "mouse_click", "x": int(m.group(1)), "y": int(m.group(2)), "button": "left"}
|
| 517 |
+
|
| 518 |
+
# rclick / right click
|
| 519 |
+
m = re.match(r'^r(?:ight)?click\s+x?(\d+)\s+y?(\d+)$', s)
|
| 520 |
+
if m:
|
| 521 |
+
return {"action": "mouse_click", "x": int(m.group(1)), "y": int(m.group(2)), "button": "right"}
|
| 522 |
+
|
| 523 |
+
# dclick / double click
|
| 524 |
+
m = re.match(r'^d(?:ouble)?click\s+x?(\d+)\s+y?(\d+)$', s)
|
| 525 |
+
if m:
|
| 526 |
+
return {"action": "mouse_click", "x": int(m.group(1)), "y": int(m.group(2)), "button": "left", "double": True}
|
| 527 |
+
|
| 528 |
+
# type <text> (يحتفظ بالحالة الأصلية)
|
| 529 |
+
m = re.match(r'^type\s+(.+)$', raw.strip(), re.IGNORECASE)
|
| 530 |
+
if m:
|
| 531 |
+
return {"action": "keyboard_type", "text": m.group(1)}
|
| 532 |
+
|
| 533 |
+
# key <keys>
|
| 534 |
+
m = re.match(r'^key\s+(.+)$', s)
|
| 535 |
+
if m:
|
| 536 |
+
k = m.group(1).strip()
|
| 537 |
+
key_map = {
|
| 538 |
+
"enter": "Return", "return": "Return", "esc": "Escape", "escape": "Escape",
|
| 539 |
+
"tab": "Tab", "space": "space", "backspace": "BackSpace", "delete": "Delete",
|
| 540 |
+
"up": "Up", "down": "Down", "left": "Left", "right": "Right",
|
| 541 |
+
"home": "Home", "end": "End", "pageup": "Prior", "pagedown": "Next",
|
| 542 |
+
"f1": "F1", "f2": "F2", "f3": "F3", "f4": "F4", "f5": "F5",
|
| 543 |
+
}
|
| 544 |
+
if '+' in k:
|
| 545 |
+
parts = [p.strip() for p in k.split('+')]
|
| 546 |
+
keys = [key_map.get(p, p) for p in parts]
|
| 547 |
+
else:
|
| 548 |
+
keys = [key_map.get(k, k)]
|
| 549 |
+
return {"action": "keyboard_hotkey", "keys": keys}
|
| 550 |
+
|
| 551 |
+
# scroll up / scroll down / scroll <n>
|
| 552 |
+
m = re.match(r'^scroll\s+(up|down|(\-?\d+))$', s)
|
| 553 |
+
if m:
|
| 554 |
+
w, h = _get_screen_size()
|
| 555 |
+
direction = m.group(1)
|
| 556 |
+
if direction == "up":
|
| 557 |
+
clicks = 4
|
| 558 |
+
elif direction == "down":
|
| 559 |
+
clicks = -4
|
| 560 |
+
else:
|
| 561 |
+
clicks = int(direction)
|
| 562 |
+
return {"action": "scroll", "x": w // 2, "y": h // 2, "clicks": clicks}
|
| 563 |
+
|
| 564 |
+
# screenshot
|
| 565 |
+
if s in ("screenshot", "screen", "ss", "snap"):
|
| 566 |
+
return {"action": "screenshot"}
|
| 567 |
+
|
| 568 |
+
# screen info
|
| 569 |
+
if re.match(r'^screen\s+info$', s):
|
| 570 |
+
return {"action": "screen_info"}
|
| 571 |
+
|
| 572 |
+
return None # لم يُعرف
|
| 573 |
+
|
| 574 |
+
|
| 575 |
async def broadcast(msg: dict):
|
| 576 |
txt = json.dumps(msg, ensure_ascii=False)
|
| 577 |
dead = []
|
|
|
|
| 605 |
async def send(obj):
|
| 606 |
await ws.send_text(json.dumps(obj, ensure_ascii=False))
|
| 607 |
|
| 608 |
+
async def auto_shot_grid(label="", delay=0.5):
|
| 609 |
+
"""screenshot مع grid"""
|
| 610 |
await asyncio.sleep(delay)
|
| 611 |
+
result = capture_screen_with_grid(scale=0.65, quality=72)
|
| 612 |
+
if result["data"]:
|
| 613 |
+
await send({
|
| 614 |
+
"type": "screenshot",
|
| 615 |
+
"data": result["data"],
|
| 616 |
+
"ts": int(time.time() * 1000),
|
| 617 |
+
"auto": True,
|
| 618 |
+
"label": label,
|
| 619 |
+
"screen_width": result["width"],
|
| 620 |
+
"screen_height": result["height"],
|
| 621 |
+
"mouse_x": result["mouse_x"],
|
| 622 |
+
"mouse_y": result["mouse_y"],
|
| 623 |
+
"has_grid": True,
|
| 624 |
+
})
|
| 625 |
+
return result["data"]
|
| 626 |
+
|
| 627 |
+
# ── screenshot ────────────────────────────────────
|
| 628 |
if action == "screenshot":
|
| 629 |
+
result = capture_screen_with_grid(scale=0.65, quality=75)
|
| 630 |
+
await send({
|
| 631 |
+
"type": "screenshot",
|
| 632 |
+
"data": result["data"],
|
| 633 |
+
"ts": int(time.time() * 1000),
|
| 634 |
+
"screen_width": result["width"],
|
| 635 |
+
"screen_height": result["height"],
|
| 636 |
+
"mouse_x": result["mouse_x"],
|
| 637 |
+
"mouse_y": result["mouse_y"],
|
| 638 |
+
"has_grid": True,
|
| 639 |
+
})
|
| 640 |
|
| 641 |
+
# ── simple_command — الأوامر البسيطة ──────────────
|
| 642 |
+
elif action == "simple_command":
|
| 643 |
+
raw_cmd = data.get("cmd", "").strip()
|
| 644 |
+
parsed = parse_simple_command(raw_cmd)
|
| 645 |
+
if parsed is None:
|
| 646 |
+
await send({
|
| 647 |
+
"type": "simple_command_result",
|
| 648 |
+
"ok": False,
|
| 649 |
+
"cmd": raw_cmd,
|
| 650 |
+
"error": f"أمر غير معروف: '{raw_cmd}'\nالأوامر المتاحة: open pc, open firefox, mouse go x y, click x y, rclick x y, type TEXT, key ENTER, scroll up/down, screenshot"
|
| 651 |
+
})
|
| 652 |
+
return
|
| 653 |
+
# نفّذ الأمر المُحلّل
|
| 654 |
+
await send({"type": "simple_command_result", "ok": True, "cmd": raw_cmd, "parsed": parsed})
|
| 655 |
+
# أعِد تشغيل نفس الـ handle_action مع الأمر المُحلّل
|
| 656 |
+
sub_msg = {"action": parsed["action"], "data": {k: v for k, v in parsed.items() if k != "action"}}
|
| 657 |
+
await handle_action(ws, sub_msg)
|
| 658 |
+
return
|
| 659 |
+
|
| 660 |
+
# ── terminal ──────────────────────────────────────
|
| 661 |
elif action == "terminal":
|
| 662 |
cmd = data.get("cmd", "")
|
| 663 |
timeout = int(data.get("timeout", 60))
|
|
|
|
| 665 |
await send({"type": "terminal_result", "cmd": "", "stdout": "",
|
| 666 |
"stderr": "no command", "returncode": -1})
|
| 667 |
return
|
|
|
|
| 668 |
res = run_command_smart(cmd, timeout=timeout)
|
|
|
|
| 669 |
await send({
|
| 670 |
"type": "terminal_result",
|
| 671 |
"cmd": cmd,
|
|
|
|
| 675 |
"fallback_used": res.get("_fallback_used", None),
|
| 676 |
"sources_tried": res.get("_sources_tried", []),
|
| 677 |
})
|
| 678 |
+
await auto_shot_grid(f"بعد: {cmd[:45]}", delay=0.4)
|
| 679 |
|
| 680 |
+
# ── mouse_move ────────────────────────────────────
|
|
|
|
| 681 |
elif action == "mouse_move":
|
| 682 |
x, y = int(data.get("x", 0)), int(data.get("y", 0))
|
| 683 |
xdo(["mousemove", str(x), str(y)])
|
| 684 |
+
await send({"type": "ack", "action": "mouse_move", "x": x, "y": y})
|
| 685 |
+
# screenshot مع grid بعد التحريك
|
| 686 |
+
await auto_shot_grid(f"ماوس → ({x},{y})", delay=0.2)
|
| 687 |
|
| 688 |
+
# ── mouse_click ───────────────────────────────────
|
| 689 |
elif action == "mouse_click":
|
| 690 |
x, y = int(data.get("x", 0)), int(data.get("y", 0))
|
| 691 |
+
btn_num = {"left": "1", "middle": "2", "right": "3"}.get(data.get("button", "left"), "1")
|
| 692 |
xdo(["mousemove", str(x), str(y)])
|
| 693 |
+
await asyncio.sleep(0.08)
|
| 694 |
if data.get("double"):
|
| 695 |
xdo(["click", "--repeat", "2", "--delay", "100", btn_num])
|
| 696 |
else:
|
| 697 |
xdo(["click", btn_num])
|
| 698 |
+
btn_name = {"1": "left", "2": "middle", "3": "right"}.get(btn_num, "left")
|
| 699 |
+
await send({"type": "ack", "action": "mouse_click", "x": x, "y": y, "button": btn_name})
|
| 700 |
+
await auto_shot_grid(f"نقر {btn_name} → ({x},{y})", delay=0.5)
|
| 701 |
|
| 702 |
+
# ── keyboard_type ─────────────────────────────────
|
| 703 |
elif action == "keyboard_type":
|
| 704 |
text = data.get("text", "")
|
| 705 |
if text:
|
| 706 |
+
result = type_text_smart(text)
|
| 707 |
+
await send({"type": "ack", "action": "keyboard_type",
|
| 708 |
+
"method": result["method"], "text_len": len(text)})
|
| 709 |
+
else:
|
| 710 |
+
await send({"type": "ack", "action": "keyboard_type"})
|
| 711 |
+
await auto_shot_grid("بعد الكتابة", delay=0.5)
|
| 712 |
|
| 713 |
+
# ── keyboard_hotkey ───────────────────────────────
|
| 714 |
elif action == "keyboard_hotkey":
|
| 715 |
keys = data.get("keys", [])
|
| 716 |
if keys:
|
| 717 |
xdo(["key", "--clearmodifiers", "+".join(keys)])
|
| 718 |
+
await send({"type": "ack", "action": "keyboard_hotkey", "keys": keys})
|
| 719 |
+
await auto_shot_grid("بعد الاختصار", delay=0.5)
|
| 720 |
|
| 721 |
+
# ── keyboard_press ────────────────────────────────
|
| 722 |
elif action == "keyboard_press":
|
| 723 |
key = data.get("key", "")
|
| 724 |
if key:
|
| 725 |
xdo(["key", "--clearmodifiers", key])
|
| 726 |
await send({"type": "ack", "action": "keyboard_press"})
|
| 727 |
+
await auto_shot_grid("بعد المفتاح", delay=0.4)
|
| 728 |
|
| 729 |
+
# ── clipboard_write ───────────────────────────────
|
| 730 |
elif action == "clipboard_write":
|
| 731 |
+
text = data.get("text", "")
|
| 732 |
+
try:
|
| 733 |
+
proc = subprocess.Popen(
|
| 734 |
+
["xclip", "-selection", "clipboard"],
|
| 735 |
+
stdin=subprocess.PIPE,
|
| 736 |
+
env={**os.environ, "DISPLAY": DISPLAY}
|
| 737 |
+
)
|
| 738 |
+
proc.communicate(text.encode("utf-8"))
|
| 739 |
+
await send({"type": "ack", "action": "clipboard_write", "length": len(text)})
|
| 740 |
+
except Exception as e:
|
| 741 |
+
await send({"type": "error", "action": "clipboard_write", "msg": str(e)})
|
| 742 |
|
| 743 |
+
# ── clipboard_read ────────────────────────────────
|
| 744 |
elif action == "clipboard_read":
|
| 745 |
res = run_raw_command("xclip -selection clipboard -o", timeout=5)
|
| 746 |
await send({"type": "clipboard_content", "text": res["stdout"]})
|
| 747 |
|
| 748 |
+
# ── scroll ────────────────────────────────────────
|
| 749 |
elif action == "scroll":
|
| 750 |
x, y = int(data.get("x", 0)), int(data.get("y", 0))
|
| 751 |
clicks = int(data.get("clicks", 3))
|
|
|
|
| 753 |
xdo(["mousemove", str(x), str(y)])
|
| 754 |
for _ in range(abs(clicks)):
|
| 755 |
xdo(["click", btn])
|
| 756 |
+
await asyncio.sleep(0.03)
|
| 757 |
+
await send({"type": "ack", "action": "scroll", "clicks": clicks})
|
| 758 |
+
await auto_shot_grid("بعد التمرير", delay=0.4)
|
| 759 |
|
| 760 |
+
# ── open_app ──────────────────────────────────────
|
| 761 |
elif action == "open_app":
|
| 762 |
app_cmd = data.get("cmd", "")
|
| 763 |
if app_cmd:
|
|
|
|
| 764 |
fixed_cmd = app_cmd
|
| 765 |
+
# ��صلاح firefox-esr
|
| 766 |
if "firefox-esr" in app_cmd:
|
| 767 |
esr_check = subprocess.run(["which", "firefox-esr"], capture_output=True, text=True)
|
| 768 |
if esr_check.returncode != 0:
|
| 769 |
fixed_cmd = app_cmd.replace("firefox-esr", BROWSER)
|
| 770 |
+
|
| 771 |
subprocess.Popen(fixed_cmd, shell=True,
|
| 772 |
env={**os.environ, "DISPLAY": DISPLAY},
|
| 773 |
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 774 |
await send({"type": "ack", "action": "open_app", "cmd": fixed_cmd})
|
| 775 |
+
|
| 776 |
+
wait_time = 7.0 if any(x in app_cmd for x in ["firefox", "chromium", "chrome"]) else 4.0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
await asyncio.sleep(2.0)
|
| 778 |
+
await auto_shot_grid(f"فتح: {fixed_cmd[:40]} (2s)", delay=0)
|
| 779 |
await asyncio.sleep(wait_time - 2.0)
|
| 780 |
+
await auto_shot_grid(f"بعد تحميل: {fixed_cmd[:40]}", delay=0)
|
| 781 |
else:
|
| 782 |
await send({"type": "ack", "action": "open_app"})
|
| 783 |
|
| 784 |
+
# ── open_browser — فتح المتصفح ────────────────────
|
| 785 |
+
elif action == "open_browser":
|
| 786 |
+
url = data.get("url", "")
|
| 787 |
+
if not url:
|
| 788 |
+
url = "about:blank"
|
| 789 |
+
browser_result = open_browser_smart(url)
|
| 790 |
+
await send({"type": "ack", "action": "open_browser", "result": browser_result})
|
| 791 |
+
await asyncio.sleep(2.0)
|
| 792 |
+
await auto_shot_grid(f"فتح المتصفح: {url[:50]}", delay=0)
|
| 793 |
+
await asyncio.sleep(5.0)
|
| 794 |
+
await auto_shot_grid("بعد تحميل المتصفح", delay=0)
|
| 795 |
+
|
| 796 |
+
# ── mouse_drag ────────────────────────────────────
|
| 797 |
elif action == "mouse_drag":
|
| 798 |
+
x1, y1 = int(data.get("x1", 0)), int(data.get("y1", 0))
|
| 799 |
+
x2, y2 = int(data.get("x2", 0)), int(data.get("y2", 0))
|
| 800 |
xdo(["mousemove", str(x1), str(y1)])
|
| 801 |
xdo(["mousedown", "1"])
|
| 802 |
await asyncio.sleep(0.1)
|
|
|
|
| 804 |
await asyncio.sleep(0.1)
|
| 805 |
xdo(["mouseup", "1"])
|
| 806 |
await send({"type": "ack", "action": "mouse_drag"})
|
| 807 |
+
await auto_shot_grid("بعد السحب", delay=0.4)
|
| 808 |
|
| 809 |
+
# ── start_stream ──────────────────────────────────
|
| 810 |
elif action == "start_stream":
|
| 811 |
global stream_active, stream_fps, stream_quality, stream_scale
|
| 812 |
stream_fps = int(data.get("fps", 3))
|
|
|
|
| 817 |
asyncio.create_task(screen_stream_loop())
|
| 818 |
await send({"type": "ack", "action": "start_stream"})
|
| 819 |
|
| 820 |
+
# ── stop_stream ───────────────────────────────────
|
| 821 |
elif action == "stop_stream":
|
| 822 |
stream_active = False
|
| 823 |
await send({"type": "ack", "action": "stop_stream"})
|
| 824 |
|
| 825 |
+
# ── screen_info ───────────────────────────────────
|
| 826 |
elif action == "screen_info":
|
| 827 |
+
w, h = _get_screen_size()
|
| 828 |
+
mx, my = _get_mouse_pos()
|
| 829 |
+
await send({
|
| 830 |
+
"type": "screen_info",
|
| 831 |
+
"width": w, "height": h,
|
| 832 |
+
"mouse_x": mx, "mouse_y": my,
|
| 833 |
+
"browser": BROWSER,
|
| 834 |
+
})
|
| 835 |
+
|
| 836 |
+
# ── paste ─────────────────────────────────────────
|
| 837 |
+
elif action == "paste":
|
| 838 |
+
text = data.get("text", "")
|
| 839 |
+
if text:
|
| 840 |
+
proc = subprocess.Popen(
|
| 841 |
+
["xclip", "-selection", "clipboard"],
|
| 842 |
+
stdin=subprocess.PIPE,
|
| 843 |
+
env={**os.environ, "DISPLAY": DISPLAY}
|
| 844 |
+
)
|
| 845 |
+
proc.communicate(text.encode("utf-8"))
|
| 846 |
+
await asyncio.sleep(0.1)
|
| 847 |
+
xdo(["key", "--clearmodifiers", "ctrl+v"])
|
| 848 |
+
await send({"type": "ack", "action": "paste"})
|
| 849 |
+
await auto_shot_grid("بعد اللصق", delay=0.5)
|
| 850 |
|
| 851 |
+
# ── unknown ───────────────────────────────────────
|
| 852 |
else:
|
| 853 |
+
await send({
|
| 854 |
+
"type": "error",
|
| 855 |
+
"msg": f"Unknown action: '{action}'. Available: screenshot, terminal, mouse_move, mouse_click, mouse_drag, keyboard_type, keyboard_hotkey, keyboard_press, clipboard_write, clipboard_read, scroll, open_app, open_browser, paste, simple_command, screen_info, start_stream, stop_stream"
|
| 856 |
+
})
|
| 857 |
|
| 858 |
|
| 859 |
# ─── WebSocket ───────────────────────────────────────
|
|
|
|
| 862 |
async def websocket_endpoint(ws: WebSocket):
|
| 863 |
await ws.accept()
|
| 864 |
active_connections.append(ws)
|
| 865 |
+
w, h = _get_screen_size()
|
|
|
|
|
|
|
| 866 |
await ws.send_text(json.dumps({
|
| 867 |
+
"type": "connected",
|
| 868 |
+
"screen_width": w,
|
| 869 |
+
"screen_height": h,
|
| 870 |
"browser": BROWSER,
|
| 871 |
+
"msg": f"Z Computer Mode v7 — Smart Control | Browser: {BROWSER} | Screen: {w}x{h}",
|
| 872 |
+
"commands": [
|
| 873 |
+
"screenshot", "terminal {cmd}", "mouse_move {x,y}", "mouse_click {x,y,button}",
|
| 874 |
+
"keyboard_type {text}", "keyboard_hotkey {keys}", "scroll {x,y,clicks}",
|
| 875 |
+
"open_app {cmd}", "open_browser {url}", "simple_command {cmd}",
|
| 876 |
+
"Simple cmds: 'open firefox', 'mouse go x y', 'click x y', 'type TEXT', 'key enter'"
|
| 877 |
+
]
|
| 878 |
}, ensure_ascii=False))
|
| 879 |
+
# أرسل screenshot أولية مع grid
|
| 880 |
+
result = capture_screen_with_grid(scale=0.65, quality=72)
|
| 881 |
+
if result["data"]:
|
| 882 |
+
await ws.send_text(json.dumps({
|
| 883 |
+
"type": "screenshot",
|
| 884 |
+
"data": result["data"],
|
| 885 |
+
"ts": int(time.time() * 1000),
|
| 886 |
+
"label": "الشاشة الأولية",
|
| 887 |
+
"screen_width": result["width"],
|
| 888 |
+
"screen_height": result["height"],
|
| 889 |
+
"mouse_x": result["mouse_x"],
|
| 890 |
+
"mouse_y": result["mouse_y"],
|
| 891 |
+
"has_grid": True,
|
| 892 |
+
}, ensure_ascii=False))
|
| 893 |
try:
|
| 894 |
while True:
|
| 895 |
raw = await ws.receive_text()
|
| 896 |
await handle_action(ws, json.loads(raw))
|
| 897 |
+
except WebSocketDisconnect:
|
| 898 |
+
pass
|
| 899 |
+
except Exception as e:
|
| 900 |
+
print(f"[ws] {e}")
|
| 901 |
finally:
|
| 902 |
if ws in active_connections:
|
| 903 |
active_connections.remove(ws)
|
|
|
|
| 907 |
|
| 908 |
@app.get("/screenshot")
|
| 909 |
async def rest_screenshot():
|
| 910 |
+
result = capture_screen_with_grid(scale=0.7, quality=73)
|
| 911 |
+
return JSONResponse({
|
| 912 |
+
"image": result["data"],
|
| 913 |
+
"ts": int(time.time() * 1000),
|
| 914 |
+
"screen_width": result["width"],
|
| 915 |
+
"screen_height": result["height"],
|
| 916 |
+
"mouse_x": result["mouse_x"],
|
| 917 |
+
"mouse_y": result["mouse_y"],
|
| 918 |
+
"has_grid": True,
|
| 919 |
+
})
|
| 920 |
+
|
| 921 |
+
@app.get("/screenshot/clean")
|
| 922 |
+
async def rest_screenshot_clean():
|
| 923 |
+
return JSONResponse({"image": capture_screen(0.75, 75), "ts": int(time.time() * 1000)})
|
| 924 |
|
| 925 |
@app.post("/terminal")
|
| 926 |
async def rest_terminal(body: dict):
|
| 927 |
+
return JSONResponse(run_command_smart(body.get("cmd", ""), body.get("timeout", 60)))
|
| 928 |
+
|
| 929 |
+
@app.post("/simple")
|
| 930 |
+
async def rest_simple(body: dict):
|
| 931 |
+
"""تنفيذ أمر بسيط عبر REST"""
|
| 932 |
+
raw = body.get("cmd", "").strip()
|
| 933 |
+
parsed = parse_simple_command(raw)
|
| 934 |
+
if not parsed:
|
| 935 |
+
return JSONResponse({"ok": False, "error": f"Unknown simple command: {raw}"}, status_code=400)
|
| 936 |
+
return JSONResponse({"ok": True, "parsed": parsed})
|
| 937 |
|
| 938 |
@app.get("/health")
|
| 939 |
async def health():
|
| 940 |
+
w, h = _get_screen_size()
|
| 941 |
+
mx, my = _get_mouse_pos()
|
| 942 |
browser_check = subprocess.run(["which", "firefox-esr"], capture_output=True, text=True)
|
| 943 |
return {
|
| 944 |
"status": "ok",
|
| 945 |
+
"version": "v7-smart-control",
|
| 946 |
"browser": BROWSER,
|
| 947 |
"firefox_esr_available": browser_check.returncode == 0,
|
| 948 |
+
"screen_width": w,
|
| 949 |
+
"screen_height": h,
|
| 950 |
+
"mouse_x": mx,
|
| 951 |
+
"mouse_y": my,
|
| 952 |
}
|
| 953 |
|
| 954 |
@app.get("/search/{query}")
|