diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 134 |
1 files changed, 67 insertions, 67 deletions
diff --git a/src/main.rs b/src/main.rs index dc238b1..0f6b6c6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,54 +12,11 @@ use crate::pkgtoolkit::build::BuildOperations; use crate::pkgtoolkit::git_source::GitSource; use crate::pkgtoolkit::index::IndexOperations; use crate::pkgtoolkit::install::InstallOperations; -use crate::router::router::{Emissary, EmissaryConfig, RouterUtils}; -use emissary_core::router::Router; -use emissary_util::runtime::tokio::Runtime as TokioRuntime; -use std::sync::Arc; -use tokio::sync::Mutex; - +use crate::router::manager::RouterManager; use clap::{Args, Parser, Subcommand}; use std::io::Write; use std::path::Path; -lazy_static::lazy_static! { - static ref ROUTER: Arc<Mutex<Option<Router<TokioRuntime>>>> = Arc::new(Mutex::new(None)); -} - -async fn init_router( - config: &crate::cfg::config::Config, -) -> Result<(), Box<dyn std::error::Error>> { - if !config.router.integrated_router { - return Ok(()); - } - - let router_config = EmissaryConfig { - storage: Some(std::path::PathBuf::from(&config.router.storage_path)), - auto_update: Some(config.router.auto_update), - http_proxy_port: Some(config.router.http_proxy_port), - socks_proxy_port: Some(config.router.socks_proxy_port), - }; - - let mut router = Emissary::new(router_config); - - // Check if router storage exists and is not empty - let storage_path = std::path::Path::new(&config.router.storage_path); - if !storage_path.exists() || storage_path.read_dir()?.next().is_none() { - log::info!("Router storage is empty, performing initial reseed..."); - router.config().await?; - router.reseed().await?; - } else { - router.config().await?; - } - - // Start the router and store the Router instance - let router = router.start().await?; - *ROUTER.lock().await = Some(router); - - log::info!("Integrated router initialized successfully"); - Ok(()) -} - #[derive(Parser)] struct Cli { #[command(subcommand)] @@ -76,7 +33,7 @@ enum Commands { Upgrade { pkgname: Option<String> }, #[command(about = "Build package from .mesk ")] Build { pkgname: String }, - #[command(about = "Install package from remote or local sources")] + #[command(about = "Install package")] Install { #[arg(help = "Package name or path to local .mesk file")] pkgname: String, @@ -124,6 +81,27 @@ struct RemoteInstallArgs { async fn main() -> Result<(), Box<dyn std::error::Error>> { let cli: Cli = Cli::parse(); + // Parse config once at the beginning + let config = Config::parse()?; + + // Setup integrated router if enabled + let router_manager = if config.router.integrated_router { + println!("Starting integrated router..."); + match RouterManager::new(&config).await { + Ok(manager) => { + println!("Router manager initialized"); + Some(manager) + } + Err(e) => { + eprintln!("Failed to initialize router manager: {}", e); + return Err(e); + } + } + } else { + println!("Integrated router disabled"); + None + }; + let result = { match &cli.command { Commands::Validate { path } => { @@ -138,7 +116,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { } } Err(e) => { - log::error!("Failed to validate package '{}': {}", path, e); + eprintln!("Failed to validate package '{}': {}", path, e); return Err(e.into()); } } @@ -165,7 +143,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { println!("Extracting archive..."); Package::extract_archive(pkgname)?; - let config = Config::parse()?; let cache_dir = &config.paths.cache_dir; // Find the package directory (should be name-version format) @@ -221,7 +198,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { println!("Extracting archive..."); Package::extract_archive(pkgname)?; - let config = Config::parse()?; let cache_dir = &config.paths.cache_dir; let mut pkg_dir_name = None; @@ -259,25 +235,34 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { pkg.install()?; println!("Package '{}' installed successfully.", pkg.name); } else { - let config = Config::parse()?; // Initialize router if it's needed for I2P connections and is enabled if !args.http && config.router.integrated_router { - init_router(&config).await?; + // Wait for router to be fully started + if let Some(ref manager) = router_manager { + println!("Waiting for router to be ready..."); + tokio::time::sleep(std::time::Duration::from_secs(3)).await; // Give router time to start + if !manager.is_running() { + eprintln!( + "Router is not running, cannot proceed with I2P installation" + ); + return Err(std::io::Error::other("Router not running").into()); + } + } } if args.http { - println!("Installing {} via HTTP", pkgname); + println!("Installing {} via non-i2p mirror", pkgname); let mut http_client = HTTPPackage::new(config); http_client.fetch_index_http().await?; - log::info!("Index fetched successfully."); + println!("Index fetched successfully."); http_client.fetch_package_http(pkgname).await?; - log::info!("Package '{}' installed successfully.", pkgname); + println!("Package '{}' installed successfully.", pkgname); } else { println!("Installing {} via I2P", pkgname); let mut i2p_client = I2PPackage::new(config); i2p_client.fetch_index().await?; - log::info!("Index fetched successfully."); + println!("Index fetched successfully."); i2p_client.fetch_package(pkgname).await?; - log::info!("Package '{}' installed successfully.", pkgname); + println!("Package '{}' installed successfully.", pkgname); } } return Ok(()); @@ -286,7 +271,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { println!("Uninstalling package: {}", pkgname); let installed_packages = Package::list_installed_packages().map_err(|e| { - log::error!("Failed to list installed packages: {}", e); + eprintln!("Failed to list installed packages: {}", e); e })?; @@ -309,23 +294,29 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { Ok(()) } Ok(false) => { - log::warn!( + eprintln!( "Some files could not be removed during uninstallation of {}", pkgname ); Err(std::io::Error::other("Partial uninstallation occurred").into()) } Err(e) => { - log::error!("Failed to uninstall package {}: {}", pkgname, e); + eprintln!("Failed to uninstall package {}: {}", pkgname, e); Err(e.into()) } } } Commands::GetSource { pkgname } => { - let config = Config::parse()?; // Initialize router for I2P connections if enabled if config.router.integrated_router { - init_router(&config).await?; + if let Some(ref manager) = router_manager { + println!("Waiting for router to be ready..."); + tokio::time::sleep(std::time::Duration::from_secs(2)).await; + if !manager.is_running() { + eprintln!("Router is not running, cannot proceed with I2P operation"); + return Err(std::io::Error::other("Router not running").into()); + } + } } println!("Getting source of {}", pkgname); @@ -347,7 +338,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { println!("{}", config); println!("---- End of generated config ----"); - log::warn!("Writing the default config to /etc/mesk/mesk.toml"); + eprintln!("Writing the default config to /etc/mesk/mesk.toml"); let path = Path::new("/etc/mesk/mesk.toml"); std::fs::create_dir_all(path.parent().unwrap())?; @@ -361,7 +352,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { println!("{:?}", config); println!("---- End of generated config ----"); - log::warn!("Writing the default config to /etc/mesk/mesk.toml"); + eprintln!("Writing the default config to /etc/mesk/mesk.toml"); let path = Path::new("/etc/mesk/mesk.toml"); std::fs::create_dir_all(path.parent().unwrap())?; @@ -372,11 +363,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { return Ok(()); } Commands::Update => { - let config = Config::parse()?; // Initialize router for I2P connections if enabled if config.router.integrated_router { - init_router(&config).await?; + if let Some(ref manager) = router_manager { + println!("Waiting for router to be ready..."); + tokio::time::sleep(std::time::Duration::from_secs(2)).await; + if !manager.is_running() { + eprintln!("Router is not running, cannot proceed with I2P operation"); + return Err(std::io::Error::other("Router not running").into()); + } + } } + println!("Updating index from {}", config.repo.repo_url); let mut i2p_client = I2PPackage::new(config); @@ -407,9 +405,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { }; // Shutdown router if it was initialized - if let Some(router) = ROUTER.lock().await.take() { - // TODO: Add proper router shutdown when implemented - log::info!("Shutting down integrated router..."); + if let Some(ref manager) = router_manager { + println!("Shutting down integrated router..."); + if let Err(e) = manager.stop().await { + eprintln!("Error stopping router: {}", e); + } } result |
