sandbox_runtime/manager/
state.rs

1//! Global state management for the sandbox manager.
2
3use std::sync::Arc;
4
5
6use crate::config::SandboxRuntimeConfig;
7use crate::proxy::{HttpProxy, Socks5Proxy};
8use crate::violation::SandboxViolationStore;
9
10/// Internal state for the sandbox manager.
11pub struct ManagerState {
12    /// The current configuration.
13    pub config: Option<SandboxRuntimeConfig>,
14
15    /// HTTP proxy server.
16    pub http_proxy: Option<HttpProxy>,
17
18    /// SOCKS5 proxy server.
19    pub socks_proxy: Option<Socks5Proxy>,
20
21    /// HTTP proxy port.
22    pub http_proxy_port: Option<u16>,
23
24    /// SOCKS5 proxy port.
25    pub socks_proxy_port: Option<u16>,
26
27    /// Unix socket path for HTTP proxy (Linux only).
28    #[cfg(target_os = "linux")]
29    pub http_socket_path: Option<String>,
30
31    /// Unix socket path for SOCKS5 proxy (Linux only).
32    #[cfg(target_os = "linux")]
33    pub socks_socket_path: Option<String>,
34
35    /// Socat bridge processes (Linux only).
36    #[cfg(target_os = "linux")]
37    pub bridges: Vec<crate::sandbox::linux::SocatBridge>,
38
39    /// Whether the manager has been initialized.
40    pub initialized: bool,
41
42    /// Whether network is ready.
43    pub network_ready: bool,
44
45    /// Violation store.
46    pub violation_store: Arc<SandboxViolationStore>,
47}
48
49impl Default for ManagerState {
50    fn default() -> Self {
51        Self {
52            config: None,
53            http_proxy: None,
54            socks_proxy: None,
55            http_proxy_port: None,
56            socks_proxy_port: None,
57            #[cfg(target_os = "linux")]
58            http_socket_path: None,
59            #[cfg(target_os = "linux")]
60            socks_socket_path: None,
61            #[cfg(target_os = "linux")]
62            bridges: Vec::new(),
63            initialized: false,
64            network_ready: false,
65            violation_store: Arc::new(SandboxViolationStore::new()),
66        }
67    }
68}
69
70impl ManagerState {
71    /// Create a new manager state.
72    pub fn new() -> Self {
73        Self::default()
74    }
75
76    /// Reset the state, cleaning up resources.
77    pub async fn reset(&mut self) {
78        // Stop proxies
79        if let Some(ref mut proxy) = self.http_proxy {
80            proxy.stop();
81        }
82        if let Some(ref mut proxy) = self.socks_proxy {
83            proxy.stop();
84        }
85
86        // Stop bridges (Linux)
87        #[cfg(target_os = "linux")]
88        {
89            for bridge in &mut self.bridges {
90                bridge.stop().await;
91            }
92            self.bridges.clear();
93            self.http_socket_path = None;
94            self.socks_socket_path = None;
95        }
96
97        // Clear state
98        self.http_proxy = None;
99        self.socks_proxy = None;
100        self.http_proxy_port = None;
101        self.socks_proxy_port = None;
102        self.config = None;
103        self.initialized = false;
104        self.network_ready = false;
105    }
106}