use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
pub mod config;
pub mod docker;
pub mod go;
pub mod import_export;
pub mod java;
pub mod javascript;
pub mod packages;
pub mod perl;
pub mod php;
pub mod python;
pub mod ruby;
pub mod runtime_installer;
pub mod rust;
pub fn get_active_docker_sandbox() -> Option<docker::DockerSandbox> {
use config::SandboxConfigFile;
if docker::inside_sandbox() {
return None;
}
let cfg = SandboxConfigFile::load().ok()?;
let (name, _config) = cfg.get_default_sandbox()?;
docker::DockerSandbox::load(name).ok()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxConfig {
pub root_dir: PathBuf,
pub enable_python: bool,
pub enable_javascript: bool,
pub enable_ruby: bool,
pub enable_perl: bool,
pub enable_php: bool,
pub enable_rust: bool,
pub enable_go: bool,
pub enable_java: bool,
pub auto_init: bool,
}
impl Default for SandboxConfig {
fn default() -> Self {
Self {
root_dir: Self::default_sandbox_dir(),
enable_python: true,
enable_javascript: true,
enable_ruby: true,
enable_perl: true,
enable_php: true,
enable_rust: true,
enable_go: true,
enable_java: true,
auto_init: true,
}
}
}
impl SandboxConfig {
pub fn default_sandbox_dir() -> PathBuf {
dirs::data_local_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("cert-x-gen")
.join("sandbox")
}
pub fn load(path: &Path) -> Result<Self> {
let content = fs::read_to_string(path)
.map_err(|e| Error::config(format!("Failed to read sandbox config: {}", e)))?;
serde_yaml::from_str(&content)
.map_err(|e| Error::config(format!("Failed to parse sandbox config: {}", e)))
}
pub fn save(&self, path: &Path) -> Result<()> {
let content = serde_yaml::to_string(self)
.map_err(|e| Error::config(format!("Failed to serialize sandbox config: {}", e)))?;
fs::write(path, content)
.map_err(|e| Error::config(format!("Failed to write sandbox config: {}", e)))
}
}
#[derive(Debug)]
pub struct Sandbox {
config: SandboxConfig,
initialized: bool,
}
impl Sandbox {
pub fn new() -> Self {
Self {
config: SandboxConfig::default(),
initialized: false,
}
}
pub fn with_config(config: SandboxConfig) -> Self {
Self {
config,
initialized: false,
}
}
pub fn root_dir(&self) -> &Path {
&self.config.root_dir
}
pub fn config(&self) -> &SandboxConfig {
&self.config
}
pub fn is_initialized(&self) -> bool {
self.initialized && self.root_dir().exists()
}
pub async fn init(&mut self) -> Result<()> {
tracing::info!("Initializing sandbox at: {}", self.root_dir().display());
fs::create_dir_all(self.root_dir())
.map_err(|e| Error::config(format!("Failed to create sandbox directory: {}", e)))?;
self.create_directory_structure()?;
if self.config.enable_python {
python::init_environment(self).await?;
}
if self.config.enable_javascript {
javascript::init_environment(self).await?;
}
if self.config.enable_ruby {
ruby::init_environment(self).await?;
}
if self.config.enable_perl {
perl::init_environment(self).await?;
}
if self.config.enable_php {
php::init_environment(self).await?;
}
if self.config.enable_rust {
rust::init_environment(self).await?;
}
if self.config.enable_go {
go::init_environment(self).await?;
}
if self.config.enable_java {
java::init_environment(self).await?;
}
let config_path = self.root_dir().join("config.yaml");
self.config.save(&config_path)?;
self.initialized = true;
tracing::info!("Sandbox initialized successfully");
Ok(())
}
fn create_directory_structure(&self) -> Result<()> {
let dirs = vec![
"python/venv",
"python/packages",
"javascript/node_modules",
"javascript/packages",
"ruby/gems",
"ruby/packages",
"perl/local",
"perl/packages",
"php/vendor",
"php/packages",
"rust/target",
"rust/packages",
"go/pkg",
"go/packages",
"java/lib",
"java/packages",
"bin",
"tmp",
"logs",
];
for dir in dirs {
let path = self.root_dir().join(dir);
fs::create_dir_all(&path).map_err(|e| {
Error::config(format!(
"Failed to create directory {}: {}",
path.display(),
e
))
})?;
}
Ok(())
}
pub fn clean(&self) -> Result<()> {
tracing::info!("Cleaning sandbox at: {}", self.root_dir().display());
if self.root_dir().exists() {
fs::remove_dir_all(self.root_dir())
.map_err(|e| Error::config(format!("Failed to clean sandbox: {}", e)))?;
}
tracing::info!("Sandbox cleaned successfully");
Ok(())
}
pub fn get_env_vars(&self) -> Vec<(String, String)> {
let mut env_vars = Vec::new();
if self.config.enable_python {
let python_path = self.root_dir().join("python/venv");
env_vars.push((
"VIRTUAL_ENV".to_string(),
python_path.to_string_lossy().to_string(),
));
env_vars.push((
"PYTHONUSERBASE".to_string(),
self.root_dir()
.join("python/packages")
.to_string_lossy()
.to_string(),
));
}
if self.config.enable_javascript {
env_vars.push((
"NODE_PATH".to_string(),
self.root_dir()
.join("javascript/node_modules")
.to_string_lossy()
.to_string(),
));
}
if self.config.enable_ruby {
env_vars.push((
"GEM_HOME".to_string(),
self.root_dir()
.join("ruby/gems")
.to_string_lossy()
.to_string(),
));
}
if self.config.enable_perl {
env_vars.push((
"PERL_LOCAL_LIB_ROOT".to_string(),
self.root_dir()
.join("perl/local")
.to_string_lossy()
.to_string(),
));
}
if self.config.enable_php {
env_vars.push((
"PHP_USER_INI".to_string(),
self.root_dir().join("php").to_string_lossy().to_string(),
));
}
if self.config.enable_rust {
env_vars.push((
"CARGO_TARGET_DIR".to_string(),
self.root_dir()
.join("rust/target")
.to_string_lossy()
.to_string(),
));
}
if self.config.enable_go {
env_vars.push((
"GOPATH".to_string(),
self.root_dir().join("go").to_string_lossy().to_string(),
));
}
if self.config.enable_java {
env_vars.push((
"JAVA_HOME".to_string(),
self.root_dir().join("java").to_string_lossy().to_string(),
));
}
env_vars
}
pub fn execute_command(&self, program: &str, args: &[&str]) -> Result<std::process::Output> {
let mut cmd = Command::new(program);
cmd.args(args);
for (key, value) in self.get_env_vars() {
cmd.env(key, value);
}
cmd.current_dir(self.root_dir());
cmd.output()
.map_err(|e| Error::command(format!("Failed to execute command: {}", e)))
}
pub fn status(&self) -> SandboxStatus {
SandboxStatus {
initialized: self.is_initialized(),
root_dir: self.root_dir().to_path_buf(),
python_ready: self.config.enable_python && self.root_dir().join("python/venv").exists(),
javascript_ready: self.config.enable_javascript
&& self.root_dir().join("javascript/node_modules").exists(),
ruby_ready: self.config.enable_ruby && self.root_dir().join("ruby/gems").exists(),
perl_ready: self.config.enable_perl && self.root_dir().join("perl/local").exists(),
php_ready: self.config.enable_php && self.root_dir().join("php/vendor").exists(),
rust_ready: self.config.enable_rust && self.root_dir().join("rust/target").exists(),
go_ready: self.config.enable_go && self.root_dir().join("go/pkg").exists(),
java_ready: self.config.enable_java && self.root_dir().join("java/lib").exists(),
}
}
}
impl Default for Sandbox {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SandboxStatus {
pub initialized: bool,
pub root_dir: PathBuf,
pub python_ready: bool,
pub javascript_ready: bool,
pub ruby_ready: bool,
pub perl_ready: bool,
pub php_ready: bool,
pub rust_ready: bool,
pub go_ready: bool,
pub java_ready: bool,
}
impl SandboxStatus {
pub fn all_ready(&self) -> bool {
self.initialized
}
pub fn ready_languages(&self) -> Vec<&str> {
let mut langs = Vec::new();
if self.python_ready {
langs.push("python");
}
if self.javascript_ready {
langs.push("javascript");
}
if self.ruby_ready {
langs.push("ruby");
}
if self.perl_ready {
langs.push("perl");
}
if self.php_ready {
langs.push("php");
}
if self.rust_ready {
langs.push("rust");
}
if self.go_ready {
langs.push("go");
}
if self.java_ready {
langs.push("java");
}
langs
}
pub fn pending_languages(&self) -> Vec<&str> {
let mut langs = Vec::new();
if !self.python_ready {
langs.push("python");
}
if !self.javascript_ready {
langs.push("javascript");
}
if !self.ruby_ready {
langs.push("ruby");
}
if !self.perl_ready {
langs.push("perl");
}
if !self.php_ready {
langs.push("php");
}
if !self.rust_ready {
langs.push("rust");
}
if !self.go_ready {
langs.push("go");
}
if !self.java_ready {
langs.push("java");
}
langs
}
}