From c0a834fe4a026c65446b759b31cce068f012c5e3 Mon Sep 17 00:00:00 2001 From: "Crom (Thibaut CHARLES)" Date: Thu, 31 Jan 2019 17:13:58 +0100 Subject: [PATCH] wip wifi --- Cargo.lock | 100 +++++++++++++++++++++++ config.yaml | 9 +- src/main.rs | 5 +- src/plugins/monitor/mod.rs | 1 + src/plugins/monitor/tester.rs | 2 +- src/plugins/monitor/wifi_availability.rs | 55 +++++++++++++ 6 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 src/plugins/monitor/wifi_availability.rs diff --git a/Cargo.lock b/Cargo.lock index 3d05df3..ff04869 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,11 @@ +[[package]] +name = "aho-corasick" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cc" version = "1.0.28" @@ -16,6 +24,20 @@ dependencies = [ "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "kernel32-sys" +version = "0.2.2" +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 = "libloading" version = "0.5.0" @@ -30,6 +52,14 @@ name = "linked-hash-map" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "memchr" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "proc-macro2" version = "0.4.26" @@ -46,6 +76,23 @@ dependencies = [ "proc-macro2 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "regex" +version = "0.1.80" +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)", +] + +[[package]] +name = "regex-syntax" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rnetmon" version = "0.1.0" @@ -55,6 +102,7 @@ dependencies = [ "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]] @@ -93,6 +141,23 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thread-id" +version = "2.0.0" +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)", +] + +[[package]] +name = "thread_local" +version = "0.2.7" +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)", +] + [[package]] name = "unicode-width" version = "0.1.5" @@ -103,6 +168,24 @@ name = "unicode-xid" version = "0.1.0" 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" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.3.6" @@ -112,6 +195,11 @@ 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" @@ -131,20 +219,32 @@ dependencies = [ ] [metadata] +"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66" "checksum cc 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "bb4a8b715cb4597106ea87c7c84b2f1d452c7492033765df7f32651e66fcf749" "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 libc 0.2.48 (registry+https://github.com/rust-lang/crates.io-index)" = "e962c7641008ac010fa60a7dfdc1712449f29c44ef2d4702394aea943ee75047" "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 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 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 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 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/config.yaml b/config.yaml index 598509e..4f8ef07 100644 --- a/config.yaml +++ b/config.yaml @@ -1,6 +1,11 @@ monitors: - - name: tester - - name: tester + - name: wifi_availability + config: + iface: lo + ssid: "" + essid: [] + ping_targets: ["127.0.0.1"] + outputs: - name: stdout \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e9fcb7d..0021ba0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,9 @@ 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), } } @@ -59,7 +62,7 @@ fn main() { } // Parse config - let config_path = matches.opt_str("c").unwrap_or("./config.yaml".to_string()); + 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 diff --git a/src/plugins/monitor/mod.rs b/src/plugins/monitor/mod.rs index 918dcc7..eb7e040 100644 --- a/src/plugins/monitor/mod.rs +++ b/src/plugins/monitor/mod.rs @@ -1 +1,2 @@ pub mod tester; +pub mod wifi_availability; diff --git a/src/plugins/monitor/tester.rs b/src/plugins/monitor/tester.rs index 26a8142..e59a341 100644 --- a/src/plugins/monitor/tester.rs +++ b/src/plugins/monitor/tester.rs @@ -14,7 +14,7 @@ impl Monitor for Tester { println!("-- Sending message {}", self.cnt); sender .send(Message { - emitter: "me".to_string(), + emitter: "tester".to_string(), level: 10, msg_type: "string".to_string(), text: format!("This is message number {}", self.cnt), diff --git a/src/plugins/monitor/wifi_availability.rs b/src/plugins/monitor/wifi_availability.rs new file mode 100644 index 0000000..10dfb43 --- /dev/null +++ b/src/plugins/monitor/wifi_availability.rs @@ -0,0 +1,55 @@ +use crate::message::Message; +use crate::monitor::*; +use serde_derive::*; + +#[derive(Debug)] +pub struct WifiAvailability { + config: WifiAvailabilityConfig, +} +#[derive(Debug, PartialEq, Serialize, Deserialize)] +struct WifiAvailabilityConfig { + iface: String, + ssid: String, + essid: Vec, + ping_targets: Vec, +} + +impl Monitor for WifiAvailability { + 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 wa = WifiAvailability { config: config }; + // Activate iface + //ip link set wlan0 up + + wa + } + fn run(&mut self, sender: &mpsc::Sender) { + // iw dev wlan0 link: get is connected + // iw dev wlan0 scan: scan all networks + + for target in &self.config.ping_targets { + use std::process::{Command, Stdio}; + let res = Command::new("ping") + .args(vec!["-c", "3", "-I", &self.config.iface, target]) + .stdout(Stdio::null()) + .status() + .expect("failed to execute process"); + + if !res.success() { + sender + .send(Message { + emitter: "wifi_availability".to_owned(), + msg_type: "ping.failed".to_owned(), + level: 10, + text: format!("Cannot ping {}", target), + }) + .unwrap(); + } + } + + std::thread::sleep(std::time::Duration::from_millis(2000)) + } +}