mirror of
https://codeberg.org/icewind/wifi-prometheus-exporter.git
synced 2026-06-03 08:34:25 +02:00
allow checking multiple interfaces
This commit is contained in:
parent
3c0e6994f0
commit
bfa79a65a3
1 changed files with 19 additions and 7 deletions
26
src/main.rs
26
src/main.rs
|
|
@ -10,26 +10,36 @@ use std::sync::Arc;
|
|||
use std::str::FromStr;
|
||||
|
||||
struct WifiLister {
|
||||
session: Session
|
||||
command: String,
|
||||
session: Session,
|
||||
}
|
||||
|
||||
impl WifiLister {
|
||||
pub fn new<A: ToSocketAddrs, S: AsRef<OsStr> + ?Sized>(addr: A, keyfile: &S) -> Result<Self, MainError> {
|
||||
pub fn new<A: ToSocketAddrs, Priv: AsRef<OsStr>, Pub: AsRef<OsStr>>(addr: A, keyfile: Priv, pubkey: Pub, interfaces: &[&str]) -> Result<Self, MainError> {
|
||||
let tcp = TcpStream::connect(addr)?;
|
||||
let mut session = Session::new()?;
|
||||
session.set_tcp_stream(tcp);
|
||||
session.handshake()?;
|
||||
let key_file = Path::new(keyfile);
|
||||
session.userauth_pubkey_file("admin", None, &key_file, None)?;
|
||||
let keyfile = Path::new(keyfile.as_ref());
|
||||
let pubkey = Path::new(pubkey.as_ref());
|
||||
session.userauth_pubkey_file("admin", Some(pubkey), keyfile, None)?;
|
||||
|
||||
let command = if interfaces.is_empty() {
|
||||
"wl assoclist".to_string()
|
||||
} else {
|
||||
let commands: Vec<String> = interfaces.iter().map(|interface| format!("wl -a {} assoclist", interface)).collect();
|
||||
commands.join(" && ")
|
||||
};
|
||||
|
||||
Ok(WifiLister {
|
||||
session
|
||||
session,
|
||||
command,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn list_connected_devices(&self) -> Result<Vec<String>, MainError> {
|
||||
let mut channel = self.session.channel_session()?;
|
||||
channel.exec("wl assoclist")?;
|
||||
channel.exec(&self.command)?;
|
||||
let mut s = String::new();
|
||||
channel.read_to_string(&mut s)?;
|
||||
channel.wait_close()?;
|
||||
|
|
@ -43,9 +53,11 @@ async fn main() -> Result<(), MainError> {
|
|||
let mut env: HashMap<String, String> = dotenv::vars().collect();
|
||||
let addr = env.remove("ADDR").ok_or("No ADDR set")?;
|
||||
let keyfile = env.remove("KEYFILE").ok_or("No KEYFILE set")?;
|
||||
let pubfile = env.remove("PUBFILE").ok_or("No PUBFILE set")?;
|
||||
let port = env.get("PORT").and_then(|s| u16::from_str(s).ok()).unwrap_or(80);
|
||||
let interfaces: Vec<&str> = env.get("INTERFACES").map(|interfaces| interfaces.split(' ').collect()).unwrap_or_default();
|
||||
|
||||
let wifi_listener = Arc::new(WifiLister::new(addr, &keyfile)?);
|
||||
let wifi_listener = Arc::new(WifiLister::new(addr, &keyfile, &pubfile, &interfaces)?);
|
||||
|
||||
// GET /hello/warp => 200 OK with body "Hello, warp!"
|
||||
let metrics = warp::path!("metrics")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue