hai/src/main.rs

129 lines
3.6 KiB
Rust
Raw Normal View History

2025-02-20 22:18:14 +01:00
/*
* SPDX-FileCopyrightText: 2025 April Faye John <april.john@denic.de> & Contributors
2025-02-19 00:08:36 +01:00
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
2020-08-21 02:47:38 +02:00
2025-02-19 00:08:36 +01:00
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
2020-08-21 02:47:38 +02:00
2025-02-19 00:08:36 +01:00
mod bininfo;
2025-02-20 22:18:14 +01:00
mod gui;
2025-02-20 14:04:16 +01:00
mod pman;
2025-02-22 17:09:33 +01:00
mod quic;
2020-08-21 02:47:38 +02:00
2025-02-28 18:15:54 +01:00
use std::fs::File;
use std::net::SocketAddr;
use std::str::FromStr;
2025-02-20 22:18:14 +01:00
use bunt::println;
use clap::{Parser, Subcommand};
2025-02-28 18:15:54 +01:00
use config::{Config, FileFormat, Source};
use log::{debug, info};
2025-02-20 22:18:14 +01:00
use pman::{init_process_manager, ProcessCommand, ProcessManager};
2025-02-19 00:08:36 +01:00
use shadow_rs::shadow;
2025-02-20 22:18:14 +01:00
use std::sync::{Arc, Mutex, OnceLock};
use tokio::{signal, sync::mpsc::Sender};
2020-08-21 02:47:38 +02:00
2025-02-19 00:08:36 +01:00
shadow!(build);
2020-08-21 02:47:38 +02:00
2025-02-20 22:18:14 +01:00
static PMAN_SENDER: OnceLock<Sender<ProcessCommand>> = OnceLock::new();
2025-02-19 00:08:36 +01:00
/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
2025-02-25 10:51:51 +01:00
/// Config File to load
#[arg(short, long)]
config: Option<String>,
2020-08-21 02:47:38 +02:00
2025-02-19 00:08:36 +01:00
/// Number of times to greet
//#[arg(short, long, default_value_t = 1)]
//count: u8,
2020-08-21 02:47:38 +02:00
2025-02-19 00:08:36 +01:00
#[command(subcommand)]
command: Commands,
}
2020-08-21 02:47:38 +02:00
2025-02-19 00:08:36 +01:00
#[derive(Debug, Subcommand)]
enum Commands {
2025-02-28 18:15:54 +01:00
#[command(about = "Start client without GUI")]
2025-02-25 10:51:51 +01:00
CliClient,
2025-02-19 00:08:36 +01:00
#[command(about = "List compile time backed info to audit binary")]
2025-02-22 17:09:33 +01:00
Shadow,
#[command(about = "Start client as GUI")]
GuiClient,
2025-02-20 22:18:14 +01:00
Devtest,
2020-08-21 02:47:38 +02:00
}
2025-02-25 10:51:51 +01:00
fn config() -> &'static Config {
static CONFIG: OnceLock<Config> = OnceLock::new();
CONFIG.get_or_init(|| {
Config::builder()
.add_source(config::Environment::with_prefix("HAI").separator("_"))
.add_source(get_config_file_source())
.build()
.unwrap()
})
}
static CONFIG_FILE: OnceLock<Option<String>> = OnceLock::new();
fn get_config_file_source() -> impl Source {
let file = CONFIG_FILE.get();
let default_dir = dirs::config_dir().unwrap();
2025-02-28 18:15:54 +01:00
let file_buf = default_dir.join("hai").join("config.toml");
println!("{}", file_buf.to_string_lossy());
let file_content = std::fs::read_to_string(file_buf).unwrap();
config::File::from_str(&*file_content, FileFormat::Toml)
2025-02-25 10:51:51 +01:00
}
2025-02-20 22:18:14 +01:00
#[tokio::main]
2025-03-07 08:43:23 +01:00
async fn main() -> anyhow::Result<()> {
2020-08-21 02:47:38 +02:00
2025-02-20 22:18:14 +01:00
env_logger::init();
2025-02-19 00:08:36 +01:00
let args = Args::parse();
2025-02-25 10:51:51 +01:00
CONFIG_FILE.get_or_init(|| {
args.config
});
2020-08-21 02:47:38 +02:00
2025-02-20 22:18:14 +01:00
let _ = init_process_manager();
2025-02-20 14:04:16 +01:00
2025-02-19 00:08:36 +01:00
match args.command {
2025-02-28 18:15:54 +01:00
Commands::CliClient => {
2025-03-07 08:43:23 +01:00
let ip_addr_str = config().get_string("remote_endpoint")?;
let ip_sock = SocketAddr::from_str(&ip_addr_str)?;
let client_endpoint = quic::client::make_client_endpoint(ip_sock.clone(), None)?;
let client = client_endpoint.connect(ip_sock, "localhost")?.await?;
println!("[client] connected: addr={}", client.remote_address());
2025-02-28 18:15:54 +01:00
}
2020-08-21 02:47:38 +02:00
2025-02-22 17:09:33 +01:00
Commands::Shadow => {
2025-02-19 00:08:36 +01:00
bininfo::print_info();
2025-03-07 08:43:23 +01:00
return Ok(());
2025-02-19 00:08:36 +01:00
}
2025-02-22 17:09:33 +01:00
Commands::GuiClient => {
2025-02-20 14:04:16 +01:00
let res = gui::gui_main();
if let Err(e) = res {
println!("{}", e);
}
2025-03-07 08:43:23 +01:00
return Ok(());
2025-02-20 14:04:16 +01:00
}
2025-02-19 00:08:36 +01:00
2025-02-20 22:18:14 +01:00
Commands::Devtest => {
2025-02-22 00:29:35 +01:00
PMAN_SENDER.get().unwrap().send(ProcessCommand::SpawnShellCmd {
cmd: "sleep 100 && echo hello meow".to_string(),
}).await.expect("TODO: panic message");
2025-02-20 22:18:14 +01:00
tokio::select! {
_ = signal::ctrl_c() => {
info!("Ctrl-c received");
let sender = PMAN_SENDER.get().unwrap();
let _ = sender.send(ProcessCommand::Shutdown).await;
}
}
}
2025-02-20 14:04:16 +01:00
};
2025-02-20 22:18:14 +01:00
//handling anything here for gui wont work
println!("exit");
2025-03-07 08:43:23 +01:00
Ok(())
2020-08-21 02:47:38 +02:00
}