Sandbox executor for running commands with proxy configuration
| 17 | |
| 18 | |
| 19 | class SandboxExecutor: |
| 20 | """Sandbox executor for running commands with proxy configuration""" |
| 21 | |
| 22 | def __init__(self, proxy: Optional[str] = None, default_timeout: int = 60): |
| 23 | """Initialize sandbox executor with configuration |
| 24 | |
| 25 | Args: |
| 26 | proxy: Proxy URL to use for network access. If None, will use WEB_PROXY environment variable |
| 27 | default_timeout: Default timeout in seconds for command execution |
| 28 | """ |
| 29 | # Get proxy from parameter, environment variable, or use default blocking proxy |
| 30 | # Use 'or None' to convert empty string to None, then apply default value |
| 31 | self.proxy = proxy or CONFIG.external_https_proxy or "http://128.0.0.1:1" |
| 32 | self.default_timeout = default_timeout |
| 33 | |
| 34 | def execute_in_sandbox(self, cmd: List[str]) -> tuple: |
| 35 | """Execute command in sandbox with proxy configuration |
| 36 | |
| 37 | Args: |
| 38 | cmd: Command to execute |
| 39 | |
| 40 | Returns: |
| 41 | Tuple of (stdout, stderr, returncode) |
| 42 | """ |
| 43 | # Try different sandbox methods in order of preference |
| 44 | sandbox_methods = [ |
| 45 | self._execute_with_proxy, |
| 46 | ] |
| 47 | |
| 48 | for method in sandbox_methods: |
| 49 | try: |
| 50 | return method(cmd) |
| 51 | except Exception as e: |
| 52 | logger.warning(f"Sandbox method {method.__name__} failed: {e}") |
| 53 | continue |
| 54 | |
| 55 | raise RuntimeError("All sandbox methods failed") |
| 56 | |
| 57 | def _execute_with_proxy(self, cmd: List[str]) -> tuple: |
| 58 | """Execute command with proxy configuration |
| 59 | |
| 60 | Args: |
| 61 | cmd: Command to execute |
| 62 | |
| 63 | Returns: |
| 64 | Tuple of (stdout, stderr, returncode) |
| 65 | """ |
| 66 | # Set up environment with proxy configuration |
| 67 | env = os.environ.copy() |
| 68 | if self.proxy: |
| 69 | env["http_proxy"] = self.proxy |
| 70 | env["https_proxy"] = self.proxy |
| 71 | env["HTTP_PROXY"] = self.proxy |
| 72 | env["HTTPS_PROXY"] = self.proxy |
| 73 | |
| 74 | logger.info(f"Executing command with proxy: {' '.join(cmd)}") |
| 75 | if self.proxy: |
| 76 | logger.info(f"Using proxy: {self.proxy}") |