1111import secrets
1212import uuid
1313from contextlib import contextmanager
14- from typing import Dict , Generator , Optional , Tuple
14+ from typing import Dict , Generator , List , Optional , Tuple , TypedDict
1515from urllib .parse import urlparse
1616
1717import boto3
2929MAX_LIVE_VIEW_PRESIGNED_URL_TIMEOUT = 300
3030
3131
32+ class BasicAuth (TypedDict , total = False ):
33+ """Basic authentication credentials for a proxy server."""
34+
35+ secretArn : str
36+
37+
38+ class ProxyCredentials (TypedDict , total = False ):
39+ """Credentials for authenticating with a proxy server."""
40+
41+ basicAuth : BasicAuth
42+
43+
44+ class ExternalProxy (TypedDict , total = False ):
45+ """Configuration for an external proxy server."""
46+
47+ server : str
48+ port : int
49+ domainPatterns : List [str ]
50+ credentials : ProxyCredentials
51+
52+
53+ class ProxyEntry (TypedDict , total = False ):
54+ """A single proxy entry in the proxy configuration."""
55+
56+ externalProxy : ExternalProxy
57+
58+
59+ class ProxyBypass (TypedDict , total = False ):
60+ """Domains that bypass all proxy servers."""
61+
62+ domainPatterns : List [str ]
63+
64+
65+ class ProxyConfiguration (TypedDict , total = False ):
66+ """Proxy configuration for routing browser traffic through external proxy servers.
67+
68+ See: https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/browser-proxies.html
69+ """
70+
71+ proxies : List [ProxyEntry ]
72+ bypass : ProxyBypass
73+
74+
3275class BrowserClient :
3376 """Client for interacting with the AWS Browser sandbox service.
3477
@@ -289,6 +332,7 @@ def start(
289332 name : Optional [str ] = None ,
290333 session_timeout_seconds : Optional [int ] = DEFAULT_SESSION_TIMEOUT ,
291334 viewport : Optional [Dict [str , int ]] = None ,
335+ proxy_configuration : Optional [ProxyConfiguration ] = None ,
292336 ) -> str :
293337 """Start a browser sandbox session.
294338
@@ -302,6 +346,9 @@ def start(
302346 Range: 1-28800 (8 hours). Default: 3600 (1 hour).
303347 viewport (Optional[Dict[str, int]]): The viewport dimensions:
304348 {'width': 1920, 'height': 1080}
349+ proxy_configuration (Optional[ProxyConfiguration]): Proxy configuration for
350+ routing browser traffic through external proxy servers. See
351+ https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/browser-proxies.html
305352
306353 Returns:
307354 str: The session ID of the newly created session.
@@ -316,6 +363,20 @@ def start(
316363 ... viewport={'width': 1920, 'height': 1080},
317364 ... session_timeout_seconds=7200 # 2 hours
318365 ... )
366+ >>>
367+ >>> # Use proxy configuration
368+ >>> session_id = client.start(
369+ ... proxy_configuration={
370+ ... "proxies": [{
371+ ... "externalProxy": {
372+ ... "server": "proxy.example.com",
373+ ... "port": 8080,
374+ ... "domainPatterns": [".example.com"],
375+ ... }
376+ ... }],
377+ ... "bypass": {"domainPatterns": [".amazonaws.com"]}
378+ ... }
379+ ... )
319380 """
320381 self .logger .info ("Starting browser session..." )
321382
@@ -328,6 +389,9 @@ def start(
328389 if viewport is not None :
329390 request_params ["viewPort" ] = viewport
330391
392+ if proxy_configuration is not None :
393+ request_params ["proxyConfiguration" ] = proxy_configuration
394+
331395 response = self .data_plane_client .start_browser_session (** request_params )
332396
333397 self .identifier = response ["browserIdentifier" ]
@@ -581,14 +645,19 @@ def release_control(self):
581645
582646@contextmanager
583647def browser_session (
584- region : str , viewport : Optional [Dict [str , int ]] = None , identifier : Optional [str ] = None
648+ region : str ,
649+ viewport : Optional [Dict [str , int ]] = None ,
650+ identifier : Optional [str ] = None ,
651+ proxy_configuration : Optional [ProxyConfiguration ] = None ,
585652) -> Generator [BrowserClient , None , None ]:
586653 """Context manager for creating and managing a browser sandbox session.
587654
588655 Args:
589656 region (str): AWS region.
590657 viewport (Optional[Dict[str, int]]): Viewport dimensions.
591658 identifier (Optional[str]): Browser identifier (system or custom).
659+ proxy_configuration (Optional[ProxyConfiguration]): Proxy configuration for
660+ routing browser traffic through external proxy servers.
592661
593662 Yields:
594663 BrowserClient: An initialized and started browser client.
@@ -602,13 +671,22 @@ def browser_session(
602671 >>> with browser_session('us-west-2', identifier='my-signed-browser') as client:
603672 ... # Automation with reduced CAPTCHA friction
604673 ... pass
674+ ...
675+ >>> # Use proxy configuration
676+ >>> with browser_session('us-west-2', proxy_configuration={
677+ ... "proxies": [{"externalProxy": {"server": "proxy.corp.com", "port": 8080}}],
678+ ... "bypass": {"domainPatterns": [".amazonaws.com"]}
679+ ... }) as client:
680+ ... ws_url, headers = client.generate_ws_headers()
605681 """
606682 client = BrowserClient (region )
607683 start_kwargs = {}
608684 if viewport is not None :
609685 start_kwargs ["viewport" ] = viewport
610686 if identifier is not None :
611687 start_kwargs ["identifier" ] = identifier
688+ if proxy_configuration is not None :
689+ start_kwargs ["proxy_configuration" ] = proxy_configuration
612690
613691 client .start (** start_kwargs )
614692
0 commit comments