pako_core/plugins/
mod.rs

1//! Plugin factory definition and default plugins implementation
2
3use std::collections::HashMap;
4
5use pako_tools::Config;
6
7use crate::{Plugin, PluginBuilder, PluginBuilderError, PluginRegistry};
8
9mod basic_stats;
10#[cfg(feature = "plugin_community_id")]
11mod community_id;
12#[cfg(feature = "plugin_examples")]
13mod examples;
14mod flows;
15#[cfg(feature = "plugins_debug")]
16mod hexdump;
17#[cfg(feature = "plugin_ospf")]
18mod ospf;
19#[cfg(feature = "plugin_rusticata")]
20mod rusticata;
21#[cfg(feature = "plugin_tls_stats")]
22mod tls_stats;
23
24/// Storage of plugin instances
25pub struct Plugins {
26    pub storage: HashMap<String, Box<dyn Plugin>>,
27}
28
29/// Plugin Factory
30///
31/// A plugin factory stores all registered builders, and is used to
32/// create all plugin instances on request.
33pub struct PluginsFactory {
34    list: Vec<Box<dyn PluginBuilder>>,
35}
36
37impl PluginsFactory {
38    /// Create a new empty plugin factory
39    pub fn new() -> PluginsFactory {
40        PluginsFactory { list: Vec::new() }
41    }
42
43    /// Add a new plugin builder to the factory
44    pub fn add_builder(&mut self, b: Box<dyn PluginBuilder>) {
45        self.list.push(b);
46    }
47
48    /// Instantiate all plugins
49    pub fn build_plugins(&self, config: &Config) -> Result<PluginRegistry, PluginBuilderError> {
50        let mut registry = PluginRegistry::new();
51
52        for b in &self.list {
53            b.build(&mut registry, config)?;
54        }
55
56        Ok(registry)
57    }
58
59    /// Instantiate plugins if they match predicate
60    pub fn build_filter_plugins<P>(
61        &self,
62        predicate: P,
63        config: &Config,
64    ) -> Result<PluginRegistry, PluginBuilderError>
65    where
66        P: Fn(&str) -> bool,
67    {
68        let mut registry = PluginRegistry::new();
69
70        for b in &self.list {
71            if predicate(b.name()) {
72                b.build(&mut registry, config)?;
73            }
74        }
75
76        Ok(registry)
77    }
78
79    /// Iterate builder names
80    pub fn iter_builders<Op>(&self, op: Op)
81    where
82        Op: Fn(&str),
83    {
84        self.list.iter().for_each(|b| op(b.name()));
85    }
86}
87
88impl Default for PluginsFactory {
89    /// Create a new plugin factory, with all default plugins
90    fn default() -> Self {
91        // we need `v` to be mutable, so we can push additional plugins
92        #[allow(unused_mut)]
93        let mut v: Vec<Box<dyn PluginBuilder>> = vec![
94            Box::new(basic_stats::BasicStatsBuilder),
95            Box::new(flows::FlowsInfoBuilder),
96        ];
97
98        #[cfg(feature = "plugin_community_id")]
99        v.push(Box::new(community_id::CommunityIDBuilder));
100
101        #[cfg(feature = "plugins_debug")]
102        v.push(Box::new(hexdump::HexDumpBuilder));
103
104        #[cfg(feature = "plugin_tls_stats")]
105        v.push(Box::new(tls_stats::TlsStatsBuilder));
106
107        #[cfg(feature = "plugin_rusticata")]
108        v.push(Box::new(rusticata::RusticataBuilder));
109
110        #[cfg(feature = "plugin_examples")]
111        {
112            v.push(Box::new(examples::EmptyBuilder));
113            v.push(Box::new(examples::EmptyWithConfigBuilder));
114        }
115
116        #[cfg(feature = "plugin_ospf")]
117        v.push(Box::new(ospf::OspfLogBuilder));
118
119        PluginsFactory { list: v }
120    }
121}