44from typing import Callable , Dict , Iterator , Literal , Optional , overload , Tuple , Union
55from uuid import uuid4
66
7- from e2b import Sandbox as SandboxBase , CommandHandle , CommandResult , TimeoutException , CommandExitException
7+ from e2b import Sandbox as SandboxBase , CommandHandle , CommandResult , TimeoutException , CommandExitException , ProxyTypes
88
99MOUSE_BUTTONS = {
1010 "left" : 1 ,
@@ -194,6 +194,7 @@ def __init__(
194194 debug : Optional [bool ] = None ,
195195 sandbox_id : Optional [str ] = None ,
196196 request_timeout : Optional [float ] = None ,
197+ proxy : Optional [ProxyTypes ] = None ,
197198 ):
198199 """
199200 Create a new desktop sandbox.
@@ -212,10 +213,12 @@ def __init__(
212213 :param debug: If True, the sandbox will be created in debug mode, defaults to `E2B_DEBUG` environment variable
213214 :param sandbox_id: Sandbox ID to connect to, defaults to `E2B_SANDBOX_ID` environment variable
214215 :param request_timeout: Timeout for the request in **seconds**
216+ :param proxy: Proxy to use for the request and for the requests made to the returned sandbox
215217
216218 :return: sandbox instance for the new sandbox
217219 """
218220 self ._display = display or ":0"
221+ self ._last_xfce4_pid = None
219222
220223 # Initialize environment variables with DISPLAY
221224 if envs is None :
@@ -232,26 +235,29 @@ def __init__(
232235 debug = debug ,
233236 sandbox_id = sandbox_id ,
234237 request_timeout = request_timeout ,
238+ proxy = proxy ,
235239 )
236240
237- self ._last_xfce4_pid = None
238-
239- width , height = resolution or (1024 , 768 )
240- self .commands .run (
241- f"Xvfb { self ._display } -ac -screen 0 { width } x{ height } x24"
242- f" -retro -dpi { dpi or 96 } -nolisten tcp -nolisten unix" ,
243- background = True ,
244- timeout = 0 ,
245- )
246-
247- if not self ._wait_and_verify (
248- f"xdpyinfo -display { self ._display } " , lambda r : r .exit_code == 0
249- ):
250- raise TimeoutException ("Could not start Xvfb" )
241+ # Only initialize desktop environment if we're not just connecting to an existing sandbox
242+ if not sandbox_id :
243+ width , height = resolution or (1024 , 768 )
244+ self .commands .run (
245+ f"Xvfb { self ._display } -ac -screen 0 { width } x{ height } x24"
246+ f" -retro -dpi { dpi or 96 } -nolisten tcp -nolisten unix" ,
247+ background = True ,
248+ timeout = 0 ,
249+ )
251250
252- self .__vnc_server = _VNCServer (self )
253- self ._start_xfce4 ()
251+ if not self ._wait_and_verify (
252+ f"xdpyinfo -display { self ._display } " , lambda r : r .exit_code == 0
253+ ):
254+ raise TimeoutException ("Could not start Xvfb" )
254255
256+ self .__vnc_server = _VNCServer (self )
257+ self ._start_xfce4 ()
258+ else :
259+ # When connecting to existing sandbox, just initialize VNC server
260+ self .__vnc_server = _VNCServer (self )
255261
256262 def _wait_and_verify (
257263 self ,
0 commit comments