-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathmain.py
More file actions
618 lines (517 loc) · 19.3 KB
/
main.py
File metadata and controls
618 lines (517 loc) · 19.3 KB
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
import time
from re import search as re_search
from shlex import quote as quote_string
from typing import Callable, Dict, Iterator, Literal, Optional, overload, Tuple, Union
from uuid import uuid4
from e2b import (
Sandbox as SandboxBase,
CommandHandle,
CommandResult,
TimeoutException,
CommandExitException,
)
from e2b.connection_config import ApiParams
from typing_extensions import Self, Unpack
MOUSE_BUTTONS = {"left": 1, "right": 3, "middle": 2}
KEYS = {
"alt": "Alt_L",
"alt_left": "Alt_L",
"alt_right": "Alt_R",
"backspace": "BackSpace",
"break": "Pause",
"caps_lock": "Caps_Lock",
"cmd": "Super_L",
"command": "Super_L",
"control": "Control_L",
"control_left": "Control_L",
"control_right": "Control_R",
"ctrl": "Control_L",
"del": "Delete",
"delete": "Delete",
"down": "Down",
"end": "End",
"enter": "Return",
"esc": "Escape",
"escape": "Escape",
"f1": "F1",
"f2": "F2",
"f3": "F3",
"f4": "F4",
"f5": "F5",
"f6": "F6",
"f7": "F7",
"f8": "F8",
"f9": "F9",
"f10": "F10",
"f11": "F11",
"f12": "F12",
"home": "Home",
"insert": "Insert",
"left": "Left",
"menu": "Menu",
"meta": "Meta_L",
"num_lock": "Num_Lock",
"page_down": "Page_Down",
"page_up": "Page_Up",
"pause": "Pause",
"print": "Print",
"right": "Right",
"scroll_lock": "Scroll_Lock",
"shift": "Shift_L",
"shift_left": "Shift_L",
"shift_right": "Shift_R",
"space": "space",
"super": "Super_L",
"super_left": "Super_L",
"super_right": "Super_R",
"tab": "Tab",
"up": "Up",
"win": "Super_L",
"windows": "Super_L",
}
def map_key(key: str) -> str:
lower_key = key.lower()
if lower_key in KEYS:
return KEYS[lower_key]
return lower_key
class _VNCServer:
def __init__(self, desktop: "Sandbox") -> None:
self.__novnc_handle: Optional[CommandHandle] = None
self._vnc_port = 5900
self._port = 6080
self._novnc_auth_enabled = False
self._novnc_password = None
self._url = f"https://{desktop.get_host(self._port)}/vnc.html"
self.__desktop = desktop
def _wait_for_port(self, port: int) -> bool:
return self.__desktop._wait_and_verify(
f'netstat -tuln | grep ":{port} "', lambda r: r.stdout.strip() != ""
)
def _check_vnc_running(self) -> bool:
try:
self.__desktop.commands.run("pgrep -x x11vnc")
return True
except CommandExitException:
return False
@staticmethod
def _generate_password(length: int = 16) -> str:
import secrets
import string
characters = string.ascii_letters + string.digits
return "".join(secrets.choice(characters) for _ in range(length))
def get_url(
self,
auto_connect: bool = True,
view_only: bool = False,
resize: str = "scale",
auth_key: Optional[str] = None,
) -> str:
params = []
if auto_connect:
params.append("autoconnect=true")
if view_only:
params.append("view_only=true")
if resize:
params.append(f"resize={resize}")
if auth_key:
params.append(f"password={auth_key}")
if params:
return f"{self._url}?{'&'.join(params)}"
return self._url
def get_auth_key(self) -> str:
if not self._novnc_password:
raise RuntimeError(
"Unable to retrieve stream auth key, check if require_auth is enabled"
)
return self._novnc_password
def start(
self,
vnc_port: Optional[int] = None,
port: Optional[int] = None,
require_auth: bool = False,
window_id: Optional[str] = None,
) -> None:
# If stream is already running, throw an error
if self._check_vnc_running():
raise RuntimeError("Stream is already running")
# Update parameters if provided
self._vnc_port = vnc_port or self._vnc_port
self._port = port or self._port
self._novnc_auth_enabled = require_auth or self._novnc_auth_enabled
self._novnc_password = self._generate_password() if require_auth else None
# Update URL with new port
self._url = f"https://{self.__desktop.get_host(self._port)}/vnc.html"
# Set up VNC command
pwd_flag = "-nopw"
if self._novnc_auth_enabled:
self.__desktop.commands.run("mkdir -p ~/.vnc")
self.__desktop.commands.run(
f"x11vnc -storepasswd {self._novnc_password} ~/.vnc/passwd"
)
pwd_flag = "-usepw"
window_id_flag = ""
if window_id:
window_id_flag = f"-id {window_id}"
vnc_command = (
f"x11vnc -bg -display {self.__desktop._display} -forever -wait 50 -shared "
f"-rfbport {self._vnc_port} {pwd_flag} 2>/tmp/x11vnc_stderr.log {window_id_flag}"
)
novnc_command = (
f"cd /opt/noVNC/utils && ./novnc_proxy --vnc localhost:{self._vnc_port} "
f"--listen {self._port} --web /opt/noVNC > /tmp/novnc.log 2>&1"
)
self.__desktop.commands.run(vnc_command)
self.__novnc_handle = self.__desktop.commands.run(
novnc_command, background=True, timeout=0
)
if not self._wait_for_port(self._port):
raise TimeoutException("Could not start noVNC server")
def stop(self) -> None:
if self._check_vnc_running():
self.__desktop.commands.run("pkill x11vnc")
if self.__novnc_handle:
self.__novnc_handle.kill()
self.__novnc_handle = None
class Sandbox(SandboxBase):
default_template = "desktop"
__vnc_server: _VNCServer
_last_xfce4_pid: Optional[str] = None
_display: str
@classmethod
def create(
cls,
template: Optional[str] = None,
resolution: Optional[Tuple[int, int]] = None,
dpi: Optional[int] = None,
display: Optional[str] = None,
timeout: Optional[int] = None,
metadata: Optional[Dict[str, str]] = None,
envs: Optional[Dict[str, str]] = None,
secure: bool = True,
allow_internet_access: bool = True,
**opts: Unpack[ApiParams],
) -> Self:
"""
Create a new sandbox.
By default, the sandbox is created from the default `desktop` sandbox template.
:param template: Sandbox template name or ID
:param resolution: Startup the desktop with custom screen resolution. Defaults to (1024, 768)
:param dpi: Startup the desktop with custom DPI. Defaults to 96
:param display: Startup the desktop with custom display. Defaults to ":0"
:param timeout: Timeout for the sandbox in **seconds**, default to 300 seconds. The maximum time a sandbox can be kept alive is 24 hours (86_400 seconds) for Pro users and 1 hour (3_600 seconds) for Hobby users.
:param metadata: Custom metadata for the sandbox
:param envs: Custom environment variables for the sandbox
:param secure: Envd is secured with access token and cannot be used without it
:param allow_internet_access: Allow sandbox to access the internet, defaults to `True`.
:return: A Sandbox instance for the new sandbox
Use this method instead of using the constructor to create a new sandbox.
"""
# Initialize environment variables with DISPLAY
display = display or ":0"
if envs is None:
envs = {}
envs["DISPLAY"] = display
sbx = super().create(
template=template,
timeout=timeout,
metadata=metadata,
envs=envs,
secure=secure,
allow_internet_access=allow_internet_access,
**opts,
)
sbx._display = display
width, height = resolution or (1024, 768)
sbx.commands.run(
f"Xvfb {display} -ac -screen 0 {width}x{height}x24"
f" -retro -dpi {dpi or 96} -nolisten tcp -nolisten unix",
background=True,
timeout=0,
)
if not sbx._wait_and_verify(
f"xdpyinfo -display {display}", lambda r: r.exit_code == 0
):
raise TimeoutException("Could not start Xvfb")
sbx.__vnc_server = _VNCServer(sbx)
sbx._start_xfce4()
return sbx
@classmethod
def beta_create(
cls,
template: Optional[str] = None,
resolution: Optional[Tuple[int, int]] = None,
dpi: Optional[int] = None,
display: Optional[str] = None,
timeout: Optional[int] = None,
auto_pause: Optional[bool] = False,
metadata: Optional[Dict[str, str]] = None,
envs: Optional[Dict[str, str]] = None,
secure: bool = True,
allow_internet_access: bool = True,
**opts: Unpack[ApiParams],
) -> Self:
"""
[BETA] This feature is in beta and may change in the future.
Create a new sandbox.
By default, the sandbox is created from the default `desktop` sandbox template.
:param template: Sandbox template name or ID
:param resolution: Startup the desktop with custom screen resolution. Defaults to (1024, 768)
:param dpi: Startup the desktop with custom DPI. Defaults to 96
:param display: Startup the desktop with custom display. Defaults to ":0"
:param timeout: Timeout for the sandbox in **seconds**, default to 300 seconds. The maximum time a sandbox can be kept alive is 24 hours (86_400 seconds) for Pro users and 1 hour (3_600 seconds) for Hobby users.
:param auto_pause: Automatically pause the sandbox after the timeout expires. Defaults to `False`.
:param metadata: Custom metadata for the sandbox
:param envs: Custom environment variables for the sandbox
:param secure: Envd is secured with access token and cannot be used without it
:param allow_internet_access: Allow sandbox to access the internet, defaults to `True`.
:return: A Sandbox instance for the new sandbox
Use this method instead of using the constructor to create a new sandbox.
"""
# Initialize environment variables with DISPLAY
display = display or ":0"
if envs is None:
envs = {}
envs["DISPLAY"] = display
sbx = super().beta_create(
template=template,
timeout=timeout,
metadata=metadata,
envs=envs,
secure=secure,
allow_internet_access=allow_internet_access,
**opts,
)
sbx._display = display
width, height = resolution or (1024, 768)
sbx.commands.run(
f"Xvfb {display} -ac -screen 0 {width}x{height}x24"
f" -retro -dpi {dpi or 96} -nolisten tcp -nolisten unix",
background=True,
timeout=0,
)
if not sbx._wait_and_verify(
f"xdpyinfo -display {display}", lambda r: r.exit_code == 0
):
raise TimeoutException("Could not start Xvfb")
sbx.__vnc_server = _VNCServer(sbx)
sbx._start_xfce4()
return sbx
def _wait_and_verify(
self,
cmd: str,
on_result: Callable[[CommandResult], bool],
timeout: int = 10,
interval: float = 0.5,
) -> bool:
elapsed = 0
while elapsed < timeout:
try:
if on_result(self.commands.run(cmd)):
return True
except CommandExitException:
continue
time.sleep(interval)
elapsed += interval
return False
def _start_xfce4(self):
"""
Start xfce4 session if logged out or not running.
"""
if self._last_xfce4_pid is None or "[xfce4-session] <defunct>" in (
self.commands.run(
f"ps aux | grep {self._last_xfce4_pid} | grep -v grep | head -n 1"
).stdout.strip()
):
self._last_xfce4_pid = self.commands.run(
"startxfce4", background=True, timeout=0
).pid
@property
def stream(self) -> _VNCServer:
return self.__vnc_server
@overload
def screenshot(self, format: Literal["stream"]) -> Iterator[bytes]:
"""
Take a screenshot and return it as a stream of bytes.
"""
@overload
def screenshot(
self,
format: Literal["bytes"],
) -> bytearray:
"""
Take a screenshot and return it as a bytearray.
"""
def screenshot(
self,
format: Literal["bytes", "stream"] = "bytes",
):
"""
Take a screenshot and return it in the specified format.
:param format: The format of the screenshot. Can be 'bytes', 'blob', or 'stream'.
:returns: The screenshot in the specified format.
"""
screenshot_path = f"/tmp/screenshot-{uuid4()}.png"
self.commands.run(f"scrot --pointer {screenshot_path}")
file = self.files.read(screenshot_path, format=format)
self.files.remove(screenshot_path)
return file
def left_click(self, x: Optional[int] = None, y: Optional[int] = None):
"""
Left click on the mouse position.
"""
if x and y:
self.move_mouse(x, y)
self.commands.run("xdotool click 1")
def double_click(self, x: Optional[int] = None, y: Optional[int] = None):
"""
Double left click on the mouse position.
"""
if x and y:
self.move_mouse(x, y)
self.commands.run("xdotool click --repeat 2 1")
def right_click(self, x: Optional[int] = None, y: Optional[int] = None):
if (x is None) != (y is None):
raise ValueError("Both x and y must be provided together")
"""
Right click on the mouse position.
"""
if x and y:
self.move_mouse(x, y)
self.commands.run("xdotool click 3")
def middle_click(self, x: Optional[int] = None, y: Optional[int] = None):
"""
Middle click on the mouse position.
"""
if x and y:
self.move_mouse(x, y)
self.commands.run("xdotool click 2")
def scroll(self, direction: Literal["up", "down"] = "down", amount: int = 1):
"""
Scroll the mouse wheel by the given amount.
:param direction: The direction to scroll. Can be "up" or "down".
:param amount: The amount to scroll.
"""
self.commands.run(
f"xdotool click --repeat {amount} {'4' if direction == 'up' else '5'}"
)
def move_mouse(self, x: int, y: int):
"""
Move the mouse to the given coordinates.
:param x: The x coordinate.
:param y: The y coordinate.
"""
self.commands.run(f"xdotool mousemove --sync {x} {y}")
def mouse_press(self, button: Literal["left", "right", "middle"] = "left"):
"""
Press the mouse button.
"""
self.commands.run(f"xdotool mousedown {MOUSE_BUTTONS[button]}")
def mouse_release(self, button: Literal["left", "right", "middle"] = "left"):
"""
Release the mouse button.
"""
self.commands.run(f"xdotool mouseup {MOUSE_BUTTONS[button]}")
def get_cursor_position(self) -> tuple[int, int]:
"""
Get the current cursor position.
:return: A tuple with the x and y coordinates
:raises RuntimeError: If the cursor position cannot be determined
"""
result = self.commands.run("xdotool getmouselocation")
groups = re_search(r"x:(\d+)\s+y:(\d+)", result.stdout)
if not groups:
raise RuntimeError(
f"Failed to parse cursor position from output: {result.stdout}"
)
x, y = groups.group(1), groups.group(2)
if not x or not y:
raise RuntimeError(f"Invalid cursor position values: x={x}, y={y}")
return int(x), int(y)
def get_screen_size(self) -> tuple[int, int]:
"""
Get the current screen size.
:return: A tuple with the width and height
:raises RuntimeError: If the screen size cannot be determined
"""
result = self.commands.run("xrandr")
_match = re_search(r"(\d+x\d+)", result.stdout)
if not _match:
raise RuntimeError(
f"Failed to parse screen size from output: {result.stdout}"
)
try:
return tuple(map(int, _match.group(1).split("x"))) # type: ignore
except (ValueError, IndexError) as e:
raise RuntimeError(f"Invalid screen size format: {_match.group(1)}") from e
def write(self, text: str, *, chunk_size: int = 25, delay_in_ms: int = 75) -> None:
"""
Write the given text at the current cursor position.
:param text: The text to write.
:param chunk_size: The size of each chunk of text to write.
:param delay_in_ms: The delay between each chunk of text.
"""
def break_into_chunks(text: str, n: int):
for i in range(0, len(text), n):
yield text[i : i + n]
for chunk in break_into_chunks(text, chunk_size):
self.commands.run(
f"xdotool type --delay {delay_in_ms} -- {quote_string(chunk)}"
)
def press(self, key: Union[str, list[str]]):
"""
Press a key.
:param key: The key to press (e.g. "enter", "space", "backspace", etc.).
"""
if isinstance(key, list):
key = "+".join(map_key(k) for k in key)
else:
key = map_key(key)
self.commands.run(f"xdotool key {key}")
def drag(self, fr: tuple[int, int], to: tuple[int, int]):
"""
Drag the mouse from the given position to the given position.
:param from: The starting position.
:param to: The ending position.
"""
self.move_mouse(fr[0], fr[1])
self.mouse_press()
self.move_mouse(to[0], to[1])
self.mouse_release()
def wait(self, ms: int):
"""
Wait for the given amount of time.
:param ms: The amount of time to wait in milliseconds.
"""
self.commands.run(f"sleep {ms / 1000}")
def open(self, file_or_url: str):
"""
Open a file or a URL in the default application.
:param file_or_url: The file or URL to open.
"""
self.commands.run(f"xdg-open {file_or_url}", background=True)
def get_current_window_id(self) -> str:
"""
Get the current window ID.
"""
return self.commands.run("xdotool getwindowfocus").stdout.strip()
def get_application_windows(self, application: str) -> list[str]:
"""
Get the window IDs of all windows for the given application.
"""
return (
self.commands.run(f"xdotool search --onlyvisible --class {application}")
.stdout.strip()
.split("\n")
)
def get_window_title(self, window_id: str) -> str:
"""
Get the title of the window with the given ID.
"""
return self.commands.run(f"xdotool getwindowname {window_id}").stdout.strip()
def launch(self, application: str, uri: Optional[str] = None):
"""
Launch an application.
"""
self.commands.run(
f"gtk-launch {application} {uri or ''}", background=True, timeout=0
)