summaryrefslogtreecommitdiff
path: root/src/plugin/example.rs
blob: bce172481ccda86e1ef5428cb5c1e86cae48dc59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use crate::event::event::*;
use crate::event::manager::EventHandle;
use crate::packet::packet::Packet;
use crate::session::session::Session;
use crate::thread::thread::ThreadContex;
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Clone)]
pub struct ExamplePulgin {
    plugin_name: &'static str,
    plugin_ctx: Rc<RefCell<String>>,
    thread_ctx: Rc<RefCell<ThreadContex>>,
    tcp_opening_event: usize,
    tcp_active_event: usize,
    tcp_expire_event: usize,
    tcp_closed_event: usize,
    http_opening_event: usize,
}

impl ExamplePulgin {
    pub fn new(plugin_name: &'static str, thread_ctx: Rc<RefCell<ThreadContex>>) -> ExamplePulgin {
        ExamplePulgin {
            plugin_name,
            plugin_ctx: Rc::new(RefCell::new(String::new())),
            thread_ctx: thread_ctx,
            tcp_opening_event: 0,
            tcp_active_event: 0,
            tcp_expire_event: 0,
            tcp_closed_event: 0,
            http_opening_event: 0,
        }
    }

    pub fn get_thread_ctx(&self) -> Rc<RefCell<ThreadContex>> {
        self.thread_ctx.clone()
    }
}

impl EventHandle for ExamplePulgin {
    fn init(&mut self) {
        let thread_ctx = self.get_thread_ctx();
        let event_mgr = thread_ctx.borrow().get_event_mgr();

        self.tcp_opening_event = event_mgr
            .borrow_mut()
            .event2index(BUILTIN_TCP_OPENING_EVENT);
        event_mgr
            .borrow_mut()
            .register(self.tcp_opening_event, Box::new(self.clone()));

        self.tcp_active_event = event_mgr.borrow_mut().event2index(BUILTIN_TCP_ACTIVE_EVENT);
        event_mgr
            .borrow_mut()
            .register(self.tcp_active_event, Box::new(self.clone()));

        self.tcp_expire_event = event_mgr.borrow_mut().event2index(BUILTIN_TCP_EXPIRE_EVENT);
        event_mgr
            .borrow_mut()
            .register(self.tcp_expire_event, Box::new(self.clone()));

        self.tcp_closed_event = event_mgr.borrow_mut().event2index(BUILTIN_TCP_CLOSED_EVENT);
        event_mgr
            .borrow_mut()
            .register(self.tcp_closed_event, Box::new(self.clone()));

        self.http_opening_event = event_mgr
            .borrow_mut()
            .event2index("USERDEF_HTTP_OPENING_EVENT");
        event_mgr
            .borrow_mut()
            .register(self.http_opening_event, Box::new(self.clone()));
    }

    fn handle(
        &mut self,
        index: usize,
        packet: Option<&Packet>,
        session: Option<Rc<RefCell<Session>>>,
    ) {
        self.plugin_ctx.borrow_mut().clear();
        self.plugin_ctx.borrow_mut().push_str("1");

        if session.is_none() {
            return;
        }

        if packet.is_some() {
            let flow_id = packet.unwrap().get_flow_id();
            println!("{} handle Packet: {:?}", self.plugin_name, flow_id);
        } else {
            println!("{} handle Packet: None", self.plugin_name);
        }

        let session = session.unwrap();
        if index == self.tcp_opening_event {
            println!(
                "{} handle BUILTIN_TCP_OPENING_EVENT: {:?}",
                self.plugin_name, session
            );
        } else if index == self.tcp_active_event {
            println!(
                "{} handle BUILTIN_TCP_ACTIVE_EVENT: {:?}",
                self.plugin_name, session
            );
        } else if index == self.tcp_expire_event {
            println!(
                "{} handle BUILTIN_TCP_EXPIRE_EVENT: {:?}",
                self.plugin_name, session
            );
        } else if index == self.tcp_closed_event {
            println!(
                "{} handle BUILTIN_TCP_CLOSED_EVENT: {:?}",
                self.plugin_name, session
            );
        } else if index == self.http_opening_event {
            println!(
                "{} handle USERDEF_HTTP_OPENING_EVENT: {:?}",
                self.plugin_name, session
            );
        } else {
            println!("{} handle UNKNOWN_EVENT: {:?}", self.plugin_name, session);
        }
    }
}