mirror of
https://github.com/Astatin3/unshell.git
synced 2026-06-08 22:38:01 -06:00
Rename things to ush for brevity. Add Tree system.
This commit is contained in:
@@ -7,9 +7,14 @@ pub type Result<T> = std::result::Result<T, ModuleError>;
|
||||
pub enum ModuleError {
|
||||
NoError,
|
||||
|
||||
// Tree errors
|
||||
TreeNotExist,
|
||||
TreeMessageError(String),
|
||||
|
||||
// Object errors
|
||||
UnsupportedMethod,
|
||||
InvalidType,
|
||||
|
||||
LibLoadingError(String),
|
||||
// LogError(log::SetLoggerError),
|
||||
LinkError(String),
|
||||
|
||||
+3
-17
@@ -3,7 +3,7 @@
|
||||
pub mod config;
|
||||
mod error;
|
||||
pub mod logger;
|
||||
pub mod manager;
|
||||
pub mod tree;
|
||||
|
||||
mod announcement;
|
||||
|
||||
@@ -12,19 +12,5 @@ pub use error::{ModuleError, Result};
|
||||
pub use announcement::Announcement;
|
||||
|
||||
// Re-exports
|
||||
// pub use unshell_crypt;
|
||||
pub use unshell_obfuscate;
|
||||
|
||||
// pub trait Component {
|
||||
// fn name(&self) -> &'static str;
|
||||
// // fn start_runtime(&self, manager: Arc<Mutex<Manager>>) -> Option<Box<dyn ModuleRuntime>>;
|
||||
|
||||
// fn get_interface(&self) -> Box<dyn Interface>;
|
||||
// fn clone_box(&self) -> Box<dyn Component>;
|
||||
// }
|
||||
|
||||
// impl Clone for Box<dyn Component> {
|
||||
// fn clone(&self) -> Box<dyn Component> {
|
||||
// self.clone_box()
|
||||
// }
|
||||
// }
|
||||
pub use serde_json::{Value, json};
|
||||
pub use ush_obfuscate as obfuscate;
|
||||
|
||||
+23
-117
@@ -1,15 +1,14 @@
|
||||
#[cfg(feature = "log_debug")]
|
||||
#[macro_export]
|
||||
macro_rules! debug {
|
||||
($fmt:tt) => {{
|
||||
use $crate::unshell_obfuscate;
|
||||
let log_result = unshell_obfuscate::format_obs!($fmt);
|
||||
macro_rules! log {
|
||||
($level:expr, $fmt:tt) => {{
|
||||
use $crate::obfuscate;
|
||||
let log_result = obfuscate::format_obs!($fmt);
|
||||
|
||||
$crate::logger::add_record(
|
||||
$crate::logger::LogLevel::Debug,
|
||||
$level,
|
||||
|
||||
#[cfg(feature = "log_debug")]
|
||||
Some(String::from(unshell_obfuscate::file_symbol!())),
|
||||
Some(String::from(obfuscate::file_symbol!())),
|
||||
#[cfg(not(feature = "log_debug"))]
|
||||
None,
|
||||
|
||||
@@ -17,15 +16,15 @@ macro_rules! debug {
|
||||
log_result
|
||||
);
|
||||
}};
|
||||
($fmt:tt, $($arg:expr),*) => {{
|
||||
use $crate::unshell_obfuscate;
|
||||
let log_result = unshell_obfuscate::format_obs!($fmt, $($arg),*);
|
||||
($level:expr, $fmt:tt, $($arg:expr),*) => {{
|
||||
use $crate::obfuscate;
|
||||
let log_result = obfuscate::format_obs!($fmt, $($arg),*);
|
||||
|
||||
$crate::logger::add_record(
|
||||
$crate::logger::LogLevel::Debug,
|
||||
$level,
|
||||
|
||||
#[cfg(feature = "log_debug")]
|
||||
Some(String::from(unshell_obfuscate::file_symbol!())),
|
||||
Some(String::from(obfuscate::file_symbol!())),
|
||||
#[cfg(not(feature = "log_debug"))]
|
||||
None,
|
||||
|
||||
@@ -35,123 +34,30 @@ macro_rules! debug {
|
||||
}};
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "log_debug"))]
|
||||
#[macro_export]
|
||||
macro_rules! debug {
|
||||
($fmt:tt) => {{
|
||||
let _ = $fmt;
|
||||
}};
|
||||
($fmt:tt, $($arg:expr),*) => {{
|
||||
let _ = $fmt;
|
||||
$(let _ = $arg;)*
|
||||
}};
|
||||
($($arg:tt)*) => {
|
||||
$crate::log!($crate::logger::LogLevel::Debug, $($arg)*)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! info {
|
||||
($fmt:tt) => {{
|
||||
use $crate::unshell_obfuscate;
|
||||
let log_result = unshell_obfuscate::format_obs!($fmt);
|
||||
|
||||
$crate::logger::add_record(
|
||||
$crate::logger::LogLevel::Info,
|
||||
|
||||
#[cfg(feature = "log_debug")]
|
||||
Some(String::from(unshell_obfuscate::file_symbol!())),
|
||||
#[cfg(not(feature = "log_debug"))]
|
||||
None,
|
||||
|
||||
std::time::SystemTime::now(),
|
||||
log_result
|
||||
);
|
||||
}};
|
||||
($fmt:tt, $($arg:expr),*) => {{
|
||||
use $crate::unshell_obfuscate;
|
||||
let log_result = unshell_obfuscate::format_obs!($fmt, $($arg),*);
|
||||
|
||||
$crate::logger::add_record(
|
||||
$crate::logger::LogLevel::Info,
|
||||
|
||||
#[cfg(feature = "log_debug")]
|
||||
Some(String::from(unshell_obfuscate::file_symbol!())),
|
||||
#[cfg(not(feature = "log_debug"))]
|
||||
None,
|
||||
|
||||
std::time::SystemTime::now(),
|
||||
log_result
|
||||
);
|
||||
}};
|
||||
($($arg:tt)*) => {
|
||||
$crate::log!($crate::logger::LogLevel::Info, $($arg)*)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! warn {
|
||||
($fmt:tt) => {{
|
||||
use $crate::unshell_obfuscate;
|
||||
|
||||
let log_result = unshell_obfuscate::format_obs!($fmt);
|
||||
|
||||
$crate::logger::add_record(
|
||||
$crate::logger::LogLevel::Warn,
|
||||
|
||||
#[cfg(feature = "log_debug")]
|
||||
Some(String::from(unshell_obfuscate::file_symbol!())),
|
||||
#[cfg(not(feature = "log_debug"))]
|
||||
None,
|
||||
|
||||
std::time::SystemTime::now(),
|
||||
log_result
|
||||
);
|
||||
}};
|
||||
($fmt:tt, $($arg:expr),*) => {{
|
||||
use $crate::unshell_obfuscate;
|
||||
|
||||
let log_result = unshell_obfuscate::format_obs!($fmt, $($arg),*);
|
||||
|
||||
$crate::logger::add_record(
|
||||
$crate::logger::LogLevel::Warn,
|
||||
|
||||
#[cfg(feature = "log_debug")]
|
||||
Some(String::from(unshell_obfuscate::file_symbol!())),
|
||||
#[cfg(not(feature = "log_debug"))]
|
||||
None,
|
||||
|
||||
std::time::SystemTime::now(),
|
||||
log_result
|
||||
);
|
||||
}};
|
||||
($($arg:tt)*) => {
|
||||
$crate::log!($crate::logger::LogLevel::Warn, $($arg)*)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! error {
|
||||
($fmt:tt) => {{
|
||||
let log_result = unshell_obfuscate::format_obs!($fmt);
|
||||
|
||||
$crate::logger::add_record(
|
||||
$crate::logger::LogLevel::Error,
|
||||
|
||||
#[cfg(feature = "log_debug")]
|
||||
Some(String::from(unshell_obfuscate::file_symbol!())),
|
||||
#[cfg(not(feature = "log_debug"))]
|
||||
None,
|
||||
|
||||
std::time::SystemTime::now(),
|
||||
log_result
|
||||
);
|
||||
}};
|
||||
($fmt:tt, $($arg:expr),*) => {{
|
||||
use $crate::unshell_obfuscate;
|
||||
let log_result = unshell_obfuscate::format_obs!($fmt, $($arg),*);
|
||||
|
||||
$crate::logger::add_record(
|
||||
$crate::logger::LogLevel::Error,
|
||||
|
||||
#[cfg(feature = "log_debug")]
|
||||
Some(String::from(unshell_obfuscate::file_symbol!())),
|
||||
#[cfg(not(feature = "log_debug"))]
|
||||
None,
|
||||
|
||||
std::time::SystemTime::now(),
|
||||
log_result
|
||||
);
|
||||
}};
|
||||
($($arg:tt)*) => {
|
||||
$crate::log!($crate::logger::LogLevel::Error, $($arg)*)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ mod pretty_logger;
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub use pretty_logger::PrettyLogger;
|
||||
pub use pretty_logger::log;
|
||||
|
||||
static mut LOGGER: &dyn Logger = &DefaultLogger;
|
||||
|
||||
|
||||
+24
-16
@@ -23,25 +23,33 @@ impl Logger for PrettyLogger {
|
||||
(*func)(&message)
|
||||
}
|
||||
|
||||
let log_level = match message.log_level {
|
||||
LogLevel::Debug => format!("{DEBUG_COLOR}DBUG"),
|
||||
LogLevel::Info => format!("{INFO_COLOR}INFO"),
|
||||
LogLevel::Warn => format!("{WARN_COLOR}WARN"),
|
||||
LogLevel::Error => format!("{ERROR_COLOR}ERR!"),
|
||||
};
|
||||
|
||||
let date: DateTime<Utc> = message.time.into();
|
||||
let date = date.to_rfc2822().to_string();
|
||||
|
||||
let location = message.location.unwrap_or("".to_string());
|
||||
|
||||
println!(
|
||||
"{OFF_WHITE}[{TIME_COLOR}{}{OFF_WHITE}] {} {WHITE}{} {GREY}{}{WHITE}",
|
||||
date, log_level, message.message, location
|
||||
);
|
||||
log(&message);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn log(message: &Record) {
|
||||
let log_level = match message.log_level {
|
||||
LogLevel::Debug => format!("{DEBUG_COLOR}DBUG"),
|
||||
LogLevel::Info => format!("{INFO_COLOR}INFO"),
|
||||
LogLevel::Warn => format!("{WARN_COLOR}WARN"),
|
||||
LogLevel::Error => format!("{ERROR_COLOR}ERR!"),
|
||||
};
|
||||
|
||||
let date: DateTime<Utc> = message.time.into();
|
||||
let date = date.to_rfc2822().to_string();
|
||||
|
||||
let location = if let Some(ref location) = message.location {
|
||||
location
|
||||
} else {
|
||||
&String::new()
|
||||
};
|
||||
|
||||
println!(
|
||||
"{OFF_WHITE}[{TIME_COLOR}{}{OFF_WHITE}] {} {WHITE}{} {GREY}{}{WHITE}",
|
||||
date, log_level, message.message, location
|
||||
);
|
||||
}
|
||||
|
||||
impl PrettyLogger {
|
||||
pub fn init() {
|
||||
crate::logger::set_logger_box(Box::new(PrettyLogger { output: None }));
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
/// Implement logging for the manager
|
||||
use crossbeam_channel::Sender;
|
||||
|
||||
use crate::{
|
||||
logger::{Logger, Record},
|
||||
manager::Tree,
|
||||
};
|
||||
|
||||
pub struct ManagerLogger {
|
||||
tx: Sender<Record>,
|
||||
}
|
||||
|
||||
impl ManagerLogger {
|
||||
pub fn new(tx: Sender<Record>) -> Self {
|
||||
Self { tx }
|
||||
}
|
||||
}
|
||||
|
||||
// impl Manager {
|
||||
// /// Initiate the unshell logger, piped through the manager
|
||||
// /// This will allow access to the logs through the tree
|
||||
// pub fn init_logger(&self) {
|
||||
// // Create the logger through the TX element of the manager
|
||||
// let logger = ManagerLogger::new(self.logs_tx.clone());
|
||||
|
||||
// // Set the logger through unshell
|
||||
// crate::logger::set_logger_box(Box::new(logger));
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Logger for ManagerLogger {
|
||||
fn log(&self, log: crate::logger::Record) {
|
||||
// This will never panic if the program is operating properly
|
||||
self.tx.send(log).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
ModuleError,
|
||||
manager::tree_structs::{TreeMessage, TreeType},
|
||||
};
|
||||
|
||||
mod log;
|
||||
mod tree_structs;
|
||||
|
||||
pub trait TreeElement {
|
||||
fn get_children(&self) -> HashMap<String, TreeType>;
|
||||
fn get_type(&self) -> TreeType;
|
||||
fn send_message(&mut self, message: TreeMessage) -> TreeMessage;
|
||||
fn send_message_child(&mut self, element: String, message: TreeMessage) -> TreeMessage;
|
||||
}
|
||||
|
||||
pub struct Tree {
|
||||
elements: HashMap<String, Box<dyn TreeElement>>,
|
||||
}
|
||||
|
||||
impl Tree {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
elements: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_element(&mut self, name: String, element: Box<dyn TreeElement>) {
|
||||
self.elements.insert(name, element);
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeElement for Tree {
|
||||
fn get_children(&self) -> HashMap<String, TreeType> {
|
||||
self.elements
|
||||
.iter()
|
||||
.map(|c| (c.0.clone(), c.1.get_type()))
|
||||
.into_iter()
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_type(&self) -> TreeType {
|
||||
TreeType::RootTree
|
||||
}
|
||||
|
||||
fn send_message_child(&mut self, element_name: String, message: TreeMessage) -> TreeMessage {
|
||||
if let Some(element) = self.elements.get_mut(&element_name) {
|
||||
element.send_message(message)
|
||||
} else {
|
||||
TreeMessage::Result(ModuleError::TreeNotExist)
|
||||
}
|
||||
}
|
||||
|
||||
fn send_message(&mut self, _message: TreeMessage) -> TreeMessage {
|
||||
TreeMessage::Response
|
||||
// if let Some(element) = self.elements.get_mut(&element_name) {
|
||||
// element.send_message(message)
|
||||
// } else {
|
||||
// TreeMessage::Result(ModuleError::TreeNotExist)
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
pub enum TreeMessage {
|
||||
Request,
|
||||
Response,
|
||||
Result(crate::error::ModuleError),
|
||||
}
|
||||
|
||||
pub enum TreeType {
|
||||
RootTree,
|
||||
|
||||
TypeA,
|
||||
TypeB,
|
||||
TypeC,
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
/// Implement logging for the manager
|
||||
use crossbeam_channel::{Receiver, Sender};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::{
|
||||
logger::{Logger, Record},
|
||||
tree::{Tree, TreeElement, symbols},
|
||||
};
|
||||
|
||||
struct LoggerTX(Sender<Record>);
|
||||
struct LoggerRX(Receiver<Record>);
|
||||
|
||||
impl Tree {
|
||||
/// Initiate the unshell logger for the local binary, piped through the manager
|
||||
/// This will allow access to the logs through the tree
|
||||
pub fn init_logger(&mut self) {
|
||||
// Create the logger through the TX element of the manager
|
||||
|
||||
let (tx, rx) = crossbeam_channel::unbounded();
|
||||
let (tx, rx) = (LoggerTX(tx), LoggerRX(rx));
|
||||
|
||||
// Set the logger through unshell
|
||||
crate::logger::set_logger_box(Box::new(tx));
|
||||
// Add the logger to the tree
|
||||
self.add_element(symbols::LOGGER.to_string(), Box::new(rx));
|
||||
}
|
||||
}
|
||||
|
||||
impl Logger for LoggerTX {
|
||||
fn log(&self, log: crate::logger::Record) {
|
||||
// This will never panic if the program is operating properly
|
||||
self.0.send(log).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeElement for LoggerRX {
|
||||
fn get_type(&self) -> Value {
|
||||
json!(symbols::TYPE_QUEUE)
|
||||
}
|
||||
|
||||
fn send_message(&mut self, target: Value, message: Value) -> Value {
|
||||
match (target, message) {
|
||||
(Value::Null, Value::String(message)) => match message.as_ref() {
|
||||
symbols::CMD_GET => {
|
||||
if !self.0.is_empty() {
|
||||
json!(self.0.recv().unwrap())
|
||||
} else {
|
||||
json!(Value::Null)
|
||||
}
|
||||
}
|
||||
symbols::CMD_GET_LENGTH => {
|
||||
json!(self.0.len())
|
||||
}
|
||||
_ => json!(symbols::ERR_INVALID_COMMAND),
|
||||
},
|
||||
_ => json!(symbols::ERR_INVALID_CHILD),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde_json::{Value, json};
|
||||
|
||||
mod log;
|
||||
pub mod symbols;
|
||||
|
||||
pub trait TreeElement {
|
||||
// fn get_children(&self) -> HashMap<TreeType, TreeType>;
|
||||
fn get_type(&self) -> Value;
|
||||
fn send_message(&mut self, target: Value, message: Value) -> Value;
|
||||
}
|
||||
|
||||
pub struct Tree {
|
||||
elements: HashMap<String, Box<dyn TreeElement>>,
|
||||
}
|
||||
|
||||
impl Tree {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
elements: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_element(&mut self, name: String, element: Box<dyn TreeElement>) {
|
||||
self.elements.insert(name, element);
|
||||
}
|
||||
}
|
||||
|
||||
impl TreeElement for Tree {
|
||||
fn get_type(&self) -> Value {
|
||||
json!(symbols::TYPE_TREE)
|
||||
}
|
||||
|
||||
// fn send_message_child(&mut self, element: Value, message: TreeMessage) -> TreeMessage {
|
||||
// let name = if let TreeType::String(name) = element {
|
||||
// name
|
||||
// } else {
|
||||
// return TreeMessage::Error(ModuleError::InvalidType);
|
||||
// };
|
||||
|
||||
// if let Some(element) = self.elements.get_mut(&name) {
|
||||
// element.send_message(message)
|
||||
// } else {
|
||||
// TreeMessage::Error(ModuleError::TreeNotExist)
|
||||
// }
|
||||
// }
|
||||
|
||||
fn send_message(&mut self, target: Value, message: Value) -> Value {
|
||||
match target {
|
||||
Value::Null => {
|
||||
if let Some(message) = message.as_str() {
|
||||
match message {
|
||||
"GetChildren" => {
|
||||
let children = self
|
||||
.elements
|
||||
.iter()
|
||||
.map(|c| (Value::String(c.0.clone()), c.1.get_type()))
|
||||
.into_iter()
|
||||
.collect::<HashMap<Value, Value>>();
|
||||
|
||||
json!(children)
|
||||
}
|
||||
|
||||
_ => Value::String("UnsupportedMethod".to_owned()),
|
||||
}
|
||||
} else {
|
||||
Value::String("UnsupportedMethod".to_owned())
|
||||
}
|
||||
}
|
||||
Value::String(target) => {
|
||||
if let Some(child) = self.elements.get_mut(&target) {
|
||||
child.send_message(Value::Null, message)
|
||||
} else {
|
||||
Value::String("UnsupportedMethod".to_owned())
|
||||
}
|
||||
}
|
||||
_ => Value::String("UnsupportedMethod".to_owned()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
use crate::obfuscate::symbol;
|
||||
|
||||
pub const LOGGER: &'static str = symbol!("Logger");
|
||||
|
||||
pub const TYPE_TREE: &'static str = symbol!("Tree");
|
||||
pub const TYPE_QUEUE: &'static str = symbol!("Queue");
|
||||
|
||||
pub const CMD_GET: &'static str = symbol!("Get");
|
||||
pub const CMD_POLL: &'static str = symbol!("Poll");
|
||||
pub const CMD_GET_LENGTH: &'static str = symbol!("GetLength");
|
||||
pub const CMD_GET_CHILDREN: &'static str = symbol!("GetChildren");
|
||||
|
||||
pub const ERR_UNSUPPORTED_METHOD: &'static str = symbol!("UnsupportedMethod");
|
||||
pub const ERR_INVALID_COMMAND: &'static str = symbol!("InvalidCommand");
|
||||
pub const ERR_INVALID_CHILD: &'static str = symbol!("InvalidChild");
|
||||
Reference in New Issue
Block a user