Add the opensmtpd_derive crate
This commit is contained in:
parent
d1d51bbaa8
commit
4598fb33e4
29 changed files with 81 additions and 39 deletions
55
opensmtpd/examples/counter.rs
Normal file
55
opensmtpd/examples/counter.rs
Normal file
|
@ -0,0 +1,55 @@
|
|||
use log;
|
||||
use opensmtpd::{run_filter, Address, Filter, ReportEntry};
|
||||
use opensmtpd_derive::register;
|
||||
use simplelog::{Config, LevelFilter, WriteLogger};
|
||||
use std::fs::File;
|
||||
|
||||
pub const DEFAULT_LOG_FILE: &str = "/tmp/counter.log";
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyCounter {
|
||||
nb_connected: u64,
|
||||
nb_total: u64,
|
||||
}
|
||||
|
||||
impl Filter for MyCounter {
|
||||
#[register]
|
||||
fn on_report_link_connect(
|
||||
&mut self,
|
||||
_entry: &ReportEntry,
|
||||
_rdns: &str,
|
||||
_fcrdns: &str,
|
||||
_src: &Address,
|
||||
_dest: &Address,
|
||||
) {
|
||||
self.nb_connected += 1;
|
||||
self.nb_total += 1;
|
||||
log::info!(
|
||||
"New client (connected: {}, total: {})",
|
||||
self.nb_connected,
|
||||
self.nb_total
|
||||
);
|
||||
}
|
||||
|
||||
#[register]
|
||||
fn on_report_link_disconnect(&mut self, _entry: &ReportEntry) {
|
||||
self.nb_connected -= 1;
|
||||
log::info!(
|
||||
"Client left (connected: {}, total: {})",
|
||||
self.nb_connected,
|
||||
self.nb_total
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let log_file = std::env::var("LOG_FILE").unwrap_or(String::from(DEFAULT_LOG_FILE));
|
||||
WriteLogger::init(
|
||||
LevelFilter::Info,
|
||||
Config::default(),
|
||||
File::create(&log_file).unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut my_counter: MyCounter = Default::default();
|
||||
run_filter(&mut my_counter);
|
||||
}
|
4
opensmtpd/examples/samples/empty.log
Normal file
4
opensmtpd/examples/samples/empty.log
Normal file
|
@ -0,0 +1,4 @@
|
|||
config|smtpd-version|6.6.1
|
||||
config|smtp-session-timeout|300
|
||||
config|subsystem|smtp-in
|
||||
config|ready
|
6
opensmtpd/examples/samples/single_session.log
Normal file
6
opensmtpd/examples/samples/single_session.log
Normal file
|
@ -0,0 +1,6 @@
|
|||
config|smtpd-version|6.6.1
|
||||
config|smtp-session-timeout|300
|
||||
config|subsystem|smtp-in
|
||||
config|ready
|
||||
report|0.5|1576146008.006099|smtp-in|link-connect|7641df9771b4ed00|mail.openbsd.org|pass|199.185.178.25:33174|45.77.67.80:25
|
||||
report|0.5|1576147242.200225|smtp-in|link-disconnect|7641dfb3798eb5bf
|
Reference in a new issue