diff --git a/src/meta_plugin.rs b/src/meta_plugin.rs index 3acbd01..81229cb 100644 --- a/src/meta_plugin.rs +++ b/src/meta_plugin.rs @@ -9,17 +9,28 @@ use enum_map::{Enum, EnumMap}; pub mod program; pub mod digest; -pub mod basic; +pub mod system; use crate::meta_plugin::program::MetaPluginProgram; use crate::meta_plugin::digest::{DigestSha256MetaPlugin, ReadTimeMetaPlugin, ReadRateMetaPlugin}; -use crate::meta_plugin::basic::{HostnameMetaPlugin, FullHostnameMetaPlugin}; -use crate::meta_plugin::system::{CwdMetaPlugin, UidMetaPlugin, UserMetaPlugin, GidMetaPlugin, GroupMetaPlugin, ShellMetaPlugin, ShellPidMetaPlugin, KeepPidMetaPlugin}; +use crate::meta_plugin::system::{CwdMetaPlugin, UidMetaPlugin, UserMetaPlugin, GidMetaPlugin, GroupMetaPlugin, ShellMetaPlugin, ShellPidMetaPlugin, KeepPidMetaPlugin, HostnameMetaPlugin, FullHostnameMetaPlugin}; #[derive(Debug, Eq, PartialEq, Clone, strum::EnumIter, strum::Display, strum::EnumString, Enum)] #[strum(ascii_case_insensitive)] pub enum MetaPluginType { FileMagic, + FileMime, + FileEncoding, + LineCount, + WordCount, + Cwd, + Uid, + User, + Gid, + Group, + Shell, + ShellPid, + KeepPid, DigestSha256, DigestMd5, ReadTime, @@ -49,6 +60,30 @@ lazy_static! { let program = MetaPluginProgram::new("file", vec!["-bE", "-"], "file_magic".to_string(), true); if program.supported { Some(program) } else { None } } + MetaPluginType::FileMime => { + let program = MetaPluginProgram::new("file", vec!["--mime", "-"], "file_mime".to_string(), true); + if program.supported { Some(program) } else { None } + } + MetaPluginType::FileEncoding => { + let program = MetaPluginProgram::new("file", vec!["-bI", "-"], "file_encoding".to_string(), true); + if program.supported { Some(program) } else { None } + } + MetaPluginType::LineCount => { + let program = MetaPluginProgram::new("wc", vec!["-l"], "line_count".to_string(), true); + if program.supported { Some(program) } else { None } + } + MetaPluginType::WordCount => { + let program = MetaPluginProgram::new("wc", vec!["-w"], "word_count".to_string(), true); + if program.supported { Some(program) } else { None } + } + MetaPluginType::Cwd => None, + MetaPluginType::Uid => None, + MetaPluginType::User => None, + MetaPluginType::Gid => None, + MetaPluginType::Group => None, + MetaPluginType::Shell => None, + MetaPluginType::ShellPid => None, + MetaPluginType::KeepPid => None, MetaPluginType::DigestSha256 => { let program = MetaPluginProgram::new("sha256sum", vec![], "digest_sha256".to_string(), true); if program.supported { Some(program) } else { None } @@ -67,6 +102,18 @@ lazy_static! { pub fn get_meta_plugin(meta_plugin_type: MetaPluginType) -> Box { match meta_plugin_type { MetaPluginType::FileMagic => Box::new(MetaPluginProgram::new("file", vec!["-bE", "-"], "file_magic".to_string(), true)), + MetaPluginType::FileMime => Box::new(MetaPluginProgram::new("file", vec!["--mime", "-"], "file_mime".to_string(), true)), + MetaPluginType::FileEncoding => Box::new(MetaPluginProgram::new("file", vec!["-bI", "-"], "file_encoding".to_string(), true)), + MetaPluginType::LineCount => Box::new(MetaPluginProgram::new("wc", vec!["-l"], "line_count".to_string(), true)), + MetaPluginType::WordCount => Box::new(MetaPluginProgram::new("wc", vec!["-w"], "word_count".to_string(), true)), + MetaPluginType::Cwd => Box::new(CwdMetaPlugin::new()), + MetaPluginType::Uid => Box::new(UidMetaPlugin::new()), + MetaPluginType::User => Box::new(UserMetaPlugin::new()), + MetaPluginType::Gid => Box::new(GidMetaPlugin::new()), + MetaPluginType::Group => Box::new(GroupMetaPlugin::new()), + MetaPluginType::Shell => Box::new(ShellMetaPlugin::new()), + MetaPluginType::ShellPid => Box::new(ShellPidMetaPlugin::new()), + MetaPluginType::KeepPid => Box::new(KeepPidMetaPlugin::new()), MetaPluginType::DigestSha256 => Box::new(DigestSha256MetaPlugin::new()), MetaPluginType::DigestMd5 => Box::new(MetaPluginProgram::new("md5sum", vec![], "digest_md5".to_string(), true)), MetaPluginType::ReadTime => Box::new(ReadTimeMetaPlugin::new()), diff --git a/src/meta_plugin/system.rs b/src/meta_plugin/system.rs index 942c859..5eb29e4 100644 --- a/src/meta_plugin/system.rs +++ b/src/meta_plugin/system.rs @@ -1,4 +1,7 @@ use anyhow::Result; +use gethostname::gethostname; +use local_ip_address::local_ip; +use dns_lookup::lookup_addr; use std::io; use std::io::Write; use std::env; @@ -279,3 +282,89 @@ impl MetaPlugin for KeepPidMetaPlugin { self.meta_name.clone() } } + +#[derive(Debug, Clone, Default)] +pub struct HostnameMetaPlugin { + meta_name: String, +} + +impl HostnameMetaPlugin { + pub fn new() -> HostnameMetaPlugin { + HostnameMetaPlugin { + meta_name: "hostname".to_string(), + } + } +} + +impl MetaPlugin for HostnameMetaPlugin { + fn create(&self) -> Result> { + Ok(Box::new(io::sink())) + } + + fn finalize(&mut self) -> io::Result { + match gethostname().into_string() { + Ok(hostname) => Ok(hostname), + Err(_) => Ok("unknown".to_string()), + } + } + + fn update(&mut self, _data: &[u8]) { + // No update needed for hostname + } + + fn meta_name(&mut self) -> String { + self.meta_name.clone() + } +} + +#[derive(Debug, Clone, Default)] +pub struct FullHostnameMetaPlugin { + meta_name: String, +} + +impl FullHostnameMetaPlugin { + pub fn new() -> FullHostnameMetaPlugin { + FullHostnameMetaPlugin { + meta_name: "full_hostname".to_string(), + } + } +} + +impl MetaPlugin for FullHostnameMetaPlugin { + fn create(&self) -> Result> { + Ok(Box::new(io::sink())) + } + + fn finalize(&mut self) -> io::Result { + // Try to get the FQDN through reverse DNS lookup + match local_ip() { + Ok(my_local_ip) => { + match lookup_addr(&my_local_ip) { + Ok(hostname) => Ok(hostname), + Err(_) => { + // Fall back to regular hostname if reverse DNS fails + match gethostname().into_string() { + Ok(hostname) => Ok(hostname), + Err(_) => Ok("unknown".to_string()), + } + } + } + } + Err(_) => { + // Fall back to regular hostname if we can't get local IP + match gethostname().into_string() { + Ok(hostname) => Ok(hostname), + Err(_) => Ok("unknown".to_string()), + } + } + } + } + + fn update(&mut self, _data: &[u8]) { + // No update needed for full hostname + } + + fn meta_name(&mut self) -> String { + self.meta_name.clone() + } +}