From 2d7548bf717c420eed5bfbff6aec286edfac8737 Mon Sep 17 00:00:00 2001 From: "Crom (Thibaut CHARLES)" Date: Tue, 5 Feb 2019 08:10:55 +0100 Subject: [PATCH] WIP modules --- Cargo.lock | 175 +++++++++++++++------- Cargo.toml | 6 +- config.yaml | 23 ++- src/main.rs | 22 +-- src/message.rs | 11 +- src/plugins/monitor/dhcp_leases.rs | 156 +++++++++++++++++++ src/plugins/monitor/mod.rs | 13 ++ src/plugins/output/bluetooth_lightbulb.rs | 37 +++++ src/plugins/output/espeak.rs | 19 +++ src/plugins/output/mod.rs | 13 ++ 10 files changed, 394 insertions(+), 81 deletions(-) create mode 100644 src/plugins/monitor/dhcp_leases.rs create mode 100644 src/plugins/output/bluetooth_lightbulb.rs create mode 100644 src/plugins/output/espeak.rs diff --git a/Cargo.lock b/Cargo.lock index ff04869..0e67c5a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,9 +1,18 @@ [[package]] name = "aho-corasick" -version = "0.5.3" +version = "0.6.9" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "blurz" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dbus 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -11,6 +20,30 @@ name = "cc" version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "cfg-if" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "chrono" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "dbus" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "libdbus-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dtoa" version = "0.4.3" @@ -25,19 +58,28 @@ dependencies = [ ] [[package]] -name = "kernel32-sys" -version = "0.2.2" +name = "hex" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "lazy_static" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "libc" version = "0.2.48" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "libdbus-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "libloading" version = "0.5.0" @@ -54,12 +96,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "memchr" -version = "0.1.11" +version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ + "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num-integer" +version = "0.1.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "num-traits" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "pkg-config" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "proc-macro2" version = "0.4.26" @@ -76,33 +137,43 @@ dependencies = [ "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "redox_syscall" +version = "0.1.51" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "regex" -version = "0.1.80" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", - "thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "regex-syntax" -version = "0.3.9" +version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] [[package]] name = "rnetmon" version = "0.1.0" dependencies = [ + "blurz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", "libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)", "serde_yaml 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)", - "wifiscanner 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -142,22 +213,28 @@ dependencies = [ ] [[package]] -name = "thread-id" -version = "2.0.0" +name = "thread_local" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "thread_local" -version = "0.2.7" +name = "time" +version = "0.1.42" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ucd-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-width" version = "0.1.5" @@ -170,20 +247,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "utf8-ranges" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "wifiscanner" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "winapi" -version = "0.2.8" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -195,11 +259,6 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "winapi-build" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -219,32 +278,40 @@ dependencies = [ ] [metadata] -"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" +"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e" +"checksum blurz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b1bc2df4d0f1d373b324672d9966aca3f5ba1f03d69edad6240144774539ea59" "checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749" +"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" +"checksum chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "45912881121cb26fad7c38c17ba7daa18764771836b34fab7d3fbd93ed633878" +"checksum dbus 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b9e1b39f3f6aa3d4a1522c4f0f9f1e9e9167bd93740a8690874caa7cf8ce47d7" "checksum dtoa 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6d301140eb411af13d3115f9a562c85cc6b541ade9dfa314132244aaee7489dd" "checksum getopts 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "0a7292d30132fb5424b354f5dc02512a86e4c516fe544bb7a25e7f266951b797" -"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" +"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" "checksum libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047" +"checksum libdbus-sys 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "18cb88963258d00f4962205dbb5933d82780d9962c8c8a064b651d2ad7189210" "checksum libloading 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3ad660d7cb8c5822cd83d10897b0f1f1526792737a179e73896152f85b88c2" "checksum linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70fb39025bc7cdd76305867c4eccf2f2dcf6e9a57f5b21a93e1c2d86cd03ec9e" -"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20" +"checksum memchr 2.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "e1dd4eaac298c32ce07eb6ed9242eda7d82955b9170b7d6db59b2e02cc63fcb8" +"checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" +"checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" +"checksum pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "676e8eb2b1b4c9043511a9b7bea0915320d7e502b0a079fb03f9635a5252b18c" "checksum proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)" = "38fddd23d98b2144d197c0eca5705632d4fe2667d14a6be5df8934f8d74f1978" "checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" -"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f" -"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957" +"checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85" +"checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f" +"checksum regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8c2f35eedad5295fdf00a63d7d4b238135723f92b434ec06774dad15c7ab0861" "checksum serde 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "534b8b91a95e0f71bca3ed5824752d558da048d4248c91af873b63bd60519752" "checksum serde_derive 1.0.85 (registry+https://github.com/rust-lang/crates.io-index)" = "a915306b0f1ac5607797697148c223bedeaa36bcc2e28a01441cd638cc6567b4" "checksum serde_yaml 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0887a8e097a69559b56aa2526bf7aff7c3048cf627dff781f0b56a6001534593" "checksum syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f92e629aa1d9c827b2bb8297046c1ccffc57c99b947a680d3ccff1f136a3bee9" -"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03" -"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5" +"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" +"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" +"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" -"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f" -"checksum wifiscanner 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "43835bdc40990a7cddc6c3bf4a40caa726256e435c5b535f1d33c7d393a475d9" -"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" +"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" -"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" "checksum yaml-rust 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "95acf0db5515d07da9965ec0e0ba6cc2d825e2caeb7303b66ca441729801254e" diff --git a/Cargo.toml b/Cargo.toml index 562867e..9dbc3d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,8 @@ libloading = "0.5.0" serde = "1.0" serde_yaml = "0.8" serde_derive = "1.0" -getopts = "0.2" \ No newline at end of file +getopts = "0.2" +blurz = "0.4.0" +regex = "1" +chrono = "0.4" +# notify = "4.0" \ No newline at end of file diff --git a/config.yaml b/config.yaml index 4f8ef07..3250a95 100644 --- a/config.yaml +++ b/config.yaml @@ -1,11 +1,22 @@ monitors: - - name: wifi_availability + # - name: wifi_availability + # config: + # iface: lo + # ssid: "" + # essid: [] + # ping_targets: ["127.0.0.1"] + # - name: tester + - name: dhcp_leases config: - iface: lo - ssid: "" - essid: [] - ping_targets: ["127.0.0.1"] + path: /home/crom/GitProjects/rnetmon/leases.example.txt + mac_rules: + d4:c9:ef:50:aa:c8: # laptop eth0 + - binding state free + - uid "\001@\210\005\355`\205" + - set vendor-class-identifier = "android-dhcp-7.1.2" + 3c:a9:f4:4c:a3:38 # laptop wlan0 outputs: - - name: stdout \ No newline at end of file + # - name: stdout + - name: espeak \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 0021ba0..94b6276 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,23 +21,6 @@ use crate::message::*; use crate::monitor::*; use crate::output::*; -fn monitor_factory(name: &str, config: &HashMap) -> Box { - match name { - "tester" => Box::new(plugins::monitor::tester::Tester::new(&config)), - "wifi_availability" => Box::new( - plugins::monitor::wifi_availability::WifiAvailability::new(&config), - ), - _ => panic!("Unknown monitor name: {}", name), - } -} - -fn output_factory(name: &str, config: &HashMap) -> Box { - match name { - "stdout" => Box::new(plugins::output::stdout::Stdout::new(&config)), - _ => panic!("Unknown monitor name: {}", name), - } -} - fn main() { let args: Vec = env::args().collect(); @@ -63,6 +46,7 @@ fn main() { // Parse config let config_path = matches.opt_str("c").unwrap_or("./config.yaml".to_owned()); + let mut config_file = File::open(config_path).expect("Could not open configuration file"); let mut config_content = String::new(); config_file @@ -85,7 +69,7 @@ fn main() { println!("Loading monitor: {}", name); let snd = mon_sender.clone(); - let mut mon = monitor_factory(name, &mon_config); + let mut mon = plugins::monitor::factory(name, &mon_config); thread::spawn(move || loop { mon.run(&snd); }); @@ -105,7 +89,7 @@ fn main() { let (out_sender, out_receiver) = mpsc::channel(); output_senders.push(out_sender); - let mut output = output_factory(name, &out_config); + let mut output = plugins::output::factory(name, &out_config); thread::spawn(move || loop { let message = out_receiver.recv().unwrap(); diff --git a/src/message.rs b/src/message.rs index accee1a..a1461d5 100644 --- a/src/message.rs +++ b/src/message.rs @@ -1,7 +1,16 @@ +#[derive(Debug, Clone)] +pub enum Level { + Debug, + Info, + Warning, + Error, + Critical, +} + #[derive(Debug, Clone)] pub struct Message { pub emitter: String, - pub level: i32, + pub level: Level, pub msg_type: String, pub text: String, } diff --git a/src/plugins/monitor/dhcp_leases.rs b/src/plugins/monitor/dhcp_leases.rs new file mode 100644 index 0000000..3afb7f3 --- /dev/null +++ b/src/plugins/monitor/dhcp_leases.rs @@ -0,0 +1,156 @@ +pub use crate::message::*; +pub use crate::monitor::*; +use serde_derive::*; + +extern crate regex; +use regex::Regex; +extern crate chrono; +use chrono::{DateTime, Local}; + +#[derive(Debug)] +pub struct DHCPLeases { + config: DHCPLeasesConfig, + rgx_lease: Regex, + rgx_mac: Regex, + rgx_date_start: Regex, + rgx_date_ends: Regex, +} +#[derive(Debug, PartialEq, Serialize, Deserialize)] +struct DHCPLeasesConfig { + path: String, + mac_rules: HashMap>, +} + +impl Monitor for DHCPLeases { + fn new(config: &HashMap) -> Self { + let config_node = config.get("config").expect("Missing `config` key").clone(); + let config = + serde_yaml::from_value(config_node).expect("Invalid config for wifi_availability"); + + // Regex compilation + let rgx_lease = Regex::new(r"(?s)lease\s+(\d+(?:\.\d+){3})\s*\{\n?(.*?)\}").unwrap(); + let rgx_mac = + Regex::new(r"^\s*hardware\s+ethernet\s([a-f0-9]{2}(?:\:[a-f0-9]{2}){5})\s*;").unwrap(); + let rgx_date_start = Regex::new(r"^\s*starts\s+(.*?)\s*;").unwrap(); + let rgx_date_ends = Regex::new(r"^\s*ends\s+(.*?)\s*;").unwrap(); + + DHCPLeases { + config: config, + rgx_lease: rgx_lease, + rgx_mac: rgx_mac, + rgx_date_start: rgx_date_start, + rgx_date_ends: rgx_date_ends, + } + } + fn run(&mut self, sender: &mpsc::Sender) { + use std::fs::File; + use std::io::prelude::*; + + let mut config_file = + File::open(&self.config.path).expect("Could not open DHCP leases file"); + let mut config_content = String::new(); + config_file + .read_to_string(&mut config_content) + .expect("Could not read DHCP leases file"); + + let mut unauthorized_macs: Vec = vec![]; + + for cap in self.rgx_lease.captures_iter(&config_content) { + let ip = cap.get(1).unwrap().as_str(); + let content = cap.get(2).unwrap().as_str(); + + let mac = self + .rgx_mac + .captures(content) + .expect("No 'hardware ethernet' field found for MAC address") + .get(0) + .unwrap() + .as_str(); + let starts_str = self + .rgx_date_start + .captures(content) + .expect("No 'starts' field found in lease") + .get(0) + .unwrap() + .as_str(); + let starts = + chrono::naive::NaiveDateTime::parse_from_str(starts_str, "%U %Y/%m/%d %H:%M:%S") + .expect("Bad date format"); + let ends_str = self + .rgx_date_ends + .captures(content) + .expect("No 'ends' field found in lease") + .get(0) + .unwrap() + .as_str(); + let ends = + chrono::naive::NaiveDateTime::parse_from_str(ends_str, "%U %Y/%m/%d %H:%M:%S") + .expect("Bad date format"); + + let now = Local::now().naive_local(); + if starts <= now && now < ends { + // Lease is active + if let Some(rules) = self.config.mac_rules.get(mac) { + for rule in rules { + if content.find(rule).is_none() { + unauthorized_macs.push(mac.to_owned()); + + sender + .send(Message { + emitter: "dhcp_leases".to_owned(), + level: Level::Error, + msg_type: "dhcp_leases.unauthorized_mac.rule".to_owned(), + text: format!("Mismatching rule '{}' for device {}", rule, mac), + }) + .unwrap(); + break; + } + } + } else { + unauthorized_macs.push(mac.to_owned()); + + sender + .send(Message { + emitter: "dhcp_leases".to_owned(), + level: Level::Error, + msg_type: "dhcp_leases.unauthorized_mac.unknown".to_owned(), + text: format!("Unauthorized device on network: {}", mac), + }) + .unwrap(); + } + } + } + + if unauthorized_macs.len() > 0 {} + + // let leases: Vec<(&str, &str)> = lease_rgx + // .captures_iter(&config_content) + // .map(|c| (c.get(1).unwrap().as_str(), c.get(2).unwrap().as_str())) + // .collect(); + // println!("{:?}", leases); + // let cap = lease_rgx.captures(config_content); + + // sender + // .send(Message { + // emitter: "dhcp_leases".to_owned(), + // level: 10, + // msg_type: "string".to_owned(), + // text: format!("frfr"), + // }) + // .unwrap(); + + std::thread::sleep(std::time::Duration::from_millis(2000)); + } +} + +/* + +lease 192.168.0.26 { + starts 4 2018/08/16 22:31:22; + ends 4 2018/08/16 23:01:22; + tstp 4 2018/08/16 23:01:22; + cltt 4 2018/08/16 22:31:22; + binding state free; + hardware ethernet 84:4b:f5:16:f4:94; +} +*/ diff --git a/src/plugins/monitor/mod.rs b/src/plugins/monitor/mod.rs index eb7e040..9072b06 100644 --- a/src/plugins/monitor/mod.rs +++ b/src/plugins/monitor/mod.rs @@ -1,2 +1,15 @@ +pub mod dhcp_leases; pub mod tester; pub mod wifi_availability; + +use crate::monitor::*; +use std::collections::HashMap; + +pub fn factory(name: &str, config: &HashMap) -> Box { + match name { + "tester" => Box::new(tester::Tester::new(&config)), + "wifi_availability" => Box::new(wifi_availability::WifiAvailability::new(&config)), + "dhcp_leases" => Box::new(dhcp_leases::DHCPLeases::new(&config)), + _ => panic!("Unknown monitor name: {}", name), + } +} diff --git a/src/plugins/output/bluetooth_lightbulb.rs b/src/plugins/output/bluetooth_lightbulb.rs new file mode 100644 index 0000000..9495037 --- /dev/null +++ b/src/plugins/output/bluetooth_lightbulb.rs @@ -0,0 +1,37 @@ +extern crate blurz; + +// use blurz::bluetooth_adapter::BluetoothAdapter; +// use blurz::bluetooth_device::BluetoothDevice; +// use blurz::bluetooth_gatt_characteristic::BluetoothGATTCharacteristic; +// use blurz::bluetooth_gatt_service::BluetoothGATTService; + +// use crate::message::Message; +// pub use crate::output::*; +// use serde_derive::*; + +// #[derive(Debug)] +// pub struct BluetoothLightbulb<'a> { +// config: BluetoothLightbulbConfig, +// adapter: BluetoothAdapter<'a>, +// } +// #[derive(Debug, PartialEq, Serialize, Deserialize)] +// struct BluetoothLightbulbConfig { +// mac: Vec, +// } + +// impl Output for BluetoothLightbulb { +// fn new(config: &HashMap) -> Self { +// let config_node = config.get("config").expect("Missing `config` key").clone(); +// let config = +// serde_yaml::from_value(config_node).expect("Invalid config for wifi_availability"); + +// let adapter = BluetoothAdapter::init().expect("Cannot initialize bluetooth adapter"); + +// BluetoothLightbulb { config: config } +// } +// fn process_message(&mut self, message: Message) { +// println!("Received message: {:?}", message); + +// let device = Device::new(dev_path); +// } +// } diff --git a/src/plugins/output/espeak.rs b/src/plugins/output/espeak.rs new file mode 100644 index 0000000..b08ef7f --- /dev/null +++ b/src/plugins/output/espeak.rs @@ -0,0 +1,19 @@ +use crate::message::Message; +pub use crate::output::*; + +#[derive(Debug)] +pub struct Espeak {} + +impl Output for Espeak { + fn new(config: &HashMap) -> Self { + Espeak {} + } + fn process_message(&mut self, message: Message) { + use std::process::{Command, Stdio}; + let res = Command::new("espeak") + .arg(message.text) + .stdout(Stdio::null()) + .status() + .expect("failed to execute process"); + } +} diff --git a/src/plugins/output/mod.rs b/src/plugins/output/mod.rs index 76c718c..cf211d1 100644 --- a/src/plugins/output/mod.rs +++ b/src/plugins/output/mod.rs @@ -1 +1,14 @@ +pub mod bluetooth_lightbulb; +pub mod espeak; pub mod stdout; + +use crate::output::*; +use std::collections::HashMap; + +pub fn factory(name: &str, config: &HashMap) -> Box { + match name { + "stdout" => Box::new(stdout::Stdout::new(&config)), + "espeak" => Box::new(espeak::Espeak::new(&config)), + _ => panic!("Unknown monitor name: {}", name), + } +}