use crate::error::{Error, Result};
use crate::sandbox::{Sandbox, SandboxConfig};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxExport {
pub config: SandboxConfig,
pub python_packages: Vec<String>,
pub javascript_packages: Vec<String>,
pub ruby_gems: Vec<String>,
pub perl_modules: Vec<String>,
pub php_packages: Vec<String>,
pub go_modules: Vec<String>,
pub rust_crates: Vec<String>,
pub java_dependencies: Vec<String>,
pub custom_env_vars: Vec<(String, String)>,
pub metadata: ExportMetadata,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportMetadata {
pub exported_at: String,
pub version: String,
pub description: Option<String>,
pub author: Option<String>,
pub tags: Vec<String>,
}
impl SandboxExport {
pub fn new(config: SandboxConfig) -> Self {
Self {
config,
python_packages: Vec::new(),
javascript_packages: Vec::new(),
ruby_gems: Vec::new(),
perl_modules: Vec::new(),
php_packages: Vec::new(),
go_modules: Vec::new(),
rust_crates: Vec::new(),
java_dependencies: Vec::new(),
custom_env_vars: Vec::new(),
metadata: ExportMetadata {
exported_at: chrono::Utc::now().to_rfc3339(),
version: env!("CARGO_PKG_VERSION").to_string(),
description: None,
author: None,
tags: Vec::new(),
},
}
}
pub fn from_sandbox(sandbox: &Sandbox) -> Result<Self> {
let mut export = Self::new(sandbox.config().clone());
if sandbox.config().enable_python {
export.python_packages = Self::read_python_packages(sandbox)?;
}
if sandbox.config().enable_javascript {
export.javascript_packages = Self::read_javascript_packages(sandbox)?;
}
if sandbox.config().enable_ruby {
export.ruby_gems = Self::read_ruby_gems(sandbox)?;
}
Ok(export)
}
fn read_python_packages(sandbox: &Sandbox) -> Result<Vec<String>> {
let pip_list = sandbox.root_dir().join("python/venv/bin/pip");
if !pip_list.exists() {
return Ok(Vec::new());
}
let output = std::process::Command::new(pip_list)
.args(["list", "--format=freeze"])
.output()
.map_err(|e| Error::command(format!("Failed to list Python packages: {}", e)))?;
let packages = String::from_utf8_lossy(&output.stdout)
.lines()
.filter(|line| !line.is_empty())
.map(|line| line.to_string())
.collect();
Ok(packages)
}
fn read_javascript_packages(sandbox: &Sandbox) -> Result<Vec<String>> {
let package_json = sandbox.root_dir().join("javascript/package.json");
if !package_json.exists() {
return Ok(Vec::new());
}
let content = fs::read_to_string(&package_json)
.map_err(|e| Error::config(format!("Failed to read package.json: {}", e)))?;
let json: serde_json::Value = serde_json::from_str(&content)
.map_err(|e| Error::config(format!("Failed to parse package.json: {}", e)))?;
let mut packages = Vec::new();
if let Some(deps) = json.get("dependencies").and_then(|d| d.as_object()) {
for (name, version) in deps {
packages.push(format!("{}@{}", name, version.as_str().unwrap_or("latest")));
}
}
Ok(packages)
}
fn read_ruby_gems(sandbox: &Sandbox) -> Result<Vec<String>> {
let gem_home = sandbox.root_dir().join("ruby/gems");
if !gem_home.exists() {
return Ok(Vec::new());
}
let output = std::process::Command::new("gem")
.args(["list", "--local"])
.env("GEM_HOME", &gem_home)
.output()
.map_err(|e| Error::command(format!("Failed to list Ruby gems: {}", e)))?;
let gems = String::from_utf8_lossy(&output.stdout)
.lines()
.filter(|line| !line.is_empty() && !line.starts_with("***"))
.map(|line| line.to_string())
.collect();
Ok(gems)
}
pub fn save(&self, path: &Path) -> Result<()> {
let content = serde_yaml::to_string(self)
.map_err(|e| Error::config(format!("Failed to serialize sandbox export: {}", e)))?;
fs::write(path, content)
.map_err(|e| Error::config(format!("Failed to write sandbox export: {}", e)))?;
tracing::info!("Sandbox exported to: {}", path.display());
Ok(())
}
pub fn load(path: &Path) -> Result<Self> {
let content = fs::read_to_string(path)
.map_err(|e| Error::config(format!("Failed to read sandbox export: {}", e)))?;
let export: Self = serde_yaml::from_str(&content)
.map_err(|e| Error::config(format!("Failed to parse sandbox export: {}", e)))?;
tracing::info!("Sandbox imported from: {}", path.display());
Ok(export)
}
pub async fn apply_to_sandbox(&self, sandbox: &mut Sandbox) -> Result<()> {
tracing::info!("Applying imported sandbox configuration...");
*sandbox = Sandbox::with_config(self.config.clone());
sandbox.init().await?;
if !self.python_packages.is_empty() {
tracing::info!(
"Installing {} Python packages...",
self.python_packages.len()
);
let packages: Vec<&str> = self.python_packages.iter().map(|s| s.as_str()).collect();
crate::sandbox::python::install_packages(sandbox, &packages).await?;
}
if !self.javascript_packages.is_empty() {
tracing::info!(
"Installing {} JavaScript packages...",
self.javascript_packages.len()
);
let packages: Vec<&str> = self
.javascript_packages
.iter()
.map(|s| s.as_str())
.collect();
crate::sandbox::javascript::install_packages(sandbox, &packages).await?;
}
if !self.ruby_gems.is_empty() {
tracing::info!("Installing {} Ruby gems...", self.ruby_gems.len());
let packages: Vec<&str> = self.ruby_gems.iter().map(|s| s.as_str()).collect();
crate::sandbox::ruby::install_gems(sandbox, &packages).await?;
}
tracing::info!("Sandbox configuration applied successfully!");
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxTemplate {
pub name: String,
pub description: String,
pub export: SandboxExport,
}
impl SandboxTemplate {
pub fn web_security() -> Self {
let mut export = SandboxExport::new(SandboxConfig::default());
export.metadata.description = Some("Web security testing sandbox".to_string());
export.metadata.tags = vec!["web".to_string(), "security".to_string()];
export.python_packages = vec![
"requests".to_string(),
"beautifulsoup4".to_string(),
"selenium".to_string(),
"scrapy".to_string(),
"sqlmap".to_string(),
];
export.javascript_packages = vec![
"puppeteer".to_string(),
"playwright".to_string(),
"axios".to_string(),
"cheerio".to_string(),
];
Self {
name: "web-security".to_string(),
description: "Pre-configured sandbox for web application security testing".to_string(),
export,
}
}
pub fn network_security() -> Self {
let mut export = SandboxExport::new(SandboxConfig::default());
export.metadata.description = Some("Network security testing sandbox".to_string());
export.metadata.tags = vec!["network".to_string(), "security".to_string()];
export.python_packages = vec![
"scapy".to_string(),
"python-nmap".to_string(),
"impacket".to_string(),
"paramiko".to_string(),
];
Self {
name: "network-security".to_string(),
description: "Pre-configured sandbox for network security testing".to_string(),
export,
}
}
pub fn api_testing() -> Self {
let mut export = SandboxExport::new(SandboxConfig::default());
export.metadata.description = Some("API testing sandbox".to_string());
export.metadata.tags = vec!["api".to_string(), "testing".to_string()];
export.python_packages = vec![
"requests".to_string(),
"httpx".to_string(),
"pytest".to_string(),
];
export.javascript_packages = vec![
"axios".to_string(),
"node-fetch".to_string(),
"jest".to_string(),
];
Self {
name: "api-testing".to_string(),
description: "Pre-configured sandbox for API testing".to_string(),
export,
}
}
}