2025-11-08 14:56:03 -07:00
|
|
|
use std::{
|
|
|
|
|
collections::HashMap,
|
|
|
|
|
sync::{Arc, Mutex},
|
2025-11-25 15:22:14 -07:00
|
|
|
thread::{self, JoinHandle},
|
2025-11-08 14:56:03 -07:00
|
|
|
time::Duration,
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
use crate::{
|
|
|
|
|
config::{NamedComponent, PayloadConfig, RuntimeConfig},
|
2025-11-25 17:31:09 -07:00
|
|
|
network::Stream,
|
2025-11-12 17:39:11 -07:00
|
|
|
*,
|
|
|
|
|
};
|
2025-11-09 12:34:52 -07:00
|
|
|
use module::Module;
|
2025-11-13 11:52:01 -07:00
|
|
|
use unshell_obfuscate::symbol;
|
2025-11-08 14:56:03 -07:00
|
|
|
|
2025-11-08 17:58:40 -07:00
|
|
|
// #[derive(Debug)]
|
2025-11-13 11:52:01 -07:00
|
|
|
pub struct Manager {
|
2025-11-12 17:39:11 -07:00
|
|
|
id: &'static str,
|
|
|
|
|
|
2025-11-25 15:22:14 -07:00
|
|
|
handle: Option<JoinHandle<()>>,
|
|
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
pub modules: Vec<Module>,
|
2025-11-12 17:39:11 -07:00
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
components: HashMap<String, NamedComponent>,
|
2025-11-25 15:22:14 -07:00
|
|
|
active_runtimes: Vec<Box<dyn ModuleRuntime>>,
|
|
|
|
|
|
2025-11-25 17:31:09 -07:00
|
|
|
pub connections: Vec<Box<dyn Stream<Announcement>>>,
|
2025-11-08 14:56:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// static mut MANAGER_RUNTIME: Option<Arc<Mutex<Manager>>> = None;
|
|
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
impl Manager {
|
2025-11-14 09:43:41 -07:00
|
|
|
fn new(id: &'static str, components: Vec<NamedComponent>, modules: Vec<Module>) -> Self {
|
2025-11-12 17:39:11 -07:00
|
|
|
Self {
|
|
|
|
|
id,
|
2025-11-25 15:22:14 -07:00
|
|
|
handle: None,
|
|
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
modules,
|
2025-11-14 09:43:41 -07:00
|
|
|
components: components
|
2025-11-13 11:52:01 -07:00
|
|
|
.into_iter()
|
|
|
|
|
.map(|c| (c.name.to_string(), c))
|
|
|
|
|
.collect(),
|
2025-11-12 17:39:11 -07:00
|
|
|
active_runtimes: Vec::new(),
|
2025-11-25 15:22:14 -07:00
|
|
|
|
|
|
|
|
connections: Vec::new(),
|
2025-11-12 17:39:11 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 15:22:14 -07:00
|
|
|
/// Create Manager, and run initialization for each Module
|
2025-11-08 14:56:03 -07:00
|
|
|
#[allow(static_mut_refs)]
|
2025-11-25 14:27:06 -07:00
|
|
|
pub fn start(config: &'static PayloadConfig, modules: Vec<Module>) -> Arc<Mutex<Self>> {
|
2025-11-12 17:39:11 -07:00
|
|
|
// Construct self
|
2025-11-13 11:52:01 -07:00
|
|
|
let mut this = Self::new(&config.id, config.components.clone(), modules);
|
2025-11-12 17:39:11 -07:00
|
|
|
|
2025-11-14 09:43:41 -07:00
|
|
|
debug!("Imported {} base components", this.components.len());
|
|
|
|
|
debug!("Imported {} base runtimes", &config.runtime_config.len());
|
|
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
// Load each of the pre-prepared modules
|
2025-11-13 11:52:01 -07:00
|
|
|
this.load_components();
|
2025-11-08 14:56:03 -07:00
|
|
|
|
|
|
|
|
let this = Arc::new(Mutex::new(this));
|
|
|
|
|
|
2025-11-25 17:31:09 -07:00
|
|
|
debug!("Creating runtimes...");
|
2025-11-25 14:27:06 -07:00
|
|
|
for runtime in &config.runtime_config {
|
2025-11-25 17:31:09 -07:00
|
|
|
Self::create_runtime(this.clone(), runtime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
debug!("Starting runtimes...");
|
|
|
|
|
for runtime in &mut this.lock().unwrap().active_runtimes {
|
|
|
|
|
if let Err(e) = runtime.init(this.clone()) {
|
|
|
|
|
warn!("Failed to start runtime: {}", e);
|
|
|
|
|
}
|
2025-11-25 14:27:06 -07:00
|
|
|
}
|
2025-11-12 17:39:11 -07:00
|
|
|
|
2025-11-25 15:22:14 -07:00
|
|
|
this.lock().unwrap().handle = Some(Self::start_thread(this.clone()));
|
|
|
|
|
|
2025-11-25 14:27:06 -07:00
|
|
|
this
|
2025-11-08 14:56:03 -07:00
|
|
|
}
|
|
|
|
|
|
2025-11-13 11:52:01 -07:00
|
|
|
fn load_components(&mut self) {
|
|
|
|
|
for module in &self.modules {
|
|
|
|
|
// Load get_components function from shared object library
|
|
|
|
|
let component_func = match module
|
2025-11-14 09:43:41 -07:00
|
|
|
.get_symbol::<fn() -> Vec<NamedComponent>>(symbol!("get_components").as_bytes())
|
2025-11-13 11:52:01 -07:00
|
|
|
{
|
|
|
|
|
Ok(func) => func,
|
|
|
|
|
Err(_) => {
|
|
|
|
|
warn!("get_components function not found");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let components = component_func();
|
2025-11-14 09:43:41 -07:00
|
|
|
let component_name = "TODO"; //TODO: Make this actually load component name
|
2025-11-13 11:52:01 -07:00
|
|
|
|
|
|
|
|
debug!("{} - Retrieved payload metadata", component_name);
|
|
|
|
|
|
|
|
|
|
// Add each component into self
|
|
|
|
|
for c in components {
|
|
|
|
|
debug!("{} - Found component '{}'", "TODO", c.name);
|
|
|
|
|
self.components.insert(c.name.to_owned(), c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-08 14:56:03 -07:00
|
|
|
|
2025-11-25 15:22:14 -07:00
|
|
|
/// The manager thread. receives announcements, and kills runtimes.
|
|
|
|
|
fn start_thread(this: Arc<Mutex<Self>>) -> JoinHandle<()> {
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
|
loop {
|
|
|
|
|
thread::sleep(Duration::from_millis(10));
|
2025-11-08 14:56:03 -07:00
|
|
|
|
2025-11-25 15:22:14 -07:00
|
|
|
let mut this_lock = this.lock().unwrap();
|
2025-11-12 17:39:11 -07:00
|
|
|
|
2025-11-25 15:22:14 -07:00
|
|
|
if this_lock.active_runtimes.len() <= 0 {
|
|
|
|
|
debug!("There are no more runtimes! Exiting...");
|
|
|
|
|
break;
|
2025-11-14 09:43:41 -07:00
|
|
|
}
|
2025-11-12 17:39:11 -07:00
|
|
|
|
2025-11-25 15:22:14 -07:00
|
|
|
this_lock.active_runtimes.retain(|runtime| {
|
|
|
|
|
if runtime.is_running() {
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
debug!("Runtime exited!"); //TODO: Make this better
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Read announcements
|
|
|
|
|
this_lock.recv_connection_announcements();
|
|
|
|
|
|
|
|
|
|
// Prune dead connections
|
|
|
|
|
this_lock.prune_connections();
|
|
|
|
|
|
|
|
|
|
drop(this_lock)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Wait for manager thread to finish.
|
|
|
|
|
pub fn join(this: Arc<Mutex<Self>>) {
|
|
|
|
|
loop {
|
|
|
|
|
if this.lock().unwrap().handle.as_ref().unwrap().is_finished() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-11-12 17:39:11 -07:00
|
|
|
|
2025-11-25 15:22:14 -07:00
|
|
|
thread::sleep(Duration::from_millis(100));
|
2025-11-08 14:56:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-25 14:27:06 -07:00
|
|
|
/// Start a runtime
|
2025-11-25 17:31:09 -07:00
|
|
|
fn create_runtime<'a>(this: Arc<Mutex<Self>>, runtime: &'static RuntimeConfig) {
|
2025-11-25 14:27:06 -07:00
|
|
|
let mut this_lock = this.lock().unwrap();
|
|
|
|
|
|
|
|
|
|
let component = match this_lock.components.get(&runtime.parent_component) {
|
|
|
|
|
Some(component) => component,
|
|
|
|
|
None => {
|
|
|
|
|
warn!(
|
|
|
|
|
"Could not find component '{}' which is referenced by runtime: {}",
|
|
|
|
|
runtime.parent_component, runtime.name
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debug!("Starting runtime: {}", runtime.name);
|
|
|
|
|
|
|
|
|
|
let runtime = match (*component.start_runtime)(runtime) {
|
|
|
|
|
Ok(runtime) => runtime,
|
|
|
|
|
Err(e) => {
|
|
|
|
|
warn!("Failed to start runtime: {:?}", e);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this_lock.active_runtimes.push(runtime);
|
|
|
|
|
}
|
2025-11-12 17:39:11 -07:00
|
|
|
|
2025-11-25 17:31:09 -07:00
|
|
|
pub fn add_runtime(
|
|
|
|
|
this: Arc<Mutex<Self>>,
|
|
|
|
|
runtime: &'static RuntimeConfig,
|
|
|
|
|
) -> Result<(), ModuleError> {
|
|
|
|
|
Self::create_runtime(this.clone(), runtime);
|
|
|
|
|
|
|
|
|
|
this.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.active_runtimes
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.last()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.init(this.clone())
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-12 17:39:11 -07:00
|
|
|
pub fn get_name(&self) -> &str {
|
|
|
|
|
self.id
|
2025-11-08 17:58:40 -07:00
|
|
|
}
|
2025-11-08 14:56:03 -07:00
|
|
|
}
|